The moose likes Cattle Drive and the fly likes Time on your hands? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Time on your hands?" Watch "Time on your hands?" New topic

Time on your hands?

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you have time on your hands and you want a nice little OO assignment to think about, try this. I copied it over from the beginner's forum. This is for your amusement only. Do not send it in to be nitpicked. If you follow the assignment exactly and carefully you won't have any problems. Use javax.swing.JOptionPane for the input.
1. Implement a class named Student representing information about a student enrolled in a course and that student's performance on an exam. The class should have the following private instance variables:
Strings name and ssNumber, to store the student�s name and Social Security number.
An integer examScore representing the student�s score on the exam.
A boolean scoreAlreadyEntered that will indicate whether or not the student�s exam score has already been entered.
The class should have the following public instance methods:
Student(), a constructor that prompts the user for the student�s name and Social Security number, and reprompts for just the Social Security number until it receives a string of exactly nine characters. (To simplify matters, we won't do any kind of error checking on the name.) Once the name and Social Security number have been entered, examScore should be initialized to zero and scoreAlreadyEntered should be initialized to false (since no exam score has been entered for that student).
void enterScore(), which prompts for the student�s score on the exam, reprompting until it is in the range 0-100. The value 100 should be defined as an additional private (final) instance variable in the class declaration, and the user should be informed of the legal range of scores when prompted to enter the exam score. Once a legal value is entered, it is assigned to the examScore instance variable. If scoreAlreadyEntered is false, it is set to true after the assignment to the instance variable is done (since now an exam score has been entered for that student). We are keeping track of scoreAlreadyEntered so that when enterScore is called, we will know whether it is the first time we are entering that student's score, or if we have entered a score previously for that student and we are now entering a new, corrected score.
2. Write a simple driver class to test this class so that you are confident that it works correctly. This checkpoint is just for your benefit to be sure that your basic class is right before we start adding static (i.e., class) variables and methods.
3. Add the following private static (class) variables, both integers that you should initialize to zero, to the class:
examTotal, representing the total of all of the exam scores for those students whose scores have been entered. (Note that if more than one score has been entered for a particular student, examTotal should only include the most recent score entered for that student.)
numberEntered, which represents the total number of students whose scores have been entered and included in the total.
Of course, just declaring these class variables does not mean that they will store the correct values. One or both of these values may have to be updated when enterScores is run. In particular:
The first time that enterScores is run by a particular student (i.e., when scoreAlreadyEntered for the student is false), you should increment numberEntered, and increase examTotal by whatever exam score is entered for the student.
When enterScores is re-run by a student who has already run it before (i.e., when scoreAlreadyEntered for the student is true), then you should not increment numberEntered (as that student has already been included in the count), and you should subtract that student's previous exam score from examTotal before adding in the new score that is entered.
This will guarantee that the class variables maintain the correct totals throughout the computation.
4. Add the following public static (class) method to the class:
void reportAverage(), which computes and displays (using System.out.println) both the number of students whose scores have been entered and the average exam score of all students who have had their exam scores entered. Note that the average should only be computed if the number of students with scores entered is greater than zero -- otherwise an error message should be displayed (again, using System.out.println).
5. Extend your driver class so that it declares a few Student objects and demonstrates that all of your methods work correctly. It is your job to be sure that the driver thoroughly tests your class, but in particular you should be sure to enter at least three students� scores, display at least two different averages, and run enterScores more than once for at least one of the students.

[ March 03, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Donald R. Cossitt
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
What? 70 odd posts from it's origin is not enough?

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The fact that the original poster cannot follow the assignment, does not mean that the assignment is a not good one. CattleDrive participants are used to the discipline of following instructions given in assignments.
[ March 03, 2003: Message edited by: Barry Gaunt ]
I agree. Here's the link:
subject: Time on your hands?
Similar Threads
jQuery in Action, 3rd edition