2.19 Warm up: Variables, input, and casting (Java) Please highlight the areas of
ID: 3781164 • Letter: 2
Question
2.19 Warm up: Variables, input, and casting (Java)
Please highlight the areas of new code that are added in each of the 3 steps...
(1) Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space. (Submit for 2 points).
(2) Extend to also output in reverse. (Submit for 1 point, so 3 points total).
(3) Extend to cast the double to an integer, and output that integer. (Submit for 2 points, so 5 points total).
Explanation / Answer
BasicInput.java
import java.util.Scanner;
public class BasicInput {
public static void main(String[] args) {
//Scanner class object is used to read the inputs entered by the user
Scanner scnr=new Scanner(System.in);
//Declaring variables
int userInt=0;
double userDouble=0.0;
char userCh;
String userStr;
//Getting the integer entered by the user
System.out.print("Enter Integer :");
userInt=scnr.nextInt();
//Getting the double type value entered by the user
System.out.print("Enter Double :");
userDouble=scnr.nextDouble();
//Getting the character entered by the user
System.out.print("Enter Character :");
userCh=scnr.next(".").charAt(0);
//Getting the String entered by the user
System.out.print("Enter String :");
userStr=scnr.next();
//1.Displaying the values
System.out.println(userInt+" "+userDouble+" "+userCh+" "+" "+userStr);
//2.Displaying the values in the reverse order
System.out.println(userStr+" "+userCh+" "+userDouble+" "+userInt);
//3.Type casting the double type value to integer
System.out.println(userDouble+" cast to an Integer is "+(int)userDouble);
}
}
_______________________
Output:
Enter Integer :99
Enter Double :3.77
Enter Character :z
Enter String :Howdy
99 3.77 z Howdy
Howdy z 3.77 99
3.77 cast to an Integer is 3
__________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.