aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes some help with student grade Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "some help with student grade" Watch "some help with student grade" New topic
Author

some help with student grade

Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239






as you can see a student has midterm, extracurricular and attendance. midterm has its own calculateGrade() and student also has calculateGrade(). A student's grade is the sum of his/her midterm calculateGrade and calculateGrade in the student class itself. among student's members, only midterm has points (calculateGrade) while others don't. I'm tempted to apply decorator pattern, but I don't want to involve inheritance. any other pattern? thanks
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Rather than trying to find a pattern to use, why don't you evaluate your design and understand what kind of problems you're having? I would start with SOLID design principles first and see what principles of good OO design have been violated by this code. Once you have that understanding, you may find it easier to match the problems you're trying to solve with the problems that certain patterns are trying to address.

You mentioned the Decorator pattern. Does your code have the same problem that the Decorator pattern tries to address? Why do you hesitate to use inheritance? What kind of problem does that create in your code?


Junilu - [How to Ask Questions] [How to Answer Questions]
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
I'm avoiding inheritance because the objects are parsed from XML using JAXB. I don't know the effect of inheritance to parsing process. Also I've opened tha SOLID page you gave, but I still don't know what principle violated by this code. Could please give me a hint?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

One problem I can immediately see in this code is the lack of conceptual cohesiveness. That is, the ideas you are trying to model in the code don't come together very well. For example, you have a calculateGrade method in a Student and a calculateGrade method in a Midterm. There's no symmetry there because a Student is a fundamentally different concept from a Midterm exam. However, if you were to say that Midterm, PopQuiz and Final are all types of Exam that you could ask for a grade by calling a calculateGrade method on them, then there is some sense in that. I can relate to those ideas and the story that your code would tell would be much clearer.

Now, whose responsibility is it to calculate a grade? Is it the Student who should be responsible? It doesn't seem right.

Also, be aware that it is sometimes counterproductive to model all objects after their counterparts in the real world. That is, while a student in the real world can certainly calculate their own grades, this idea doesn't necessarily translate very well into a good object-oriented design. Rather, you need to look at the data and the actions that need to be performed on that data. The actions and data combine to become objects. Good design seeks to bind information and actions performed on them very tightly. Bad designs arise when that tight binding (encapsulation) is violated or when there is no tight binding between information and actions at all.

So, if not Student, then who should be responsible for calculating grades? Remember, a good OO design doesn't necessarily have to mimic the real world... I would not recommend a Teacher class here.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Hendra Kurniawan wrote:I'm avoiding inheritance because the objects are parsed from XML using JAXB. I don't know the effect of inheritance to parsing process.

I'm sorry, but I don't even know how to start making the connection between inheritance and parsing XML using JAXB... Can you elaborate a little more so I can see where you are going / coming from with this line of thinking?
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
for inheritance, say student object inherits person object. Now I haven't played with XML before and barely managed to do POJO <-> XML. I don't know what will happen if this:


turns to this


So, I'm just trying to keep things simple.

Second, about why I put grade in student and midterm, it's because I'm following example about coffee making if I'm not mistaken in the design pattern book. It seems so similar except the example used inheritance. So how do you suggest grading the student? and yes, I thought of moving the responsibility to Teacher class. Do you have better approach?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Rather than me giving you my design ideas, how about I try to guide you to one of your own making? Or at least one that you came to with some poking and prodding

Start with what you want to accomplish:

As a (WHO will get the benefit of this program?), I would like to (DO WHAT) so that (WHAT is the BENEFIT?)

Here, it's perfectly fine to say "As a TEACHER, I would like the computer to CALCULATE GRADES OF STUDENTS so that I DON'T HAVE TO DO IT MANUALLY."

Is this pretty much your problem/solution statement?
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
sort of, yeah. I'll be given a list of students, and I'm required to grade them. both teacher and student benefit from it because it's published openly.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

OK, so what information do you have? List it all down. Try to make basic logical groupings of that information as well. Forget about your original design first.
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
info? I've listed the information I have. I don't really understand. all the parameter needed to grading are listed in first post.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

so does attendance go into the grade? does extracurricular go into the grade? activity name? teacher name?

If your problem is calculating a grade, why do you have all this stuff? What is the exact information that goes into a grade? Ok, I admit, that's a rhetorical question. So it's just points, right? So again, why all this other stuff?
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
the only ones with points : midterm and attendance. Extracurricular is just additional information to be displayed. As for name, well, it's quite obvious why it's needed. Extracurricular also serves as a "different" member so I can see how decorator solves problem where some member doesn't have grading ability.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

So a student's grade is made up only of points from a Midterm plus points for attendance?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Hendra Kurniawan wrote:Extracurricular also serves as a "different" member so I can see how decorator solves problem where some member doesn't have grading ability.

Again, I am struggling to make a connection here. What does a Decorator have to do with extracurricular not having a grading ability?
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
nothing, that's the point. I want to know if decorator that I suggested in the first place can handle a case where not all of the decorated object's member have grading ability. but then you said decorator pattern doesn't suit this business logic, and so I wonder what other approach suitable for this requirement.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Are you talking about the requirement to calculate a grade based on midterm + attendance? Why are you so bent on finding a pattern? There's no rule that says you have to use a pattern. Patterns are nice to have if your problem is similar to the problem that the pattern addresses. Otherwise, looking for a pattern to solve a problem becomes more of a distraction. Forget about patterns for now.

Answer this simple question: How do you calculate a grade? Just one single student's grade -- how do you calculate it?
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
point=(attendance + grade)/2:
attendance = attendance days/effective teaching days
grade : grade/4 --> grade are whole number between 0 - 4 inclusive
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Ok, seems we're getting somewhere now. Some things still not clear with these formulas:

grade : grade / 4 ; where grade is a whole number from 0 to 4.

So grade (left side) can be any of 0/4, 1/4, 2/4, 3/4, 4/4? If this is correct, you need to be clear about what the left side "grade" is vs. the right side "grade" -- using "grade" to refer to both the left-hand and right-hand side is ambiguous and confusing

Also, I didn't see where you had "effective teaching days" in the first post. So let me try to remove some ambiguities and you tell me if I got it right:

grade_points = grade / 4 where grade has a value from 0 to 4 which is presumably the "score" they got for their Midterm exam.

attendance_points = days_in_attendance / effective_teaching_days

final_grade = (attendance_points + grade_points) / 2 (essentially, the average of grade_points and attendance_points)
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
yes, grade_score= grade/4. and teaching days is a constant updated every new school year
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Hendra, it's way past my bedtime now. Was just up waiting for my computer to finish a long-running task but I need to get some rest. I'll leave you with this: If I got the formulas right, then I don't see any need WHATSOEVER for a pattern here. A single class that contains the information you have to use to calculate a final grade is all that's really needed here. Nothing much fancier than that. Think about it: Where does a teacher keep all the information about students' scores and attendance? Back in my day, this was done manually in a class record book. It looks like a ledger, where each row is for one student and each column after the student's name is some component that goes towards the student's final grade.

As I see it, the only object you really need to do all the work for the requirement of "Calculate a grade for a Student" is something that corresponds to a single row in that "Class Ledger". Like I said before, a good OO design doesn't really have to use patterns and it doesn't have to be modeled just like the real world.

Think of it this way: somebody or something says to the Ledger, "Hey Ledger, calculate the final grades for all the Students you have listed in you" and the Ledger goes to each LedgerRow and says "Hey, LedgerRow, calculate the final grade for the Student whose scores and attendance you are tracking."

Something like that. If you don't get it, ask some other bartenders or sheriffs to help you out as I'm going to be on the trail for a few days to drive some cattle. If, after all my pleading for you to forget about patterns, you are still bent on finding one to solve your "problem", then I wish you luck in your quest. So long for now!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

I hate it when an idea pops into my head just as I'm about to fall asleep...

If you really want a pattern name to attach to all this, how about I give you two: "Delegation" and "Loop Iteration" Not sure if those are actually patterns though. At the very least, they are idioms, I suppose. Anyway, that might scratch your particular itch. Hopefully.

I'm off to bed now. This time for real...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: some help with student grade