Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Shape {
- //private instance variables
- private String colour;
- private boolean filled;
- //constructors
- public Shape() {
- this.colour = "red";
- this.filled = true;
- System.out.println("Constructed a Shape with Shape()");
- }
- public Shape(String colour, boolean filled) {
- this.colour = colour;
- this.filled = filled;
- }
- public String getColour() {
- return colour;
- }
- public void setColour(String colour) {
- this.colour = colour;
- }
- public boolean isFilled() {
- return filled;
- }
- public void setFilled(boolean filled) {
- this.filled = filled;
- }
- public String toString() {
- return "Shape[colour =" + colour + ", filled =" + filled + "]";
- }
- }
- //next class
- public class Circle extends Shape {
- //private instance variable
- private double radius;
- //constructors
- public Circle() {
- super(); //to invoke superclass' constructor Shape()
- this.radius = 1.0;
- System.out.println("Constructed a Circle with Circle()"); //for debugging
- }
- public Circle(double radius) {
- super();
- this.radius = radius;
- System.out.println("Constructed a Circle with Circle()"); //for debugging
- }
- public Circle(double radius, String colour, boolean filled) {
- super(colour, filled);
- this.radius = radius;
- System.out.println("Constructed a Circle with Circle()"); //for debugging
- }
- //getters and setters
- public double getRadius() {
- return radius;
- }
- public void setRadius(double radius) {
- this.radius = radius;
- }
- //returns the area of the circle
- public double getArea() {
- return radius * radius * Math.PI;
- }
- //returns the circumference of the circle
- public double getPerimeter() {
- return 2 * Math.PI * radius;
- }
- //overrides inherited toString()
- @Override
- public String toString() {
- return "Circle[" + super.toString() + ", radius =" + radius + "]";
- }
- }
- //next class
- public class Rectangle extends Shape {
- //private instance variables
- private double width;
- private double length;
- //constructors
- public Rectangle() {
- super(); //invokes the superclass
- this.width = 1.0;
- this.length = 1.0;
- System.out.println("Constructed a Rectangle with Rectangle()"); //for debugging
- }
- public Rectangle(double width, double length) {
- super(); //invokes the superclass
- this.width = width;
- this.length = length;
- System.out.println("Constructed a Rectangle with Rectangle()"); //for debugging
- }
- public Rectangle(double width, double length, String colour, boolean filled) {
- super(colour, filled); //invokes the superclass
- this.width = width;
- this.length = length;
- System.out.println("Constructed a Rectangle with Rectangle()"); //for debugging
- }
- //getters and setters
- public double getWidth() {
- return width;
- }
- public void setWidth(double width) {
- this.width = width;
- }
- public double getLength() {
- return length;
- }
- public void setLength(double length) {
- this.length = length;
- }
- //returns the area of the rectangle
- public double getArea() {
- return width * length;
- }
- public double getPerimeter() {
- return (width + length) * 2;
- }
- @Override
- public String toString() {
- return "Rectangle[" + super.toString() + ", width =" + width + ", length =" + length + "]";
- }
- }
- // next class
- public class Square extends Rectangle {
- private double side;
- //constructors
- public Square() {
- super();
- }
- public Square(double side) {
- super(side, side);
- }
- public Square(double side, String colour, boolean filled) {
- super(side, side, colour, filled);
- }
- public double getSide() {
- return side;
- }
- public void setSide(double side) {
- this.side = side;
- }
- public void setWidth(double side) {
- }
- public void setLength(double side) {
- }
- @Override
- public String toString() {
- return "Square[" + super.toString() + "]";
- }
- }
- // test class
- public class testShape {
- public static void main(String[] args) {
- //to test circle class
- Circle c1 = new Circle();
- c1.setRadius(1.0);
- c1.setColour("red");
- c1.setFilled(true);
- System.out.println(c1);
- System.out.println("Area is " + c1.getArea() + ", Perimeter is " + c1.getPerimeter());
- System.out.println(); //blank print statement to make output look neater
- //to test rectangle class
- Rectangle r1 = new Rectangle();
- r1.setWidth(1.0);
- r1.setLength(1.0);
- r1.setColour("blue");
- r1.setFilled(true);
- System.out.println(r1);
- System.out.println("Area is " + r1.getArea() + ", Perimeter is " + r1.getPerimeter());
- System.out.println();
- //to test square class
- Square s1 = new Square();
- s1.setSide(1.0);
- s1.setSide(1.0);
- s1.setColour("green");
- s1.setFilled(true);
- System.out.println(s1);
- System.out.println("Area is " + r1.getArea() + ", Perimeter is " + r1.getPerimeter());
- }
- }
- //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