Question

How to complete these methods: /**    * Returns a reference to the course with title equals to the argument. This    * m...

How to complete these methods:

/**
   * Returns a reference to the course with title equals to the argument. This
   * method searches in the courses stored in the HashMap {@code courses} to find
   * the course whose title equals to the argument {@code title}. If the course is
   * not found, {@code null} is returned.
   *
   * @param title the title of the course
   * @return a reference to the course, or {@code null} if the course is not
   * found.
   */
   public static Course getCourseByTitle(String title) {

   }

   /**
   * Returns a reference to the Student whose name equals to the argument. This
   * method searches in the students stored in the HashMap {@code students} to
   * find the student whose name equals to the argument {@code name}. If the
   * student is not found, {@code null} is returned.
   *
   * @param name the student name
   * @return the student whose name is specified in the argument, or {@code null}
   * if the student is not found
   */
   public static Student getStudentByName(String name) {

   }

   /**
   * Add a new student entry to {@code students} HashMap.
   *
   * @param id id of the new student
   * @param student object of the new student
   * @throws Exception if the new {@code id} exists in the {@code students}
   * HashMap
   */
   public static void addStudent(String id, Student student) throws Exception {

   }

   /**
   * Remove an existing student entry from {@code students} HashMap.
   *
   * @param id the 'id' of the student to be removed
   * @throws Exception if the {@code id} does not exist in the {@code students}
   * HashMap
   */
   public static void removeStudent(String id) throws Exception {

   }

   /**
   * Returns the entry in the {@link offerings} list that has the information
   * provided in the arguments. If the entry is not found, {@code null} is
   * returned.
   *
   * @param courseCode the code of the course offered
   * @param section section of the course
   * @param term term
   * @param year year
   * @return Returns the entry in the {@link offerings} list that has the
   * information provided in the arguments
   */
   public static CourseOffering getCourseOffering(String courseCode, char section, char term, int year) {

   }

   public static void main(String[] args) {
       // Reading the list of course from the file COURSES_FILE
       readCourses();
       if (courses.isEmpty())
           System.err.println("courses shall not be empty");

       // Reading the list of course from the file STUDENTS_FILE
       readStudents();
       if (students.isEmpty())
           System.err.println("students shall not be empty");
       // add few offerings
       addOfferings();
       if (offerings.isEmpty())
           System.err.println("offerings shall not be empty");

       CourseOffering courseOffer1 = getCourseOffering("EECS2030", 'A', 'W', 2020);
       if (courseOffer1 != null) {
           // enroll the student whose name is "Maximilian" in the first offering
           Student s = getStudentByName("Maximilian");
           if (s != null) {
               try {
                   enroll(s, courseOffer1);
                   System.out.println(s + " is enrolled in " + courseOffer1);
               } catch (Exception e) {
                   System.err.println("Was unable to enroll " + s + " in " + courseOffer1 + "\n" + e.getMessage());
               }
           } else {
               System.err.println("Student Maximilian is not found");
           }

           s = getStudentByName("Alphonso");
           if (s != null) {
               try {
                   enroll(s, courseOffer1);
                   System.out.println(s + " is enrolled in " + courseOffer1);
               } catch (Exception e) {
                   System.err.println("Was unable to enroll " + s + " in " + courseOffer1 + "\n" + e.getMessage());
               }
           } else {
               System.err.println("Student Maximilian is not found");
           }

       } else {
           System.err.println("Course EECS2030 is not offered in W2020");
       }
   }

   /**
   * Populate the course offerings list with few entries.
   */
   public static void addOfferings() {
       offerings.add(new CourseOffering(courses.get("EECS1021"), 20, 'F', 'A', 2020, LocalDate.of(2020, 9, 30)));
       offerings.add(new CourseOffering(getCourseByTitle("Object Oriented Programming"), 20, 'F', 'B', 2020,
               LocalDate.of(2020, 9, 30)));
       offerings.add(new CourseOffering(courses.get("EECS1022"), 30, 'F', 'A', 2020, LocalDate.of(2020, 9, 30)));
       offerings.add(new CourseOffering(courses.get("EECS2030"), 15, 'W', 'A', 2020, LocalDate.of(2020, 9, 30)));
   }

   /**
   * Read a list of courses from the file {@link COURSES_FILE} and populate
   * {@code course} HashMap.
   */
   public static void readCourses() {
       Scanner inputStream = null;
       try {
           inputStream = new Scanner(new FileInputStream(PATH + "/eecs2030/lab4/" + COURSES_FILE));
       } catch (FileNotFoundException e) {
           System.out.println("File Courses.csv was not found");
           System.out.println("or could not be opened.");
           System.out.println(e.toString());
           System.exit(0);
       }
       String line;
       while (inputStream.hasNext()) {
           line = inputStream.nextLine();
           String[] info = line.split(",");
           Course c = new Course(info[0].trim(), info[1].trim());
           if (info.length > 2) {
               String[] preList = info[2].split(";");
               for (String p : preList)
                   c.addPrerequisite(p.trim());
           }
           courses.put(c.getCode(), c);
       }
       inputStream.close();
   }

   /**
   * Read a list of students from the file {@link STUDENTS_FILE} and populate
   * {@code students} HashMap.
   */
   public static void readStudents() {
       Scanner inputStream = null;
       try {
           inputStream = new Scanner(new FileInputStream(PATH + "/eecs2030/lab4/" + STUDENTS_FILE));
       } catch (FileNotFoundException e) {
           System.out.println("File " + STUDENTS_FILE + " was not found");
           System.out.println("or could not be opened.");
           System.out.println(e.toString());
           System.exit(0);
       }
       String line;
       while (inputStream.hasNext()) {
           line = inputStream.nextLine();
           String[] s = line.split(",");
           String id = s[0].trim();
           String name = s[1].trim();
           int y = Integer.parseInt(s[2].trim());
           int m = Integer.parseInt(s[3].trim());
           int d = Integer.parseInt(s[4].trim());
           String email = s[5].trim();
           LocalDate joinDate = LocalDate.of(y, m, d);
           Student stu = new Student(id, name, joinDate, email);
           if (s.length > 6) {
               String[] cc = s[6].split(";");
               for (String w : cc) {
                   stu.completeCourse(w.trim());
               }
           }
           students.put(stu.getId(), stu);
       }
       inputStream.close();

0 0
Add a comment Improve this question Transcribed image text
Answer #1

as your code doesnot have any class i am assuming this is only for understanding purpose, this code will not run.


/**
   * Returns a reference to the course with title equals to the argument. This
   * method searches in the courses stored in the HashMap {@code courses} to find
   * the course whose title equals to the argument {@code title}. If the course is
   * not found, {@code null} is returned.
   *
   * @param title the title of the course
   * @return a reference to the course, or {@code null} if the course is not
   * found.
   */
   public static Course getCourseByTitle(String title) {
       if(!Courses.containsValue(title))
       return null;
       for (Map.Entry<String,String> entry : Courses.entrySet())
       {
           if(entry.getValue().contains(title))
           return entry;
       }
   }

   /**
   * Returns a reference to the Student whose name equals to the argument. This
   * method searches in the students stored in the HashMap {@code students} to
   * find the student whose name equals to the argument {@code name}. If the
   * student is not found, {@code null} is returned.
   *
   * @param name the student name
   * @return the student whose name is specified in the argument, or {@code null}
   * if the student is not found
   */
   public static Student getStudentByName(String name) {
       if(!students.containsValue(name))
       return null;
       for (Map.Entry<String,String> entry : students.entrySet())
       {
           if(entry.getValue().contains(name))
           return entry;
       }
   }

   /**
   * Add a new student entry to {@code students} HashMap.
   *
   * @param id id of the new student
   * @param student object of the new student
   * @throws Exception if the new {@code id} exists in the {@code students}
   * HashMap
   */
   public static void addStudent(String id, Student student) throws Exception {
       student temp = new student(id, student);
       students.put(c.getCode(), c);
   }

   /**
   * Remove an existing student entry from {@code students} HashMap.
   *
   * @param id the 'id' of the student to be removed
   * @throws Exception if the {@code id} does not exist in the {@code students}
   * HashMap
   */
   public static void removeStudent(String id) throws Exception {
       for (Map.Entry<String,String> entry : students.entrySet())
       {
           if(entry.getValue().contains(name))
           students.remove(entry.getKey());
       }
   }

   /**
   * Returns the entry in the {@link offerings} list that has the information
   * provided in the arguments. If the entry is not found, {@code null} is
   * returned.
   *
   * @param courseCode the code of the course offered
   * @param section section of the course
   * @param term term
   * @param year year
   * @return Returns the entry in the {@link offerings} list that has the
   * information provided in the arguments
   */
   public static CourseOffering getCourseOffering(String courseCode, && char section, char term, int year) {
       for (String temp : offerings)
       {
           if(temp.contains(courseCode) && temp.contains(courseCode) && section.contains(term) && temp.contains(year))
           {
               return temp;
           }
       }
       return null;
   }

   public static void main(String[] args) {
       // Reading the list of course from the file COURSES_FILE
       readCourses();
       if (courses.isEmpty())
           System.err.println("courses shall not be empty");

       // Reading the list of course from the file STUDENTS_FILE
       readStudents();
       if (students.isEmpty())
           System.err.println("students shall not be empty");
       // add few offerings
       addOfferings();
       if (offerings.isEmpty())
           System.err.println("offerings shall not be empty");

       CourseOffering courseOffer1 = getCourseOffering("EECS2030", 'A', 'W', 2020);
       if (courseOffer1 != null) {
           // enroll the student whose name is "Maximilian" in the first offering
           Student s = getStudentByName("Maximilian");
           if (s != null) {
               try {
                   enroll(s, courseOffer1);
                   System.out.println(s + " is enrolled in " + courseOffer1);
               } catch (Exception e) {
                   System.err.println("Was unable to enroll " + s + " in " + courseOffer1 + "\n" + e.getMessage());
               }
           } else {
               System.err.println("Student Maximilian is not found");
           }

           s = getStudentByName("Alphonso");
           if (s != null) {
               try {
                   enroll(s, courseOffer1);
                   System.out.println(s + " is enrolled in " + courseOffer1);
               } catch (Exception e) {
                   System.err.println("Was unable to enroll " + s + " in " + courseOffer1 + "\n" + e.getMessage());
               }
           } else {
               System.err.println("Student Maximilian is not found");
           }

       } else {
           System.err.println("Course EECS2030 is not offered in W2020");
       }
   }

   /**
   * Populate the course offerings list with few entries.
   */
   public static void addOfferings() {
       offerings.add(new CourseOffering(courses.get("EECS1021"), 20, 'F', 'A', 2020, LocalDate.of(2020, 9, 30)));
       offerings.add(new CourseOffering(getCourseByTitle("Object Oriented Programming"), 20, 'F', 'B', 2020,
               LocalDate.of(2020, 9, 30)));
       offerings.add(new CourseOffering(courses.get("EECS1022"), 30, 'F', 'A', 2020, LocalDate.of(2020, 9, 30)));
       offerings.add(new CourseOffering(courses.get("EECS2030"), 15, 'W', 'A', 2020, LocalDate.of(2020, 9, 30)));
   }

   /**
   * Read a list of courses from the file {@link COURSES_FILE} and populate
   * {@code course} HashMap.
   */
   public static void readCourses() {
       Scanner inputStream = null;
       try {
           inputStream = new Scanner(new FileInputStream(PATH + "/eecs2030/lab4/" + COURSES_FILE));
       } catch (FileNotFoundException e) {
           System.out.println("File Courses.csv was not found");
           System.out.println("or could not be opened.");
           System.out.println(e.toString());
           System.exit(0);
       }
       String line;
       while (inputStream.hasNext()) {
           line = inputStream.nextLine();
           String[] info = line.split(",");
           Course c = new Course(info[0].trim(), info[1].trim());
           if (info.length > 2) {
               String[] preList = info[2].split(";");
               for (String p : preList)
                   c.addPrerequisite(p.trim());
           }
           courses.put(c.getCode(), c);
       }
       inputStream.close();
   }

   /**
   * Read a list of students from the file {@link STUDENTS_FILE} and populate
   * {@code students} HashMap.
   */
   public static void readStudents() {
       Scanner inputStream = null;
       try {
           inputStream = new Scanner(new FileInputStream(PATH + "/eecs2030/lab4/" + STUDENTS_FILE));
       } catch (FileNotFoundException e) {
           System.out.println("File " + STUDENTS_FILE + " was not found");
           System.out.println("or could not be opened.");
           System.out.println(e.toString());
           System.exit(0);
       }
       String line;
       while (inputStream.hasNext()) {
           line = inputStream.nextLine();
           String[] s = line.split(",");
           String id = s[0].trim();
           String name = s[1].trim();
           int y = Integer.parseInt(s[2].trim());
           int m = Integer.parseInt(s[3].trim());
           int d = Integer.parseInt(s[4].trim());
           String email = s[5].trim();
           LocalDate joinDate = LocalDate.of(y, m, d);
           Student stu = new Student(id, name, joinDate, email);
           if (s.length > 6) {
               String[] cc = s[6].split(";");
               for (String w : cc) {
                   stu.completeCourse(w.trim());
               }
           }
           students.put(stu.getId(), stu);
       }
       inputStream.close();

COMMENT DOWN FOR ANY QUERY

PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
How to complete these methods: /**    * Returns a reference to the course with title equals to the argument. This    * m...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How to complete this methods: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.time.Loc...

    How to complete this methods: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.time.LocalDate; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; /** * Utility class that deals with all the other classes. * * @author EECS2030 Summer 2019 * */ public final class Registrar {    public static final String COURSES_FILE = "Courses.csv";    public static final String STUDENTS_FILE = "Students.csv";    public static final String PATH = System.getProperty("java.class.path");    /**    * Hash table to store the list of students using their...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • Create an abstract Student class for Parker University. The class contains fields for student ID number,...

    Create an abstract Student class for Parker University. The class contains fields for student ID number, last name, and annual tuition. Include a constructor that requires parameters for the ID number and name. Include get and set methods for each field; the setTuition() method is abstract. Create three Student subclasses named UndergraduateStudent, GraduateStudent, and StudentAtLarge, each with a unique setTuition() method. Tuition for an UndergraduateStudent is $4,000 per semester, tuition for a GraduateStudent is $6,000 per semester, and tuition for...

  • DO NOT COPY AND PASTED!!!!!!!!!!!!! IF YOU DO NOT HOW TO DO IT, JUST DO NOT...

    DO NOT COPY AND PASTED!!!!!!!!!!!!! IF YOU DO NOT HOW TO DO IT, JUST DO NOT ANSWER MY QUESTIONS package scheduler; import java.util.List; public class Scheduler { /** * Instantiates a new, empty scheduler. */ public Scheduler() { } /** * Adds a course to the scheduler. * * @param course the course to be added */ public void addCourse(Course course) { } /** * Returns the list of courses that this scheduler knows about. * * This returned object...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • (JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private...

    (JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private String author; private double price; /** * default constructor */ public Book() { title = ""; author = ""; price = 0.0; } /** * overloaded constructor * * @param newTitle the value to assign to title * @param newAuthor the value to assign to author * @param newPrice the value to assign to price */ public Book(String newTitle, String newAuthor, double newPrice)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT