You will write a Java class called Grades that should be saved into a file calle
ID: 3587388 • Letter: Y
Question
You will write a Java class called Grades that should be saved into a file called Grades.java.
Your program will read in grades for a student in a fictional course, and it will calculate the overall course grade for the student. Below is one example log of execution of the program (user input is underlined for the sake of clarity in the log --- you do not need to duplicate this behavior).
an example run of the program
This program accepts your homework and two exa m scores as input and computes your grade in the course.
Homework weight? 50
Exa m 1 weight? 20
Using weights of 50 20 30
Homework:
Number of assignments? 10
Average Homework grade? 8.45
Number of late days used? 2
Labs attended? 4
Total points = 100.5 / 140
Weighted score = 35.89
Exa m 1:
Score? 81
Curve? 0
Total points = 81 / 100
Weighted score = 16.2
Exa m 2:
Score? 95
Curve? 10
Total points = 100 / 100
Weighted score = 30.0
Course grade = 82.09
The course grade is a weighted average of three components: a homework grade, an exa m 1 grade, and an exa m 2 grade. To compute a weighted average, the student's point scores for each component are divided by the total points for that component and multiplied by that component's weight.
The homework score is determined by the student's average score on a series of assignments, as well as points for attending labs. Homeworks are worth 10 points, and each lab is worth 4 points, and the number of homeworks and labs is the same. The number of assignments is entered by the user, as is the student's average homework score and the number of labs attended, as shown in the execution log. If a student hands an assignment in late, he or she uses "late days." For this course, if the student uses more late days than half the number of assignments, the assignment grade should be reduced by 10%. If the student uses no late days, the student should get 5 extra credit points for the homeworks. However, a student cannot earn more points than the maximum available for homeworks; a student that averages 10 points per assignment and never hands any in late has already earned the maximum number of homework points and will get no extra credit for being punctual.
Exa ms are worth 100 points each, and potentially are curved by adding some "curve points" to every students' grade. However, students cannot earn more than 100 points on an exa m.
Your program should be careful to correct bad numeric values entered by the user. If a user enters a negative number or zero for the number of assignments, the student should get full credit for the homework grade. If a user enters a negative number for the average homework grade or an exa m grade, it should be treated like a zero. If a user enters a higher score than the maximum number of points available for homeworks or an , the score should be capped.
In the log of execution shown, the course has 50% weight for homework, 20% weight for exa m1, and 30% weight for exa m 2. There are 10 homework assignments. The student attended 4 labs (earning 16 points for doing so). The student received an exa m 1 score of 81. The student earned an exa m 2 score of 95; the exa m was curved by +10 points, but exa m scores are capped at 100, so the student was given 100 for exa m 2.
Explanation / Answer
//Grades.java
public class Grades {
float avg_homework_score,exam1_score,exam2_score;
int no_of_assignmnts,no_of_labs,no_of_hw,late_days_used,hw_weight,ex1_weight,ex2_weight,ex1_curve,ex2_curve;
int lab_points=4,hw_points=10,ex_points=100;
float total_points_hw,total_points_ex1,total_points_ex2,points_gained_hw,points_gained_ex1,points_gained_ex2;
float weighted_scr_hw,weighted_scr_ex1,weighted_scr_ex2;
float course_grade;
public static void main(String args[]){
/* In the question , its not specified how the inputs
* should be given. Here, I'm giving it directly from
* the main class using an instance of Grades.java. If
* you want to use some other methods, you can easily
* modify yourself. Just call the calculateScores() after
* passing values
*/
Grades student=new Grades();
student.hw_weight=50;
student.ex1_weight=20;
student.ex2_weight=100-(student.hw_weight+student.ex1_weight);
student.avg_homework_score=8.45f;
student.exam1_score=81;
student.exam2_score=95;
student.no_of_assignmnts=10;
student.no_of_labs=4;
student.no_of_hw=student.no_of_labs;
student.late_days_used=2;
student.ex1_curve=0;
student.ex2_curve=10;
student.calculateScores();
}
public void calculateScores(){
total_points_ex1=100;
total_points_ex2=100;
total_points_hw=no_of_assignmnts*hw_points + no_of_assignmnts*lab_points; // in this case 10*10 + 10*4 = 140
calculateHomeworkPoints();
calculateExamPoints();
course_grade=weighted_scr_ex1+weighted_scr_ex2+weighted_scr_hw;
displayGrades();
}
public void calculateHomeworkPoints(){
if(avg_homework_score<0){
avg_homework_score=0; //treating negative average homework scores as 0
}
if(no_of_assignmnts<0){
points_gained_hw=total_points_hw; //giving maximum points if no of assignments is negative or zero
}else{
if(late_days_used> (no_of_assignmnts/2)){ //if late days used are more than half of the total assignments
points_gained_hw=avg_homework_score*hw_points+ no_of_labs*lab_points;
points_gained_hw=points_gained_hw-(points_gained_hw * 0.10f); //deducting 10% from assignments grade (I'm guessing it's homework grade that you meant)
}
else if(late_days_used==0){ //no late days used
points_gained_hw=avg_homework_score*hw_points+ no_of_labs*lab_points;
points_gained_hw=points_gained_hw+(points_gained_hw* 0.05f); //crediting 5% extra points
if(points_gained_hw>total_points_hw){ //if it exceeds the total points available for homeworks, cap it
points_gained_hw=total_points_hw; //giving the maximum available points
}
}else {
points_gained_hw=avg_homework_score*hw_points+ no_of_labs*lab_points; // in this case 8.45*10 + 4*4 = 100.5
}
}
weighted_scr_hw=(points_gained_hw/total_points_hw)*hw_weight; //calculating the final weighted score for homework
}
public void calculateExamPoints(){ //calculating exams scores
points_gained_ex1=exam1_score+ex1_curve;
if(points_gained_ex1>total_points_ex1){ //if the score+curves exceeds total available points, cap it
points_gained_ex1=total_points_ex1;
}
points_gained_ex2=exam2_score+ex2_curve;
if(points_gained_ex2>total_points_ex2){
points_gained_ex2=total_points_ex2;
}
weighted_scr_ex1=(points_gained_ex1/total_points_ex1)*ex1_weight; // finding the final weighted scores for exam1 and exam2
weighted_scr_ex2=(points_gained_ex2/total_points_ex2)*ex2_weight;
}
public void displayGrades(){ //displaying final results
System.out.println("Homework weight? "+hw_weight);
System.out.println("Exam1 weight? "+ex1_weight);
System.out.println("Using weights of "+hw_weight+" "+ex1_weight+" "+ex2_weight);
System.out.println("Homework:");
System.out.println("Number of assignments? "+no_of_assignmnts);
System.out.println("Average Homework grade "+avg_homework_score);
System.out.println("Number of late days used? "+late_days_used);
System.out.println("Labs attended? "+no_of_labs);
System.out.println("Total points = "+points_gained_hw +" / "+total_points_hw);
System.out.println("Weighted score= "+weighted_scr_hw);
System.out.println("Exam1:");
System.out.println("Score? "+exam1_score);
System.out.println("Curve? "+ex1_curve);
System.out.println("Total points = "+points_gained_ex1 +" / "+total_points_ex1);
System.out.println("Weighted score= "+weighted_scr_ex1);
System.out.println("Exam2:");
System.out.println("Score? "+exam2_score);
System.out.println("Curve? "+ex2_curve);
System.out.println("Total points = "+points_gained_ex2 +" / "+total_points_ex2);
System.out.println("Weighted score= "+weighted_scr_ex2);
System.out.println("Course Grade: "+course_grade);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.