Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a Junit test CourseTest for my Course class. *** Course.java *** import java.time.LocalTime; import java.util.HashSet; import java.util.Set; public class Course { private String courseName;

Create a Junit test CourseTest for my Course class.

*** Course.java ***

import java.time.LocalTime; import java.util.HashSet; import java.util.Set; public class Course { private String courseName; private Set weekDays; // a set used to store days private int noOfCredits, classDuration; private LocalTime startTime; public Course() { weekDays=new HashSet(); //{'M','T','W'} } public Course(String courseName, Set weekDays, int noOfCredits, int classDuration, LocalTime startTime) { if(weekDays.isEmpty()) { throw new IllegalArgumentException("WeekDays is empty"); //IllegalArgumentException is thrown if weekdays is empty } if(noOfCredits>=1 && noOfCredits<=5) { throw new IllegalArgumentException("Credit should be from 1 to 5"); //IllegalArgumentException is thrown if credits is not in range of 1-5 } this.courseName = courseName; this.weekDays = weekDays; this.noOfCredits = noOfCredits; this.classDuration = classDuration; this.startTime = startTime; } public LocalTime getStartTime() { return startTime; } public LocalTime getEndTime() { return startTime.plusMinutes(classDuration); } public void setStartTime(LocalTime startTime) { this.startTime = startTime; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public Set getWeekDays() { return weekDays; } public void setWeekDays(Set weekDays) { this.weekDays = weekDays; } public int getNoOfCredits() { return noOfCredits; } public void setNoOfCredits(int noOfCredits) { this.noOfCredits = noOfCredits; } public int getClassDuration() { return classDuration; } public void setClassDuration(int classDuration) { this.classDuration = classDuration; } @Override public int hashCode() { //generated hashcode method; always better to use it along with equals method final int prime = 31; int result = 1; result = prime * result + classDuration; result = prime * result + ((courseName == null) ? 0 : courseName.hashCode()); result = prime * result + noOfCredits; result = prime * result + ((startTime == null) ? 0 : startTime.hashCode()); result = prime * result + ((weekDays == null) ? 0 : weekDays.hashCode()); return result; } @Override public boolean equals(Object obj) { // equals method to compare the state of the two objects based on all the variables value if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Course other = (Course) obj; if (classDuration != other.classDuration) return false; if (courseName == null) { if (other.courseName != null) return false; } else if (!courseName.equals(other.courseName)) return false; if (noOfCredits != other.noOfCredits) return false; if (startTime == null) { if (other.startTime != null) return false; } else if (!startTime.equals(other.startTime)) return false; if (weekDays == null) { if (other.weekDays != null) return false; } else if (!weekDays.equals(other.weekDays)) return false; return true; } public boolean conflictsWith(Course course) //method to check if a course schedule conflicts with given course's schedule or not { if(this.getWeekDays().contains(course.getWeekDays())) { if(this.getStartTime().equals(course.getStartTime()) || isOverlapping(course)) { return true; } } return false; } public boolean isOverlapping(Course course) //helper function for conflictsWith() function { LocalTime start1=getStartTime(); LocalTime end1=getEndTime(); LocalTime start2=course.getStartTime(); LocalTime end2=course.getEndTime(); return start1.isBefore(end2) && start2.isBefore(end1); } public boolean contains(char day, LocalTime time) //checks whether the given date & time lies in between the course schedule or not { if(getWeekDays().contains(day)) { if(getStartTime().isBefore(time) && getEndTime().isAfter(time)) { return true; } } return false; } @Override public String toString() // string representation of the Course object { String days=""; for (char c : getWeekDays()) { days=days+c; } return courseName + ", "+ noOfCredits+ ", "+ days +", "+ startTime + ", " + classDuration; } }

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

Recommended Textbook for

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899

Students also viewed these Databases questions