JAVA expert please help me the program with hashCode() URL: https://en.wikipedia
ID: 3810026 • Letter: J
Question
JAVA expert please help me the program with hashCode() URL: https://en.wikipedia.org/wiki/Java_hashCode()
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, "Bob Charlotte", "Intro to Java");
Section s2 = new Section("CSC", 151, 0001, "Bob C", "Introduction to Java");
Section s3 = new Section("CSC", 151, 0001, "B Charlotte", "Java Introduction");
Section s4 = new Section("CSC", 151, 0002, "Bob C", "Introduction to Java");
Section s5 = new Section("DBA", 151, 0001, "Bob Charlotte", "SQL 1");
Section s6 = new Section("CSC", 251, 0001, "Bob Charlotte", "Adv Java");
registerAStudent(roster, s1, "bob");
registerAStudent(roster, s2, "mike");
registerAStudent(roster, s3, "peter");
registerAStudent(roster, s4, "delco");
registerAStudent(roster, s5, "tobias");
registerAStudent(roster, s6, "adam");
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);
}
}
Create a/chas Section% 5contain Create woks SectionA+containite follmoix4 informatio C4tautSThe r oUMUuul uUformation department (e-4., "CSS" ) courseNumber (e.g., 451 ) e-41 utrsectonNumber (c4.1 1121 ) instructor (eg., "Bob" ) · S4417 title. (ay.,"Teolato'lt-Jan' ) Use Section, objects aite keyr a/ HashMap/uite attached, Register class. Bof, Mike aalPeter shoullMKMP same Register class. Boc, Mike, and Peter should all Ce in same eduster class. ure and leter sito utthe same section. Everyone·k.cue(hint secti 0tla Everyone, eke should be uudt enem secti on sExplanation / Answer
Register.java
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, ArrayList<String>>();
Section s1 = new Section("CSC", 151, 0001, "Bob Charlotte", "Intro to Java");
Section s2 = new Section("CSC", 151, 0001, "Bob C", "Introduction to Java");
Section s3 = new Section("CSC", 151, 0001, "B Charlotte", "Java Introduction");
Section s4 = new Section("CSC", 151, 0002, "Bob C", "Introduction to Java");
Section s5 = new Section("DBA", 151, 0001, "Bob Charlotte", "SQL 1");
Section s6 = new Section("CSC", 251, 0001, "Bob Charlotte", "Adv Java");
registerAStudent(roster, s1, "bob");
registerAStudent(roster, s2, "mike");
registerAStudent(roster, s3, "peter");
registerAStudent(roster, s4, "delco");
registerAStudent(roster, s5, "tobias");
registerAStudent(roster, s6, "adam");
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<String>();
victims.add(student);
}
else {
victims.add(student);
}
roll.put(s, victims);
}
}
Section.java
public class Section {
private String department;
private int courseNumber;
private int sectorNumber;
private String instructor;
private String title;
public Section(String d, int c, int s, String i, String t){
department = d;
courseNumber = c;
sectorNumber = s;
instructor = i;
title = t;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getCourseNumber() {
return courseNumber;
}
public void setCourseNumber(int courseNumber) {
this.courseNumber = courseNumber;
}
public int getSectorNumber() {
return sectorNumber;
}
public void setSectorNumber(int sectorNumber) {
this.sectorNumber = sectorNumber;
}
public String getInstructor() {
return instructor;
}
public void setInstructor(String instructor) {
this.instructor = instructor;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + (department.length());
result = PRIME * result + (courseNumber);
result = PRIME * result + (sectorNumber);
result = PRIME * result + (title.length());
result = PRIME * result + (instructor.length());
result = PRIME * result + (department.length());
return result;
}
}
Output:
{Section@42556246=[mike], Section@42552aef=[tobias], Section@425557bd=[peter], Section@425548f7=[bob], Section@47d66496=[adam], Section@4255d6a5=[delco]}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.