Answered step by step
Verified Expert Solution
Question
1 Approved Answer
ont Maps Maps are similar to collections, however, they deal with collections of ordered pairs. They are useful fin the construction of databases. Consider
ont Maps Maps are similar to collections, however, they deal with collections of ordered pairs. They are useful fin the construction of databases. Consider the following program that shows the implementation of a Student database. The class Hash Demo show how a database of students can be created and stored in a map. The Hashmap consist of the pair String, and Student. The String is the key that is used to search for the value linked to that key. import java.util.HashMap; import java.util.Scanner; on3" 1692!) Jinw( q.tuo.metay2 2 public class HashMapDemo { (920. public static void main(String[] args) { // TODO Auto-generated method stub return hireDate; Scanner scan = new Scanner (System.in); String searchName = ""; T HashMap student = new duq di staving HashMap (10); ram args student.put("Mike", new Student (23667,' Mike Mannix", new Date ("June", 15, 2006))); student.put("Julie", new Student (45771, " Julie Grosvenor", new Date ("May", 31, 2006))); : (2005 do { System.out.pr :008 System.exit 129T" = 9msn "Vsun6C")960 wen = 9160d System.out.println(" Enter the student name that you wish to look up or press enter to end processing"); } (si searchName = scan.nextLine(); if(student.containsKey(searchName)) { Student stu = :("95 student.get(searchName); System.out.println(" Student Found: " + stu); Sy wmployee hire} else if(!searchName.equals("")){ 1109/00/8 10 insbu12)insbu2 Iduq 1 System.out.println(" Student: " + searchName + " not found"); } } while (!searchName.equals("")); System.out.println(" End of Processing"); scan.close(); } } The Student class Jduq public class Student { 12 CH >qeMdzsH private int id; private String name; private Date hireDate; "Juq.Jn55012 public Student ( ) 60 wen,"honeva Siluc { id = 1000; } name = "Test Name"; hireDate = new Date("January",11, 2005); .. .. public Student (int theid, String the Name, Date theDate) { } ((smat{ if (theName == null || theDate == null) employee"); :br } 032 System.out.println("Fatal Error creating System.exit(0); id = theid; name = theName; hireDate = theDate; 12 } public Student (Student originalObject) { name = originalObject.name; hireDate = new Date(originalObject.hireDate); } public int getId() { } return id; public String getName() { } return name; public Date getHire Date() { reo) munters te Erro return hireDate;); } ) asupe nes po neeJood olduq te.mo th; yea year) } /** bonadto = ad * Precondition newName is not null * @param args */ public void setName(String newName) { day, int /** if (newName == null) { int { System.out.println ("Fatal Error"); T System.exit(0); this.day = day; } thi else this year) ar; v):1oqmi } 9160 228J iJduq name = newName; thom print2.steving else { ysb ni staving 169y tni staving * Precondition newDate is not null ror"); * @param args.exit(0); */ ()9760 dug public void setHire Date (Date newDate) { publif (newDate == null) { ysb System.out.println("Fatal Error setting wmployee hire " + "date. "); (169 System.exit(0); om } this.month = month; :(169y.,ysb Idnom)9780192 niyab tni.Qnin12tnom pn112)9160 Jduq 3 (9160 + } else }()bI9p this.hireDate = newDate; 91 12 Sifdug public String toString() { teman mu return (getId() + " " +getName() + " " getHireDate().toString()); } 90 9160 jdug 19] public boolean equals (Student otherObject) { public } } return this == otherObject; 20 19 732) 9m6Mt92 biov oiJduq ic St } (JJun == 9ms/wn)ti public class Date { The Date class import java.util.Scanner;(8)tixs.mstay2 private String month; mswap 261") nJtning.tuo.mstay2 new Date("January. private int day; heid, private int year; 2805); te public Date() { month = "January"; H2 biov ilduq day = 1; year = 1900; } Gwen)ti rev2 d 99yofgmw vearl public Date (int monthInt, int day, int year) { } setDate(month Int, day, year); public Date(String monthString, int day, int { } setDate(monthString, day, year); public Date (int year). sb.eing { setDate(1,1, year); } public Date(Date aDate) x9.mstay2 { if (aDate == null) return "June"; { System.out.println("Fatal Date Error"); System.exit(0); duq iduq } 160792 biov month = aDate.month; day = aDate.day; soy,1,1)9760192 year = aDate.year; } return y 192 biov year) public void setDate(int monthInt, int day, int { return ateo if (dateOK(monthInt, day, year)){ defa this.month = monthName (monthInt); Errer" 92 Je this.day = day; tln("Fatal Err this. year = year; } else { return (190 System.out.println("Fatal error"); ((SI 13)) } year) (19d { System.out!"EVE public void setDate(String month, int day, int else if (dateOK(month, day, year)){ dinom this.month = month; 112 Ju (danom) das is } } this.day = day; this. year = year; } else { Idug System.out.println("Fatal error"); System.exit(0); un == 93606) TI dev2 public void setDate(int year) eve {ublic bool equal 1 = d dinom set Date (1,1, year); } 97808 = 769% public void setYear(int year) { if ((year < 1000 || year > 9999)) { } else } Jduq (160y System.out.println("Fatal Error"); System.exit(0); this.year = year; aid? sevizing public void setMonth (int monthNumber) if((monthNumber 12)) { { Ysb} else } System.out.println("Fatal Error"); System.exit(0); biov month = monthName (monthNumber); (159 teb) public String monthName (int month) { switch (month) { case 1: return "January"; case 2: return "February"; case 3: food staving return "March"; case 4: return "April"; case 5: 30Iyab) 33 (I= < inIysb) :((eeee >> Joy) 33 return "May"; case 6: return "June"; 33 (I= < the mont case 10: return "October"; case 7: return "July"; case 8: eyboard = return "August"; case 9: return "September"; nsefood steving (Jissy tni,JIvsb Scanner(System.in); Snom) the nativeb) :(eeee yboard.next(); case 11: [n = 1 nextInt(); return "November"; ard.nextInt(); case 12: return "December"; default: tnom) muterIn); 11 ("d7sM System.out.println("Fatal Error"); nom System.exit(0); return "Error"; dnom JCA") aJ6ups.tnom } Reent} { publi{ } 1.20QUA") al sups.dtnom. .d dinom } public void setDay(int day) do edme if ((day 13)) else t System.out.println("Fatal Error"); System.exit(0); this.day = day; = 169) 30 (yeb.ysbd ()dnoMag ont2 tduq entinom.eint 0") )aJsups.dtnom ") aj aJsups.dtnom 7 :I 9280 private boolean dateOK(int monthInt, int dayInt, int yearInt) { return((monthInt >= 1) && (monthInt =1) && (dayInt = 1990 ) && (yearInt =1) && (dayInt = 1990 ) && (yearInt } public int getDay() { return this.day; } public int getYear() { return this.year; } public void readInput() { boolean loop = true; I] args) C thod stuff Scanner keyboard = new Scanner(System.in); while (loop) { System.out.println("Enter the name of the month, day, year"); == String monthIn = keyboard.next(); int dayIn = keyboard.nextInt(); int year In = keyboard.nextInt(); if (dateOK (monthIn, dayIn, yearIn)) setDate(monthIn, dayIn, yearIn); loop = false; { } else Reenter data"); } keyboard.close(); } System.out.println("illegal date. e(); chName)) stud public String toString() { ht return(month + " "+ day + " " + year); //ystem.out.println(getMonth() + " " + getDay() + ", + getYear() ); 9
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Ive made corrections and completed the missing parts of the code Additionally Ive renamed the student HashMap to studentMap for clarity Please note th...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started