IC211 Spring AY 2020

HW Threads

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

  1. [20pts] Consider the example code Ex1.java/Foo.java from the "Getting data in and out" section of this lesson's notes. What's the difference between the example as it is presented, versus a modified version in which the call t.start(); is replaced with t.run();? Your answer should include
    • how do the two differ in terms of what we see in the terminal as it executes?, and
    • how do the two differ in terms of what's going on "under the hood" in the VM as they execute?
  2. [20pts] One way to create threads is to extend the Thread class, overriding the run() method to do whatever your thread is supposed to do, then instantiate an object of your new class type, and call its start() method. Another way is to create a class that implements the Runnable interface . From the API documentation: "A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target [argument to the Thread constructor]."

    Take the Example code Ex1.java/Foo.java from the "Getting data in and out" section of this lesson's notes, and rewrite it so that Foo implements Runnable rather than extending Thread.

    Check to verify that you actually compiled and ran the modified code.

    Write down the two lines of the program that you actually had to change to change the program to implement Runnable rather than extend Thread:

  3. [10pts] What is the big reason it's important to have the option to implement the Runnable interface rather than extending the Thread class?
  4. [50pts] OOP Review (not about threads!): Duplicating code is a sin in any programming language. Duplicating data (except for backup purposes) is likewise a sin. In Object Oriented Programming, when we see duplicate code or data in two classes, we usually look for an opportunity to create a parent class for the two, and pull the duplicated code and data into that new parent class. Note that this sometimes requires adding some get-er methods to the parent class to allow access to the data that's now encapsulated in the new parent class. The program below has some glaring instances of duplication of data and code. Your job is to fix this in a nice object oriented way.
    Turn In a codeprint printout of your solution along with a screen capture of it running.

    We zipped the below code up for you to download.

    Point.java Shape.java
    import java.util.*;
    
    public class Point {
      private double x, y;
    
      public Point(double x, double y) {
        this.x = x;
        this.y = y;
      }
    
      public double d() {
        return Math.sqrt(x * x + y * y);
      }
    
      public Point subtract(Point p) {
        return new Point(x - p.x, y - p.y);
      }
    }
    
    public class Shape {
      public double perimeter() {
        return 0;
      }
    
      public double area() {
        return 0;
      }
    
      public String name() {
        return "";
      }
    }
    
    HW21.java Circle.java
    public class HW21 {
      public static void main(String[] args) {
        Point[] p = {
          new Point(1, 2), new Point(1, 3),
          new Point(4, 3), new Point(4, 2)
        };
    
        Shape[] s = {
          new Circle(p[0], 2.5),
          new Triangle(p),
          new Rectangle(p)
        };
    
        for (int i = 0; i < s.length; i++) {
          System.out.println(s[i].name() + " " +
                             s[i].perimeter() + " " +
                             s[i].area());
        }
      }
    }
    
    public class Circle extends Shape {
      private Point center;
      private double radius;
    
      public Circle(Point c, double r) {
        center = c;
        radius = r;
      }
    
      public double perimeter() {
        return 2 * Math.PI * radius;
      }
    
      public double area() {
        return Math.PI * radius * radius;
      }
    
      public String name() {
        return "circle";
      }
    }
    
    Triangle.java Rectangle.java
    public class Triangle extends Shape {
      private Point[] tp;
    
      public Triangle(Point[] p) {
        cp(3,p);
      }
    
      private void cp(int n, Point[] p) {
        tp = new Point[n];
        for(int i = 0; i < n; i++)
          tp[i] = p[i];
      }
    
      public double perimeter() {
        int N = tp.length;
        double sum = 0;
    
        for(int i = 0; i < N; i++){
          sum += tp[i].subtract(tp[(i+1)%N]).d();
        }
        return sum;
      }
    
      public double area() {
        double s = perimeter()/2;
        double sa = s - tp[0].subtract(tp[1]).d();
        double sb = s - tp[1].subtract(tp[2]).d();
        double sc = s - tp[2].subtract(tp[0]).d();
        return Math.sqrt(s*sa*sb*sc);
      }
    
      public String name() {
        return "triangle";
      }
    }
    
    public class Rectangle extends Shape {
      private Point[] rp;
    
      public Rectangle(Point[] p) {
        cp(4, p);
      }
    
      private void cp(int n, Point[] p) {
        rp = new Point[n];
    
        for (int i = 0; i < n; i++) {
          rp[i] = p[i];
        }
      }
    
      public double perimeter() {
        int n = rp.length;
        double sum = 0;
    
        for (int i = 0; i < n; i++) {
          sum += rp[i].subtract(rp[(i + 1) % n]).d();
        }
        return sum;
      }
    
      public double area() {
        double h = rp[0].subtract(rp[1]).d();
        double w = rp[1].subtract(rp[2]).d();
        return h * w;
      }
    
      public String name() {
        return "rectangle";
      }
    }