In: Computer Science
Write a program that prompts the user to enter two characters and display the corresponding major and year status. The first character indicates the major. And the second character is a number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. We consider only the following majors: B (or b): Biology C (or c): Computer Science I (or i): Information Technology and Systems M (or m): Marketing H (or h): Healthcare Management A (or a): Accounting Note that your program needs to let the user know if the major or year is invalid. Also, your program should be case-insensitive: your program should tell ''Marketing'' either user type 'm' or 'M'. Here are three sample runs: Sample 1: Enter two characters: i3 Information Technology and Systems Junior Sample 2: Enter two characters: B5 Biology Invalid year status Sample 3: Enter two characters: t2 Invalid major Sophomore 1 ITSS3311 Introduction to Programming Project 2 What to deliver? Your .java file including: 1. Five sample runs with the following five input: (20 points, 4 points each) (1) h1 (2) T3 (3) A2 (4) I0 (5) c4 Note that you need to run your program 5 times. Each time you run it, copy and paste the program output to the top of your program. (5 points for pasting the code in the beginning) 2. Your code with other appropriate comments. (Code: 50 points, Comments:25points)
/*
* The java program prompts the user to enter two characters. Then
find the major and course name of the student.
* Print the result of the two characters on console. The program is
tested for 5 different input values.
* Print the results on the console output.
* */
//MajorYearStatus.java
import java.util.Scanner;
public class MajorYearStatus{
public static void main(String[] args)
{
//Create an instance of Scanner
class to read input from keyboard
Scanner console = new
Scanner(System.in);
//Prompt to enter two
characters
System.out.print("Enter two
characters: ");
String studentStatus =
console.nextLine();
//checking if lenght of the user
input is less than 2
//Then print the input is
invalid
if(studentStatus.length()<2)
System.out.println("Invalid input");
else
{
//Convert the
first character in studentStatus string to upper case
//and assign the
result to the major character
char major =
Character.toUpperCase(studentStatus.charAt(0));
//Get the second
character and assign the result to the year character
char year =
studentStatus.charAt(1);
//calling
getMajor method
String
courseName = getMajor(major);
//calling
getYear method
String yearName
= getYear(year);
//Print course
name and year name values on cosnole
System.out.printf("%s %s%n", courseName, yearName);
}
}
/*The method getMajor that takes a major as character
as input argument
* and then matches the corresponding charactor in
switch case .Then return the course name
* of student*/
public static String getMajor(char major)
{
String courseName="";
switch(major)
{
case 'A':
courseName="Accounting";
break;
case 'B':
courseName="Biology";
break;
case 'C':
courseName =
"Computer Science";
break;
case 'H':
courseName="Healthcare Management";
break;
case 'I':
courseName =
"Information Technology and Systems";
break;
case 'M':
courseName =
"Mathematics";
break;
default:
courseName
="Invalid major";
}
return courseName;
} //end of the method , getMajor
/*The method getYear that takes a character year as
input argument
* and then matches the corresponding year .Then return
the year name
* of student*/
public static String getYear(char year)
{
String yearName;
switch(year)
{
case '1':
yearName =
"Freshman";
break;
case '2':
yearName =
"Sophmore";
break;
case '3':
yearName =
"Junior";
break;
case '4':
yearName =
"Senior";
break;
default:
yearName
="Invalid year";
}
return yearName;
} //end of the method,getYear
} //end of the class
-------------------------------------
Sample Output:
Sample Run1:
Enter two characters: h1
Healthcare Management Freshman
Sample Run2:
Enter two characters: T3
Invalid major Junior
Sample Run3:
Enter two characters: A2
Accounting Sophmore
Sample Run4:
Enter two characters: I0
Information Technology and Systems Invalid year
Sample Run5:
Enter two characters: c4
Computer Science Senior