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

Warm up: Variables, input, and casting (Java) Instructor notes While you will be

ID: 3817288 • Letter: W

Question

Warm up: Variables, input, and casting (Java)

Instructor notes

While you will be submitting this assignment through Zybooks, make sure you are still applying appropriate formatting and in-line commenting.

(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


Please rate me well.If you area satisfied.