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?