i need help modelling a use case diagram from a topic, it will be in java GUI
Design a Calculator that
1.Allow user to key in a legitimate arithmetic statement that involves number, operator +, - and bracket '(' and ')' ;
2.When user press “Calculate” button, display result;
3.Some legitimate statement would be ((3+2)-4+2) (equals 3) and (-2+3)-(3-1) (equals -1);
4.You should NOT use a pre-existing function that just take in the statement as a parameter and returns the result but you should write the logic of parsing every character in your code.
5.Store the last statement and answer so it is displayed when user press the “Last calculation” button.
i have designed two use case diagrams using UML on netbeans 6.5.1, one of the use case i am not sure whether is it containing too much use cases etc, while the other is what i think could be too vague for the topic.i hope to get some feedback on whether the use case diagram are appropriate, thanks.i included a what it would be like in GUI
First thing you must know about use case diagrams is that its supposed to describe functionality of a system for which actor. It should be on such a high level that anyone without knowledge of programming can understand it. As a programmer, use cases might look very vague to you but thats fine. Its not supposed to say anything about the system, just what it can do.
Some more specific comments:
As i mentioned use cases should describe high level functions. Press Calculate is not a function, Calculate is. Press Last Calculation should be Store Last Calculation, etc
Its not clear what Press Backspace does. Backspace is just a key, not a use case.
The ParserSys package tries to describe internals of a system. This does not belong in a use case diagram. Other diagrams should be used for this.
Use case Store Result (first pic) should not be in this diagram. But if thats something User can do, it should be associated with User.
Edit:
..i believe the main problem is i am having trouble identifying use case..
A good way of identifying use cases is as simple as asking yourself the question: "[Actor] should be able to [what]" (or something similar). [What] is then your use case. If it doesn't fit in this sentence, its probably not a use case.
In the second use case diagram, you have user having use cases based on the sequence of actions performed to implement the use cases in the first. These would be better represented as either an activity diagram or state machine - the user cares about getting the results of a calculation, and it is incidental that to get these results expressions need to be keyed in buttons need to be pressed. When creating use cases concentrate on the goals that the originator of the use case has, rather than how the system might help them achieve these goals .
On another point, the spec you give says nothing about simulating a keyboard using a Java GUI, or a backspace key as in your mock-up. Check with the stakeholders whether 'allow the user to key in' just means giving them somewhere to type, or providing an on-screen keypad.
Related
I'm programming a game that has user interaction through the console. The user would enter a command followed by a space and then the parameters of said command. Example:
move-piece 5-right
The command is "move-piece" and the parameters are "5-right" (5 spots to the right). I have a method that checks if the command and parameters were entered in the correct format. The method then calls a move method with the parameters that moves the piece on the board. An example of an exception case could be that the game is already over, in which case the user is not allowed to move any pieces. Another exception case is moving the piece outside the board. I always read about how using exceptions for control flow is bad, but I don't really understand what that means.
if (gameOver){
throw new CustomGameException("you cannot move any pieces, because the game is over.");
}
Is the above considered controlling the flow with exceptions? Of course this isn't the only situation in which a game rule is broken. I have many more if statements that check for edge cases, but I'm not sure if that's a good thing or not. If the above is considered bad practice, how else should I handle such cases? I want to be able to print out a unique message for each edge case to inform the user about his/her mistake. I should note that this is for a school project and we are not allowed to have any System.out.print in our main code. The output gets printed to the user in the main method.
EDIT for clarity:
We are not allowed to have print methods within our code. Output is done in the main method. The suggested solution given to us by our teacher also used exceptions to handle such cases. An example of such usage in the solution is when the user attempts to roll a die twice in a row (which is not allowed according to the game rules). My teacher threw an exception in the roll method for such a scenario. The teacher also says that we are not supposed to use exceptions to control the flow, which is why I'm a bit confused.
Don't take the statement about not using exceptions for control flow too literally. There are many cases where exceptions work just fine.
One is where it's hard (or outside the responsibility) for the code that knows how to handle the error to detect whether the error will occur or not. Your situation might be an example of this: You want to handle all user interaction in the main method, but all kinds of things at various depth into your code can invalidate a command. So throw an exception and be done with it. Don't worry about the performance of something that will happen at most once every time a user enters some text.
Another reason to use exceptions would be where there are natural race conditions in checking if an operation might succeed or not. e.g. you want to create a file, but a common occurrence is that a file with the same name might already exist or you might be out of disk space or something similar. In that case it's no use to check if the operation will succeed as it might still fail later when you do the operation. Just try to do it and catch the right exceptions.
Advice on the form "never use X" is common in programming and almost never worthy of the zeal with witch it is presented.
Some information (don't want to confuse you with a lot of shitty code):
I've done a pretty large console programm (my largest project so far) which helps me a lot with managing some accounts / assets and more. I'm constantly adding more features but at the same time I reshape the code to work on my shitty coding style.
The console program has a lot of commands the user can type and for every command different methods get called / objects get created / manipulated and so on.
My keywords which are saved in an ArrayList<String> and my commands have this type: [keyword] [...n more Strings]
DESIGN PROBLEM 1:
I have a method cmdProcessor(String[] arguments) which handles the input (command) of the user, and the [keyword] is always the first argument arguments[0]. That means I have a large number of if-statements of this type:
if(arguments[0].equalsIgnoreCase("keyword") callMethod(argmts); where in the String[] argmts the remaining arguments[1] ... [n] are.
Is this a good way to handle this or should I go with switch-case?
Or something else (what?)? Is it better to save the keywords in a HashMap<String, Method>?
DESIGN PROBLEM 2:
The methods (see above callMethod(argmts) ), which are triggered by the entered keyword look even more chaotic. Since the same method can have different numbers and forms of arguments saved in the String[] argmts the method is full of if(argmts.length == ...) to check length, and every of these if-blocks has a bunch of switch-case options which also have a lot of ifs and so on. The last else and the default-case in switch-case I always use for error-handling (throwing error codes and and explanation why the pattern doesn't match and so on).
Is this good or are there better ways?
I thought about using lots of submethods, which would also blow up
my program and cost a lot of time but maybe improve readability / overview. Is this okay, or what is the best
option in such cases (lots of ifs and switch-case)?
Since I want to build more and more around this program maybe I should start now to fix bad design before it's too late. :)
About Design-Problem 1:
My go-to would be to register a lot of Handlers, which you can base on a common interface and then implement the specific behavior individually. This is good, because the central method handling your input is slim, and you only need to register a lot of singletons once, on initialization. Disadvantage: if you forget one, it will not work. So maybe, you can register them automatically (reflection or something thelike).
Aside from that, a map is better than a List in this case, because (I assume) you don't need a sorting. You need a mapping from key to behavior, so a map seems better (though even a very large set of keywords would probably not be very inefficient, if you stick to a list).
About Design Problem 2:
If I was you, I'd use actual Regular-Expression patterns. Take a look at the java.util.regex.Pattern-class. You can isolate groups and validate the values you receive. Though it does not spare you the exception/error-handling, it does help a lot in segmentation and interpretation efforts.
What I want to do is to print results in english and other language in java output.
Is there any "easy" way to do this other than copy paste the code and just translate the output in the language I need?
I am building a game with frequent input/output from the user and I would like to have the option the questions (output) to be in more than 1 languages. The input from users is only numbers, thus there won't be any differentiation there.
One way is to copy classes with output (printf) and translate them accordingly and ask user in the beginning of the game to choose the language, but in this case every time i make change in the code, I need to make those change in both copies of the code, thus making it more difficult.
Is there any shorter and more robust way?
Look at the java.text.MessageFormat class. It lets you define template messages with placeholders for data (like scores, user names, etc.) and then to repeatedly merge those messages with data to produce the kind of Strings you're looking for. The idea is that you'd have one set of templates for each language you want to support.
I want to make a Java program to help people with basic discrete mathematics (that is to say, checking the truth values of statements). To do this, I need to be able to detect how many variables the user inputs, what operators there are, and what quantifiers there are, if any (∃ and ∀). Is there a good algorithm for being able to do all these things?
Just so you know, I don't just want a result; I want full control over their input, so I can show them the logical proof. (so doing something like passing it to JavaScript won't work).
Okay, so, your question is a bit vague, but I think I understand what you'd like to do: an educational aid that processes first-order logic formulas, showing the user step by step how to work with such formulas, right? I think the idea has merit, and it's perfectly doable, even as a one-man project, but it's not terribly easily, and you'll have to learn a lot of new things -- but they're all very interesting things, so even if nothing at all comes out of it, you'd certainly get yourself some valuable knowledge.
I'd suggest you to start small. I'd start by building a recursive descent parser to recognize zero-order logic formulas (a machine that would decide if a formula is valid, i.e. it'd accept "A ^ B" but it'd reject "^ A ^"). Next up you'd have to devise a way to store the formula, and then you'd be able to actually work on it. Then again, start small: a little machine that accepts valid zero-order logic formulas like TRUE AND NOT (TRUE AND FALSE), and successfully reduces it step by step to true is already something that people can learn from, and it's not too hard to write. If you're feeling adventurous, add variables and make equations: A AND TRUE = TRUE -- it's easy to work these out with reductions and truth tables.
Things get tricky with quantifiers that bind variables, that's where the Automated theorem proving may come into play; but then, it all depends on exactly what you'd like to do: implementing transformations into the various normal forms, and showing the process step by step to the student would be fairly easy, and rather useful.
At any rate, I think it's a decent personal project, and you could learn a lot from it. If you're in a university, you could even get some credit for it eventually.
The technique I have used is to parse the input string using a context free grammar. There are many frameworks to help you do this, I have personally used ANTLR in the past to parse an input string into a descrete logic tree. ANTLR allows you to define a CFG which you can map to Java types. This allows you to map to a data structure to store and evaluate the truth value of the expression. Of course, you would also be able to pull out the variables contained in the data structure.
I now have an idea, that I use the function as a string, and I calculate the real integral by hand, and ask a question to the user what the definite integral is, but that isn't a real solution.
I was wondering if there was a way to input a function and output an integral/derivative (depending on user choice). My initial step was to put it into an array somehow, but given the many types of functions, this wasn't happening.
I researched everywhere, and I haven't found a method that actually does this with no additional code, nor any code that actually does this, period.
Also, I want to see if there was a way to make a GUI interface and plot inputted functions on to that, if that's possible too.
Thanks :)
What you're describing is known as symbolic integration. There's currently no fully general way to implement it, but there are some techniques available. One such is the Risch algorithm.
Alternatively, an easier problem than symbolic integration is [symbolic differentiation -- and, if the differential of the user's input is equivalent* to the expression which they were asked to integrate, then their integral is probably correct.
You may also want to consider using an existing CAS**, such as Mathematica, to implement this. They've already implemented most of the tools you're after.
*: Keep in mind, though, that two mathematical expressions may be equivalent without being identical, either in trivial ways (e.g, terms in a different order), more complex ones (e.g, large expressions factored differently), or fundamentally (e.g, trig functions replaced with complex exponentials or vice versa).
**: Computer algebra system
Javacalculus is what you are looking for.
Good luck!