Print the results of 5 simple math operations to the screen; then answer a few questions related to variables.
Textbook: 2.1 - 2.3
New concepts: Literals, data types, variables, assignment, initialization, string concatanation, simple math operators.
Create a class named UsernameA01a in a file named
UsernameA01a.java. Remember to replace Username with your actual UH username.
main method, do the following:
First, create (that is, declare and initialize) two
int variables to hold two whole numbers. Print these to the screen so that your output looks something like this:
First number: 8 Second number: 3
Now print the results of using these two variables in five different math expressions using the +, -, *, /, and % operators. Don't just print out the result though; also print out what the expression is. So your final output should now look like this:
First number: 8 Second number: 3 8 + 3 = 11 8 - 3 = 5 8 * 3 = 24 8 / 3 = 2 8 % 3 = 2
In all your calculations and printing, use the two variables you declared at the beginning of your program. So, for example, in producing the above output, the number '8' and the number '3' each occur only once in my code--when I initialize my two variables. You can check you have this correct by changing the initial values of your variables to 5 and 2 and all your output should change appropriately. (Change them back to 8 and 3 when you're done though.)
You may use more than just two variables if you like. For instance, you might want a variable that holds the result of each operation until you print it out.
Now that you have written your program, change the values assigned to your two variables in order to answer the following questions. Remember to recompile each time you change your code, and to change your code back to its original form after each question. You may also need to look in your book to learn more.
Save your answers in a plain text file with a
When you are done answering these questions, set your two variables back to 8 and 3, so that your program produces output as above.
Part A01a: Upload your
UsernameA01a.java file to Tamarin.
Part A01b: Upload your
UsernameA01b.txt file to Tamarin.
8 - 3 = 5will actually show up in your output. However, the numbers (8 and 3, in this case) should each only occur once in your code.
System.out.println("8 + 3 = ");But this alone doesn't work, since now, if you change the value of your variables, you need to go through every line of your code changing all the the "8"s and "3"s. So how do you print out
"8 + 3 = ", but replace the 8 and 3 in your code with the appropriate variable names? (Hint: string concatanation.)