Code for Review Exercises (Set C1)

public class ShapeTester {

public static void main(String[] args) {
Square square = new Square(5,3,4);  			//line A
System.out.println();

Polygon polygon = new Polygon(4,5);  			//line B
System.out.println();

Circle circle = new Circle(5,1,2);
System.out.println();

//Shape shape = new Shape(3,4);  			//line C

Shape myShape1 = square;  				//line D

System.out.println(myShape1);  				//line E
System.out.println(polygon);
System.out.println(circle);				//line F
System.out.println();

square.printWhoAmI();					//line G
}
}
public class Square extends Polygon {

public Square(double sideLength) {
super(4,sideLength);
System.out.println("square created");
}

public Square(double x, double y, double sideLength) {
super(4, sideLength, x, y);
System.out.println("square created at given position");
}

public double getArea() {
return this.getSideLength() * this.getSideLength();
}

public String toString() {
return "square " + super.toString();
}

public void printWhoAmI(int confidenceLevel) {
if (confidenceLevel == 0) {
System.out.println("I think I'm a square?");
}
else {
System.out.println("I am a square!");
}
}

}

public  class Polygon extends Shape {

private int numSides_;
private double sideLength_;

public Polygon(int numSides, double sideLength) {
numSides_ = numSides;
sideLength_ = sideLength;
System.out.println("polygon created");
}

public Polygon(int numSides, double sideLength, double x, double y) {
super(x,y);
numSides_ = numSides;
sideLength_ = sideLength;
System.out.println("polygon created at given position");
}

public double getArea() {
double theta = 2 * Math.PI / numSides_;
double r = sideLength_ / (2 * Math.sin(theta));
return numSides_ * r * r * Math.sin(theta) * Math.cos(theta);
}

public double getPerimeter() {
return numSides_ * sideLength_;
}

public double getSideLength() {
return sideLength_;
}

public String toString() {
return "polygon " + super.toString();
}

public void printWhoAmI() {
System.out.println("I am a polygon!");
}

}

public class Circle extends Shape {

System.out.println("circle created");
}

public Circle(double radius, double x, double y) {
super(x,y);
System.out.println("circle created at given position");
}

public double getArea() {
}

public double getPerimeter() {
return 2 * Math.PI * radius_;
}

}

public String toString() {
return "circle " + super.toString();
}

public boolean equals (Object o) {
}
}

import java.awt.Color;

public abstract class Shape implements Colorable{

double x_;
double y_;
Color color_;

public Shape() {
x_ = 0;
y_ = 0;
System.out.println("shape created");
}

public Shape(double x, double y) {
x_ = x;
y_ = y;
System.out.println("shape created at (" + x_ + ", " + y_ + ")");
}

public abstract double getArea();
public abstract double getPerimeter();

public double getX() {
return x_;
}

public double getY() {
return y_;
}

public void setX(double x) {
x_ = x;
}

public void setY(double y) {
y_ = y;
}

public void setColor(Color color) {
color_ = color;
}

public String toString() {
return "shape at " + x_ + ", " + y_;
}
}

import java.awt.Color;

public interface Colorable {

public void setColor(Color c);

}