0

I'm having trouble with calling a method. The basis of the program is to read in data from data.txt, grab the name token given, then all of the grades that follow, then implement some operations on the grades to give details of the person's grades. I do all of the methods in a separate file named Grades.java, which has the Grades class. I'm just having trouble because I MUST have the testGrades method in my code (which I don't find necessary). I have done everything I need to do for the results to be perfect in a different program without having two different .java files. But it's necessary to do it this way. I think I have mostly everything pinned down, I'm just confused on how to implement and call the testGrades method. I commented it out and have the question on where it is in the program. Quite new to classes and objects, and java in general. Sorry for the lame question.

public class Lab2 {

public static void main(String[] args) {

    Scanner in = null; //initialize scanner
    ArrayList<Integer> gradeList = new ArrayList<Integer>(); //initialize gradeList
     //grab data from data.txt 
    try {
        in = new Scanner(new File("data.txt"));
    } catch (FileNotFoundException exception) {
        System.err.println("failed to open data.txt");
        System.exit(1);
    }
    //while loop to grab tokens from data
    while (in.hasNext()) {
        String studentName = in.next();   //name is the first token
        while (in.hasNextInt()) {   //while loop to grab all integer tokens after name
            int grade = in.nextInt();   //grade is next integer token
            gradeList.add(grade);       //adding every grade to gradeList
        }

      //grab all grades in gradeList and put them in an array to work with
        int[] sgrades = new int[gradeList.size()];
        for (int index = 0; index < gradeList.size(); index++) {
            sgrades[index] = gradeList.get(index);  //grade in gradeList put into grades array 
        }

     //testGrades(sgrades);     How would I implement this method call? 

    }
}

public static void testGrades(Grades grades) {
    System.out.println(grades.toString()); 
    System.out.printf("\tName:    %s\n", grades.getName());
    System.out.printf("\tLength:  %d\n", grades.length());
    System.out.printf("\tAverage: %.2f\n", grades.average());
    System.out.printf("\tMedian:  %.1f\n", grades.median());
    System.out.printf("\tMaximum: %d\n", grades.maximum());
    System.out.printf("\tMininum: %d\n", grades.minimum());
}

   }

This is a little snippet of the beginning of the Grades.java file

     public class Grades {

private String studentName; // name of student Grades represents
private int[] grades; // array of student grades

public Grades(String name, int[] sgrades) {
    studentName = name; // initialize courseName
    grades = sgrades; // store grades
} 

      public String getName() {
    return studentName;
} // end method getName

public int length() {
    return grades.length; 
}
2
  • What are you supposed to do with the result of the computations? Commented Sep 17, 2013 at 0:44
  • All I need to do with computations is print them out, which is in the static method testGrades, at the bottom of the program. My only problem is knowing what arguments go into calling the testGrades method. Commented Sep 17, 2013 at 0:48

2 Answers 2

1

well your test grades take a Grades object so you need to construct a Grades object using your data and pass it to your test grades method i.e.

Grades myGrade = new Grades(studentName,sgrades);
testGrades(myGrade);  
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It does link now! This was really bugging me, I'll try fixing out all of my output bugs now. Much appreciated.
1

It looks like what you need to do is have some type of local variable in your main method, that would hold your custom Grade type. So you need add a line like..

Grades myGrades = new Grades(studentName, sgrades);

Then you can call your testGrades method with a line like...

testGrades(myGrades);

Looks like you may also need a toString method in your Grades class.

Seems like homework, so I tried to leave a bit to for you to figure out :)

2 Comments

Same as Sean F from up above, but yes thank you very much! The input is very much appreciated.
Last question bud, my results are coming out funky, and I know it's because I need to reinitialize the array and average, median, etc., but I don't know where to reinitialize the variables and array back to 0 and null? I put them in places I thought would work but no luck. Do you have any suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.