In: Computer Science
JAVA
Project: Student Major and status
Problem Description: 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 a number character 1, 2, 3 or 4, which indicates whether a student is a freshman, a sophomore, junior or senior. Suppose the following characters are used to denote the majors:
M: Mathematics
C: Computer Science
I: Information Technology
Here is a sample run: Sample: Enter two characters: M3 Mathematics Junior Enter two characters: C4 Computer Science Senior Enter two characters: T3 Invalid input: Wrong major code Enter two characters: I5 Information Technology Invalid input: wrong status code
What to deliver? Your .java file including: • All the above sample runs Your code with other appropriate comments.
Hint:
1. Use charAt() and if else loops
2. Use the sample run in the instruction to test your program to make sure it is correct before you submit it.
Please find the code below:::
StudentMajor.java
package classes3;
import java.util.Scanner;
public class StudentMajor {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
String input;
while(true){
System.out.print("\nEnter two characters : ");
input =
sc.nextLine();
boolean valid =
true;
if(input.length()==2){
if(input.charAt(0)=='M'){
System.out.print("Mathematics
");
}else if(input.charAt(0)=='C'){
System.out.print("Computer
Science ");
}else if(input.charAt(0)=='I'){
System.out.print("Information
Technology ");
}else{
System.out.println("Invalid
input: Wrong major code");
valid = false;
}
if(valid){
if(input.charAt(1)=='1'){
System.out.print("freshman ");
}else
if(input.charAt(1)=='2'){
System.out.print("a sophomore");
}else
if(input.charAt(1)=='3'){
System.out.print("junior ");
}else
if(input.charAt(1)=='4'){
System.out.print("senior ");
}else{
System.out.println("Invalid input: wrong status code");
valid =
false;
}
}
}else{
System.out.println("Invalid input");
}
}
}
}
output: