Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Assignment #3: SimpleFigure and Circles Program Description: This as

ID: 3592394 • Letter: P

Question

Programming Assignment #3: SimpleFigure and Circles Program Description: This assignment will give you practice with value parameters, using Java objects, and graphics. This assignment has 2 parts; therefore you should turn in two Java files. You will be using a special class called DrawingPanel written by the instructor, and classes called Graphics and Color that are part of the Java class libraries. Part 1 of 2 (4 points) Simple Figure, or your own drawing For the first part of this assignment, turn in a file named SimpleFigure.java that draws a figure on the screen using the DrawingPanel provided in class. You may draw any figure you like that meets the following criteria: • The DrawingPanel's size must be at least 100 x 100 pixels. • You must draw at least one line, one rectangle, and one oval that are visible on the screen. • You must use at least three different visible colors in your figure. The loose guidelines for Part 1 mean that if you like, you can be creative and draw any figure you like! After the assignment is turned in, the instructor may anonymously show off some of the neat figures drawn by students for everyone to see. If you do not want to create your own figure, the following is a default figure you may draw for Part 1. If your code draws this figure correctly, you will receive full credit for Part 1. This figure contains the following properties and shapes: • A DrawingPanel of size 250 x 200 with a yellow background. • A green rectangle with top-left corner at (10, 20) and size 200 x 160 pixels. • Red ovals with top-left corners at (10, 20) and (110, 100) of size 100 x 80 pixels. • Black lines from (10, 60) to (110, 60), from (60, 20) to (60, 100), from (110, 140) to (210, 140), and from (160, 100) to (160, 180). Since a major new concept for this assignment is parameter-passing, you may optionally wish to try using parameterized methods to draw your figure. For example, the default picture above has a repeated figure of an oval with black lines through it. This figure could be turned into a parameterized static method with parameters for the figure's position. However, parameterized methods are not required for Part 1, and you will not be penalized if you do not use them. Your entire score for Part 1 will be based on its external correctness as defined above.

Explanation / Answer

public class Circle {           // save as "Circle.java"
   // private instance variable, not accessible from outside this class
   private double radius;
   private String color;
  
   // 1st constructor, which sets both radius and color to default
   public Circle() {
      radius = 1.0;
      color = "red";
   }
  
   // 2nd constructor with given radius, but color default
   public Circle(double r) {
      radius = r;
      color = "red";
   }
  
   // A public method for retrieving the radius
   public double getRadius() {
     return radius;
   }
  
   // A public method for computing the area of circle
   public double getArea() {
      return radius*radius*Math.PI;
   }
}

public class TestCircle {        // save as "TestCircle.java"
   public static void main(String[] args) {
      // Declare and allocate an instance of class Circle called c1
      // with default radius and color
      Circle c1 = new Circle();
      // Use the dot operator to invoke methods of instance c1.
      System.out.println("The circle has radius of "
         + c1.getRadius() + " and area of " + c1.getArea());
  
      // Declare and allocate an instance of class circle called c2
      // with the given radius and default color
      Circle c2 = new Circle(2.0);
      // Use the dot operator to invoke methods of instance c2.
      System.out.println("The circle has radius of "
         + c2.getRadius() + " and area of " + c2.getArea());
   }
}

public abstract class Shape { // abstract class, can't instantiate
   // to implement an idNumber
   private static int counter = 0;
   private int idNumber;
   public Shape () {
      idNumber = ++counter;
   }
   public int getIdNumber() { return idNumber;}

   public double area() { return 0.0; }
   public double volume() { return 0.0; }
   public abstract String getName(); // abstract, so omit body
}

// Point.java: the class Point
public class Point extends Shape {
   protected int x, y; // coordinates of the Point

   // constructor
   public Point( int a, int b ) { x = a; y = b; }

   // get x coordinate
   public int getX() { return x; }

   // get y coordinate
   public int getY() { return y; }

   // convert the point into a String representation
   public String toString()
      { return "[" + x + ", " + y + "]"; }

   // return the class name
   public String getName() { return "Point"; }   
}

// circle.java: the class Circle
public class Circle extends Point { // inherits from Point
   protected double radius;    

   // Constructor
   public Circle( double r, int a, int b ) {
      super( a, b ); // call the superclass constructor
      radius = ( r >= 0 ? r : 0 );
   }
     
   // Calculate area of Circle
   public double area() { return Math.PI * radius * radius; }

   // convert the Circle to a String
   public String toString()
      { return "Center = " + super.toString() +
               "; Radius = " + radius; }

   // return the class name
   public String getName() { return "Circle"; }
}

// Cylinder.java: the class Cylinder
public class Cylinder extends Circle {
   protected double height; // height of Cylinder

   // constructor
   public Cylinder( double h, double r, int a, int b ) {
      super( r, a, b );   // call superclass constructor
      height = ( h >= 0 ? h : 0 );
   }

   // Calculate area of Cylinder (i.e., surface area)     
   public double area() {
      return 2 * super.area() +
             2 * Math.PI * radius * height;
   }

   // Calculate volume of Cylinder
   public double volume() { return super.area() * height; }

   // Convert a Cylinder to a String
   public String toString() {
      return super.toString() + "; Height = " + height;
   }

   // Return the class name
   public String getName() { return "Cylinder"; }
}


// Rectangle.java: the class Rectangle
public class Rectangle extends Point { // inherits from Point
   protected double length;
   protected double width;

   // Constructor
   public Rectangle( double len, double wid, int a, int b ) {
      super( a, b ); // call the superclass constructor
      length = ( len >= 0 ? len : 0 );
      width = ( wid >= 0 ? wid : 0 );
   }

   // Calculate area of Rectangle
   public double area() { return length * width; }

   // convert the Rectangle to a String
   public String toString() {
      return "Center = " + super.toString() +
         "; Length = " + length + ", Width = " + width; }

   // return the class name
   public String getName() {
      if (length == width) return "Square";
      else return "Rectangle";
   }
}

// RectangularSolid.java: the class RectangularSolid
public class RectangularSolid extends Rectangle {
   protected double height; // height of RectangularSolid

   // constructor
   public RectangularSolid(double h, double len, double wid, int a, int b) {
      super(len, wid, a, b);   // call superclass constructor
      height = ( h >= 0 ? h : 0 );
   }

   // Calculate area of RectangularSolid (i.e., surface area)
   public double area() {
      return 2 * super.area() +
             2 * length * height +
             2 * width * height;
   }

   // Calculate volume of RectangularSolid
   public double volume() { return super.area() * height; }

   // Convert a RectangularSolid to a String
   public String toString() {
      return super.toString() + "; Height = " + height;
   }

   // Return the class name
   public String getName() {
      if (length == width && length == height)
         return("Cube");
      else return "RectangularSolid";
   }
}


// ShapeTest.java: test point, circle, cylinder hierarchy
public class ShapeTest {

   private Shape shapes[];

   public void createShapes() {

      shapes = new Shape[15]; // generic array of any shape

      shapes[0] = new Rectangle(3.0, 4.0, 6, 8);
      shapes[1] = new Point(7, 11);
      shapes[2] = new Circle(3.5, 22, 8);
      shapes[3] = new Cylinder(10, 3.3, 10, 10);
      shapes[4] = new RectangularSolid(2.0, 3.0, 4.0, 6, 8);
      shapes[5] = new Point(8, 12);
      shapes[6] = new Rectangle(2.0, 2.0, 7, 9);
      shapes[7] = new Circle(3.6, 23, 9);
      shapes[8] = new Cylinder(12, 3.4, 20, 20);
      shapes[9] = new Rectangle(6.0, 6.0, 5, 7);
      shapes[10] = new RectangularSolid(3.0, 3.0, 3.0, 10, 4);
      shapes[11] = new Point(9, 13);
      shapes[12] = new Circle(3.7, 24, 10);
      shapes[13] = new Cylinder(14, 3.5, 30, 30);
      shapes[14] = new RectangularSolid(4.0, 4.0, 4.0, 8, 9);
   }

   public void printShapes() {

      // Loop through arrayOfShapes. Use polymorphism to print the name,
      // area, and volume of each object.
      System.out.println("PRINT THE SHAPES AS AN ARRAY OF SHAPE");
      for ( int i = 0; i < shapes.length; i++ ) {
         System.out.println(shapes[i].getName() + ": " +
                       shapes[i].toString() + ", ID: " +
                       shapes[i].getIdNumber());
         System.out.println("Area = " + shapes[i].area());
         System.out.println("Volume = " + shapes[i].volume());
         System.out.println();
      }
   }

   // insertionSort: Sorts array of Shapes using the insertion sort
   public void insertionSort () {
      for (int index = 1; index < shapes.length; index++) {
         Shape key = shapes[index];
         int position = index;
         // shift larger values to the right
         while (position > 0 && compareShapes(shapes[position-1], key) > 0) {
            shapes[position] = shapes[position-1];
            position--;
         }
         shapes[position] = key;
      }
   }

   private int compareShapes (Shape s1, Shape s2) {
      if ((s1.getName()).equals(s2.getName()))
         return s1.getIdNumber() - s2.getIdNumber();
      else return (s1.getName()).compareTo(s2.getName());
   }

   public static void main (String[] args) {
      ShapeTest shapeTest = new ShapeTest();
      shapeTest.createShapes();
      shapeTest.insertionSort();
      shapeTest.printShapes();   
   }
}