Create a class named Majors that includes an enumeration for the six majors offe
ID: 3705959 • Letter: C
Question
Create a class named Majors that includes an enumeration for the six majors offered by a college as follows: ACC, CHEM, CIS, ENG, HIS, PHYS. Display the enumeration values for the user, then prompt the user to enter a major. Display the college division in which the major falls. ACC and CIS are in the Business Division, CHEM and PHYS are in the Science Division, and ENG and HIS are in the Humanities Division. Save the file as Majors.java. Don’t forget to create the application/project MajorsTest.java Class that has the main method and an object to use the Majors class.
Explanation / Answer
MajorsTest.java
import java.util.Scanner;
public class MajorsTest {
public static void main(String[] args) {
Majors m= new Majors();
m.displayMAjors();
Scanner scan= new Scanner(System.in);
System.out.println("Enter the major:");
String major = scan.next();
m.verifyAndDisplayDivision(major);
}
}
Majors.java
public class Majors {
public enum MajorsEnum {
ACC, CHEM, CIS, ENG, HIS, PHYS
}
public void displayMAjors(){
System.out.println("Majors: ");
for(MajorsEnum major: MajorsEnum.values()) {
System.out.print(major.name()+" ");
}
}
public void verifyAndDisplayDivision(String m) {
if(m.equals(MajorsEnum.CIS.name())&&m.equals(MajorsEnum.ACC.name())) {
System.out.println("Business Division");
} else if(m.equals(MajorsEnum.CHEM.name())&&m.equals(MajorsEnum.PHYS.name())) {
System.out.println("Science Division");
} else {
System.out.println("Humanities Division");
}
}
}
Output:
Majors:
ACC CHEM CIS ENG HIS PHYS Enter the major:
HIS
Humanities Division
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.