0

I'm making a small program for myself that will calculate my average mark.

Right now, I have a fully working program but it's highly inefficient and is very limited. Code:

import java.util.Scanner;
public class GradeCalculator {

public static void main(String[] args) {

Scanner count = new Scanner(System.in);
    int counter = count.nextInt();

        Scanner input = new Scanner(System.in);

    double mark = input.nextDouble();
    double promark = (mark/100);
    double weight = input.nextDouble();
    double total = (promark*weight);
    double finalmark = (total);
    if (counter==1){
    System.out.println("The average mark for your "+counter+" assignments/exams is "+finalmark);
    System.exit(0);
    }

    double mark1 = input.nextDouble();
    double promark1 = (mark1/100);
    double weight1 =input.nextDouble();
    double total1 = (promark1*weight1);
    double finalmark1 = (total+total1);
    if (counter==2){
    System.out.println("The average mark for your "+counter+" assignments/exams is "+finalmark1);
    System.exit(0);
    }

    double mark2 = input.nextDouble();
    double promark2 = (mark2/100);
    double weight2 =input.nextDouble();
    double total2 = (promark2*weight2);
    double finalmark2 = (total+total1+total2);
    if (counter==3){
    System.out.println("The average mark for your "+counter+" assignments/exams is "+finalmark2);
    System.exit(0);
    }


    double mark3 = input.nextDouble();
    double promark3 = (mark3/100);
    double weight3 =input.nextDouble();
    double total3 = (promark3*weight3);
    double finalmark3 = (total+total1+total2+total3);
    if (counter==4){
    System.out.println("The average mark for your "+counter+" assignments/exams is "+finalmark3);
    System.exit(0);
    }

    double mark4 = input.nextDouble();
    double promark4 = (mark4/100);
    double weight4 = input.nextDouble();
    double total4 = (promark4*weight4);
    double finalmark4 = (total+total1+total2+total3+total4);
    if(counter==5){
    System.out.println("The average mark for your "+counter+" assignments/exams is "+finalmark4);
    System.exit(0);
    }

So this program firstly asks the user for the amount of different of marks they have.

Then after the user will put in the mark and the weighted percentage of that work.

After that the program will calculate the average mark of all the marks with the percentage of each mark taken into consideration.

The program is basically a replica of this website:

http://www.benegg.net/grade_calculator.html

There are so many if loops/variables. How can I use OOP in Java to sort this out? Such as using methods/constructors etc?

Vogel612
  • 5,620
  • 5
  • 48
  • 73
bob9123
  • 725
  • 1
  • 9
  • 31

4 Answers4

3

You should use a loop:

int sumTotal=0;
int sumWeight=0;
for(int i=0;i<counter;i++){
    double mark = input.nextDouble();
    double promark = (mark/100);
    double weight = input.nextDouble();
    sumWeight+=weight; //add to sum of weight
    sumTotal+=promark*weight; //add to sum of total
}
System.out.println("Mark is: "+(double)sumTotal/sumWeight);

Not tested, just the way you should use

maskacovnik
  • 3,080
  • 5
  • 20
  • 26
1

You can create a class Exam as a placeholder for your input data:

public static class Exam {
  final double mark;
  final double weight;

  public Exam(double mark, double weight) {
    this.mark = mark;
    this.weight = weight;
  }

  public double getTotal() {
    return mark * weight / 100;
  }

  public static double getAverage(Exam[] arr) {
    double sum = 0;
    for (int i=0; i < arr.length; i++) sum += arr[i].getTotal();
    return sum / arr.length;
  }

}

Now you can use a loop to create Exam data array:

public static void main(String[] args) {

   Scanner input = new Scanner(System.in);
   int counter = input.nextInt();
   Exam[] arr = new Exam[counter];

   for (int i = 0 ; i < counter ; i++) {
     double mark = input.nextDouble();
     double weight = input.nextDouble();
     arr[i] = new Exam(mark, weight);
   }
   System.out.println("The average mark for your "+counter+" assignments/exams is "+ Exam.getAverage(arr));
}

Hopefully it looks more like OOP code.

MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • This is exactly want I wanted so I can learn Java more. Thank you very much. If I have any questions I will reply to you – bob9123 Jul 06 '15 at 22:05
  • Glad I could help. Please accept answer when applicable. – MaxZoom Jul 06 '15 at 22:14
  • What is the reason for making mark and weight final and the Exam class static? I know what final and static mean individually but I don't see how they apply in this case. – bob9123 Jul 06 '15 at 22:22
  • To not reinvent the wheel, see [this link](http://stackoverflow.com/q/253492/4454454) for static class. Final class member benefits are listed [here](http://javarevisited.blogspot.com/2011/12/final-variable-method-class-java.html) – MaxZoom Jul 06 '15 at 22:31
  • When adding Static to the Exam class it comes up with the error 'modifier static not allowed here'? – bob9123 Jul 06 '15 at 22:34
  • It should be correct if you include it inside the class with `main` method. – MaxZoom Jul 06 '15 at 22:36
  • The (((GradeCalculatorv2[] arr = new GradeCalculatorv2[counter];))) section. What is going on here? – bob9123 Jul 07 '15 at 20:36
  • and also (((arr[i] = new GradeCalculatorv2(mark, weight); )))) – bob9123 Jul 07 '15 at 20:43
0

Since this is a very specific application, I will not rewrite it for you but I will give you some general tips on how to reduce your repeated code.

What you really want to do is do all the repeated calculations of markX, promarkX, weightX, and totalX in a loop. Once you have that, you want to add the total to a variable outside of the for loop (finalmark). Once the loop exits, you can then print your statement.

0

Since you are using a numeric counter to keep track of how many times you want to loop, you should use a for loop

wggn
  • 77
  • 6