Assignment #2: Java Classes, Interfaces, and Polymorphism


Assigned:   Wednesday, February 14
Due:   Wednesday, February 21

Contents:


Overview

Topic(s): interfaces, polymorphism, implementing and using interfaces, Comparable and Comparator interfaces, anonymous classes, intro to Java Swing, inheritance, class hierarchies, invoking superclass methods, abstract classes
Related Reading: Chapters 1 and 4 (primarily 4.1 - 4.5)


Practice Problems

    To see the solutions to these problems, go to Solutions, and select the desired chapter and exercise number.

  1. Exercise #1.11 at the end of Chapter 1 (p. 32)

  2. Exercise #1.13 at the end of Chapter 1 (p. 32)

  3. Exercise #4.3 at the end of Chapter 4 (p. 170)

  4. Exercise #4.5 at the end of Chapter 4 (p. 170)

  5. Exercise #4.7 at the end of Chapter 4 (p. 170)

  6. Exercise #4.15 at the end of Chapter 4 (p. 172)


Problems to be Submitted (25 points)

When you turn in your assignment, you must include a signed cover sheet (PDF version) with your assignment (you're assignment will not be graded without a completed cover sheet).

You are allowed to submit your assignment via email, but if you choose to do so, you must bring a hardcopy of your assignment along with a completed cover sheet to the instructor at the next class. (Note: Do not email the instructor any .zip file attachments, as SLU's email may not deliver these emails, i.e. the instructor may not receive your email.)

  1.   (4 points)

    Exercise #4.4 at the end of Chapter 4 (p. 170)

    In addition to your modified BankAccount class, be sure to provide some Tester class that demonstrates your addition of Comparable to BankAccount works as expected.

    Note: You are welcome to use this BankAccount class as a starting point, if you desire.

  2.   (5 points)

    Exercise #1.20 at the end of Chapter 1 (p. 33)

    Assume the input has one number per line, like:   hw02_prob1_input.txt

    Note: You'll need to use either Double's parseDouble(), or Scanner class's nextDouble() and hasNextDouble().

    Hint: Based on the Single Responsibility Principle (SRP), which class should handle reading data from the file?

  3.   (6 points)

    Create a new version of the program for Excercise #1.20, but this one reads in an input file like:

    which gives a student's name, followed by their scores on various assignments. The program should compute and store the min, max, and average score for each person. Create a new class, StudentGrades, that stores and manages this information, and then have the main program keep track of all students in an ArrayList.

    Make sure all instance variables are private. In other words, only method calls should be used to set or retrieve data from instances (objects) of that class.

    You should not assume that each student has exactly five scores (like the example in the given file) -- the code should work if there are fewer or more scores per students.

    Note: You may find the following method useful...

        public boolean isNumeric (String s)
        {
            try {
        	    Double.parseDouble (s);
                // if this doesn't throw exception, s is numeric
                return true;
            }
            catch (NumberFormatException e) {
                // s is not numeric
                return false;
            }
        }
    

  4.   (5 points)

    Create two Comparator objects for your StudentGrades class from problem #3, above. The first should compare two students by their average grade, while the second should compare two students by their names.

    In the main program, create a GUI with a JFrame and two JButtons. The first button should open a JOptionPane that shows the list of students sorted by average grade, and displayed in decreasing numeric order. The second button should open a JOptionPane that shows the list of students sorted by their names, and displayed in increasing alphabetic order. In both cases, both the student name and average grade should be displayed for each student.

    Hint: Does StudentGrades need to be modified when using the Comparator interface?

  5.   (5 points)

    Exercise #4.14 at the end of Chapter 4 (p. 171)

    Hint: Start with the MarsIcon class. Now, how can you change its color dynamically? Adding a method is very helpful...