Results 1 to 18 of 18
Thread: Array object help please...
- 07-10-2012, 06:30 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 13
- 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
Student classJava 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, 06:50 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Array object help please...
What it means is this: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.
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.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, 06:55 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
-
- 07-10-2012, 07:30 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Array object help please...
That's my take on it. But you're quite right: a dynamic list would be easier.it's not meant to be dynamic
- 07-10-2012, 07:45 AM #7
Member
- Join Date
- Jun 2012
- Posts
- 13
- 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, 09:40 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Array object help please...
You don't use the parameter types when calling a method or constructor, so try
... or is it Student(name,grade)?Java Code:Student studentTotals = new Student (grade, name);
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.Would I do something like std [i] = studentTotals after the loop ends?
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.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.
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.)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 09:49 AM.
- 07-10-2012, 10:28 PM #9
Member
- Join Date
- Jun 2012
- Posts
- 13
- 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.
Oh this does not compile I know my showMax loop is garbage.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, 12:36 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Array object help please...
If you don't understand the compiler message, post it and say to which line of your code it is referring.this does not compile
- 07-11-2012, 04:28 AM #11
Member
- Join Date
- Jun 2012
- Posts
- 13
- 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, 05:57 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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, 06:51 AM #13
Member
- Join Date
- Jun 2012
- Posts
- 13
- 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, 07:44 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Array object help please...
Yes. But stated more precisely that particular line ought to compare a specific student's grade with max. That student being std[i].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.
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.I probably need to use newStudent somewhere
- 07-11-2012, 05:42 PM #15
Member
- Join Date
- Jun 2012
- Posts
- 13
- 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, 06:13 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
- 07-11-2012, 06:53 PM #17
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
- 07-11-2012, 10:15 PM #18
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Array object help please...
If there is a compiler message you can't understand, post it and the code it refers to to.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, 10:16 AM -
Look if an array has an specified object
By Josep_16 in forum New To JavaReplies: 0Last Post: 10-09-2011, 11:43 AM -
Is an object array considered to be an object?
By guest_user in forum New To JavaReplies: 1Last Post: 07-13-2011, 06: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, 08:12 PM -
An object array into int
By tyang in forum New To JavaReplies: 1Last Post: 04-15-2010, 05:04 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks