Problem:
Dr. Khazzam is a very lazy CSC teacher. He got 40 students. He's in his office and wants to create an program in java that reads the grades of the students and do some calculations.
Each student has three grades:
-The first one is from the first exam, and counts 30% of the overall grade.
-The second one is from the second exam, and count 30% of the overall grade.
-The third one is from the final exam, and count 40% of the overall grade.
The program scans the three grades for each student, and then calculates their average according to their percentage criteria. The program calculates also the average of all students for the first exam, second exam, final exam, and the average of the average of each student taken the three exams.
Insert a condition in the program that stops reading values when -1 value is pressed for the first exam.
There are three effective methods of solving this problem:
-The first one is with using only arrays
-The second one is with using object of arrays
-The third one is with using two-dimensional arrays
Solve this problem by using each of these three methods.
Output:
Enter student 1 grades
64.0 9.0 23.0
Enter student 2 grades
64.0 12.0 76.0
Enter student 3 grades
65.0 86.0 23.0
Enter student 4 grades
86.0 45.0 32.0
Enter student 5 grades
75.0 34.0 75.5
Enter student 6 grades
-1
64.0 9.0 23.0
Enter student 2 grades
64.0 12.0 76.0
Enter student 3 grades
65.0 86.0 23.0
Enter student 4 grades
86.0 45.0 32.0
Enter student 5 grades
75.0 34.0 75.5
Enter student 6 grades
-1
Method 1:
import java.util.Scanner; public class ExamRemake5 { public static void main (String[] args) { double sum1=0,sum2=0,sum3=0,allsum =0, count =0; double[] exam1 = new double[40]; double[] exam2 = new double[40]; double[] finale = new double[40]; double[] average = new double[40]; Scanner scan = new Scanner(System.in); for (int i =0;i<40;i++) { System.out.println("Enter student " + (i+1) + " grades"); double zigi = scan.nextDouble(); if(zigi!=-1) { exam1[i] = zigi; sum1 += exam1[i]; exam2[i] = scan.nextDouble(); sum2 += exam2[i]; finale[i] = scan.nextDouble(); sum3+= finale[i]; average[i] += exam1[i]*0.3 + exam2[i]*0.3 + finale[i]*0.4; allsum +=average[i]; } else { count = i; i = 1000; } } System.out.println("Exam1 " + "\t" + "Exam2" + "\t" + "Final" + "\t" + "Average"); for (int i =0;i<count;i++) System.out.println(exam1[i] + "\t" + exam2[i] + "\t" + finale[i] + "\t" + average[i]); System.out.println("Total1 " + "\t" + "Total2" + "\t" + "Total3" + "\t" + "Total"); System.out.println((double) sum1/count + "\t" + (double) sum2/count + "\t" + (double) sum3/count + "\t" + (double)allsum/count); } }
Method 2:
import java.util.Scanner; public class Problem2 { private double exam1; private double exam2; private double finale; double average; public Problem2(double senti,double senti2,double senti3) { this.exam1 = senti; this.exam2 = senti2; this.finale = senti3; this.average = senti*0.3 +senti2*0.3 + senti3 *0.4; } public String toString() { return exam1 + "\t" + exam2 + "\t" + finale + "\t" + average + "\t"; } public static void main (String[] args) { int MAX = 40; double total1 =0,total2=0,total3=0,totalall=0,count=0; Scanner scan = new Scanner (System.in); Problem2[] prob2 = new Problem2[MAX]; for (int i =0;i<MAX;i++) { System.out.println("Enter student " + (i+1) + " grades"); double senti = scan.nextDouble(); if (senti != -1) { double senti2= scan.nextDouble(); double senti3 = scan.nextDouble(); prob2[i] = new Problem2 (senti,senti2,senti3); total1+=senti; total2+=senti2; total3+=senti3; totalall+=senti*0.3 + senti2*0.3+senti3*0.4; } else { count=i; i = 1000; } } System.out.println("Exam1" + "\t" + "Exam2" +"\t"+ "Final" + "\t" + "Average"); for (int i =0;i<count;i++) System.out.println(prob2[i]); System.out.println("Total1" + "\t" + "Total2" +"\t"+ "Total3" + "\t" + "TotalAll"); System.out.println((double) total1/count + "\t" + (double) total2/count +"\t"+ (double) total3/count + "\t" + (double) totalall/count); } }Method 3:
import java.util.Scanner; public class Problem3 { public static void main (String[] args) { int j =0; Scanner scan = new Scanner (System.in); double[][] nums = new double[40][4]; //Storing the values double total1 =0, total2=0,total3=0, totalall =0; int count =0; int z =0; for(int i =0;i<nums.length;i++) { System.out.println("Enter student " + (i+1) + " grades"); for (j=0;j<nums[i].length-1;j++) { //The length is less than columns length - 1 //since the user does not input the average double n = scan.nextDouble(); if (n != -1) { nums[i][j] = n; } else { z=-1; count = i; } if (n==-1) break; } if (z==-1) break; nums[i][3] = nums[i][0]*0.3 + nums[i][1]*0.3+nums[i][2]*0.4; totalall+= nums[i][3]; total3+=nums[i][2]; total2+=nums[i][1]; total1+=nums[i][0]; } System.out.println("Exam1" + "\t" + "Exam2" + "\t" + "Exam3" + "\t" + "Average"); //Printing the values for(int i =0;i<count;i++) { for (j=0;j<nums[i].length;j++) { //The length is less than columns length //since we want print the values System.out.print(nums[i][j] + "\t"); } System.out.println(); } System.out.println("Total1" + "\t" + "Total2" +"\t"+ "Total3" + "\t" + "TotalAll"); System.out.println((double) total1/count + "\t" + (double) total2/count +"\t"+ (double) total3/count + "\t" + (double) totalall/count); } }
No comments :
Post a Comment