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
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
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
Re: Array object help please...
Quote:
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:
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.
Re: Array object help please...
Quote:
Originally Posted by
.paul.
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.
Re: Array object help please...
Quote:
Originally Posted by
pbrockway2
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
Re: Array object help please...
Quote:
it's not meant to be dynamic
That's my take on it. But you're quite right: a dynamic list would be easier.
Re: Array object help please...
Quote:
Originally Posted by
pbrockway2
What it means is this:
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.
Re: Array object help please...
You don't use the parameter types when calling a method or constructor, so try
Code:
Student studentTotals = new Student (grade, name);
... or is it Student(name,grade)?
Quote:
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.
Quote:
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.
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.
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.
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.
Re: Array object help please...
Quote:
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.
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
Re: Array object help please...
Quote:
Originally Posted by
VettesRus
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.
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?
Re: Array object help please...
Quote:
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].
Quote:
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.
Re: Array object help please...
Quote:
Originally Posted by
pbrockway2
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.
Re: Array object help please...
Look for a method on Student that gives you the thing you want to compare against max...
Re: Array object help please...
Quote:
Originally Posted by
Tolls
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.
Re: Array object help please...
Quote:
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.