Skip to content

Commit 0f56ba5

Browse files
Added Exercise 5
1 parent 89f6a58 commit 0f56ba5

File tree

6 files changed

+474
-0
lines changed

6 files changed

+474
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package ExerciseFive;
2+
3+
/**
4+
The CourseGrades class stores data about a course's
5+
graded activities for the Course Grades programming
6+
challenge.
7+
*/
8+
9+
public class CourseGrades
10+
{
11+
public final int NUM_GRADES = 4;
12+
private GradedActivity[] grades;
13+
14+
/**
15+
Constructor
16+
*/
17+
18+
public CourseGrades()
19+
{
20+
grades = new GradedActivity[NUM_GRADES];
21+
}
22+
23+
/**
24+
The setLab method stores a GradedActivity object
25+
for the lab grade.
26+
@param aLab Represents the lab grade.
27+
*/
28+
29+
public void setLab(GradedActivity aLab)
30+
{
31+
grades[0] = aLab;
32+
}
33+
34+
/**
35+
The setPassFailExam method stores a PassFailExam object
36+
for the pass/fail exam grade.
37+
@param aPassFailExam Represents the pass/fail exam grade.
38+
*/
39+
40+
public void setPassFailExam(PassFailExam aPassFailExam)
41+
{
42+
grades[1] = aPassFailExam;
43+
}
44+
45+
/**
46+
The setEssay method stores an Essay object
47+
for the essay grade.
48+
@param anEssay Represents the essay grade.
49+
*/
50+
51+
public void setEssay(Essay anEssay)
52+
{
53+
grades[2] = anEssay;
54+
}
55+
56+
/**
57+
The setFinalExam method stores a FinalExam object
58+
for the final exam grade.
59+
@param aFinalExam Represents the final exam grade.
60+
*/
61+
62+
public void setFinalExam(FinalExam aFinalExam)
63+
{
64+
grades[3] = aFinalExam;
65+
}
66+
67+
/**
68+
The toString method returns a string representation
69+
of the object.
70+
@return A string representation of the object.
71+
*/
72+
73+
public String toString()
74+
{
75+
String str = "Lab Score: " + grades[0].getScore() +
76+
"\tGrade: " + grades[0].getGrade() +
77+
"\nPass/Fail Exam Score: " + grades[1].getScore() +
78+
"\tGrade: " + grades[1].getGrade() +
79+
"\nEssay Score: " + grades[2].getScore() +
80+
"\tGrade: " + grades[2].getGrade() +
81+
"\nFinal Exam Score: " + grades[3].getScore() +
82+
"\tGrade: " + grades[3].getGrade();
83+
84+
return str;
85+
}
86+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package ExerciseFive;
2+
3+
import ExerciseFive.GradedActivity;
4+
5+
/**
6+
The Essay class stores data about a graded
7+
activity that is an essay for the Course
8+
Grades programming challenge.
9+
*/
10+
11+
public class Essay extends GradedActivity
12+
{
13+
private double grammar; // Points for grammar
14+
private double spelling; // Points for spelling
15+
private double correctLength; // Points for length
16+
private double content; // Points for content
17+
18+
/**
19+
The setScore method sets points for grammar,
20+
spelling, length, and content.
21+
This method overloads the base class method.
22+
Note that the other "set" methods are
23+
private. Those methods are for validating
24+
points before they are assigned.
25+
@param gr Grammar points.
26+
@param sp Spelling points.
27+
@param len Length points.
28+
@param cnt Content points.
29+
*/
30+
31+
public void setScore(double gr, double sp, double len, double cnt)
32+
{
33+
// Set the individual scores.
34+
setGrammar(gr);
35+
setSpelling(sp);
36+
setCorrectLength(len);
37+
setContent(cnt);
38+
39+
// Set the total score.
40+
super.setScore(grammar + spelling + correctLength + content);
41+
}
42+
43+
/**
44+
The setGrammar method sets the grammar
45+
points, validating them before they are set.
46+
@param g Grammar points.
47+
*/
48+
49+
private void setGrammar(double g)
50+
{
51+
if (g <= 30.0)
52+
grammar = g;
53+
else // Invalid points
54+
grammar = 0.0;
55+
}
56+
57+
/**
58+
The setSpelling method sets the spelling
59+
points, validating them before they are set.
60+
@param s Spelling points.
61+
*/
62+
63+
private void setSpelling(double s)
64+
{
65+
if (s <= 20.0)
66+
spelling = s;
67+
else // Invalid points
68+
spelling = 0.0;
69+
}
70+
71+
/**
72+
The setCorrectLength method sets the points
73+
for correct length, validating them before
74+
they are set.
75+
@param c Correct length points.
76+
*/
77+
78+
private void setCorrectLength(double c)
79+
{
80+
if (c <= 20.0)
81+
correctLength = c;
82+
else // Invalid points
83+
correctLength = 0.0;
84+
}
85+
86+
/**
87+
The setContent method sets the points
88+
for content, validating them before
89+
they are set.
90+
@param c Content points.
91+
*/
92+
93+
private void setContent(double c)
94+
{
95+
if (c <= 30)
96+
content = c;
97+
else // Invalid points
98+
content = 0.0;
99+
}
100+
101+
/**
102+
The getGrammar method returns the points
103+
awarded for grammar.
104+
@return Gramar points.
105+
*/
106+
107+
public double getGrammar()
108+
{
109+
return grammar;
110+
}
111+
112+
/**
113+
The getSpelling method returns the points
114+
awarded for spelling.
115+
@return Spelling points.
116+
*/
117+
118+
public double getSpelling()
119+
{
120+
return spelling;
121+
}
122+
123+
/**
124+
The getCorrectLength method returns the points
125+
awarded for correct length.
126+
@return Correct length points.
127+
*/
128+
129+
public double getCorrectLength()
130+
{
131+
return correctLength;
132+
}
133+
134+
/**
135+
The getContent method returns the points
136+
awarded for content.
137+
@return Content points.
138+
*/
139+
140+
public double getContent()
141+
{
142+
return content;
143+
}
144+
145+
/**
146+
The getScore method returns the overall
147+
numeric score. Overrides the base class
148+
method.
149+
@return Overall numeric score.
150+
*/
151+
152+
@Override
153+
public double getScore()
154+
{
155+
return grammar + spelling + correctLength + content;
156+
}
157+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package ExerciseFive;
2+
3+
import ExerciseFive.GradedActivity;
4+
5+
/**
6+
The Essay class stores data about a graded
7+
activity that is a final exam for the Course
8+
Grades programming challenge.
9+
*/
10+
11+
public class FinalExam extends GradedActivity
12+
{
13+
private int numQuestions; // Number of questions
14+
private double pointsEach; // Points for each question
15+
private int numMissed; // Questions missed
16+
17+
/**
18+
The constructor sets the number of questions on the
19+
exam and the number of questions missed.
20+
@param questions The number of questions.
21+
@param missed The number of questions missed.
22+
*/
23+
24+
public FinalExam(int questions, int missed)
25+
{
26+
double numericScore; // To hold a numeric score
27+
28+
// Set the numQuestions and numMissed fields.
29+
numQuestions = questions;
30+
numMissed = missed;
31+
32+
// Calculate the points for each question and
33+
// the numeric score for this exam.
34+
pointsEach = 100.0 / questions;
35+
numericScore = 100.0 - (missed * pointsEach);
36+
37+
// Call the inherited setScore method to
38+
// set the numeric score.
39+
setScore(numericScore);
40+
}
41+
42+
/**
43+
The getPointsEach method returns the number of
44+
points each question is worth.
45+
@return The value in the pointsEach field.
46+
*/
47+
48+
public double getPointsEach()
49+
{
50+
return pointsEach;
51+
}
52+
53+
/**
54+
The getNumMissed method returns the number of
55+
questions missed.
56+
@return The value in the numMissed field.
57+
*/
58+
59+
public int getNumMissed()
60+
{
61+
return numMissed;
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package ExerciseFive;
2+
3+
/**
4+
The GradedActivity class stores data about a graded
5+
activity for the Course Grades programming challenge.
6+
*/
7+
8+
public class GradedActivity
9+
{
10+
private double score; // Numeric score
11+
12+
/**
13+
The setScore method sets the score field.
14+
@param s The value to store in score.
15+
*/
16+
17+
public void setScore(double s)
18+
{
19+
score = s;
20+
}
21+
22+
/**
23+
The getScore method returns the score.
24+
@return The value stored in the score field.
25+
*/
26+
27+
public double getScore()
28+
{
29+
return score;
30+
}
31+
32+
/**
33+
The getGrade method returns a letter grade
34+
determined from the score field.
35+
@return The letter grade.
36+
*/
37+
38+
public char getGrade()
39+
{
40+
char letterGrade;
41+
42+
if (score >= 90)
43+
letterGrade = 'A';
44+
else if (score >= 80)
45+
letterGrade = 'B';
46+
else if (score >= 70)
47+
letterGrade = 'C';
48+
else if (score >= 60)
49+
letterGrade = 'D';
50+
else
51+
letterGrade = 'F';
52+
53+
return letterGrade;
54+
}
55+
}

0 commit comments

Comments
 (0)