Back to 111 Main Page

Mobius strip

Assignment 15

Task

Sort a list of user-entered grades.

Textbook: 3.1, 4.1 - 4.2
Concepts: creating instances (objects), instance variables.

Steps

Ask the user to enter a series of grades, associating a name (a String) with each grade (a double). When the user is done (indicated by entering nothing for a name), print all the grades they entered in sorted order, starting with the highest grade value.

Internally, your program must store the grades in an ArrayList of Student objects. Use this class definition for Student: Student.java. (You don't need to do anything special to use this class in your program--no importing or pasting required. Just save the file in the same directory as your UsernameA15.java file, and Java will find it.)

Sample Output

This program will sort the grades you enter.
Enter a student name (or nothing to stop): Jim T.
Enter grade for Jim T.: 9.9
Enter a student name (or nothing to stop): Bobby
Enter grade for Bobby: 10
Enter a student name (or nothing to stop): Sally
Enter grade for Sally: 5.4
Enter a student name (or nothing to stop): Jenny
Enter grade for Jenny: Fred
Bad input: please enter a number.
Enter grade for Jenny: oops
Bad input: please enter a number.
Enter grade for Jenny: 9.5
Enter a student name (or nothing to stop): Fred
Enter grade for Fred: -1
Enter a student name (or nothing to stop): Jim P.
Enter grade for Jim P.: 8.3
Enter a student name (or nothing to stop):

You entered:
Bobby:  10.0
Jim T.: 9.9
Jenny:  9.5
Jim P.: 8.3
Sally:  5.4
Fred:   -1.0

Tips

  • Review A14--most of the program logic is the same.
  • It would be possible to use only nextLine() and nextDouble() for this one, and forgo parseDouble. However, if you do, you'll need to remember to clear the input stream of the newline (by calling nextLine()) before reading the next name. [Update: see FAQ below for more.]
  • Don't forget to create a new Student object for each student you're putting into the ArrayList. (If you forget to do this, every object in the ArrayList will actually be a reference to the same single Student.)
  • The tab character ('\t') is handy for formatting columned output.

What to Submit

Upload your UsernameA15.java file to Tamarin. (You do not need to include Student.java at all; Tamarin already has a copy of it.)

Grading [5 points]

1 - Compiles + Coding Standards
Your program compiles successfully (no errors). Your code follows Java coding standards.
1 - Input
Your program repeatedly prompts the user to enter a student grade (a name followed by a double grade). The program stops if the user enters nothing for the name (empty string); does not stop on bad numerical input.
1 - ArrayList<Student>
Your program uses an ArrayList of Student objects to store the grades read in from the user
1.5 - Output
Your program then prints out a list of all the grades the user entered, displaying both the name and the grade for each .
0.5 - Correctly sorted.
When the program prints out the entered grades, they are in sorted order, from highest to lowest grade value.

FAQs

I'm using nextDouble() to read in the number, but my program is acting weird, such as ending after it reads in the number or infinitely looping if the user doesn't enter a number!
I didn't get to talk about clearing the input stream as much as I would have liked in lab this week. (Not at all in Robertson's section!) But have a look at the comments in this sample code (and try what they suggest) and hopefully this behavior will make sense: InputTrouble.java
How do I access the instance variables within an object in the list?
The point of this assignment is to show that an object (without methods) is just a bundle of variables. Specifically, every Student object is just a name and a grade. This means we can pass around or sort whole Student objects, and all their internal fields are passed around and sorted along with the object.

So, this means that when you get an object out of the list, you get a Student object. So you may want to do something like this:

  Student fromList = list.get(i);

Now, if you just try to print this whole object, Java is only going to display the class name and address of this object, giving you something like this: Student@1a748cb. (It does this because you haven't defined how Java should print out a Student object; we'll learn how to do this soon.) Instead, you need to access and print each of its fields separately:

 System.out.println("Name: " + fromList.name);
 System.out.println("Grade: " + fromList.grade);

If you only need access to a single field (such as when sorting), you could combine these next two lines:

  Student s = list.get(i);
  if (s.grade ...
as follows:
  if (list.get(i).grade ...

The lesson here is: first, get the whole Student object out of the list, and then access the particular field/instance variable that you want within that particular object.

How do I load the list with Students in the first place?
Assuming you have an ArrayList<Student> named list, you would need to do something like this:
while (/*user still wants to enter things*/) {
  Student nextStudent = new Student();
  //get name and grade details from user and then load Student object
  nextStudent.name = ...
  nextStudent.grade = ...
  list.add(nextStudent);
}

Of course, this adds the next item to the end of the list, so now you'd have to sort the list after the user is done entering data. The alternative is to add each nextStudent into the list in sorted order. Any sorting is now based on the grade field of each student in the list, but otherwise the sorting process is exactly the same as for A14.



~ztomasze Index : TA Details: ICS111: A15
http://www2.hawaii.edu/~ztomasze
Last Edited: 16 Mar 2009
©2009 by Z. Tomaszewski.