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

Please follow the coments and write the code as it is 4.6 Lab Warm up: Number, S

ID: 3737469 • Letter: P

Question

Please follow the coments and write the code as it is

4.6 Lab Warm up: Number, String and char input/output

(1) Prompt the user to input an integer, then a character, and then a String, storing each into separate variables.
Output a blank line, and then output the three values entered by the user on a single line, with each separated by a space.
Assumption: The user entered String will not contain any spaces, and will be 10 or less characters long.

Note: This zyLab outputs a newline after each user-input prompt.
For convenience in the examples below, the user's input value is shown on the next line,
but such values don't actually appear as output when the program runs.


(2) Extend the program to output the values again in the next line, but in reverse order and each right-justified on a field width of 10.
End the output with a newline.

import java.util.Scanner;

public class VariableUsage {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int userInteger = 0;
// FIXME (1a): Define char and String variables similarly
  
System.out.println("Enter integer:");
userInteger = scnr.nextInt();
  
// FIXME (1b): Finish reading other items into variables
// Then output a newline, and then output the three user-entered values on a single line separated by a space

// FIXME (2): Output the user-entered values in reverse, on field widths of 10, ending with a newline
  
return;
}
}

Explanation / Answer

VariableUsage.java

import java.util.Scanner;

public class VariableUsage {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int userInteger = 0;
// FIXME (1a): Define char and String variables similarly
  
System.out.println("Enter integer:");
userInteger = scnr.nextInt();
  
// FIXME (1b): Finish reading other items into variables
System.out.println("Enter character:");
char c = scnr.next().charAt(0);
System.out.println("Enter String:");
String s = scnr.next();

// Then output a newline, and then output the three user-entered values on a single line separated by a space
System.out.println(userInteger+" "+c+" "+s);
// FIXME (2): Output the user-entered values in reverse, on field widths of 10, ending with a newline
System.out.printf("%10s%10s%10s ",s,c,userInteger);
return;
}
}

Output:

Enter integer:
99
Enter character:
z
Enter String:
Regis
99 z Regis
Regis z 99