Advertisement
thotfrnk

ex4-5.java

Jun 7th, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.58 KB | None | 0 0
  1. public class Shape {
  2.     //private instance variables
  3.     private String colour;
  4.     private boolean filled;
  5.  
  6.     //constructors
  7.     public Shape() {
  8.         this.colour = "red";
  9.         this.filled = true;
  10.  
  11.         System.out.println("Constructed a Shape with Shape()");
  12.     }
  13.  
  14.     public Shape(String colour, boolean filled) {
  15.         this.colour = colour;
  16.         this.filled = filled;
  17.     }
  18.  
  19.     public String getColour() {
  20.         return colour;
  21.     }
  22.  
  23.     public void setColour(String colour) {
  24.         this.colour = colour;
  25.     }
  26.  
  27.     public boolean isFilled() {
  28.         return filled;
  29.     }
  30.  
  31.     public void setFilled(boolean filled) {
  32.         this.filled = filled;
  33.     }
  34.  
  35.     public String toString() {
  36.         return  "Shape[colour =" + colour + ", filled =" + filled + "]";
  37.     }
  38. }
  39.  
  40. //next class
  41.  
  42. public class Circle extends Shape {
  43.     //private instance variable
  44.     private double radius;
  45.  
  46.     //constructors
  47.     public Circle() {
  48.         super(); //to invoke superclass' constructor Shape()
  49.         this.radius = 1.0;
  50.  
  51.         System.out.println("Constructed a Circle with Circle()"); //for debugging
  52.     }
  53.  
  54.     public Circle(double radius) {
  55.         super();
  56.         this.radius = radius;
  57.  
  58.         System.out.println("Constructed a Circle with Circle()"); //for debugging
  59.     }
  60.  
  61.     public Circle(double radius, String colour, boolean filled) {
  62.         super(colour, filled);
  63.         this.radius = radius;
  64.  
  65.         System.out.println("Constructed a Circle with Circle()"); //for debugging
  66.     }
  67.  
  68.     //getters and setters
  69.  
  70.     public double getRadius() {
  71.         return radius;
  72.     }
  73.  
  74.     public void setRadius(double radius) {
  75.         this.radius = radius;
  76.     }
  77.  
  78.     //returns the area of the circle
  79.     public double getArea() {
  80.         return radius * radius * Math.PI;
  81.     }
  82.  
  83.     //returns the circumference of the circle
  84.     public double getPerimeter() {
  85.         return 2 * Math.PI * radius;
  86.     }
  87.  
  88.     //overrides inherited toString()
  89.     @Override
  90.     public String toString() {
  91.         return "Circle[" + super.toString() + ", radius =" + radius + "]";
  92.     }
  93. }
  94.  
  95. //next class
  96. public class Rectangle extends Shape {
  97.     //private instance variables
  98.     private double width;
  99.     private double length;
  100.  
  101.     //constructors
  102.     public Rectangle() {
  103.         super(); //invokes the superclass
  104.         this.width = 1.0;
  105.         this.length = 1.0;
  106.  
  107.         System.out.println("Constructed a Rectangle with Rectangle()"); //for debugging
  108.     }
  109.  
  110.     public Rectangle(double width, double length) {
  111.         super(); //invokes the superclass
  112.         this.width = width;
  113.         this.length = length;
  114.  
  115.         System.out.println("Constructed a Rectangle with Rectangle()"); //for debugging
  116.     }
  117.  
  118.     public Rectangle(double width, double length, String colour, boolean filled) {
  119.         super(colour, filled); //invokes the superclass
  120.         this.width = width;
  121.         this.length = length;
  122.  
  123.         System.out.println("Constructed a Rectangle with Rectangle()"); //for debugging
  124.     }
  125.  
  126.     //getters and setters
  127.     public double getWidth() {
  128.         return width;
  129.     }
  130.  
  131.     public void setWidth(double width) {
  132.         this.width = width;
  133.     }
  134.  
  135.     public double getLength() {
  136.         return length;
  137.     }
  138.  
  139.     public void setLength(double length) {
  140.         this.length = length;
  141.     }
  142.  
  143.     //returns the area of the rectangle
  144.     public double getArea() {
  145.         return width * length;
  146.     }
  147.  
  148.     public double getPerimeter() {
  149.         return (width + length) * 2;
  150.     }
  151.  
  152.     @Override
  153.     public String toString() {
  154.         return "Rectangle[" + super.toString() + ", width =" + width + ", length =" + length + "]";
  155.     }
  156. }
  157.  
  158. // next class
  159. public class Square extends Rectangle {
  160.     private double side;
  161.     //constructors
  162.     public Square() {
  163.         super();
  164.     }
  165.  
  166.     public Square(double side) {
  167.         super(side, side);
  168.     }
  169.  
  170.     public Square(double side, String colour, boolean filled) {
  171.         super(side, side, colour, filled);
  172.     }
  173.  
  174.     public double getSide() {
  175.         return side;
  176.     }
  177.  
  178.     public void setSide(double side) {
  179.         this.side = side;
  180.     }
  181.  
  182.     public void setWidth(double side) {
  183.     }
  184.  
  185.     public void setLength(double side) {
  186.     }
  187.  
  188.     @Override
  189.     public String toString() {
  190.         return "Square[" + super.toString() + "]";
  191.     }
  192. }
  193.  
  194. // test class
  195. public class testShape {
  196.     public static void main(String[] args) {
  197.  
  198.         //to test circle class
  199.         Circle c1 = new Circle();
  200.         c1.setRadius(1.0);
  201.         c1.setColour("red");
  202.         c1.setFilled(true);
  203.  
  204.         System.out.println(c1);
  205.  
  206.         System.out.println("Area is " + c1.getArea() + ", Perimeter is " + c1.getPerimeter());
  207.  
  208.         System.out.println(); //blank print statement to make output look neater
  209.  
  210.         //to test rectangle class
  211.         Rectangle r1 = new Rectangle();
  212.         r1.setWidth(1.0);
  213.         r1.setLength(1.0);
  214.         r1.setColour("blue");
  215.         r1.setFilled(true);
  216.  
  217.         System.out.println(r1);
  218.  
  219.         System.out.println("Area is " + r1.getArea() + ", Perimeter is " + r1.getPerimeter());
  220.  
  221.         System.out.println();
  222.  
  223.         //to test square class
  224.         Square s1 = new Square();
  225.         s1.setSide(1.0);
  226.         s1.setSide(1.0);
  227.         s1.setColour("green");
  228.         s1.setFilled(true);
  229.  
  230.         System.out.println(s1);
  231.  
  232.         System.out.println("Area is " + r1.getArea() + ", Perimeter is " + r1.getPerimeter());
  233.     }
  234. }
  235.  
  236.  
  237. //i do not care if its correct or not, this is just for archiving purposes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement