Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi I'm trying to write a programme that prompts the user for unit subject and students enrolled in it. I need to implement a HashMap

Hi I'm trying to write a programme that prompts the user for unit subject and students enrolled in it. I need to implement a HashMap in my unit class where the key is the student ID and the value is a student object containing the students name and which units he or she is enrolled in. I'm supposed to modify the ?University class ?to use the HashMap so that it prints out this:

Welcome to Java University. FIT1234 Advanced Bogosorts

Enrolled Students:

12345678 Victor Chang

12345679 Fred Nurke FIT2027 Introduction to Spaghetti Coding

Enrolled Students:

12345680 Indira Naidu FIT3456 Enterprise Fizzbuzz

Enrolled Students: 12345680 Indira Naidu

Thanks for using Java University.

And this is my student.java:

public class Student { private String studentID; private String givenName; private String familyName; //Use given and family name because first name and last name // might be confused with first and last student name

public Student(String newStudentId) { studentID=newStudentId; }

public Student (String newStudentId, String newGivenName, String newFamilyName) { studentID= newStudentId; givenName=newGivenName; familyName=newFamilyName; }

//Mutator methods public void setGivenName(String givenName ) { this.givenName=givenName; } public void setFamilyName(String familyName ) { this.familyName=familyName; } // Accessor methods

public String description() { return studentID + " " + givenName + " " + familyName; }

public String getStudentId() { return studentID; }

}

Unit.java

import java.util.HashMap;

public class Unit { private String code; private String name; private HashMap enrolledStudents = new HashMap<>(); int i=0; public Unit (String unitCode, String unitName) { code=unitCode; name=unitName; } public void enrolStudent (Student newStudent) { enrolledStudents.put(newStudent.getStudentId(), newStudent); }

public void description(Unit unit) { System.out.println( code + " " + name); } }

?University.java

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;

public class University { Unit [] units = new Unit [3]; private String studentID; private String givenName; private String familyName; private String readString(String prompt ) { System.out.print(prompt); BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); String s = null; try { s = in.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } public void createUnits() { units[0]= new Unit("FIT1008", "IntroToCompSc"); units[1]= new Unit("FIT1045", "Python"); units[2]= new Unit("FIT2081", "MobileAppDev"); for (int i=0; i< units.length; i++) { studentID=readString("Enter a studentID:"); givenName=readString("Enter a firstName:"); familyName=readString("Enter a familyName:"); units[i].enrolStudent(new Student(studentID, givenName, familyName)); } } public void displayUnits() { for (int i=0; i< units.length; i++) { units[i].description(units[i]); System.out.println("Enrolled Students"); } } public void printStatus() { System.out.println("Welcome to Java University"); System.out.println(); System.out.println("Thank you for using Java University"); createUnits(); displayUnits(); System.out.println("Goodbye"); }

}

?UniversityDriver.java public class UniversityDriver {

public static void main(String[] args) { University a = new University(); a.printStatus();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

5/3 1/6 x Answered: 1 week ago

Answered: 1 week ago

Question

Discuss the importance of workforce planning.

Answered: 1 week ago

Question

Differentiate between a mission statement and a vision statement.

Answered: 1 week ago