Do not use on any
shared computer
July 25, 2008 02:34pm pdt
null
[x]
Attachment Details

OOD Java Design Simplication

This is more a OOD rather than Java but the problem is is Java

Question:

Could I simplify these 7 classes into less complicated design that would require fewer class by either using abstract class or an interface...

Here are the classes an relationships:  (Complete code in code snippet)
CourseGrades and GradedActivity have an Aggregation Relationship
Final Exam, Essay, PassFailActivity are all derived from Graded Activity (inheritance)
and
PassFailExam is derived from PassFail Activity...


Note the seven classes are not difficult (basically just get/set methods)

Is there a method or formula to this for simplification...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
public class CourseGrades
{
   // Constant for the number of grades
   public final int NUM_GRADES = 4;
   
   // Variable to reference a GradedActivity
   // array
   private GradedActivity[] grades;
   
   /**
      Constructor
   */
      
   public CourseGrades()
   {
      // Create the grades array.
      grades = new GradedActivity[NUM_GRADES];
   }
   
   /**
      The setLab method stores a GradedActivity object
      for the lab grade.
      @param aLab Represents the lab grade.
   */
   
   public void setLab(GradedActivity aLab)
   {
      grades[0] = aLab;
   }
   
   /**
      The setPassFailExam method stores a PassFailExam object
      for the pass/fail exam grade.
      @param aPassFailExam Represents the pass/fail exam grade.
   */
   
   public void setPassFailExam(PassFailExam aPassFailExam)
   {
      grades[1] = aPassFailExam;
   }
 
   /**
      The setEssay method stores an Essay object
      for the essay grade.
      @param anEsay Represents the essay grade.
   */
      
   public void setEssay(Essay anEssay)
   {
      grades[2] = anEssay;
   }
 
   /**
      The setFinalExam method stores a FinalExam object
      for the final exam grade.
      @param aFinalExam Represents the final exam grade.
   */
   
   public void setFinalExam(FinalExam aFinalExam)
   {
      grades[3] = aFinalExam;
   }
 
   /**
      The toString method returns a string representation
      of the object.
      @return A string representation of the object.
   */
   
   public String toString()
   {
      String str = "Lab Score: " + grades[0].getScore() +
                   "\tGrade: " + grades[0].getGrade() +
                   "\nPass/Fail Exam Score: " + grades[1].getScore() +
                   "\tGrade: " + grades[1].getGrade() +
                   "\nEssay Score: " + grades[2].getScore() +
                   "\tGrade: " + grades[2].getGrade() +
                   "\nFinal Exam Score: " + grades[3].getScore() +
                   "\tGrade: " + grades[3].getGrade();
      
      return str;
   }
}
 
public class GradedActivity
{
   private double score;  // Numeric score
 
   /**
      The setScore method sets the score field.
      @param s The value to store in score.
   */
 
   public void setScore(double s)
   {
      score = s;
   }
 
   /**
      The getScore method returns the score.
      @return The value stored in the score field.
   */
 
   public double getScore()
   {
      return score;
   }
 
   /**
      The getGrade method returns a letter grade
      determined from the score field.
      @return The letter grade.
   */
 
   public char getGrade()
   {
      char letterGrade;
 
      if (score >= 90)
         letterGrade = 'A';
      else if (score >= 80)
         letterGrade = 'B';
      else if (score >= 70)
         letterGrade = 'C';
      else if (score >= 60)
         letterGrade = 'D';
      else
         letterGrade = 'F';
 
      return letterGrade;
   }
}
public class Essay extends GradedActivity
{
   private double grammar;				// Points for grammar
   private double spelling;			// Points for spelling
   private double correctLength;		// Points for length	
   private double content;				// Points for content
 
   /**
   	The setScore method sets points for grammar,
		spelling, length, and content.
		This method overloads the base class method.
		Note that the other "set" methods are
		private. Those methods are for validating
		points before they are assigned.
		@param gr Grammar points.
		@param sp Spelling points.
		@param len Length points.
		@param cnt Content points.
   */
 
   public void setScore(double gr, double sp, double len, double cnt)
   {
		// Set the individual scores.
		setGrammar(gr);
		setSpelling(sp);
		setCorrectLength(len);
		setContent(cnt);
		
		// Set the total score.
		super.setScore(grammar + spelling + correctLength + content);
	}
 
   /**
   	The setGrammar method sets the  grammar
		points, validating them before they are set.
		@param g Grammar points.
   */
 
   private void setGrammar(double g)
   {
		if (g <= 30.0)
      	grammar = g;
		else						// Invalid points
			grammar = 0.0;
   }
 
   /**
   	The setSpelling method sets the spelling
		points, validating them before they are set.
		@param s Spelling points.
   */
	
   private void setSpelling(double s)
   {
		if (s <= 20.0)
      	spelling = s;
		else						// Invalid points
			spelling = 0.0;
   }
 
   /**
   	The setCorrectLength method sets the points
		for correct length, validating them before
		they are set.
		@param c Correct length points.
   */
	
   private void setCorrectLength(double c)
   {
		if (c <= 20.0)
      	correctLength = c;
		else						// Invalid points
			correctLength = 0.0;
   }
 
   /**
   	The setContent method sets the points
		for content, validating them before
		they are set.
		@param c Content points.
   */
	
   private void setContent(double c)
   {
		if (c <= 30)
      	content = c;
		else						// Invalid points
			content = 0.0;
   }
 
   /**
   	The getGrammar method returns the points
		awarded for grammar.
		@return Gramar points.
   */
 
   public double getGrammar()
   {
      return grammar;
   }
 
   /**
   	The getSpelling method returns the points
		awarded for spelling.
		@return Spelling points.
   */
 
   public double getSpelling()
   {
      return spelling;
   }
 
   /**
   	The getCorrectLength method returns the points
		awarded for correct length.
		@return Correct length points.
   */
 
   public double getCorrectLength()
   {
      return correctLength;
   }
 
   /**
   	The getContent method returns the points
		awarded for content.
		@return Content points.
   */
	
   public double getContent()
   {
      return content;
   }
 
   /**
   	The getScore method returns the overall
		numeric score. Overrides the base class
		method.
		@return Overall numeric score.
   */
 
   public double getScore()
   {
      return grammar + spelling + correctLength + content;
   }
}
/**
   This class determines the grade for a final exam.
*/
 
public class FinalExam extends GradedActivity
{
   private int numQuestions;  // Number of questions
   private double pointsEach; // Points for each question
   private int numMissed;     // Questions missed
 
   /**
      The constructor sets the number of questions on the
      exam and the number of questions missed.
      @param questions The number of questions.
      @param missed The number of questions missed.
   */
 
   public FinalExam(int questions, int missed)
   {
      double numericScore;  // To hold a numeric score
 
      // Set the numQuestions and numMissed fields.
      numQuestions = questions;
      numMissed = missed;
 
      // Calculate the points for each question and
      // the numeric score for this exam.
      pointsEach = 100.0 / questions;
      numericScore = 100.0 - (missed * pointsEach);
 
      // Call the inherited setScore method to
      // set the numeric score.
      setScore(numericScore);
   }
 
   /**
      The getPointsEach method returns the number of
      points each question is worth.
      @return The value in the pointsEach field.
   */
 
   public double getPointsEach()
   {
      return pointsEach;
   }
 
   /**
      The getNumMissed method returns the number of 
      questions missed.
      @return The value in the numMissed field.
   */
 
   public int getNumMissed()
   {
      return numMissed;
   }
}
 
public class PassFailActivity extends GradedActivity
{
   private double minPassingScore; // Minimum passing score
 
   /**
      The constructor sets the minimum passing score.
      @param mps The minimum passing score.
   */
 
   public PassFailActivity(double mps)
   {
      minPassingScore = mps;
   }
 
   /**
      The getGrade method returns a letter grade
      determined from the score field. This
      method overrides the superclass method.
      @return The letter grade.
   */
 
   public char getGrade()
   {
      char letterGrade;
 
      if (super.getScore() >= minPassingScore)
         letterGrade = 'P';
      else
         letterGrade = 'F';
 
      return letterGrade;
   }
}
public class PassFailExam extends PassFailActivity
{
   private int numQuestions;  // Number of questions
   private double pointsEach; // Points for each question
   private int numMissed;     // Number of questions missed
 
   /**
      The constructor sets the number of questions, the
      number of questions missed, and the minimum passing
      score.
      @param questions The number of questions.
      @param missed The number of questions missed.
      @param minPassing The minimum passing score.
   */
 
   public PassFailExam(int questions, int missed, 
               double minPassing)
   {
      // Call the superclass constructor.
      super(minPassing);
 
      // Declare a local variable for the score.
      double numericScore;
 
      // Set the numQuestions and numMissed fields.
      numQuestions = questions;
      numMissed = missed;
 
      // Calculate the points for each question and
      // the numeric score for this exam.
      pointsEach = 100.0 / questions;
      numericScore = 100.0 - (missed * pointsEach);
 
      // Call the superclass's setScore method to
      // set the numeric score.
      setScore(numericScore);
   }
 
   /**
      The getPointsEach method returns the number of
      points each question is worth.
      @return The value in the pointsEach field.
   */
 
   public double getPointsEach()
   {
      return pointsEach;
   }
 
   /**
      The getNumMissed method returns the number of 
      questions missed.
      @return The value in the numMissed field.
   */
 
   public int getNumMissed()
   {
      return numMissed;
   }
}
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: CPlusJavaCSharp
Solution Provided By: girionis
Participating Experts: 3
Solution Grade: B
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by CEHJ

Rank: Genius

Expert Comment by CEHJ:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by jeffreychang
Expert Comment by jeffreychang:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by girionis

Rank: Genius

Accepted Solution by girionis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628