Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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).

Basiclnput java Load default template 1 import java util Scanner; 3 public class Basic Input 4 public static void main(String[] args) Scanner scnr new Scanner (System int userInt 0; double userDouble 0.0 FIXME Define chan and string variables similarly System.out.println("Enter integer 10 user Int scnr.nextInt() then output the four values on a single line separated by a space FIXME (1): inish reading other items into variables 13 14, FIXME (2): Output the four values in reverse 15 16 FIXME (3): Cast the double to an integer, and output that integer 17 18 19 return; 20 21

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