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 2 of 2

Thread: Beginning Programming - Need help with Ideas and Such- Please Help!! URGENT

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Beginning Programming - Need help with Ideas and Such- Please Help!! URGENT

    Here's my practice assignment. How should I start it?

    The Comparable interface is a very commonly implemented interface. Any class that implements it must define a method called compareTo that compares two objects of that class. If the object that invokes the method is “less than” the object being passed as a parameter, the method returns a negative number. If the object that invokes the method is “greater than” the object being passed as a parameter, the method returns a positive number. If the objects are “equal” the method returns zero. It is up to the programmer to decide what it means for one object to “less than” or “greater than” another object. In this lab, we’re going to extend the return values a bit further and be a little more specific. Each object will be compared based on several criteria (one at a time) to determine comparability. Each non-zero value returned by the compareTo method will indicate where the first determined difference lies. Create a Student class that stores the name, address, GPA, age, and major for a student. Additionally, the Student class should implement the Comparable interface. Make the comparison of Student objects based on the name, then the address, then the major, and finally the GPA. If the difference between two objects when compared lies in the name, return a 1 or -1 depending on which object is lessor/greater. If the difference in objects is in the address, return +/- 2. If the difference is in the major, return +/- 3. If the difference is in the GPA, return +/- 4. Only return zero if all 4 fields are the same. This behavior deviates slightly from the normal compareTo behavior (<0, 0, or >0), but we are ok with that for the purposes of this lab. Write a class called StudentDriver to test your new method. Hard code several students into the main method of the StudentDriver class with small differences in different fields. That is, make one student have one name and another a second name. Make a third student have the same name as the first, but a have the third student have a different address, etc. Make sure your compareTo method finds each the differences correctly.
    Implement the interface by passing an Object into the compareTo method and to cast the object parameter into the desired type – in this case a Student object).
    An example of this approach (note the parameter passing and
    casting) would be as follows:
    public int compareTo(Object anotherPerson) throws ClassCastException {
    if (!(anotherPerson instanceof Person))
    throw new ClassCastException("A Person object is expected by this compareTo.");
    int anotherPersonAge = ((Person) anotherPerson).getAge();
    return this.age - anotherPersonAge;
    }


    IDEAS/HELP PLEASE?!


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Beginning Programming - Need help with Ideas and Such- Please Help!! URGENT

    1) I start all of my projects with a detailed description of the problem to be solved.
    2) Then I come up with an algorithm that will solve the problem in question.
    3) Then I come up with pseudocode to implemet the algorithm.
    4) Then I turn the pseudocode into code.
    5) Then I run tests on the code.

    Looks like you are on step 2. (some steps may be a multi-step process)

Similar Threads

  1. Very interesting idea; beginning programmer. Please help if you can!
    By PufferFishX in forum Java Theory & Questions
    Replies: 0
    Last Post: August 4th, 2012, 11:37 PM
  2. Need urgent help regarding java word wrap function.. URGENT
    By coldice in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 05:43 AM
  3. Restarting at the beginning of an array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2010, 10:58 AM
  4. Error in printing method as always it prints the last value
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 1st, 2009, 10:37 AM
  5. Beginning java programmer!!!!!!!!!!!!!!!!!! need help
    By raidcomputer in forum Java Theory & Questions
    Replies: 3
    Last Post: September 15th, 2009, 08:52 PM