Exception Class for class that compares objects variables. Help Please.
I desperately need your help as I have been stuck for two days now with no idea how to move on with my java project.
I need to develop the class called Course that describes a university course. The skeleton of the Course class has instance variables as follows:
code - is a reference to the code of the course, a String;
title - is a reference to the title of the course, a String;
points - stores the number of points the course is worth, an int.
I have started my code:
public class Course
private String code;
private String title;
private int points;
// the class constructor
public Course(String aCode, String aTitle, int pts)
code = aCode;
title = aTitle;
points = pts;
That class should have following methods:
toString method – that return a string containing the details of the course, including the course code, its title and the number of points it is worth in parentheses, for example P101, Maintaining your carpet(20);
Code that I have developed for that method is:
public String toString()
return (code + ", " + title + " (" + points + ").");
equals method - should return true if the receiver object has the same course code as the object referenced by the argument to this method, otherwise it should return false. (assume that course codes are unique.)
Code that I came up with is:
public boolean equals (Object o)
// cast the Object argument to a Course type and compare curse code variable
Course u = (Course) o;
The Course class will require the following helper methods to check the validity of the course data:
booleanvalidCode() receives a Stringcourse code as an argument and returns true if the course code begins with a letter and is followed by three digits, otherwise it returns false. You should use methods of the Character class to determine if characters are letters or digits.
booleanvalidPoints() receives an int value as an argument and returns true if the points value is a positive multiple of 10, up to and including the value 60, otherwise the method returns false.
booleanvalidTitle() receives a String and returns true if its value is not null; otherwise the method returns false.
the validCode method - not sure how to put together this one
the validPoints method - not sure how to put together this one
the validTitle method - not sure how to put together this one
and getters method that I have written:
public String getCode()
public String getTitle()
public int getPoints()
Now I need to create exception class called InvalidCourse in the same package as the Course class. We want this to be a checked exception, and it should have a single-argument constructor that takes a String message as an argument.
This class will be used to throw an exception in the event that an invalid course construction is attempted and is to be used in Curse class constructor.
After long hours of research I came up with formula:
public class InvalidCourse extends Exception
// add a zero-argument constructor here
// implicitly invokes the superclass zero-argument
// add a one-argument constructor here
public InvalidCourse(String message)
// invoke the superclass one-argument constructor
Please can anyone help me to finish that exception class, as well as check that methods that I have created so far are build correctly and maybe provide me with some examples of similar classes so I can learn from them and write the rest of the methods? I am stuck now for two days and I’m feeling quite stressed at the moment :(