• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a simple Algebra Calculator  RSS feed

 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am testing my Java ability by creating a simple algebra calculator (y = 2x).  Where a user inputs any number, x, and it will return a value of 2*x.  But I am having a bit of difficulty with it.  Any help or suggestions would be great!  Here is the code that I have so far.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what exactly is the difficulty you are having?  It helps us help you if you tell all the details. 

having said that...

look carefully at your code, and the order you do things.
You create a scanner.
you declare a variable x, but don't initialize it to a value.
You then try to set y to...something...but i'm not sure what you're trying to do...
THEN you prompt the user, but try and print out the value of x (which is not initialized...

it's really kind of a mess.

I think you need to stop writing java, and start writing English (or whatever your natural language is). Write out the steps, in order, of what you need to do.  for example:

prompt the user for input
get input from user
...what's next???

once you have those, revise them into simpler, more discrete steps, until you know how to code each one. 
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code will be even worse if you manage to get it to compile, particularly line 13. Look at the method's documentation in the Scanner class.
 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to have the user input any number, and return a value that is twice the value of the user put in.

This is what I want the program to do:

1. Wait for the user to input a value for x.
2. Program computes: 2x
3. Program returns a value for 2x.

How do I translate this into code?
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timothy Han wrote:I'm trying to have the user input any number, and return a value that is twice the value of the user put in.

This is what I want the program to do:

1. Wait for the user to input a value for x.
2. Program computes: 2x
3. Program returns a value for 2x.

How do I translate this into code?


Are you sure this accurately describes what you want to do? Consider this alternative description for your #1 above:

1. Display a prompt for the user to type in a number.
2. Save the number that the user entered in a variable, x.

Writing a correct computer program is very precise work. There's not a lot of room for ambiguity. If you can't describe what you want to do precisely, it's very difficult to write code that will do precisely what you want the program to do.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the example for an alternative way to write your #1 requirement, can you come up with a more precise description for "Program computes 2x", one that includes a description of the operations involved? (Hint: there are two operations involved)
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for your #3, "Program returns 2x" -- I don't think this is accurate at all. In the context of a Java program, the word "return" has a very specific meaning and I don't think it matches with what you actually want to do with the result of the calculation you are performing.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even my suggestion can be improved to make it clearer. "Display a prompt for the user to enter a number" is still probably not the best way to phrase what needs to be done. "Display a message that instructs the user to enter a number" or simply "Display a message that tells the user to enter a number" are probably better ways to word it.

When you can clearly say what needs to be done in plain English, then it's easier to find information on how this can be translated to Java code: "How do you display a message in Java?" Unfortunately, that search term finds tips on how to display GUI dialog boxes.

In this case, it's helpful to know some synonyms for words like "Display" and the fact that you're trying to print to a console window -- then you can try searching for "How do you print something to the console in Java?" - try the following the links to the Official Java Tutorials first.

Your success in using online search engines to find relevant information depends on you knowing what kind of words to include in your search terms. This is another benefit of clearly expressing what you're trying to do.
 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So here is the procedure I have for the program.  If anyone can revise this for me and help me turn it into Java code that would be great!

1. Define x as an integer

2. Setup program for input from user on console.

3. Wait for the user for input on console (value is name x)

4. User input value for x

5. Program computes the mathematical operation; 2 times x

6. Console prints the new value
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That actually looks better and more logical than how your original code was written.  You already had most of the right Java statements that you needed, you just didn't have them in the right order as what you just described. Why don't you try to reorder your original code so that it reflects the logic you just laid out?

Also, you need to be clear on how to properly use a Scanner to get user input. The way you had written it is incorrect.  Follow the link you were previously given to the Scanner API documentation and read it carefully. There are a number of examples there that will help you write the correct code.
 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, but I still don't know which scanner API to use.  I've looked a the link you sent me earlier and it's a bit confusing since there is so many.  Any help would be great!
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only one such Scanner class. Start by reading the introduction to the Scanner class, which you can find by following the link I posted on Tuesday and scrolling to the top of the page. It will probably show you simple examples of what you want.
If you are confused, please tell us what you have read and what you don't understand about it. If you simply say you are confused it won't encourage anybody to help.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your use of nextInt(...) is incorrect.
Your call to nextInt(int) does not do what you think it does. See http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextInt-int-.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!