Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Desparate for Help

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Desparate for Help

    Hi,

    i'm a complete newbie and i've been trying to do some course wotrk but i am completely lost with one of the activities i ahve been given. Here is the question;

    In this scenario you are writing code related to a college record-keeping system that maintains information about a number of courses, each of which has a code (such as A 123), a title (such as ‘Advanced Florestry’) and a number of points gained (such as 60).
    The college records certain details about its students, including their names and identifiers. Note that Instances of courses may have a number of students registered on them.
    Develop the class called Course that describes a college course. The skeleton of the Courseclass 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.

    You are also provided with getter methods for these variables and a constructor to be completed in part (c). In this part, you should create toString and equals methods to override those inherited from the Object class. You are required to write the following methods:

    the toString method should return a string containing the details of the course, including the course code, its title and the number of points gained in parentheses, for example A 123, Advanced Florestry(60);
    the 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. (We assume that course codes are unique.)

    In this part, you should create an exception class called InvalidCoursein 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 part (c) below.

    In this part you should complete the Course class constructor, which receives a code, a title and a number of points gained, and sets the instance data of the constructed object to the received values.
    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
    booleanvalidTitle() receives a String and returns true if its value is not null; otherwise the method returns false.

    You should ensure that the Course class constructor uses these helper methods to determine if the course data it receives is valid.

    If the course data is valid, the constructor should complete normally, setting the instance data to the received values. If any of the arguments is not valid, the constructor should throw an InvalidCourse exception with an appropriate error message, which should include the faulty data value.

    Now i'm just not getting anything to work here at all and i'm just getting in a mess. Here is what i have so far as i can't work out how to do the rest can anyone help me??

    public class Course
    {
        private String code;
        private String title;
        private int points;
     
        // complete the class constructor
        public Course(String aCode, String aTitle, int pts) 
        {
           code = aCode;
           title = aTitle;
           points = pts;
        }    
     
       //the toString method
        public String toString()
        {
           return (code + ", " + title + " (" + points + ") ");
        }
     
        //the equals method
        public boolean equals (Course c)
        {
           return code.equals(c.code);
        }
     
        //the validCode method 
     
     
        //the validPoints method    
     
     
        //the validTitle method
     
        //provided getters
        public String getCode()
        {
            return code;
        }
     
        public String getTitle()
        {
            return title;
        }
     
        public int getPoints()
        {
            return points;
        }   
     
    }

  2. #2
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Desparate for Help

    Can anyone offer a little help

  3. #3
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Desparate for Help

    Hello pumpkin.

    So, your exercise is split into 3 parts as I see it.
    -Making a course class
    -Making an exception for the course class
    -Implementing the exception into the course class, using the 3 helper methods.

    1.
    You are almost done with the first point, but you need a little work on the overwritten equals method. You are currently only checking the code variable, where you should ideally check all 3 of the required variables.
    Making a good equals method is not always as easy as it would first look. Hint: a good equals method should be: reflexive, symmetric and transitive.

    2. Exceptions.
    You are gonna need some reading here. You need to make clear to yourself, the difference between a checked and an unchecked exception. These two types exception make for different implementation, so understanding how they work is key here. The course literature should provide the information needed, if not, google will never let you down.

    3.
    Once you have a good understanding of the exception types, this part should not provide too much trouble.

    Feel free to post further questions.
    Last edited by Johannes; January 20th, 2010 at 04:16 AM.

  4. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Desparate for Help

    Thank you, i'll get reading and see what i can come up with!!

  5. #5
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Desparate for Help

    If you run into concrete problems with implementation post it here and I will have a look.

  6. #6
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Desparate for Help

    Although your code doesn't currently do what it needs to, it should compile, right?

    You should be able to write the validation methods reasonably easily (the validCode method is the only one which might be a bit difficult), which will let you write your constructor properly, and then worry about writing the exception class.