Java Help- please include comments. Create a class Section that contains the fol
ID: 3730472 • Letter: J
Question
Java Help- please include comments.
Create a class Section that contains the following information:
String department (e.g., "CSC")
int courseNumber (e.g., 151)
int sectonNumber (e.g., 4101)
String instructor (e.g., "Sophie")
String title (e.g., "Introduction to Java")
Use Section objects as the key for a HashMap in the attached Register class. Sophie, Sally and Jack should all be in the same section. Everyone else should be in different sections. We should be able to test with different data sets as well.
Register Class:
import java.util.ArrayList;
import java.util.HashMap;
public class Register {
public static void main(String[] args) {
HashMap<Section, ArrayList<String>> roster = new HashMap<>();
Section s1 = new Section("CSC", 151, 0001, "Sophie Charlotte", "Intro to Java");
Section s2 = new Section("CSC", 151, 0001, "Sophie C", "Introduction to Java");
Section s3 = new Section("CSC", 151, 0001, "S Charlotte", "Java Introduction");
Section s4 = new Section("CSC", 151, 0002, "Sophie C", "Introduction to Java");
Section s5 = new Section("DBA", 151, 0001, "Sophie Charlotte", "SQL 1");
Section s6 = new Section("CSC", 251, 0001, "Sophie Charlotte", "Adv Java");
registerAStudent(roster, s1, "sophie");
registerAStudent(roster, s2, "sally");
registerAStudent(roster, s3, "jack");
registerAStudent(roster, s4, "lassie");
registerAStudent(roster, s5, "freckles");
registerAStudent(roster, s6, "zipper");
System.out.println(roster);
}
public static void registerAStudent(HashMap<Section, ArrayList<String>> roll, Section s, String student) {
ArrayList<String> victims = roll.get(s);
if (victims == null) {
victims = new ArrayList<>();
victims.add(student);
}
else {
victims.add(student);
}
roll.put(s, victims);
}
}
Correct output will look something like this, depending on Section's toString:
[sophie, sally, jack] in Section [dept=CSC, courseNum=151, section=1, instructor=Sophie Charlotte, title=Intro to Java]
[freckles] in Section [dept=DBA, courseNum=151, section=1, instructor=Sophie Charlotte, title=SQL 1]
[lassie] in Section [dept=CSC, courseNum=151, section=2, instructor=Sophie C, title=Introduction to Java]
[zipper] in Section [dept=CSC, courseNum=251, section=1, instructor=Sophie Charlotte, title=Adv Java]
Invalid output will look something like this:
[sophie] in Section [dept=CSC, courseNum=151, section=1, instructor=Sophie Charlotte, title=Intro to Java]
[lassie] in Section [dept=CSC, courseNum=151, section=2, instructor=Sophie C, title=Introduction to Java]
[freckles] in Section [dept=DBA, courseNum=151, section=1, instructor=Sophie Charlotte, title=SQL 1]
[jack] in Section [dept=CSC, courseNum=151, section=1, instructor=S Charlotte, title=Java Introduction]
[sally] in Section [dept=CSC, courseNum=151, section=1, instructor=S Charlotta, title=Introduction to Java]
[zipper] in Section [dept=CSC, courseNum=251, section=1, instructor=Sophie Charlotte, title=Adv Java]
Explanation / Answer
PROGRAM CODE:
public class Section {
String department;
int courseNumber;
int sectionNumber;
String instructor;
String title;
Section(String dept,int courseNo, int SectionNo,String inst, String tit)//constructor overloading
{
this.department=dept;
this.courseNumber=courseNo;
this.sectionNumber=SectionNo;
this.instructor=inst;
this.title=tit;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Register {
public static void main(String[] args) {
HashMap<Section, ArrayList<String>> roster = new HashMap<>();
//Section(String dept,int courseNo, int SectionNo,String inst, String tit)
Section s1 = new Section("CSC", 151, 0001, "Sophie Charlotte", "Intro to Java");
Section s2 = new Section("CSC", 151, 0001, "Sophie C", "Introduction to Java");
Section s3 = new Section("CSC", 151, 0001, "S Charlotte", "Java Introduction");
Section s4 = new Section("CSC", 151, 0002, "Sophie C", "Introduction to Java");
Section s5 = new Section("DBA", 151, 0001, "Sophie Charlotte", "SQL 1");
Section s6 = new Section("CSC", 251, 0001, "Sophie Charlotte", "Adv Java");
registerAStudent(roster, s1, "sophie");
registerAStudent(roster, s2, "sally");
registerAStudent(roster, s3, "jack");
registerAStudent(roster, s4, "lassie");
registerAStudent(roster, s5, "freckles");
registerAStudent(roster, s6, "zipper");
System.out.println(roster.values());
for(Map.Entry<Section,ArrayList<String>> entry:roster.entrySet())
{
Section key =entry.getKey();
ArrayList<String> list=entry.getValue();
System.out.print( "Details :-->");
System.out.println(list );
}
}
public static void registerAStudent(HashMap<Section, ArrayList<String>> roll, Section s, String student) {
ArrayList<String> victims = roll.get(s);
if (victims == null) {
victims = new ArrayList<>();
victims.add(student);
} else {
victims.add(student);
}
roll.put(s, victims);
}
}
//To get the sophie, shelly and jack in same section you have to write same section number of these guys while caliing the overload constructor of Section.
The prototype of Section constructor is -->
Section(String dept,int courseNo, int SectionNo,String inst, String tit)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.