15-110 FALL 2009 (CORTINA)

HOMEWORK 9 - due Friday, November 20, 2009 in class

Please remember that homework must be handed in on time. No late homework will be accepted.

  1. (2 pts) Write one sentence that explains what each of the following LINUX/UNIX commands do:

    ls
    
    cd Desktop/MyWorkspace/BookShelf/src
    
    javac *.java
    
    java RemoveTester1
    

  2. (1 pt) Write a Java class named SquareGenerator that contains a main method that extracts an integer passed in the command line parameter array args and outputs the square of this integer. For example, if the user types the following command on the command line

    java SquareGenerator 9
    

    your program should output 81. If the user supplies no command line arguments or more than 1 command line argument, output the error message "INVALID USAGE" instead.

  3. (2 pts) A set of objects are related to one another through inheritance. For example, the following shape classes are related as follows:

                                  Shape
                                 /     \
                            Circle    Polygon
                                     /       \
                                 Triangle   Parallelogram
                                                  |
                                              Rectangle
                                                  |
                                               Square   
    

    For example, Square is a subclass of Rectangle, so every instance of Square inherits all of the properties and behaviors of its superclass Rectangle. Rectangle inherits from Parallelogram, and Triangle and Parallelogram are both subclasses of Polygon, and so on. For the following list of objects, draw a diagram similar to the diagram above that indicates the class hierarchy of these objects with respect to one another using the principle of inheritance. (Each class should have no more than one superclass.)

    Amphibian
    Animal
    Bee
    Bird
    Bumblebee
    Cardinal
    Dog
    Eagle
    Fish
    Frog
    Human
    Insect
    Invertebrate
    Mammal
    Mosquito
    Reptile
    Snake
    Vertebrate
    Whale
    

  4. (2 pts) A 24-hour (military) Time is modeled using a Java class with two fields as shown below:

    public class Time implements Comparable
    {
        private int hour;       // an integer between 0 and 23
        private int minute;     // an integer between 0 and 59
    
        public Time(int hourNum, int minuteNum) {
            // creates a Time with the given hourNum and minuteNum if valid
            // otherwise creates the Time 00:00
            if (hourNum < 0 || hourNum > 23 || minuteNum < 0 || minuteNum > 59) {
                hour = 0;
                minute = 0;
            }
            else {
                hour = hourNum;
                minute = minuteNum;
            }
        }
    
        public int getHour() {
             return hour;
        }
    
        public int getMinute() {
             return minute;
        }
    
        // other methods not shown
    }
    

    The Time class inherits from the Object class in Java. Two Time instances are equal if they have the same hour and the same minute.

    1. Write an equals method for the Time class so that it overrides the equals method inherited from the Object class.

    2. Why do we need to override the equals method of Object? What does Object's equals method do instead?

  5. (1 pt) Using the class definition for Time above, write a toString method for the Time class so that it overrides the toString method inherited from the Object class. It should return a String with the current time in the format HH:MM, where HH is the hour using 2 digits and MM is the minute using 2 digits. Add a leading zero if necessary for hour and/or minute.

  6. (1 pt) Using the class definition for Time above, we see that this class implements the Comparable interface. This means that there must be a compareTo method that compares this time with another time for relative ordering (less than, greater than, equal to). Complete the compareTo method below for the Time class so that it returns a negative integer if this time comes before the other time chronologically (i.e. "less than", a positive integer if this time comes after the other time chronologically (i.e. "greater than"), or 0 if this time is equal to the other time. You may assume that the object supplied in the parameter is an instance of the Time class.

    public int compareTo(Object obj) {
         Time otherTime = (Time) obj;
         // complete the rest of this method:
    
    
    
    
    }
    

  7. (1 pt) Write a main method that creates two 24-hour (military) Time instances of your choice, and then outputs each time in the format HH:MM using the toString method you wrote, outputs if the two times are equal to each other or not using the equals method you wrote, and outputs whether the first time comes before the second time chronologically using the compareTo method you wrote.