Question
Write code to follow these steps: 1. Define a class CsCourse that contains two String fields, courseNumber and courseName. Write a constructor that takes two
Write code to follow these steps:
1. Define a class CsCourse that contains two String fields, courseNumber and courseName. Write a constructor that takes two String input parameters and sets the class fields from them.
2. Write your class to
2.a) implement the Comparable interface. The interface requires "implements Comparable
public int compareTo(CsCourse other)
This doesn't require an import as Comparable is in the java.lang package that is auto-imported in all Java code.
Compares this object with the other object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the other object. (For instance "Introduction to Computer Programming" compares as less than "Introduction to Web Engineering".) Your class should order the courses by their courseName alone.
2.b) have a method toString() with the following header:
public String toString();
This method should return a String that is the courseName + " " + courseNumber (name followed by number, separated by spaces).
3. Write your main method to read the file cs_courses.txt, create a CsCourse object for each line containing a course name, and store all the objects into an array. The cs_courses.txt file has 39 lines in it. Each line has the info for a single course, namely the course number followed by the name of the course (with a space separating number of name).
I suggest that you put the file-reading code into a "helper method" that returns a CsCourse[] array. This way the code to read the file does not clutter up your main() method. Header for the helper method would look like this:
public static CsCourse[] readCourses() throws IOException {
4. After reading the array of CsCourses (and saving into a CsCourse[] variable), your main should use the java.util.Arrays sort(Object[] o) method to sort all courses by courseName. This depends on the Comparable
5. Next your main should use the java.util.Arrays toString(Object[] o) method to print out the array of all courses. They must print in sorted order to be considered correct.
6. Remember, write Javadoc for the class, its fields, and all methods in the file.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started