• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with a java Calculator  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I am asked to make a Program that will do simple math operations AND assign and store variables with operations. I know that may of sounded confusing, so here is an example. Say I type b = 3 + 4 into the command line, it would calculate 3 + 4 = 7 and store that as b, then it would print a new line with the value of b on it, 7. Here's how it would look.

c = 3 + 4
7
d = c + 4
11
etc...

Sorry If I rambled, I just wanted to explain it in a way that wasn't confusing.

I have some code that will separate the command line arguments into different tokens of values, but I do not know how to store those in an array list. I will post the code I have. Any pointers or other help would be greatly appreciated. Thank you! - I've attached a url to a pastebin for this code. http://pastebin.com/05bdVmtK

 
Marshal
Posts: 56607
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

When you have that sort of problem, you should stop using silicon‑based technology (computers) and go back to carbon‑based technology. A pencil. Write down exactly what you want and repeat the procedure until it is simple enough for me to understand. Only then should you even think of writing any code.
Show us what you have in plain English, and then let's see what sort of code you can create.
 
Matrica Kovolunchy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Thank you, I will start working on that. Should I post it as a response or a new topic?
 
Matrica Kovolunchy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Here is my idea (written out).
--------------------------------------

First things first, I need to print "Command line calculator by"

I need to see what the user enters into the program:

If the first character is a letter and is not followed by an equals sign,I need to create a new variable that has the same "name" as what was typed in: Ex. type in a, create a new variable a
If there is an equals sign after the letter, I know that that the letter is being assigned a value. I need to calculate the value and assign it to the letter.
I need to print the value of the new letter I created.

If the first character is a letter and the next character is an operator, call the value of the first character and prepare to calculate a number (Ex. Typing a + b (Press enter) return 5 if a is 4 and b is 1). Print the return of this on a new line.

If the first character is a letter:
Check if the character exists, if so, simply print the value, if not, print an error message.

If the first character is a number:
Print the number on a new line.

If "quit" is typed in, quit the program.
If "clear" is typed in, clear the screen.
If "var" is typed in, print all the variables and their respective values.


Here is the examples my Assignment gives me:

a = 3 + 4
7
bee = a * 3
21
a + bee
28
a = 4
4
57
57
c
c not found
var
a: 4
bee: 21
quit


 
Greenhorn
Posts: 10
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your current approach seems a bit complicated. I can give you another hint.

Store the user input in a String variable. Then Convert that string to an array using the toCharArray() function on the String. Your characters will be seperated and then you can use the array index to access the numbers on which you want to perform the mathematical operations.

Also, make a habit of looking through the java api docs. I find them very useful in such cases.
 
Campbell Ritchie
Marshal
Posts: 56607
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid that reply fails the test of being plain English. I also think a char[] will not be helpful.
 
Campbell Ritchie
Marshal
Posts: 56607
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I think that approach is not too complicated. I think that is a good bit of work.

Do you always have
variable‑assign‑number‑operator‑number?
When you see
x = 3 × 7
…your eye automatically divides the text as above. You can therefore divide the text like that. If the text does not divide into five parts like that, you can consider another option. You can direct the process depending on what the text begins with.
Are you required to record variables and their values? How are you going to do that?
 
Matrica Kovolunchy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example that the assignment gives me are the types of acceptable inputs that are going to be tested for.

a = 3 + 4
7
bee = a * 3
21
a + bee
28
a = 4
4
57
57
c
c not found
var
a: 4
bee: 21
quit
 
Campbell Ritchie
Marshal
Posts: 56607
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's leave out the printing of the title. That is really easy. You have already done that.
I think you have worked out what you want in English nicely. What you do depends on how many tokens you pass. So we need to work out how to divide your text into tokens. Important question about formatting: are the tokens separated by spaces? That is a very important question. It is much easier to divide
x = 3 × 4
than to divide
x=3×4
Are you familiar with the operations of the command‑line arguments? If not, try running this little class with the following inputs
CommandDemo Matrica Kovolunchy
CommandDemo 1
CommandDemo x=3+4
CommandDemo xxx=3+4
CommandDemo x = 3 + 4
CommandDemo xxx = 3 + 4
CommandDemo "xxx = 3 + 4"
CommandDemo 
The last represents an empty line
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Were you able to complete the program? If so could you post the finished code. I'm interested in seeing how it is done.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!