Java In a course, a professor gives the following tests and assignments: - A lab
ID: 3604438 • Letter: J
Question
Java
In a course, a professor gives the following tests and assignments:
- A lab activity that is observed by the teacher and assigned a numerical score.
- A Pass/Fail test that has 10 questions. Minimum passing score is 70%.
- An essay that is assigned a numerical score.
- A final test that has 50 questions.
Write a class named CourseGrades. The class should have a GradedActivity array named grades as a field. The array should have four elements, one for each of the assignments previously described above. The class should have the following methods:
- setLab: This method should accept a GradedActivity object as its argument. This object should already hold the student's score for the lab activity. Element 0 of the grades field should reference this object. -
setPassFailTest: This method should accept a PassFailTest object as its argument. This object should already hold the student's score for the Pass/Fail test. Element 1 of the grades field should reference this object.
- setEssay: This method should accept an Essay object as its argument. This object should already hold the student's score for the Essay. Element 2 of the grades field should reference this object.
- setFinalTest: This method should accept a FinalTest object as its argument. This object should already hold the student's score for the final test. Element 3 of the grades field should reference this object.
- toString: This method should return a string that contains the numerical scores and grades for each element in the grades array. Demonstrate the class in the program. *
**This program requires multiple class files and focuses on using Inheritance**
Explanation / Answer
GradedActivity.java :
package chegg1;
public abstract class GradedActivity {
public abstract int getScore();
public abstract void calcluateGrade();
public abstract String getGrade();
}
LabActivity.java:
package chegg1;
public class LabActivity extends GradedActivity{
@Override
public String toString() {
return "LabActivity [score=" + score + ", grade=" + grade + "]";
}
private int score;
private String grade;
public LabActivity(int s) {
this.score = s;
}
@Override
public void calcluateGrade() {
// supposing the score is out of 100.
if(score>90)
grade="A";
else if(score>80)
grade="B";
else if(score>70)
grade="C";
else if(score>60)
grade="D";
else if(score>40)
grade="E";
else
grade="Fail";
}
@Override
public String getGrade() {
return grade;
}
@Override
public int getScore() {
return score;
}
}
PassFailTest.java:
package chegg1;
public class PassFailTest extends GradedActivity{
private int score;
private String grade;
public PassFailTest(int s) {
this.score = s;
}
@Override
public void calcluateGrade() {
if(score>=7)
grade="Pass";
else
grade="Fail";
}
@Override
public String getGrade() {
return grade;
}
@Override
public int getScore() {
return score;
}
}
Essay.java:
package chegg1;
public class Essay extends GradedActivity {
private int score;
private String grade;
public Essay(int s) {
this.score = s;
}
@Override
public void calcluateGrade() {
// supposing the score is out of 100.
if(score>90)
grade="A";
else if(score>80)
grade="B";
else if(score>70)
grade="C";
else if(score>60)
grade="D";
else if(score>40)
grade="E";
else
grade="Fail";
}
@Override
public String getGrade() {
return grade;
}
@Override
public int getScore() {
return score;
}
}
FinalTest.java:
package chegg1;
public class FinalTest extends GradedActivity {
private int score;
private String grade;
public FinalTest(int s) {
this.score = s;
}
@Override
public void calcluateGrade() {
// supposing the score is out of 50.
if(score>45)
grade="A";
else if(score>40)
grade="B";
else if(score>35)
grade="C";
else if(score>30)
grade="D";
else if(score>20)
grade="E";
else
grade="Fail";
}
@Override
public String getGrade() {
return grade;
}
@Override
public int getScore() {
return score;
}
}
CourseGrades.java:
package chegg1;
import java.util.Arrays;
public class CourseGrades {
private GradedActivity grades[];
public CourseGrades() {
this.grades = new GradedActivity[4];
}
public CourseGrades(GradedActivity[] grades) {
this.grades = grades;
}
public void setLab(GradedActivity g)
{
this.grades[0] = g;
}
public void setPassFailTest(GradedActivity g)
{
this.grades[1] = g;
}
public void setEssay(GradedActivity g)
{
this.grades[2] = g;
}
public void setFinaltest(GradedActivity g)
{
this.grades[3] = g;
}
@Override
public String toString() {
String temp="";
for(int i=0;i<4;i++)
temp = temp + "Score: " + grades[i].getScore() + " Grade: "+grades[i].getGrade()+" ";
return temp;
}
}
CourseClient.java:
package chegg1;
public class CourseClient {
public static void main(String[] args) {
GradedActivity ga[] = new GradedActivity[4];
ga[0] = new LabActivity(74);
ga[1] = new PassFailTest(8);
ga[2] = new Essay(56);
ga[3] = new FinalTest(47);
for(int i=0;i<4;i++)
ga[i].calcluateGrade();
CourseGrades cg = new CourseGrades();
cg.setLab(ga[0]);
cg.setPassFailTest(ga[1]);
cg.setEssay(ga[2]);
cg.setFinaltest(ga[3]);
System.out.println(cg);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.