IC211 Spring AY 2020

HW Polymorphism

Name (Last, First): ____________________________________________________ Alpha: _____________________
Describe help received: _________________________________________________________________

  1. [24pts]

    Apple.java
    public class Apple {
      public String foo() {
        return "apple-foo";
      }
    
      public String foo(char c) {
        return foo() + "-" + c;
      }
    
      public String foo(int i) {
        return "red-foo";
      }
    }
    
    Jazz.java
    public class Jazz extends Apple {
      public String foo() {
        return "jazz-foo";
      }
    
      public static String bar(Apple a) {
        return "bar-" + a.foo();
      }
    }
    
    Fruit.java
    public class Fruit {
      public static void f() {
        Apple v1 = new Apple();
        Apple v2 = new Jazz();
        Jazz  v3 = new Jazz();
    
        // <--- right here!
      }
    }
    

    Consider each expression below as appearing at the right here! in file Fruit.java, string that the resulting function call would produce.

    1. v1.foo()     ____________
    2. v1.foo('x')  ____________
    3. v1.foo(7)    ____________
    4. Jazz.bar(v1) ____________
    1. v2.foo()     ____________
    2. v2.foo('x')  ____________
    3. v2.foo(7)    ____________
    4. Jazz.bar(v2) ____________
    1. v3.foo()     ____________
    2. v3.foo('x')  ____________
    3. v3.foo(7)    ____________
    4. Jazz.bar(v3) ____________

  2. [76pts]

    Take a look at this program which is a tool for analyzing files. If you have a file foo, then cat foo | java HW10 will tell you whatever the program is able to determine about the type of the file foo.

    Your job is to extend it so that in addition to recognizing ASCII files and JPG files, it also recognizes PDF files (which begin with four bytes corresponding to the ASCII characters %, P, D, and F (i.e. bytes %PDF). You may not modify the files Categorizer.java, RecogASCII.java or RecogJPG.java, but you may modify HW10.java and you may create new classes.

    Don't get overwhelmed. Look at HW10.java and think about how to add a PDF recognizer. Then look at RecogASCII.java and think about how to make your own PDF recognizer. You don't have to thoroughly understand all the other code.

    Download these input files to your directory with your Java solution: hw10in1, hw10in2, hw10in3, hw10in4. And then test your program as follows:

    cat hw10in1 | java HW10     // should output JPG
    cat hw10in2 | java HW10     // should output PDF
    cat hw10in3 | java HW10     // should output ASCII
    cat hw10in4 | java HW10     // should output ASCII

    If you don't want to manually download them, you can alternatively use curl to pipe the download directly to your program:

    curl -s http://faculty.cs.usna.edu/IC211/docs/hw10in1 | java HW10     // should output JPG
    curl -s http://faculty.cs.usna.edu/IC211/docs/hw10in2 | java HW10     // should output PDF
    curl -s http://faculty.cs.usna.edu/IC211/docs/hw10in3 | java HW10     // should output ASCII
    curl -s http://faculty.cs.usna.edu/IC211/docs/hw10in4 | java HW10     // should output ASCII
Turn In: This sheet, of course, and a printout (using codeprint!) of HW10.java and ANY NEW .JAVA files you created, along with a screencapture showing your program's output on the above examples. Do not print out the .java files that we provided for you.