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

Thread: NEED HELP WITH IDEAS

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

    Default NEED HELP WITH IDEAS

    hi everybody! i need help basically figuring out how to start this assignment using the comparable interface:

    1. 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.
    Note that there are two approaches to implementing the interface:
    • one using Java generics – we’ll talk about generics in a few lectures,
    • the other to pass an Object into the compareTo method and to cast the object parameter into
    the desired type – in this case a Student object).
    For this assignment, use the latter approach. 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;
    }




    any ideas??!


  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: NEED HELP WITH IDEAS

    I would say the first step is to create the class which will store all the data. Then spend the next fifteen days banging on the screen when the code doesnt do what it should. Turn in a half-assed assignment the following day and get a score of the same quality as the work. But that is the kinda guy I am...

    Doesn't your teacher say to do something like an outline first? or some fake code thingy?
    you done that yet?

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

    Default Re: NEED HELP WITH IDEAS

    honestly, i missed the class in which we were assigned this because of a death in the family. and now the code's due tomorrow and i've got to do this as well as like three other assignments. i'm a little confused on everything and i'm trying to catch up on reading and figure this out.

    i'm having trouble with syntax. i know this reply is pretty much the most useless thing ever, but i'm really frustrated. sorry

Similar Threads

  1. Project ideas
    By seal308 in forum Collections and Generics
    Replies: 2
    Last Post: August 3rd, 2012, 03:27 PM
  2. Game Ideas...
    By SupportIsPower in forum Java Theory & Questions
    Replies: 0
    Last Post: March 11th, 2012, 04:03 PM
  3. Any ideas?
    By ahender1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 2nd, 2012, 03:58 PM
  4. Any Program Ideas?
    By Java Programmer in forum Java Theory & Questions
    Replies: 7
    Last Post: January 18th, 2012, 08:05 AM
  5. Running out of ideas...
    By Cat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2011, 11:21 AM