• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning from a toString method

 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, am having this problem that wont go away: when i enter 2 to the first time i gives me 100 which is correct,but when i add another student record,instead of 160, i get 360.Here is a copy of the responses at command line and the results i get.


Here are the codes



 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Anderson wrote: . . .
. . .
I would change that. Same idea, same result, two unnecessary lines gone.
return String.format("%-15s%15s%n", studentName, exam.getExamMark());
Don’t use += with Strings; it risks slow performance. If you need to do such adding with Strings, either do it all in one statement with repeated + operators (special optimisation available), or use a StringBuilder. Don’t use new String(). It is never necessary. Why are you using %15s for examMark? Is the mark really a 15‑character String?

So where are you getting the odd marks? Put some print statements into the class which contains those marks. At the end of every method in that class simply write System.out.printf("xyz() method: mark = %d%n", mark);//test or similar. You can search for //test later and remove or comment out your testing code. Then you can watch the values change, and see where they change. And then you can work out where you are doing any adding, and whether you are adding the same thing twice and doubling up your results.
 
Winston Gutkowski
Bartender
Pie
Posts: 10273
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Anderson wrote:Here is a copy of the responses at command line and the results i get...

I hate to say, but I wouldn't use toString() at all for this sort of thing.

toString() is generally used for debugging or very basic display, whereas what you want is a report with very specific formatting.

I think you might also make things easier for yourself if your Teacher.addStudents() method (which looks more like addStudent() to me) maintains the totalMarks field.

HIH

Winston
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Alan Anderson wrote:Here is a copy of the responses at command line and the results i get...

I hate to say, but I wouldn't use toString() at all for this sort of thing.

toString() is generally used for debugging or very basic display, whereas what you want is a report with very specific formatting.

what do you suggest i use? Am basically following the design pattern we use in class (not that i think it's flawless though)
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Alan Anderson wrote: . . .
[code=java] @Override

So where are you getting the odd marks? Put some print statements into the class which contains those marks. At the end of every method in that class simply write System.out.printf("xyz() method: mark = %d%n", mark);//test or similar. You can search for //test later and remove or comment out your testing code. Then you can watch the values change, and see where they change. And then you can work out where you are doing any adding, and whether you are adding the same thing twice and doubling up your results.

I got your suggestion up until this point...i do not understand this paragraph
 
Winston Gutkowski
Bartender
Pie
Posts: 10273
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Anderson wrote:what do you suggest i use? Am basically following the design pattern we use in class (not that i think it's flawless though)

Then I guess you'll have to follow their lead. It's certainly not how I'd do it though.

In answer to your question, I would probably add a Report (or Reportable) interface with a single:
public String report();
method.

and have my Teacher class (because that's the one that you seem to "report" on) implement it.

But maybe it's a bit early for that sort of stuff yet . Don't worry, you'll get there.

Winston
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the strange use and implementation of toString(), the design of the classes has much to be desired. Both the Exam and Student classes have instance variables that track numberOfStudents. The responsibility of tracking the total number of students does not belong in either of these classes and the duplication and misplacement of this information is at the heart the problem you're seeing.

Edit: I just noticed that the duplication extends into the Teacher class as well, which again is an inappropriate assignment of responsibility, IMO.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Apart from the strange use and implementation of toString(), the design of the classes has much to be desired. Both the Exam and Student classes have instance variables that track numberOfStudents. The responsibility of tracking the total number of students does not belong in either of these classes and the duplication and misplacement of this information is at the heart the problem you're seeing.

Edit: I just noticed that the duplication extends into the Teacher class as well, which again is an inappropriate assignment of responsibility, IMO.


What would you do? you grand masters are only pushing in the dagger at the back of my head deeper and deeper...how do i pull it out?
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Anderson wrote:What would you do? you grand masters are only pushing in the dagger at the back of my head deeper and deeper...how do i pull it out?

That is not the intent. We are trying to tell where you're hurting yourself. By telling you what the problem is, we're hoping that's enough information to help you solve it yourself. See what this rancher did, for a good example: http://www.coderanch.com/t/608725/java/java/Binary-Search-loop#2779143
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with the principles of responsibility. A class takes care of itself. any information about that class is maintained inside that class, and any alterations are done inside that class.
Does a mark belong to the student, the module, the teacher, the exam or what? When you have decided that, put that information in that class, and all code which handles it. If any other classes need that information, let them ask the object of the class which has the information. Decide which of the following quotes is most sensible:-
Teacher, what did Campbell get in Java Programming?
Can I have a look at the Java exam results, please? I would like to know whether I got more than Campbell.
What did you get in Java Programming, Campbell?
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Does a mark belong to the student, the module, the teacher, the exam or what?



That's where am losing the plot...In the specification of the program am supposed to write, each Student object is supposed to have a result, as a Result object.isn't that going to require altering the Result class from the Student class?
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you share what that specification is, then we could all be on the same page and it will help us see better where you might be going astray.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If they specify that the result belongs to the Student, then the result goes in the Student class. Along with methods to get the result, add new results, display the student object with its results, as required.

I can’t remember whether you have a result class, but what you need to do is write one class, and make it very simple. One field. Test it a few times until you are sure it is working all right. If you think the class ought to have more fields, add them one by one, along with associated methods, and test again. It is very easy to get all confused because you have tried to do too much all at once, and the slice‑by‑slice technique is much quicker and easier.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Apart from the appropriate xxxTest classe for main, the following classes will be required:

Module
has an array of Student objects as well as module details (name, percentage contribution of the coursework, percentage contribution of the exam)
Student
has the name and banner id of the student and their result on the module (as a Result object)
Result
has the coursework mark, exam mark, overall mark, grade and result

This is the specification...
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then why do you have an Exam and Teacher class? I don't see anything in the specification that would require you to have these two classes.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original code is quite large,this one is just to test the problem i was having
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You design centrifugally, starting from the centre, with the big picture and working towards the periphery where the fine details are.
You develop code centripetally, starting from the periphery and doing the fine details first. Get the results class working first, with test cases: lots of test cases. Then you can move onto the student or teacher classes.
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Anderson wrote:The original code is quite large,this one is just to test the problem i was having

Regardless of the size of the codebase, the specification you gave has a very limited scope which does not require the involvement of either Exam or Teacher class.
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, I don't agree with the design as specified. In particular, "A Student has a Result" does not sit well with me. I would rather have the Module track the students enrolled in that module and each student's result for that module as well. That is, a module "knows" what students are taking it and what each student's result is for that module.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the Exam and Teacher classes are names i gave to the Result and Module classes respectively.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:BTW, I don't agree with the design as specified. In particular, "A Student has a Result" does not sit well with me. I would rather have the Module track the students enrolled in that module and each student's result for that module as well. That is, a module "knows" what students are taking it and what each student's result is for that module.

Sounds like a good idea also,along with the others suggested.I'll try them out and see how it goes.
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Anderson wrote:No, the Exam and Teacher classes are names i gave to the Result and Module classes respectively.

I would advise against doing that. Semantics are important and Exam and Result mean two different things. So do Teacher and Module. I would stick with the names given in the specification; they make more sense.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Boss
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree that a real‑life module has data for all its students’ results. But the question specification said something different. If you want marks, you need to answer the question asked. And if, as mentioned previously, the teacher class was not in the question, it will earn exactly 0 marks as unnecessary.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand, i created the Teacher and Exam classes as part of my own test classes and for use on this site...The program i intend to submit will definitely have the classes required by the specification...
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This again is one of those cases where you need to have a conversation with your instructor about how much leeway you have in creating a solution. As mentioned earlier, the specification says the Student should have a Result. If the instructor is not willing to entertain any challenges to that design choice, then you'll have to submit a solution that follows that design. However, if your instructor is open-minded and is willing to accept changes that improve upon the originally specified design, then you may even get extra credits for that. I haven't coded it myself but I can already imagine code that follows the original specification as having a number of problems.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay then...i'll have a word with him.Thanks for your time everyone,i'll be back.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic