Refer to the CourseComparer class for Problem 8. Using the variable c, of class
ID: 3777248 • Letter: R
Question
Refer to the CourseComparer class for Problem 8. Using the variable c, of class CourseComparer, perform three method calls using the variables initialized in Problem 7 such that the output following Problem 8 being printed is as follows: -1 a before b 1 a after b 0 either way Refer to the Predicate interface as well as ByHours and ByDept classes for this problem. You will not need to change any of these files, but should understand them. Your task is to replace the String Literal "?"s with calls to the test method on predicates p1 and p2, as described in the comments on each line. The expected output is: Problem0789.java S3 public static void main(String[] args) {//PROBLEM 7. System.out.printin("PROBLEM 7");//Dept Num. Hours//COMP 401 4//COMP 410 3//ECON 410 3 Course comp401, comp410, econ410; comp401 - new Course(4, "COMP", 401);//TODO://insi System.out.println(comp401); comp410 - new Course(3, "COMP", 410);//TODO://insi System.out.println(comp410); econ410 - new Course(3, "ECON", 410);//TODO://insi System.out.println(econ410);//PROBLEM 8 System.out.println("PROBLEM 8"); CourseComparer c = new CourseComparer(); CourseComparer.java package comp110; public class CourseComparer {public void compare(Course a, Course b) {if (a.getNum() > b.getNumO) {System.out.println("l a after b");} else if (a.getNum() - b.getNumO) {System.out.println("0 either way");} else {System.out.println("-1 a before b");}}}Explanation / Answer
Please follow the code and comments for description :
CODE :
a) Problem0789.java :
public class Problem0789 { // class to run the code
public static void main(String[] args) { // driver method
System.out.println("PROBLEM 7"); // message
Course comp401, comp410, econ410; // object creation for the course class
comp401 = null; // initialise the data
comp401 = new Course(4, "COMP", 401); // pass the data to the constructor
System.out.println(comp401);// print the data to console
comp410 = null; // initialise the data
comp410 = new Course(3, "COMP", 410); // pass the data to the constructor
System.out.println(comp410); // print the data to console
econ410 = null; // initialise the data
econ410 = new Course(3, "ECON", 410); // pass the data to the constructor
System.out.println(econ410); // print the data to console
System.out.println(""); // new line character
System.out.println("PROBLEM 8"); // message
CourseComparer c = new CourseComparer(); // call the class
c.compare(comp401, comp410); // pass the data to the method
c.compare(comp410, comp401);
c.compare(comp410, econ410);
}
}
b) CourseComparer.java :
public class CourseComparer { // class to run the code
public void compare(Course a, Course b) { // method that takes in two objects
if (a.getNumber() > b.getNumber()) { // check for the value
System.out.println("1 a after b"); // message
} else if (a.getNumber() == b.getNumber()) {
System.out.println("0 either way");
} else {
System.out.println("-1 a before b");
}
}
}
c) Course.java :
public class Course { // parent class
private String _department; // instance variables
private int _number;
private int _hours;
public Course(int h, String d, int n) { // constructor
this._department = d;
this._number = n;
this._hours = h;
}
public String getDepartment() { // getter methods
return _department;
}
public int getNumber() {
return _number;
}
public int getHours() {
return _hours;
}
@Override
public String toString() { // to string method to return the data
return _department + _number;
}
}
OUTPUT :
PROBLEM 7
COMP401
COMP410
ECON410
PROBLEM 8
-1 a before b
1 a after b
0 either way
Hope this is helpful.