Question: Java I - Variable Practice Worksheet: Example Code: 3.2. The Final Program VariablePractice /* Program Name: DoeJoVariablePractice * Author : John Doe * Date Written
Java I - Variable Practice Worksheet:
Example Code:
3.2. The Final Program
VariablePractice
/* Program Name: DoeJoVariablePractice
* Author : John Doe
* Date Written : 01/01/15
* Class : CIT 149 Java I
* Description: Put a good description here
* of what this program does.
*/
public class ExampleVariablePractice
{
public static void main(String [ ] args)
{
int theFirstNumber = 16; // Initialized variables
int theSecondNumber = 7;
int sum = 0;
int difference = 0;
System.out.println("**** Addition ****");
// Do the calculation and store the result in the variable sum
sum = theFirstNumber + theSecondNumber;
// System.out.println(sum); // 1
System.out.println(theFirstNumber + "+" + theSecondNumber + "=" + sum);
// System.out.println(theFirstNumber + theSecondNumber); // 2
System.out.println("**** Subtraction ****");
// Subtract the two numbers and store the answer in the variable difference.
difference = theFirstNumber - theSecondNumber;
// System.out.println(difference); // 1
System.out.println(theFirstNumber + "-" + theSecondNumber + "=" + difference);
// System.out.println(theFirstNumber - theSecondNumber); // 2
}
}
-------------------------------------------------------------------------------
The Assignment
Utilizing what you have learned above, create a Java program that will take two declared integers, do simple calculations, and output the formatted results with appropriate labels.
5.1.1. Initializing The Variables
Declare an integer called theFirstNumber and initialize it to 9.
Declare an integer called theSeconNumber and initialize it to 7.
Declare a double called sum and initialize it to 0.
Declare a double called difference and initialize it to 0.
Declare a double called product and initialize it to 0.
Declare a double called quotient and initialize it to 0.
5.1.2. Setting Up the Two Sections
Create a section heading that looks like this: SECTION I . This is simply a line of text. Use System.out.println() to display the heading.
Refer to the Output Format section below for what the output should look like.
For this section you will not be using a variable to store the result of the calculation prior to displaying the result.
Do the calculation within the display statement like this : System.out.println(The sum of the two numbers is: + (theFirstNumber + theSecondNumber)+ .);
Output the two numbers with labels to match the output specifications.
Output the two numbers you will be using in the calculations.
Output the sum of the two numbers.
Output the difference between the two numbers.
Output the product of the two numbers.
Output the quotient of the two numbers.
The calculation for the quotient will not be correct in the line above. Use type casting and redo the quotient calculation here to cause it to calculate correctly.
Do not change the initial declarations above.
Create a second section heading that looks like this: SECTION II
Refer to the Output Format section below for what the output should look like.
In this section you will be using a variable to hold the result of the calculation before displaying the result. That means that, in this section, you will use an expression (equation) to calculate the answer, and then in the next line use the println() method to display the answer.
Use the appropriate double variables above to hold the result of each of the calculations.
Repeat the same calculations as in Section I, but use an expression to calculate the answer before using a statement to display the answer.
Your calculations prior the the display statement would be (for the addition) : sum = (theFirstNumber + theSecondNumber);
The calculation for the quotient will not be correct in the line above. Use type casting and redo the quotient calculation here to cause it to calculate correctly.
Do not change the initial declarations above.
5.2. Output Format
Format the output to look similar to the following: (The 9s just indicate where numbers will be.)
Use the wording just as it is shown below.
******* Section I *******
The two numbers are: 9 and 7.
The sum of the two numbers is: 99.
The difference between the two numbers is: 9.
The product of the two numbers is: 99.
The quotient of the two numbers is: 9.9.
Using type casting, the quotient of the two numbers is: 9.9.
******* Section II *******
The sum is: 9.
The difference is: 9.
The product is: 9.
The quotient is: 9.9.
Using type casting, the quotient is: 9.9
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
