Results 1 to 2 of 2
  1. #1
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Unhappy Exception Class for class that compares objects variables. Help Please.

    Hi All

    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:



    Java 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:


    Java Code:
        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:


    Java Code:
        public boolean equals (Object o)
        {
            // cast the Object argument to a Course type and compare curse code variable 
            Course u = (Course) o;
            return code.equals(u.code);
        }


    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.



    Java Code:
    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:


    Java Code:
        public String getCode()
        {
            return code;
        }
        
        public String getTitle()
        {
            return title;
        }
        
        public int getPoints()
        {
            return points;
        }


    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:


    Java Code:
    public class InvalidCourse extends Exception
    {
        // add a zero-argument constructor here
        public InvalidCourse()
        {
            // implicitly invokes the superclass zero-argument
            // constructor
        }
        // add a one-argument constructor here
        public InvalidCourse(String message)
        {
            // invoke the superclass one-argument constructor
            super(message);
        }
    }


    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 Im feeling quite stressed at the moment :(

    Thanks Guys

    M

  2. #2
    OutOfMemory is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi darkblue,

    Class u created is enough u can very well use it as Custom exception.
    but if u want more elaborative one here it is....



    public class InvalidCourse extends Exception
    {
    /**
    * The underlying cause of this exception.
    */
    protected Throwable cause = null;

    public InvalidCourse()
    {
    super();
    }
    public InvalidCourse(String message)
    {
    super(message);
    }

    public InvalidCourse(Throwable cause) {

    this((cause == null) ? null : cause.toString(), cause);

    }


    public InvalidCourse(String message, Throwable cause) {

    super(message + " (Caused by " + cause + ")");
    this.cause = cause;
    }

    public Throwable getCause() {

    return (this.cause);

    }
    }

    Cheers,
    Drink and Drive

Similar Threads

  1. exception class
    By arulmozs in forum AWT / Swing
    Replies: 2
    Last Post: 10-29-2009, 02:18 PM
  2. Replies: 4
    Last Post: 10-22-2009, 05:45 PM
  3. Replies: 3
    Last Post: 03-14-2009, 07:22 AM
  4. Replies: 13
    Last Post: 08-23-2008, 09:09 PM
  5. Getting objects of a class
    By ravian in forum New To Java
    Replies: 1
    Last Post: 12-04-2007, 12:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •