Results 1 to 18 of 18
  1. #1
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Array object help please...

    I am having trouble with this program. I am pretty sure I am only suppoed to use a single array to hold my names and grades, but I do not know how to do that. I think my Student class is complete, so I am working on the client class to test it. I am at a stand still with the fillArray method. My directions say to loop for teh names and grades, then create a new student object as the next element in the student array. I have no idea what that means. I appreciate any help.

    Client class
    Java Code:
    import java.util.Scanner;
    import java.text.*;
    
    /**
     Class to demonstrate the Student class
    */
    public class StudentClient
    {
        public static void main( String[] args )
        {
            System.out.println( "name" );
    
            Scanner kb = new Scanner( System.in );
    
            System.out.print( "How many students? " );
            int howMany = kb.nextInt();
    
            // Create the Student array; call it roster
    
            Student[] roster = new Student[howMany];
    
    
            // Leave these lines alone. Write methods below.
            // Call a method to fill the array of Students
            fillArray( roster );
    
            // Call a method to display the roster (name & grade)
            System.out.println("\nStudents:");
            display( roster );
    
            // Call a method to show the name(s) of the
            // Student(s) with the highest grade
            showMax( roster );
    
            // Call a method to find the average grade.
            showAvg( roster );
    
            // Call a method to determine and show the
            // letter grade for each student.
            showLetterGrade( roster );
    
            // Call a method to sort an array of Students
            // by name. Then redisplay it.
            System.out.println("\nAlphabetical listing:");
            sortName( roster );
            display( roster );
    
            // Call a method to sort an array of Students
            // by grade, high to low. Then redisplay it.
            System.out.println("\nStudents by grade, high to low");
            sortGrade( roster );
            display( roster );
    
        }
    
    	 public static void fillArray( Student[] std )
        {
    		 Scanner kb = new Scanner (System.in);
    
    		 for (int i = 0; i < std.length; i++)
    		 {
    			 System.out.println();
    			 System.out.print("Name: ");
    			 String name = kb.next();
    
    			 System.out.print("Grade <0 - 100>: ");
    			 int grade = kb.nextInt();
    
    
    			 while ( grade < 0 || grade > 100)
    			 {
    				 System.out.print("Re-enter grade <0 - 100>: ");
    				 grade = kb.nextInt();
    			 }
    		 }
    
        }
    
        public static void display( Student[] std )
        {
    
        }
    
        public static void showMax( Student[] std )
        {
    
        }
    
        public static void showAvg( Student[] std )
        {
    
        }
    
        public static void showLetterGrade( Student[] std )
        {
    
        }
    
        public static void sortName( Student[] std )
        {
    
        }
    
        public static void sortGrade( Student[] std )
        {
    
    	}
    
    }
    Student class
    Java Code:
    public class Student
    {
        private String name;
        private int grade;
    
        public Student( String newName )
        {
    		 // Sets name to newName, grade to 0.
    
    		 name = newName;
    
           grade = 0;
    
        }
    
        public Student(String newName, int newGrade )
        {
            // Validates newGrade: between 0 and 100
            // If newGrade isn't valid, error message and quit.
            // Otherwise, sets name to newName, grade to newGrade.
    
            if ( newGrade < 0 || newGrade > 100 )
            {
    			  System.out.println("Error: Invalid Grade");
    
    			  System.exit(1);
    		  }
    
    		  else
    		  {
    			  name = newName;
    
    			  grade = newGrade;
    		  }
    
    
        }
    
        public void setGrade( int newGrade )
        {
            // Validates newGrade: between 0 and 100.
            // If newGrade isn't valid, error message and
            // leave grade unchanged.
    
            if ( newGrade < 0 || newGrade > 100 )
            {
    			  System.out.println("Error: Invalid Grade");
    		  }
    
    	  }
    
        public int getGrade()
        {
    		 return grade;
    
        }
    
        public String getName()
        {
    		 return name;
    
        }
    
        public String toString()
        {
    		 // Form should be:
    		 // Name: John Smith
    		 // Grade: 98
    
    		 String str = "Name: " + name + "\nGrade: " + grade;
    
    		 return str;
    	 }
    
    }

  2. #2
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Array object help please...

    i'd use an arraylist, which is a dynamic array. declare it at StudentClient class level. here's an example:

    Java For Complete Beginners - array lists

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Array object help please...

    My directions say to loop for teh names and grades, then create a new student object as the next element in the student array. I have no idea what that means.
    What it means is this:

    Java Code:
    public static void fillArray( Student[] std )
    {
        Scanner kb = new Scanner (System.in);
     
        for (int i = 0; i < std.length; i++)
        {
            System.out.println();
            System.out.print("Name: ");
            String name = kb.next();
     
            System.out.print("Grade <0 - 100>: ");
            int grade = kb.nextInt();
     
            while ( grade < 0 || grade > 100)
            {
                System.out.print("Re-enter grade <0 - 100>: ");
                grade = kb.nextInt();
            }
    
            // (1) create a new student object
            // (2) put that student into the array
         }
    }
    As far as (1) is concerned you have a constructor that will do this using name and grade. As far as (2) is concerned you have the array index i at which place the newly created student can be assigned.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Array object help please...

    Quote Originally Posted by .paul. View Post
    i'd use an arraylist, which is a dynamic array. declare it at StudentClient class level. here's an example:...
    Lists are nice (ArrayList, LinkedList, etc). But the OP says "I am pretty sure I am only suppoed to use a single array" and the supplied code adds "// Create the Student array;", "// Leave these lines alone." etc.

  5. #5
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Array object help please...

    Quote Originally Posted by pbrockway2 View Post
    Lists are nice (ArrayList, LinkedList, etc). But the OP says "I am pretty sure I am only suppoed to use a single array" and the supplied code adds "// Create the Student array;", "// Leave these lines alone." etc.
    ok. how do you create an array + dynamically resize it as necessary?

    ok. i spotted it... it's not meant to be dynamic

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Array object help please...

    it's not meant to be dynamic
    That's my take on it. But you're quite right: a dynamic list would be easier.

  7. #7
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Array object help please...

    Quote Originally Posted by pbrockway2 View Post
    What it means is this:

    Java Code:
    public static void fillArray( Student[] std )
    {
        Scanner kb = new Scanner (System.in);
     
        for (int i = 0; i < std.length; i++)
        {
            System.out.println();
            System.out.print("Name: ");
            String name = kb.next();
     
            System.out.print("Grade <0 - 100>: ");
            int grade = kb.nextInt();
     
            while ( grade < 0 || grade > 100)
            {
                System.out.print("Re-enter grade <0 - 100>: ");
                grade = kb.nextInt();
            }
    
            // (1) create a new student object
            // (2) put that student into the array
         }
    }
    As far as (1) is concerned you have a constructor that will do this using name and grade. As far as (2) is concerned you have the array index i at which place the newly created student can be assigned.
    Ok so bear with me this 8 week course is way to fast paced for my lace of programming experience. For 1. I just want to create an object like Student studentTotals = new Student (int grade, String name), or do I leave the parameters out? Then for 2. I am confused how I incorporate studentTotals into the array. Would I do something like std [i] = studentTotals after the loop ends? That probably makes no sense.

    .paul I appreciate the help. I will continue working on it, and I am sure I will have more questions.

    Another thing that confuses me is will I always be using the std[] array? I assumed I was going to use the roster array I created in the beginning.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Array object help please...

    You don't use the parameter types when calling a method or constructor, so try

    Java Code:
    Student studentTotals = new Student (grade, name);
    ... or is it Student(name,grade)?

    Would I do something like std [i] = studentTotals after the loop ends?
    No, try it right where I suggested inside the loop. The point is that this method has nothing to do with student totals - it is simply filling the array with one student for each lot of data entered. In fact, studentTotals is not such a good name for this student. student might be better. Or, I'm partial to toAdd when I'm constructing something that is going to be immediately added to an array. The actual variable you use doesn't matter much: the important thing is to create the student and add the student inside the loop so that lots of students get added.

    will I always be using the std[] array? I assumed I was going to use the roster array
    Inside the fillArray() method you use std because that's what the array was called in the first line that declares the method: "public static void fillArray( Student[] std )". Of course when the method actually get called it will be the roster array that gets added to. Same array, different names. In main() it is called roster while in fillArray() it is called std.

    This business of variables takes a bit of getting used to but it turns out to be *very* useful to refer to the same thing with different names like this. Basically variables (names) only exist within the method (or constructor or class) where they are declared. Actually it's just within the block where they are declared, but don't worry too much about that. Variables declared as part of a method like std (so called local variables) are a sort of "throw away" name that you use for just a short time. And that means you only have to worry about what a variable means (what it refers to as a meaningful unit of your program) for a very short time: just for the duration of the method. Personally I would document a method like fillArray() to say, amongst other things, was std was all about.

    Java Code:
        /**
         * Fills a given array with students created from data read from the console.
         * @param  std  the array to be filled
         */
    public static void fillArray( Student[] std )
    {
        Scanner kb = new Scanner (System.in);
        // etc
    Let me emphasise that you don't *need* this comment. But it's the sort of thing I always include so I know later what it was exactly the method does, and what role is played by std. (The last point was what you asked about, and hence this excursion into javadoc comments, as they are called. Google "javadoc" if you're interested.)

    [Edit] I've just realised that the fillArray() method illustrates another reason for variables as "throw away" names - it is deliberate that you use the same variable to add lots of students. And you can get away with one variable for lots of students (constructed and added one at a time) because of the impermanent nature of variables.
    Last edited by pbrockway2; 07-10-2012 at 09:49 AM.

  9. #9
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Array object help please...

    Ok I thought the showMax method would be pretty straight forward but I am having trouble. I get the concept of it but I do not know how to write it to appearently. Also I already know that two grades are going to be the max and am confused how to get both to display. I can probably figure out how to get one to display, but two I am not so sure. Here is my client class, the student is unchanged.

    Java Code:
    import java.util.Scanner;
    import java.text.*;
    
    /**
     Class to demonstrate the Student class
    */
    public class StudentClient
    {
        public static void main( String[] args )
        {
            System.out.println( "name" );
    
            Scanner kb = new Scanner( System.in );
    
            System.out.print( "How many students? " );
            int howMany = kb.nextInt();
    
            // Create the Student array; call it roster
    
            Student[] roster = new Student[howMany];
    
    
            // Leave these lines alone. Write methods below.
            // Call a method to fill the array of Students
            fillArray( roster );
    
            // Call a method to display the roster (name & grade)
            System.out.println("\nStudents:");
            display( roster );
    
            // Call a method to show the name(s) of the
            // Student(s) with the highest grade
            showMax( roster );
    
            // Call a method to find the average grade.
            showAvg( roster );
    
            // Call a method to determine and show the
            // letter grade for each student.
            showLetterGrade( roster );
    
            // Call a method to sort an array of Students
            // by name. Then redisplay it.
            System.out.println("\nAlphabetical listing:");
            sortName( roster );
            display( roster );
    
            // Call a method to sort an array of Students
            // by grade, high to low. Then redisplay it.
            System.out.println("\nStudents by grade, high to low");
            sortGrade( roster );
            display( roster );
    
        }
    
    	 public static void fillArray( Student[] std )
        {
    		 Scanner kb = new Scanner (System.in);
    
    		 for (int i = 0; i < std.length; i++)
    		 {
    			 System.out.println();
    			 System.out.print("Name: ");
    			 String name = kb.next();
    
    			 System.out.print("Grade <0 - 100>: ");
    			 int grade = kb.nextInt();
    
    
    			 while ( grade < 0 || grade > 100)
    			 {
    				 System.out.print("Re-enter grade <0 - 100>: ");
    				 grade = kb.nextInt();
    			 }
    
    			 Student newStudent = new Student (name, grade);
    
    			 std[i] = newStudent;
    
    		 }
    
        }
    
        public static void display( Student[] std )
        {
    		 for (int i = 0; i < std.length; i++)
    
    			 System.out.println(std[i].toString());
        }
    
        public static void showMax( Student[] std )
        {
    		 int max = -1;
    		 int maxIndex;
    
    		 for (int i = 0; i < std.length; i++)
    		 {
    			 if ( std[i] >= max )
    			 {
    				 max = std[i];
    				 maxIndex = i;
    			 }
    		 }
    
        }
    
        public static void showAvg( Student[] std )
        {
    
        }
    
        public static void showLetterGrade( Student[] std )
        {
    
        }
    
        public static void sortName( Student[] std )
        {
    
        }
    
        public static void sortGrade( Student[] std )
        {
    
    	}
    
    }
    Oh this does not compile I know my showMax loop is garbage.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Array object help please...

    this does not compile
    If you don't understand the compiler message, post it and say to which line of your code it is referring.

  11. #11
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Array object help please...

    C:\Users\Mike\Documents\School\Java\MichaelMarsocc i-J6\StudentClient.java:106: error: bad operand types for binary operator '>='
    if ( std[i] >= max )
    ^
    first type: Student
    second type: int
    C:\Users\Mike\Documents\School\Java\MichaelMarsocc i-J6\StudentClient.java:108: error: incompatible types
    max = std[i];
    ^
    required: int
    found: Student
    2 errors

    Tool completed with exit code 1

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Array object help please...

    Quote Originally Posted by VettesRus View Post
    C:\Users\Mike\Documents\School\Java\MichaelMarsocc i-J6\StudentClient.java:106: error: bad operand types for binary operator '>='
    if ( std[i] >= max )
    ^
    first type: Student
    second type: int
    The >= operator is meant for comparing numeric quantities (numbers). What you are trying to do in this line is compare a student (std[i]) with a number (max) and the compiler is telling you that such a comparison makes no sense.

    What number do you really wish to compare with max?

    -----

    The other error is similar. You are trying to assign a student to the numeric variable max. What you should be assigning is a numeric magnitude.

  13. #13
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Array object help please...

    Im trying to compare the entered grades to see if they are greater than max, then store the new grade into max so I can display it as the highest grade. Ill try it right now anyway, but I probably need to use newStudent somewhere in there considering it is holding the values right?

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Array object help please...

    Im trying to compare the entered grades to see if they are greater than max, then store the new grade into max so I can display it as the highest grade.
    Yes. But stated more precisely that particular line ought to compare a specific student's grade with max. That student being std[i].

    I probably need to use newStudent somewhere
    newStudent doesn't exist in the showMax() method - it was a variable declared in a different method. std[i] is the student whose grade is important.

  15. #15
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Array object help please...

    Quote Originally Posted by pbrockway2 View Post
    Yes. But stated more precisely that particular line ought to compare a specific student's grade with max. That student being std[i].



    newStudent doesn't exist in the showMax() method - it was a variable declared in a different method. std[i] is the student whose grade is important.
    Makes sense, I am having trouble comparing the std[i] with max though. The error says std is a student and it needs to be an int to compare with max. Is it possible to extract just the grades from the student object in the showMax method? Im guessing my problem std[i] >= max is trying to compare both the names and grades instead of just grades.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: Array object help please...

    Look for a method on Student that gives you the thing you want to compare against max...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Array object help please...

    Quote Originally Posted by Tolls View Post
    Look for a method on Student that gives you the thing you want to compare against max...
    I appreciate the help. I have been playing around with the getGrade method and student method without much luck though. I am not really sure how to do that, but I am guessing that is what needs to be done.

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Array object help please...

    I have been playing around with the getGrade method
    If there is a compiler message you can't understand, post it and the code it refers to to.

    You have a student (std[i]) and a getGrade() method to get the grade and a number (max) that you want to compare the grade to. It's a matter of putting those three things together.

Similar Threads

  1. 2D Object array
    By Shashir Bharadwaj in forum New To Java
    Replies: 1
    Last Post: 02-03-2012, 10:16 AM
  2. Look if an array has an specified object
    By Josep_16 in forum New To Java
    Replies: 0
    Last Post: 10-09-2011, 11:43 AM
  3. Is an object array considered to be an object?
    By guest_user in forum New To Java
    Replies: 1
    Last Post: 07-13-2011, 06:40 AM
  4. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  5. An object array into int
    By tyang in forum New To Java
    Replies: 1
    Last Post: 04-15-2010, 05:04 AM

Posting Permissions

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