Java programming write a program that prompts the user to enter two characters a
ID: 3673090 • Letter: J
Question
Java programming
write a program that prompts the user to enter two characters and displays the major and status represented in the characters. The first
character indicates the major and the second is number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. Suppose
the following chracters are used to denote the majors:
M: Mathematics
C: Computer Science
I: Information Technology
Here is a sample run:
Enter two characters: M1
Mathematics Freshman
Enter two characters: C3
Computer Science Junior
Enter two characters: T3
Invalid
Explanation / Answer
Hi below i have included the JAVA code for your reference,
import java.util.Scanner;
public class Sampleprogram {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter two characters:");
String in = input.nextLine();
char major = in.charAt(0);
char level = in.charAt(1);
String sMajor = "";
String sLevel = "";
switch (major) {
case 'M':
sMajor = "Mathematics";
break;
case 'C':
sMajor = "Computer Science";
break;
case 'I':
sMajor = "Information Technology";
break;
default:
System.out.println("Invalid input");
System.exit(0);
break;
}
switch (level) {
case '1':
sLevel = "freshman";
break;
case '2':
sLevel = "sophomore";
break;
case '3':
sLevel = "junior";
break;
case '4':
sLevel = "senior";
break;
default:
System.out.println("Invalid input");
System.exit(0);
break;
}
System.out.println(sMajor + " " + sLevel);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.