Results 1 to 11 of 11
  1. #1
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Talking private data members?

    For one of my classes I need to do this:

    private data members include a class name, an array of Students, a class average gpa, a number of students, and a reference to a Student object called Valedictorian

    Java Code:
    		private String className;
    		private String[] students;
    		private double classAverageGPA;
    		private int numStudents;
    how do I do the reference to a Student object called Valedictorian?
    +1 rep to all responses!

  2. #2
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    ... all helpful responses lol

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If this were my program, I'd consider not having a valedictorian variable or field but instead only a getValedictorian() method. Then in this method I'd iterate through the Student[] array looking for the student with the best GPA and return that.

  4. #4
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    Yeah, I agree with you; however, my teacher told us we have to do it this way.
    how do you think i should do it?

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Java Code:
    private Student valedictorian;
    I think that's what you're looking for. If so, remember that you would do it the same way you instantiate a String (private String className;) String is an object and so is Student. This is how it would look:
    Java Code:
    		private String className;
    		private String[] students;
    		private double classAverageGPA;
    		private int numStudents;
                    private Student valedictorian;
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  6. #6
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Fubarable, I dont think that he has a Student array (Student[]) he just has an array of Strings. I really don't see a Student object anywhere.
    @ OP: In order to find the valedictorian, you need to have all the students GPA's, where are they?

    Oh, after reading Lil Aziz's post I think I might be over thinking it.
    Last edited by collin389; 01-19-2010 at 02:47 AM.

  7. #7
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Shouldn't

    Java Code:
    private String[] students;
    be
    Java Code:
    private Student[] students;
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  8. #8
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    yeah i think i got it
    thanks everyone
    +1reps all around lol

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Also tell your teacher that since this is Java and not C, you don't need to carry the length of an array around with you. The student array's length is the number of students.

    Edit: If you rep me for that I'll slap you in the face with a fish.
    Last edited by r035198x; 01-19-2010 at 08:00 AM.

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by r035198x View Post
    Also tell your teacher that since this is Java and not C, you don't need to carry the length of an array around with you. The student array's length is the number of students.

    Edit: If you rep me for that I'll slap you in the face with a fish.
    Yes, but it is still common place to have an array that is larger than necessary, and keeping the index of the "end" and/or "begin" items. String itself does this with its underlying char array, which is how Strings created using substring share the same char array with the string it was created from.

    And, of course, since "numberOfStudents" would be the last index + 1 it may be meant for the same purpose.

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Fair enough. It's probably one of those manual management of arrays exercises anyway.

    <puts fish back in bag />

Similar Threads

  1. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 04:19 AM
  2. Hello to all members!
    By tmdurand in forum Introductions
    Replies: 3
    Last Post: 01-18-2009, 02:54 PM
  3. simple question about private data
    By littleBean in forum New To Java
    Replies: 12
    Last Post: 07-02-2008, 04:09 PM
  4. hi all members
    By ismailsaleh in forum Introductions
    Replies: 2
    Last Post: 01-07-2008, 03:23 PM

Tags for this Thread

Posting Permissions

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