This is the requirements for Course.java *****Implement Course.java.***** Make s
ID: 3690226 • Letter: T
Question
This is the requirements for Course.java
*****Implement Course.java.*****
Make sure all of your fields are private and your method signatures are written exactly as defined below.
*You can assume that all parameters being passed into the constructors will be in their valid ranges.*
*You can also assume that the objects passed into the ‘equals’ or ‘compareTo’ methods will NOT be null.”
Course.java :
public Course(int department, int courseNum, String name, char day, int timeSlot, int credits)
department and course numbers can range from 0 to 999
the day and time slot are used to create a Period object – do not store the day and time slot individually
credits range from 1-4
each course maintains a list (array) of students – maximum number of students for a course is 20
getter methods for all of the variables passed into the constructor:
public int getDepartment() //returns the department number
public int getCourseNumber() //returns the course number
public String getName() //returns the name of the course
public Period getPeriod() //returns the period of the course
public int getCredits() //returns the number of credits for the course
public Student[] getRoster() //returns the roster – list of students who are enrolled in the course
public String toString()return the course in the same format as in catalog.txt: “department:courseNum [name] period credits:credits”
ex: 198:111 [Introduction to Computer Science] T5 credits:4
public boolean equals(Course other)
two courses are considered equal if their department and course numbers are the same
This is my Course.java code:
class Course{
int department;
int courseNum;
String name;
char day;
int timeSlot;
int credits;
Student students[] = new Student[20];
Period period = new Period(this.day, this.timeSlot);
public Course(int department, int courseNum, String name, char day, int timeSlot, int credits) {
this.department = department;
this.courseNum = courseNum;
this.name = name;
this.day = day;
this.timeSlot = timeSlot;
this.credits = credits;
}
public int getDepartment(){
return department;
}
public int getCourseNumber(){
return courseNum;
}
public String getName(){
return name;
}
public Period getPeriod(){
return period;
}
public int getCredits(){
return credits;
}
public Student[] getRoster(){
return students;
}
public String toString(){
return ""+ this.department+":"+this.courseNum+" "+"["+this.name+"]"+" "+this.period +" " + "credits:"+this.credits;
}
public boolean equals(Course other){
return courseNum==other.courseNum && department==other.department;
}
}
This is my Period.java code
public class Period{
private char day;
private int timeSlot;
public static void main(String[] args){
Period p1 = new Period('M', 2);
Period p2 = new Period('M', 2);
Period p3 = new Period('F', 4);
int status = p1.compareTo(p2);
if(status == 0){
System.out.println(p1 + " is equal to " + p2);
}
else if(status == -1){
System.out.println(p1 + " comes before " + p2);
}
else{
System.out.println(p1 + " comes after " + p2);
}
status = p1.compareTo(p3);
if(status == 0){
System.out.println(p1 + " is equal to " + p3);
}
else if(status == -1){
System.out.println(p1 + " comes before " + p3);
}
else{
System.out.println(p1 + " comes after " + p3);
}
}
public Period(char day, int timeSlot){
if(day >= 'a' && day <= 'z'){
day = (char) (day + (int)'A' - (int)'a'); // converting to uppercase here
}
this.day = day;
this.timeSlot = timeSlot;
}
public char getDay(){
return this.day;
}
public int getTimeSlot(){
return this.timeSlot;
}
public String toString(){
return ""+this.day + this.timeSlot;
}
public boolean equals(Period p){
return day == p.day && timeSlot == p.timeSlot;
}
public int compareTo(Period other){
if(equals(other)){
return 0;
}
else{
if(other.day == day){
if(timeSlot < other.timeSlot){
return -1;
}
else{
return 1;
}
}
else{
String days = "SMTWF";
int ind1 = days.indexOf(day);
int ind2 = days.indexOf(other.day);
if(ind1 < ind2){
return -1;
}
else{
return 1;
}
}
}
}
public void setTimeSlot(char newSlot){
int count = 1;
while (count !=0){
if ((newSlot < 1) || (newSlot > 9)){
System.out.println("Time Slot must be between 1-9");
}
else {
this.timeSlot = newSlot;
count = 0;
}
}
}
}
This is the error I am getting while submitting the code to online System:
The following hint(s) may help you locate some ways in which your solution may be improved:
Did you initialize your instance variables correctly? Are your getters implemented correctly?
Did you implement toString() correctly?
Can anyone point out the what's wrong with Course.java code. The period code is there to help understand the public Period getPeriod().
Explanation / Answer
class Course{ int department; int courseNum; String name; int credits; Student students[] = new Student[20]; Period period; public Course(int department, int courseNum, String name, char day, int timeSlot, int credits) { this.department = department; this.courseNum = courseNum; this.name = name; this.credits = credits; this.period = new Period(day,timeSlot); } public int getDepartment(){ return department; } public int getCourseNumber(){ return courseNum; } public String getName(){ return name; } public Period getPeriod(){ return period; } public int getCredits(){ return credits; } public Student[] getRoster(){ return students; } public String toString(){ return ""+ this.department+":"+this.courseNum+" "+"["+this.name+"]"+" "+this.period +" " + "credits:"+this.credits; } public boolean equals(Course other){ return courseNum==other.courseNum && department==other.department; } } class Period{ private char day; private int timeSlot; public static void main(String[] args){ Period p1 = new Period('M', 2); Period p2 = new Period('M', 2); Period p3 = new Period('F', 4); int status = p1.compareTo(p2); if(status == 0){ System.out.println(p1 + " is equal to " + p2); } else if(status == -1){ System.out.println(p1 + " comes before " + p2); } else{ System.out.println(p1 + " comes after " + p2); } status = p1.compareTo(p3); if(status == 0){ System.out.println(p1 + " is equal to " + p3); } else if(status == -1){ System.out.println(p1 + " comes before " + p3); } else{ System.out.println(p1 + " comes after " + p3); } } public Period(char day, int timeSlot){ if(day >= 'a' && dayRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.