0

I need to fix my addQuiz() in my student class. Then, with that class, I pull all the info into my main of prog2. I have everything working except two things. I need to get the formula fixed for my addQuiz() so it totals the amount of points entered, and fix the while statement in my main so that I can enter a word to tell the program that I am done entering my quizzes.

Here is my main file.

import java.util.Scanner;

public class Prog2 {

 public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  Student student = new Student();
  //creates array for quizzes
  double[] grades = new double[99];
  //counter for total number of quizzes
  int num = 0;

  //requests user to enter students name
  System.out.print("Enter name of student: ");
  String name = in .nextLine();

  //requests user to enter students quizzes
  System.out.print("Enter students quiz grades: ");
  int quiz = in .nextInt();

  while (quiz >= 1) {

   System.out.print("Enter students quiz grades: ");
   quiz = in .nextInt();
   grades[num] = quiz;
   num++;
  }
  //prints the name, total, and average of students grades
  System.out.println();
  System.out.println(name);
  System.out.printf("\nTotal: ", student.addQuiz(grades, num));
  System.out.printf("\nAverage: %1.2f", student.Average(grades, num));
 }
}

here is my student file:

    public class Student {
private String name;
private int total;
private int quiz;
static int num;

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 int getQuiz() {
    return quiz;
}

public void setQuiz(int quiz) {
    this.quiz = quiz;
}

public static double addQuiz( double[] grades, int num){
    int totalQuiz = 0;
    for( int x = 0; x < num; x++){
        totalQuiz += grades[x];
    }
            return totalQuiz;
    }
public static double Average( double[] grades, int num){
    double sum = 0;
    for( int x = 0; x < num; x++){
        sum += grades [x];
    }
    return (double) sum / num;
}
}

Any help would be much appreciated!

T.Brown68
  • 11
  • 1
  • 8
  • see your friends question @ http://stackoverflow.com/questions/35352769/how-to-fix-my-counter-and-average-calculator/35352840#35352840 – Scary Wombat Feb 12 '16 at 02:51
  • I did see that, yet it doesnt answer what issues i have as we are working on the same assignment. Since i do not have the ability to respond to peoples posts yet i am not capable of asking my own questions on that forum post. That is why i created my own. – T.Brown68 Feb 12 '16 at 02:57

2 Answers2

0

Your requirement is not clear but as I guess it should be something like this.

Prog2 class:

    public static void main(String[] args) {

    // requests user to enter students name
    System.out.print("Enter name of student: ");
    Scanner in = new Scanner(System.in);
    String name = in.nextLine();

    Student student = new Student(name);

    System.out.print("Enter number of quiz: ");
    int count = in.nextInt();

    for (int i = 0; i < count; i++) {
        // requests user to enter students quizzes
        System.out.print("Enter students quiz grades: ");
        int quiz = in.nextInt();
        student.addGrade(quiz);
    }

    // prints the name, total, and average of students grades
    System.out.println(name);
    System.out.println("Total: " + student.getTotal());
    System.out.println("Average: " + student.getAverage());
}

Student class:

    private String name;
private List<Double> grades = new ArrayList<Double>();

public Student(String name) {
    super();
    this.name= name;
}

public String getName() {
    return name;
}

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

public void addGrade(double grade) {
    this.grades.add(grade);
}

public double getTotal() {
    double total = 0;
    for (double grade : grades) {
        total += grade;
    }
    return total;
}

public double getAverage() {
    return (double) getTotal() / grades.size();
}

Accept the answer if it helps.

Layansan
  • 46
  • 5
0

Well for the stop condition you could try something like this

String stopFrase="stop";
String userInput="";
int quiz;
//Other code
for(;;) //This basically means loop until I stop you
{
  System.out.print("Enter students quiz grades type 'stop' to finish:");
  userInput=in.nextLine();
  if(userInput.equals(stopFrace))
  {
    break;//Stop the loop
  }
  quiz= Integer.parseInt(userInput);
  //The rest of your code
}

Your addQuiz() method seems fine, if you are not getting the desired result please check your parameters, specifically make sure that number matches the number of quiz entered.

ivan
  • 21
  • 2