Re: lost
- From: "Rhino" <no.offline.contact.please@xxxxxxxxxx>
- Date: Sat, 29 Oct 2005 18:18:21 -0400
"CPSCmajor" <bmerritt1987@xxxxxxxxx> wrote in message
news:1130615596.053969.170420@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Write a program that reads in the names and scores of students and then
> computes and displays the names of the students with the highest and
> lowest scores.
> A simple method of carrying out this task would be to have two parallel
> arrays.
>
> String[] names;
> int[] scores;
> However, you should avoid parallel arrays in your solution.
> First, write a class Student to solve the parallel array problem. Leave
> the StudentScores class and tester class for the following exercises. A
> Student simply holds a student name and a score.
> What is your Student class?
>
> Here is my Student class...
>
> public class Student
> {
> // instance variables
> private String name;
> private int score;
> private int s;
>
> /**
> * Constructor for objects of class StudentScores
> */
> public Student()
> {
> String name = "null";
> int score = 0;
> }
> public String getName() // Gets the name of the Student
> {
> return name;
> }
> public int addScore(int s) // Add the new quiz grade to the others
> {
> score = score + s;
> return score;
> }
>
> }
>
The instructions TOLD you that the Student class needs only the student name
and student score: why have you added a third element, 's'? I don't see why
you think you need 's' either.
> I can't come up with the Student Scores class though, I don't even know
> where to start. Any ideas?
>
According to the question, you are supposed to "Leave the StudentScores
class and tester class for the following exercises." You have not posted
"the following exercises"; how are we supposed to guess what is in it if we
don't know what it is supposed to do? I don't think anyone can help you with
the StudentScores class until you give more information about what the
"following exercises" require.
Fortunately, I think the information you HAVE provided is quite sufficient
to write the code that will store the student information in the Student
class and display the students who got the highest and lowest scores.
Do you have any restrictions on how the input can be obtained? The
professional way to ask for the student names and scores is to build a GUI
that has a form which prompts for the information for one student; you
simply invoke that form until you have input the information for all
students. However, if this is a beginner's Java class, it's entirely
possible that you are supposed to prompt at the command line.
You haven't specified if this is an application, applet, servlet, MIDlet, or
whatever. Assuming you are writing an application and haven't learned how to
do GUIs yet, you can easily prompt the user to give input at the command
line. Here's what you need to do:
0. Create a method with a meaningful name like promptForInput.
1. Within your new method, add this line:
BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
[That's the hardest part to discover for yourself: it creates a Stream
reader that will read from the console and process the input as full lines,
rather than as individual characters.]
2. Display some text as a prompt before each of the things you're going to
ask for. For instance:
System.out.println("Enter the name of the student:");
3. Read the data value that you get and store it in a String. For example:
String studentName = rdr.readLine();
Be sure to prompt for both the student name and the test score and to do a
readLine() for each value.
4. Assuming the test score is an integer (87, for example) and not a letter
grade, you will need to convert the score, which is initially entered as a
String, into an integer. For example:
String stringScore = rdr.readLine();
int intScore = Integer.parseInt(stringScore);
5. Now that you have prompted for the two things you need about each
student, store those values in the Student class. Store each occurrence of
the Student class in an array of Student classes.
Put all of this code in a method and invoke the method via a loop so that
each iteration of the loop creates one instance of the Student class and
adds that instance to the array of Student instances.
You'll need a way to tell the loop to stop. The traditional method of doing
that is to give it a special value that means "no more data". For example,
you could say that if the user ever enters "X" when prompted for a student
name, that it means that there are no more records to create. Your code
needs to test for that special value and then stop gracefully when it
encounters that value. I'll let you make the first stab at that code.
Why don't you write that much of the program now, then come back when you
have that done (or get stuck)? Once you have the input phase done, the rest
shouldn't be too bad.
Rhino
.
- References:
- lost
- From: CPSCmajor
- lost
- Prev by Date: Re: network is hindering my applet
- Next by Date: Re: tictactoe
- Previous by thread: lost
- Next by thread: Re: Desing Question re Multiple Constructors
- Index(es):
Relevant Pages
|