Results 1 to 18 of 18
Thread: Array object help please...
- 07-10-2012, 07:30 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 35
- Rep Power
- 0
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 ) { } }
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; } }
-
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
- 07-10-2012, 07:50 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
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.
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 } }
- 07-10-2012, 07:55 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
-
- 07-10-2012, 08:30 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Array object help please...
it's not meant to be dynamic
- 07-10-2012, 08:45 AM #7
Member
- Join Date
- Jun 2012
- Posts
- 35
- Rep Power
- 0
Re: Array object help please...
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.
- 07-10-2012, 10:40 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
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);
Would I do something like std [i] = studentTotals after the loop ends?
will I always be using the std[] array? I assumed I was going to use the roster array
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
[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 10:49 AM.
- 07-10-2012, 11:28 PM #9
Member
- Join Date
- Jun 2012
- Posts
- 35
- Rep Power
- 0
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 ) { } }
- 07-11-2012, 01:36 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Array object help please...
this does not compile
- 07-11-2012, 05:28 AM #11
Member
- Join Date
- Jun 2012
- Posts
- 35
- Rep Power
- 0
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
- 07-11-2012, 06:57 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Array object help please...
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.
- 07-11-2012, 07:51 AM #13
Member
- Join Date
- Jun 2012
- Posts
- 35
- Rep Power
- 0
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?
- 07-11-2012, 08:44 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
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.
I probably need to use newStudent somewhere
- 07-11-2012, 06:42 PM #15
Member
- Join Date
- Jun 2012
- Posts
- 35
- Rep Power
- 0
Re: Array object help please...
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.
- 07-11-2012, 07:13 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 07-11-2012, 07:53 PM #17
Member
- Join Date
- Jun 2012
- Posts
- 35
- Rep Power
- 0
- 07-11-2012, 11:15 PM #18
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Array object help please...
I have been playing around with the getGrade method
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
-
2D Object array
By Shashir Bharadwaj in forum New To JavaReplies: 1Last Post: 02-03-2012, 11:16 AM -
Look if an array has an specified object
By Josep_16 in forum New To JavaReplies: 0Last Post: 10-09-2011, 12:43 PM -
Is an object array considered to be an object?
By guest_user in forum New To JavaReplies: 1Last Post: 07-13-2011, 07:40 AM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 09:12 PM -
An object array into int
By tyang in forum New To JavaReplies: 1Last Post: 04-15-2010, 06:04 AM
Bookmarks