Write a Java program named DataValues.
The program is to do the following:
- declare three variables of type int named num1, num2 and intSum each variable is initialized to the value 0
- declare three variables of type double named dbl1, dbl2 and dblDifference each variable is initialized to 0.0
- declare a variable of type char named firstChar initialized to ‘a’;
- declare a variable of type String named str initialized to null.
Next, the program will do the following:
- assign the value 7 to num1 and the value 12 to num2;
- add num1 and num2 and store the resulting value in sum;
- display the value stored in sum using the following message: 7 added to 12 is 19
note: only variables may be used to generate the output statement
Continuing, the program will then do the following with the values of type double:
- initialize dbl1 to 7.3, dbl2 to 75.45
- storing the difference in dblDifference.
- display each value using the following statement: The difference of 7.3 less 75.45 is ………
note: only variables may be used to generate the output statement.
Finally, the program will do the following:
- display the value stored in firstChar;
- assign the value Z to firstChar;
- display the value now in firstChar.
- display the value stored in str;
- assign the message Welcome to CS200 to the variable str;
- display the current value in str.
Answer the following questions:
- What task or tasks is the problem asking my code to perform?
- What variable or variables must I declare?
- What program logic must I perform?
- What steps am I going to take to solve the programming problem?
Using the answers to the questions above,
write the program by following the steps below:
- define the class
- write the stub for method main
- declare the variables to be used within the program
- provide the required program logic
- compile the code
- make any necessary adjustments or corrections
- save and recompile the code
- run the program
public class DataValues
{
public static void main(String [] args)
{
//variable declaration
int num1 = 0;
int num2 = 0;
int intSum = 0;
double dbl1 = 0.0;
double dbl2 = 0.0;
double dblDifference = 0.0;
char firstChar = 'a';
String str = null;
boolean isTrue = false;
//program logic
num1 = 7;
num2 = 12;
intSum = num1 + num2;
System.out.println(num1 + " added to " +
num2 + " is " + intSum);
//variable assignment
dbl1 = 7.3;
dbl2 = 75.45;
dblDifference = dbl1 - dbl2;
System.out.println("The difference of " + dbl1 +
" less " + dbl2 +
" is " + dblDifference);
System.out.println(firstChar);
firstChar = 'Z';
System.out.println(firstChar);
System.out.println(str);
str = "Welcome to CS200";
System.out.println(str);
System.out.println(isTrue);
isTrue = true;
System.out.println(isTrue);
}
}
7 added to 12 is 19
The difference of 7.3 less 75.45 is -68.15
a
Z
null
Welcome to CS200
false