//TASK #2 Add import statement here to use the Scanner class //import java.util.
ID: 3558253 • Letter: #
Question
//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
}
}
Task #1 Correcting Logic Errors in Formulas
average = (score1+score2) / numberOfScores
C = 5/9 (F-32)
Task #2 Using the Scanner Class for User Input
Task #3 Working with Strings
Explanation / Answer
import java.util.Scanner;
public class NumericTypes
{
public static void main (String [] args)
{
//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
String lastName; // declare user's last name similar way for the first name
String fullName; // 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 = ((double)(SCORE1 + SCORE2)) / NUMBER;
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
// prompt the user for first name
System.out.print("Enter your first name: ");
// read the user's first name
firstName = keyboard.nextLine();
// To do - prompt the user for last name - follow the style of firstName
System.out.print("Enter your last name: ");
// To do - read the user's last name follow the style of firstName
lastName = keyboard.nextLine();
// concatenate the user's first and last names
fullName = firstName + " " + lastName;
// print out the user's full name
System.out.println(fullName + " has " + fullName.length() + " characters");
System.out.println(); // to leave a blank line
// get the first character from the user's first name
firstInitial = firstName.charAt(0);
// To do - print out the user's first initial
System.out.println("first initial: " + firstInitial);
// To do - convert the user's full name to all capital letters
fullName = fullName.toUpperCase();
// print out the user's full name in all capital letters
System.out.println("Full name in all capital: " + fullName);
System.out.println(); // to leave a blank line
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.