0

I have to create a code that takes user input for grades based on a students name that the user has inputted.

The input is to stop when a number less than 0 is inputted and the output should be the student name, the total of all the scores, and the average score.

For some reason I cannot get the average or the total to print, and my counter in my student class is showing an error "remove this token '++'"

Here is my main class, and my student class :

/**
* COSC 210-001 Assignment 2
* Prog2.java
* 
* description
* 
* @author Tristan Shumaker
*/
import java.util.Scanner;

public class main {

    public static void main( String[] args) {
        double[] addQuiz = new double[99];
        int counter = 0;
        //Creates new scanner for input
        Scanner in = new Scanner( System.in);

        //Prompts the user for the student name
        System.out.print("Enter Student Name: ");
        String name = in.nextLine();

        // requests first score and primes loop
        System.out.print("Enter Student Score: ");
        int scoreInput = in.nextInt();

        while( scoreInput >= 0 ) {
            System.out.print("Enter Student Score: ");
            scoreInput = in.nextInt();
            counter++;
        }
        System.out.println( );
        System.out.println("Student name: " + name);
        System.out.printf( "\nAverage: %1.2f", total(addQuiz, counter) );
        System.out.printf( "\nAverage: %1.2f", average(addQuiz, counter) );
    }
}

and my student class:

public class Student {
    private String name;
    private int total;
    private int counter;

    public Student() {
        super();
    }

    public String getName() {
        return name;
    }

     public void setName(String name) {
        this.name = name;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public void addQuiz( int scoreInput) {
        total += scoreInput;
        int counter++;
    }

    public static double average( double[] addQuiz, int counter ) {
        double sum = 0;
        for( int t = 0; t < counter; t++) {
            sum += addQuiz[t];
        }
        return (double) sum / counter;
    }
}

Any help you guys are able to give would be greatly appreciated, thanks in advanced.

John Slegers
  • 45,213
  • 22
  • 199
  • 169
theshu
  • 15
  • 6

2 Answers2

0

change int counter++; in the addQuiz() method to just counter++;, as otherwise you're trying to declare a variable with identifier counter++ which is not a valid identifier. Also since you've declared average() to be a static method on the Student class you'll need to call it like so:

Student.average(addQuiz, counter);

I'm not seeing a definition for total() in your code so I don't know if the same would apply to that.

EDIT

To answer why average() is returning zero, it looks like you never set any values in the addQuiz double array that you're passing in, so it will contain all zeros, and as a result sum will be 0. I think what you want to do is to change your while loop in the main method to put the scoreInput value in the array at the counter index like so:

while( scoreInput >= 0 ) {
    System.out.print("Enter Student Score: ");
    scoreInput = in.nextInt();
    addQuiz[counter] = scoreInput;
    counter++;
}
jonk
  • 1,494
  • 11
  • 13
  • Thanks so much. that was an error I looked at a hundred times and didn't notice. – theshu Feb 12 '16 at 00:47
  • `addQuiz` is a method on `Student` – Scary Wombat Feb 12 '16 at 00:51
  • @ScaryWombat yes it is; and it contains an error (which OP asked about). It's also a double array in OP's main class used when calling the `average()` method... what's your point? – jonk Feb 12 '16 at 00:53
  • and, while if got you helpful people here, do you guys know any reason why my average would be returning 0? – theshu Feb 12 '16 at 00:59
  • @thesu edited my answer with what I think you're wanting – jonk Feb 12 '16 at 01:10
  • @jonk you've already done plenty for me so i understand if you're busy or don't want to. But if you're free and dont mind taking a look, here is my partners post. he's got a few more questions that would help me out too http://stackoverflow.com/questions/35353994/issue-with-trying-to-fix-my-printf-statement/35354015#35354015 – theshu Feb 12 '16 at 03:11
  • @theshu it looks like your partner has got some help with his issues, one thing I noticed though is that neither of you are adding the first score entered (before the while loop), which might be leading to incorrect results. Also, as others have said to you both I also think you should be using the `Student` class and keep everything nice and encapsulated, if it's just a utility class for calculating the total and average I'd remove everything except the `static` methods from it. Finally, if this has answered your issues could you mark it as accepted and close the question. – jonk Feb 12 '16 at 09:38
0

In your main class you are not using your Student class at all.

Consider doing

Student student = new Student (name);

and then using the methods such as

student.addQuiz (scoreInput);

and later

student.getTotal ();

etc.

You also do not need to store the variable counter in the Student Object at all as it is being passed as a parameter.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Okay, I think I understand the concept of what your saying I'll try to figure it out. Thankyou – theshu Feb 12 '16 at 00:48