MindTap - Cengage Learning- Mozilla Firefox https://ng.cengage.com/static/nb/ui/
ID: 3756214 • Letter: M
Question
MindTap - Cengage Learning- Mozilla Firefox https://ng.cengage.com/static/nb/ui/evo/index.html?deploymentId=58471110483762530358179838&eISBN:97813373970948 id=346355415&snapshot!d:892675& MINDTAP Search this course Gorman From Cengage Programming Exercise 9.9 Instructions Majors,java Create a class named Majors that includes an enumeration for the six majors offered by a college as 1 inport java.util.Scanner 2 import java.util.ArrayList; 3 public class Majors [ 4 hi follows: ACC, CHEM, CIS, ENG, HIS, enum Major {ACC, CHEM, CIS, ENG, HIS, PHYS); PHYS. Display the enumeration values for the user, and 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 7public static void main(String[] args) { 8 // Write your code here 10 Grading Write your Java code in the area on the right. Use the Run button to compile and run the code. Clicking the Run Checks button will run pre-configured tests against your code to calculate a grade. Once you are happy with your results, click the Submit button to record your score 10:35 AM 9/28/2018Explanation / Answer
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Majors {
//It is a good practive to have enum with values
enum Major {
ACC("ACC"), CHEM("CHEM"), CIS("CIS"), ENG("ENG"), HIS("HIS"), PHYS("PHYS");
private String name;
Major(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public static void main(String[] args) {
//Create a Division map and assign a division to each major
Map<String, String> divisionMap = new HashMap();
divisionMap.put(Major.ACC.getName().toLowerCase(), "Business Division");
divisionMap.put(Major.CHEM.getName().toLowerCase(), "Science Division");
divisionMap.put(Major.CIS.getName().toLowerCase(), "Business Division");
divisionMap.put(Major.ENG.getName().toLowerCase(), "Humanities Division");
divisionMap.put(Major.HIS.getName().toLowerCase(), "Humanities Division");
divisionMap.put(Major.PHYS.getName().toLowerCase(), "Science Division");
System.out.println("Majors offered by college:");
for(Major m: Major.values()){
System.out.println(m.getName());
}
System.out.println("Enter a Major");
//Get the input major
Scanner in = new Scanner(System.in);
String majorInput = in.next();
//Checks if division map has user entered major
if(divisionMap.containsKey(majorInput.toLowerCase())){
System.out.println("Division: " + divisionMap.get(majorInput.toLowerCase()));
}else {
System.out.println("Not a valid Major");
}
}
}
Sample Output :
Majors offered by college:
ACC
CHEM
CIS
ENG
HIS
PHYS
Enter a Major
ACC
Division: Business Division