[RESOLVED]Printing...simple <<just newb
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 );
}
}
Re: Printing...simple <<just newb
Welcome to the forums! :)
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?
Re: Printing...simple <<just newb
Im new to this...and im just confused on how I would print the highest grade along with the name that goes along with it.
Re: Printing...simple <<just newb
Is that all the code you have? Cause you don't seem to be calling the method anywhere. I'm a little confused...
Re: Printing...simple <<just newb
sorry, im also using...
VB Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class InputReader
{
private BufferedReader reader;
/**
* Create a new InputReader that reads text from the text terminal.
*/
public InputReader()
{
reader = new BufferedReader(new InputStreamReader(System.in));;
}
/**
* Read a line of text from standard input (the text terminal), and
* return it as a String.
*
* @return A String typed by the user.
*/
public String getInput()
{
System.out.print("> "); // print prompt
String inputLine = readInputLine();
return inputLine;
}
/**
* Read one line of input and return it as a String.
*
* @return A String representing the input, or an empty String
* if an error occurs.
*/
private String readInputLine()
{
String line = "";
try {
line = reader.readLine();
}
catch(java.io.IOException exc) {
System.out.println ("Read error: " + exc.getMessage());
}
return line;
}
}
and
VB Code:
public class info
{
// instance variables - replace the example below with your own
private String name;
private double grade;
/**
* Constructor for objects of class info
*/
public info(String nam, double grad)
{
name = nam;
grade = grad;
}
/**
* 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 String toString()
{
return name + " " + grade;
}
}
sorry
Re: Printing...simple <<just newb
I guess your code is missing the "main" so:
Code:
/**
* Main Program
*
* @param args String[]
*/
public static void main (String args[]) {
course c = new course() ;
c.addStudnet() ;
c.getAverage() ;
c.printStudents() ;
c.printHighandLow() ;
}
Re: Printing...simple <<just newb
But I'm still wondering why all this complex code
most of it is useless
Re: Printing...simple <<just newb
its what is required of me, is why I have all this code...I don't know haha.
Thanks for the help.
so what would I do next...
Re: Printing...simple <<just newb
compile and run!
your code is ready to be compiled
Re: Printing...simple <<just newb
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.
Re: Printing...simple <<just newb
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.
Re: Printing...simple <<just newb
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
Re: Printing...simple <<just newb
<--- 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
Re: Printing...simple <<just newb
Quote:
Originally Posted by stinkoman
<--- Really confused, im trying to figure this out....Not only am I new to this, I have been away from it for a while.
What's confusing you?
Re: Printing...simple <<just newb
Quote:
Originally Posted by ComputerJy
What's confusing you?
Everything, I don't know exactly what I should do with the code you gave me...Ive just been messing with it..I can't figure it out....I feel newb haha
Re: Printing...simple <<just newb
The code doesn't need any changing
just apply the previous instructions and write a similar method to get the lowest info given
Re: Printing...simple <<just newb
so you saying it should work if I just paste that in? and where do I need to put the getGrade()
1 Attachment(s)
Re: Printing...simple <<just newb
I've changed a few things in your code
if you have any problem understanding it don't be shy to ask
Re: Printing...simple <<just newb
Thank you, I am going over it right now...Thanks for all the help.