Read the following code skeleton and add your own code according to the comments. Your TA is available if you have and questions and you may collaborate with your classmates if you need help.
Note: //--> Indicates a place where you should add code.
// class name matches the file name
public class Lab2
{ // we must have a main method to run the program
public static void main(String[] args)
{
// declare some variables of different types:
// an int called age
//-->
// a double called num
//-->
// a String called firstName
//-->
// a String called lastName
//-->
After Part 1, write a segment of code that will assign values to the variables you just declared. In addition, this code will print out the values of some of these variables.
// Assign a value to the "age" integer
//-->
// Assign a value to the "firstName" String
//-->
// Assign a value to the "num" double.
//-->
// Print the values of firstName and age in the following string:
// "< firstName > is < age > years old."
//-->
// Print the values of firstName and num in the following string:
// "< firstName >'s favorite number is < num >."
//-->
After Part 2, write a segment of code that will assign a value to the lastName variable after asking the user for the appropriate input.
// Use the Scanner class to ask the user for their last name and store the
// input in the 'lastName' variable.
//-->
// Print values of firstName and lastName in the following string:
// "< firstName >'s last name is < lastName >."
//-->
After Part three write a segment of code that will calculate the sum of products of four numbers and the product of sums of those same numbers.
// Declare and define the variables.
int x = 3, x2 = 4
int y = 5, y2 = 6
//Write one line of code that adds the values of the 'x' variables and 'y' //variables then multiplies these resultant values. Assign the result to a
//new integer 'pos' that you declare.
//-->
// Write one line of code that multiplies the values of the 'x' variables and
// 'y' variables then adds the resultant values. Assign the result to a new
// integer 'sop' that you declare.
//-->
// Print the value of 'sop' in the string: "The sum of products is: < sop >."
//-->
// Print the value of 'pos' in the string: "The product of sums is: < pos >."
//-->
} // End main()
} // end class Lab2
Below is an example of what your output should look like when this lab is completed.
Sample Run:
John is 22 years old John's favorite number is 3.1415
John's last name is Doe
The sum of products is: 42
The product of sums is: 77