aspose file tools*
The moose likes Beginning Java and the fly likes  Exam question for a beginner coder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Exam question for a beginner coder" Watch " Exam question for a beginner coder" New topic
Author

Exam question for a beginner coder

Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
Hey all, I've been teaching myself Java from the headfirst books but am struggling with it. I have an exam coming up and this is the sample question he gave us as an example. My question is this: Can you tell me why I am getting a run-time error in my code? Also any other feedback in terms of structure or format would be much appreciated. I know I haven't encapsulated but I was going to add that at the end because I'm still not sure how to do it yet.

QUESTION:
Write a Java class called ExamMarks, which specifies a module name (String), module code (int), year (int) and an array of 20 integer marks. The class should have an appropriate three argument constructor, a toString() method and methods displaying the highest mark and the average mark. It should have a separate method to obtain up to 20 marks from the user via the console. Use appropriate visibility modifiers to ensure encapsulation. In the main(String[]) method create an ExamMarks object. Marks will be given for good program design, code layout, and choice of identifiers as well as a satisfactory implementation of the requirements above. No marks will be given for comments or the inclusion of extra functionality not described above.




K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1966
    
    7

Joe Pegler wrote:QUESTION:
Write a Java class called ExamMarks, which specifies a module name (String), module code (int), year (int) and an array of 20 integer marks. The class should have an appropriate three argument constructor, a toString() method and methods displaying the highest mark and the average mark. It should have a separate method to obtain up to 20 marks from the user via the console. Use appropriate visibility modifiers to ensure encapsulation. In the main(String[]) method create an ExamMarks object. Marks will be given for good program design, code layout, and choice of identifiers as well as a satisfactory implementation of the requirements above. No marks will be given for comments or the inclusion of extra functionality not described above.


Welcome the Ranch

Since we ranchers aren't code mills, I'm not writing the code for you. Some hints and directions:
1) The ExamMarks class is a JavaBean with 4 fields, toString(), getHighestMark(), getLowestMark(), printMarks() etc (you may want to implement Comparable interface to sort the marks)
2) The main method should be in some other class outside ExamMarks like TestExamMarks. (this will not clutter your logic from the data)
3) since there is an array, it's better to work lists then convert it to array (find out what method from Arrays class)

Given these suggestions, your current approach for main method will need rewriting.

If you know the number of iterations it is unusual to use while loop, better to use for loop. For lists you can use for-each loop too.

Happy coding.

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Sorry, K. Tsang, I have to disagree with most of your suggestions. For one, your suggestion gives more than what is asked for, which will not get the OP any marks and perhaps even result in some marks deducted. Secondly, the instructions clearly state that ExamMarks is to be the main implementation class, as well as the class that has the main(String[]) method. Lastly, I see no reason to go beyond using an array, as stated in the instructions, to hold the data for exam marks. About the only thing I agree with is that the main method needs to be rewritten.

My comments: There is no requirement to keep the student number or the name. There is the module name, module code, the year, and the exam marks. I don't think the module name and module code are the same as student name and student id.


Junilu - [How to Ask Questions] [How to Answer Questions]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Joe Pegler wrote:Can you tell me why I am getting a run-time error in my code?

Not until you tell us what it is.

Also any other feedback in terms of structure or format would be much appreciated. I know I haven't encapsulated but I was going to add that at the end because I'm still not sure how to do it yet.

Well I'd get into the habit of doing it automatically, because it's very important. And the first step is to make all your fields private.

As to other feedback:
Write a Java class called ExamMarks, which specifies a module name (String), module code (int), year (int) and an array of 20 integer marks.
That's 4 fields; you only have 3.
The class should have an appropriate three argument constructor
Don't see that.
a toString() method
Or that.
and methods displaying the highest mark and the average mark. It should have a separate method to obtain up to 20 marks from the user via the console.
Or any of those.

And as a general comment: you seem to be trying to do everything in your main() method. DON'T.
(but don't worry; it's a common beginner's mistake ).

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 632
    
    7

As Junilu pointed out, the student name is not part of the requirements, it's the module name. Read your requirements carefully.

Use a for loop instead of while. This is just making it more confusing.

Your error is related to the the fact that your println statements running for all values of z but you only have 2. I won't say anymore about that until you give it another shot.

Finally, encapsulation has to do with the visibility of your variables from outside of this class. How would you hide those variables?

Try again and post your revised code and we'll be happy to help.

"There is no reason for any individual to have a computer in his home" ~ Ken Olson, Co-founder of DEC, 1977
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
Thank you all for the feedback, will certainly give it another go and repost. Much appreciated.

Joe
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
Alright I'm getting closer. Thanks for all your help. After a lot of time tweaking I've come up with this. The array works but I haven't done it in the class. How would I go about doing that? Any other suggestions?

Cheers

Joe




Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Joe Pegler wrote:Alright I'm getting closer. Thanks for all your help. After a lot of time tweaking I've come up with this. The array works but I haven't done it in the class. How would I go about doing that?

Move your 'numbers' array into the class itself, so it becomes a field.

Any other suggestions?

Yes. You still haven't made:
1. Your fields private.
2. A 3-argument constructor, as per the exam question.
3. A toString() method.
4. Any methods displaying the highest or average mark, or to obtain up to 20 marks from the user via the console.

My suggestion: tackle them one at a time, so you're not overwhelmed by it all.

Winston
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
Slowly but surely wins the race. I feel like I'm learning!

How do I go about writing a method for the averageMarks and the highestMarks? There isn't a variable [i] yet defined so I can't yet write the code in the method. How would you suggest I go about doing this?
Any other advice appreciated.

Cheers, Joe.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Joe Pegler wrote:How do I go about writing a method for the averageMarks and the highestMarks?

Exactly the same way as you should write any method: Forget about Java and think about how you would do it yourself; and write down all the steps in English (or your native language). Draw pictures if you need to.

Just think about an "average" method for a moment:
1. What does it need? (Hint: Can you calculate an average of 0 items, 1 item, 173 items...?)
2. What is the formula for calculating an average?
3. What should the method return?

Do this for ALL methods you write before you even turn your computer on. You'll be a better programmer for it.

Winston
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
I'm going to presume it's something like:



But I can't put that in the method because 'i' has not yet been defined that happens in the main object... Does that make sense?

*This is meant for highest obv.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Joe Pegler wrote:I'm going to presume it's something like:
...
But I can't put that in the method because 'i' has not yet been defined that happens in the main object... Does that make sense?

Not really. And don't presume anything.

Tip (and it's a really good one): StopCoding (←click; and read carefully).

You're tying yourself in knots because you're trying to think in Java.
DON'T.

Work out what a "highest" method needs to do on ON PAPER and in English before you write a line of Java code.

Winston
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
Will do that and come back to it. Thank you for the advice.

Joe
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Joe Pegler wrote:Will do that and come back to it. Thank you for the advice.

You're welcome, and good luck.

Winston
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
AVERAGE
PLAIN ENGLISH:



CODE:




HIGHEST
PLAIN ENGLISH:



CODE:



This look OK?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Joe Pegler wrote:This look OK?

Much better. But you still haven't defined what you're going to do with the result - or maybe you were going to set some class variable to it?

In general, methods - especially functional methods like this - should return something. It's also better if you pass the thing that you want to work on ("the array" ?) to them.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Joe Pegler wrote: . . .

. . .



This look OK?
No. Neither does, I am afraid.
Potential error no 1: trying to do two things simultaneously. Work out the average or the highest, but not both together. Let’s try the average (arithmetical mean) first.
What will happen in your method if the marks are like this? new int[]{24, 43, 52, 61, 11, 97, 53, 61, 47, 72, 57, 49, 63}? That should give you a hint (BTW: that array has 13 elements in). Or what if there are 25 elements?
Work out how often you are calculating the average.
When you have worked out the average, what are you doing with it? Is it a field of the class?
Get the average method working, and you can look at the highest and smallest methods later. BTW: Highest and smallest are very similar; if you get one right the other will be very easy.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
For the average, do you need to perform average = sum/numbers.length each time through the loop? Or can you maybe do it just once, after the loop?

For "highest", what happens if your input array something like this?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Campbell: I guess the shoe's on the other foot this time.
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
Campbell Ritchie wrote:
Or what if there are 25 elements? >>


. . .

better!? cheers.
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
Winston Gutkowski wrote:
But you still haven't defined what you're going to do with the result. In general, methods - especially functional methods like this - should return something


It's supposed to be a method so I should 'return;' 'highest' and 'average'.?

Winston Gutkowski wrote:
It's also better if you pass the thing that you want to work on ("the array" ?) to them.


I'm afraid you've lost me there.
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
Winston Gutkowski wrote:
It's also better if you pass the thing that you want to work on ("the array" ?) to them.


Will read up on it and go again.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Joe Pegler wrote:It's supposed to be a method so I should 'return;' 'highest' and 'average'.?

Deal with one thing at a time: but, yes, a method called 'average' should probably return the result of its findings - ie, the value of average when it's finished.

I'm afraid you've lost me there.

Have you not learnt about parameters yet? If not, you really need to read up them before you write another line of code.

However, just to start you off; your 'average' method signature should probably look more like this:

public double averageMark(int[] numbers) { ...

Winston
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
Mike Simmons wrote:For the average, do you need to perform average = sum/numbers.length each time through the loop? Or can you maybe do it just once, after the loop?

For "highest", what happens if your input array something like this?





Better!? Cheers.

***Scratch that*** nothing's changed I presume... Gimme a minute!



Better now?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Joe Pegler wrote:Better now?

Yes, but I hate to say Joe, simply machine-gunning us with possibilities is not really a great use of your time or ours.
1. Did you read my last post?
2. Did you understand it?
3. Does your method signature look anything like what I showed you? If not, what's missing?

Winston
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
As for the issues I raised, the new code doesn't really seem to fix them either. If you run you method, with numbers array set to a new int[] {3, 2, 1 }, you will see results which point to a couple problems. Trying to compile and run the code yourself will give you a lot more information than guessing and asking us.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Winston Gutkowski wrote:
Joe Pegler wrote:Better now?

Yes...

Beg pard, scratch that (I didn't look at the method body properly) - No - although the return looks better.

Follow Mike's advice and test it.

Winston
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
Has this been sufficiently encapsulated? Can you mark my assignment out of 10? What would you do to improve my code.

Thank you,

Joe.



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Joe Pegler wrote:Has this been sufficiently encapsulated?
No
Can you mark my assignment out of 10?
What would you do to improve my code. . . .
Why are those average and highest marks methods in the exam marks class? They have nothing to do with exam marks. They have to do with arrays, so they should be in a class called ArrayUtilities or similar.
How often are you calculating the average mark?
How did you work out that highest method?
Why have you got the main method inside the same class?
Why are you only accepting 20 marks per exam? Why can’t you have 19 or 21 marks?
Why are you not calling the static methods on the name of the class?
Why have you put those default values in the no‑arguments constructor?
Why have you got a no‑arguments constructor at all?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

I have to disagree with you, Campbell, about the ArrayUtilities thing. The array is an implementation detail but I would argue that the highest and average actually say something interesting about the data, and that's what encapsulation is about: hiding implementation details about data while exposing information about it. In this case, the raw scores are data and the calculation of average and max are interesting things about that data. Granted, some of the names could be better but I see no problem with having ExamMarks.highest() and ExamMarks.average() methods. Under the covers, the programmer is free to implement the calculation in any way he pleases, including calling some kind of general "ArrayUtility" class to iterate over an array (or list or whatever) to do the actual calculation. I'm not that big of a fan of utility classes but if it eliminates some duplication, that's fine. However, having highest and average methods on the ExamMarks is more OO, IMO, because clients of ExamMarks will only have to depend on ExamMarks directly and not have to know about ArrayUtilities.

Another thing, if you change the implementation later to something that would make it unnecessary or inappropriate to use an ArrayUtilities type class, client code will be insulated from the change. Otherwise, the change to the way average and highest is calculated may need to be rippled out to all clients, and that's not a good thing.

BTW, I don't think that average and highest should be static. They should be instance methods that apply to a particular collection of exam marks.

[sorry for so many edits, it's hard to type with one hand in a splint]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Here are some things I think will improve your design:

- have main() be the place from which you control the general flow of interaction with the user: displaying messages, getting input, choosing to exit, etc.
- have main() instantiate a new ExamMarks for a given set of marks for a module.
- ExamMarks can have a constructor that takes the module code, module name, and year as arguments and assigns these values to final fields.
- The constructor can also initialize an empty data structure such as an array or list to hold the marks that it will be responsible for keeping.
- have ExamMarks be responsible for calculating things like highest and average based on the data it has.

This separates the user interaction concerns from the domain concerns (e.g. highest, average), thus giving you better encapsulation.

Edit: looks like you're already doing most of the above based in your most recent revision, except you have highest and average as static methods instead of instance methods.

I'd be more generous in the grade, maybe a 6 or 7. You could chose better names. "grade" is not a particularly good name since the object is not really a grade.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Junilu Lacar wrote:I have to disagree with you, Campbell, . . .

[sorry for so many edits, it's hard to type with one hand in a splint]
As long as your hand is getting better.

I am not convinced that you disagree with me a lot. What you are saying is that the Exam class should have an array of marks, and methods which show highestMark(), lowestMark(), averageMark(), etc. Agree.
You are saying that how the average is calculated, and that a utility class is a valid way to do it. Agree. What I meant, and didn’t put clearly, is that calculating averages of arrays doesn’t belong in an exam class.
What about the exam candidate? You will want a Student class, and that student will have an average of all their exams.
What about a SkatingScores class, where you have an array of marks from the six judges?
What about a FootballClub class, where you have an array of goals for and goals against over the season? They will all require similar functionality, summating and averaging arrays, and I think putting those methods into a utility class is a good way to go. If you change functionality and no longer use the utility class, as you said, no problem. It is still available for other users.
Having looked at your post, it is pretty obvious what such a method would look like:Whether you would retain such a value as a field, or recalculate it every time it is requested, I am not sure.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Calculating the average of any array is a good candidate for a static method in a utility class.
Returning the highest value of the array which is a field of your object is obviously not a static method.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Additionally, somewhere there would need to be a Comparable or Comparator, in order for a static utility method to know how to compare two different ExamMarks instances. Merely calling ArrayUtilities.lowestValue(marksArray) can't possibly be enough, unless ExamMarks implements Comparable.
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
FYI it's a written exam. I'll have about 45 mins to answer a similar question to this without a computer. For that reason he suggests you just use one class with the main method inside it. 'lowestMark' wasn't mentioned in the question. 20 grades were specifically mentioned in the requirements.
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
Junilu Lacar wrote: except you have highest and average as static methods instead of instance methods.


Will work on this for now and come back to other problems later, thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
I meant that the ArrayUtilities#lowestValue() method would take an int[] as its parameter, so you don’t need a Comparator. Sorry that I wasn’t clear about that. I haven’t been clear about anything on this thread, have I?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Joe Pegler wrote: . . . 'lowestMark' wasn't mentioned in the question. . . .
So, if I mention lowestValue() and you work that out, and then realise that highestValue() is very similar but the other way round and you work it out, nobody can accuse you of cheating because you brought up the question here.
Joe Pegler
Greenhorn

Joined: Jan 16, 2013
Posts: 16
Campbell Ritchie wrote:So, if I mention lowestValue() and you work that out, and then realise that highestValue() is very similar but the other way round and you work it out, nobody can accuse you of cheating because you brought up the question here.


Is there something wrong with my highestMarks()? It works...
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
I don't think it does work - not consistently. Not unless you've changed it since you posted it above. As I suggested earlier, try it on an input array of

What does it give you for "highest"?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exam question for a beginner coder
 
Similar Threads
Program Review
to Dave Lander,Marilyn de Queiroz
help in fixing the error
Grade Calculator Alot of Errors While Compiling
Averaging Grades Assignment