Question
Write arithmetic expressions to accomplish a task Use casting to convert between primitive types Use a value-returning library method and a library constant Use string
Write arithmetic expressions to accomplish a task
Use casting to convert between primitive types
Use a value-returning library method and a library constant
Use string methods to manipulate string data
Communicate with the user by using the Scanner class
Task #1 Correcting Logic Errors in Formulas
Copy and compile the source file NumericTypes.java, run the program, and observe the output. Some of the output is incorrect. You need to correct logic errors in the average formula and the temperature conversion formula. The logic errors could be due to conversion between data types, order of operations, or formula problems. The necessary formulas are
average = (score1+score2) / numberOfScores
C = 5/9 (F-32)
Make sure that the output makes sense before you continue. The average of 95 and 100 should be 97.5 and the temperature that water boils is 100 degrees Celsius
Task #2 Using the Scanner Class for User Input
Add an import statement above the class declaration to make the Scanner class available to your program. In the main method, create a Scanner object and connect it to the System.in object.
Prompt the user to enter his/her first name.
Read the name from the keyboard using the nextLine method, and store it into a variable called firstName (you will need to declare any variables you use).
Prompt the user to enter his/her last name.
Read the name from the keyboard and store it in a variable called lastName.
Concatenate the firstName and lastName with a space between them and store the result in a variable called fullName.
Print out the fullName.
Compile, debug, and run, using your name as test data.
Task #3 Working with Strings
Use the charAt method to get the first character in firstName and store it in a variable called firstInitial (you will need to declare any variables that you use).
Print out the users first initial.
Use the toUpperCase method to change the fullName to all capitals and store it back into the fullName variable
Add a line that prints out the value of fullName and how many characters (including the space) are in the string stored in fullName (use the method length to obtain that information).
Compile, debug, and run. The new output added on after the output from the previous tasks should have your initials and your full name in all capital letters.
/** This program demonstrates how numeric types and operators behave in Java */
//TASK #2 Add import statement here to use the Scanner class //import java.util.Scanner; // TASK #2 to do - uncomment this line by deleting the leading // // Note: You need to import a class if you do not define it in the same directory with your source code.
public class NumericTypes { public static void main (String [] args) { //TASK #2 Create a Scanner object here - help : uncomment the line below by deleting the leading // //Scanner keyboard = new Scanner (System.in); //identifier declarations final int NUMBER = 2 ; // number of scores final int SCORE1 = 100; // first test score final int SCORE2 = 95; // second test score final int BOILING_IN_F = 212; // freezing temperature int fToC; // temperature in celsius double average; // arithmetic average String output; // line of output to print out //TASK #2 declare variables used here String firstName; // user's first name // TASK #2 to do - declare user's last name similar way for the first name // TASK #2 to do - declare user's full name similar way for the first name //TASK #3 declare variables used here char firstInitial; // user's first initial
// Find an arithmetic average average = SCORE1 + SCORE2 / NUMBER; // Task #1 help: add (score1 + score2) to get addition before division output = SCORE1 + " and " + SCORE2 + " have an average of " + average; System.out.println(output);
// Convert Fahrenheit temperatures to Celsius fToC = 5/9 * (BOILING_IN_F - 32); // Task #1 help: Use cast to change data type // fToC = (int) (5/(double)9 * (BOILING_IN_F - 32)); // Note: 5/9 is integer division and result is 0 // 5/(double)9 is double devision and result is 0.5555 output = BOILING_IN_F + " in Fahrenheit is " + fToC + " in Celsius."; System.out.println(output); System.out.println(); // to leave a blank line
// ADD LINES FOR TASK #2 HERE // prompt the user for first name // read the user's first name //System.out.print("Enter your first name: "); //help : uncomment the line below by deleting the leading // //firstName = keyboard.nextLine(); //help : uncomment the line below by deleting the leading // // To do - prompt the user for last name - follow the style of firstName // To do - read the user's last name follow the style of firstName // concatenate the user's first and last names //fullName = firstName + " " + lastName; //help : uncomment the line below by deleting the leading // // print out the user's full name //System.out.println(fullName + " has " + fullName.length() + " characters"); // help : uncomment the line below by deleting the leading //
System.out.println(); // to leave a blank line // ADD LINES FOR TASK #3 HERE // get the first character from the user's first name //firstInitial = firstName.charAt(0); //help : uncomment the line below by deleting the leading // // To do - print out the user's first initial // To do - convert the user's full name to all capital letters // help - use fullName = fullName.toUpperCase(); // To do - print out the user's full name in all capital letters
System.out.println(); // to leave a blank line } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started