This is just a small project that I am working on for class, and for my own benefit. Basically, what I want to do is...when I "printHighandLow()" I want it to print out the name as well as the grade the person has rather then just the high and low grade. Did I explain this well enough, anyone know a solution?
VB Code:
import java.util.ArrayList;
public class course
{
// instance variables
ArrayList students;
ArrayList grades;
private InputReader reader;
double sum;
double totalgrades;
double avg;
double highgrade;
double lowgrade;
/**
* Constructor for objects of class course
*/
public course()
{
//
students = new ArrayList();
grades = new ArrayList();
reader = new InputReader();
highgrade = highgrade;
lowgrade = 999999;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void addStudnet()
{
info newstudent;
String name;
boolean finished = false;
String gradeSt;
double grade;
welcome();
while(!finished)
{
System.out.println(" Enter the name of the student");
name = reader.getInput();
if(name.startsWith("bye"))
{
finished = true;
System.out.println("Good Bye");
}
else
{
System.out.println(" Enter a grade");
gradeSt = reader.getInput();
System.out.println("Done");
grade = Double.parseDouble(gradeSt);
newstudent = new info(name , grade);
students.add(newstudent);
// totals up the grades(used in the Average method)
sum = sum + grade;
if (grade > highgrade)
{
highgrade = grade;
}
if (grade < lowgrade)
{
lowgrade = grade;
}
/*if (gradeSt.compareTo(lowgrade) < 0)
{
lowgrade.equals(newstudent);
}*/
// totals up the number of grades.
totalgrades++;
}
}
}
public void welcome()
{
System.out.println("Type bye to leave");
}
public void printStudents()
{
int ndx;
for (ndx =0; ndx< students.size(); ndx++)
System.out.println(students.get(ndx));
}
public void printHighandLow()
{
System.out.println(highgrade);
System.out.println(lowgrade);
}
public void getAverage()
{
avg = sum/totalgrades;
System.out.println(" Average = "+ avg );
}
}
Last edited by stinkoman; Mar 4th, 2006 at 12:25 PM.
To print the name as well as the marks you have to do something like you already have done in the getAverage method. What part more specifically are you having trouble with?
Has someone helped you? Then you can Rate their helpful post.
well I know that, but I don't need to change anything else? That didn't quite work....what im trying to do is print out the HighandLow with the owners of the HighandLow grades right next to each other. So I know who had that high and low.
Last edited by stinkoman; Mar 3rd, 2006 at 11:01 AM.
somehow I need to associate the grades with the names in the array...so I can print them both. I need to see the name next to the grade so that you can see who had the grades.
Then you'll have to replace the ArrayList of students and the ArrayList of grades with one ArrayList of infos
Then you can use the following method to get high and low:
Code:
private info highestInfo () {
//infos is supposed to be the ArrayList that contains info objects
info tmp = (info) infos.get(0) ;
Iterator i = infos.iterator() ;
//visit all members in the ArrayList
while (i.hasNext()) {
info tmp2 = (info) i.next() ;
if (tmp2.getGrade() > tmp.getGrade()) {
tmp = tmp2 ;
}
}
return tmp ;
}
This one get the high, write another one to get the low
p.s. Add getGrade() to info class
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson My Blog
<--- Really confused, im trying to figure this out....Not only am I new to this, I have been away from it for a while. Thank you for the help though so far
Last edited by stinkoman; Mar 3rd, 2006 at 07:20 PM.