The course was nice by the way (great for beginners), it’s basically like Codecademy for Java except it costs $40, but I think I got my money’s worth, so there.
For this program, we’ll be declaring 3 int variables and call them a, b, and r.
We won’t print these out yet, that’ll be discussed later on in the lesson.
4. Reading said variables from keyboard
But instead of declaring them or hardcoding them to certain values, we’ll ask what the user wants to assign them by using the Scanner class.
A Scanner class in Java can be created by an import statement in the beginning of the program.
Then we create a Scanner object.
Scanner keyboard = new Scanner(System.in);\
This object that I call keyboard although you can call it whatever you want lets us get input from the user throughout the program.
Then we can use this Scanner key word to get our input.
String tv\_show = keyboard.nextLine();\
Now whatever the person types in, Java will get the input and store it into the String variable tv_show.
nextLine() is used for Strings nextInt() is used for int nextDouble() is used for doubles and so forth…
So the object doesn’t have to be keyboard, I could have called the Scanner “user”, and it still would have worked out the same way, like this:
String tv\_show = user.nextLine();\
For this program, instead of defining the int variables a, b, and r with certain values, we’ll ask the user to give us numbers. So import the Scanner class, create a Scanner object, and get values from the user using the object.
5. Calculating results with variables
Okay here’ s the more tricky stuff. Making equations that the compiler can understand and compute. Then again, if you’ve ever taken a math class and had to use the calculator a lot, this might come off as easy for you. Basic rules with these equations are to use parenthesis and exponents in the right places.
Java has the built-in function pow. It’s formatted as pow(x, y) where x is raised to the y.
So let’s say we wanted to compute 3\^2 or 3 to the power of 2, which is 9 for the ape in all of us.
So in this case we could write that pow(3,2) and that too would give us 9. Huzzah!
For this program, we will declare result1, result2, result3, and result4 and initialize them to the following four equations shown here:
For the equation of result4, I realize it is 4r5. Yes, it could be written as 20r, but just write it as it is. Deal bro. Come on now.
6. Printing said results
Now for easy stuff. Using the System.out.println() function and the + operator we learned already, we’ll print all this shiz out.
For this program, print out the values of a, b, and r, with a little message like this:
“a is equal to: “
Also print out the values of result1 through result4 using an appropriate message like this:
“Result 1 is equal to: “
Tags bits , digital , equations , fvcproductions , hieroglyphics , initializing , intro , java program , java programming , java , java , math functions , math java , pow , result , results
Share the love
Leave a comment