Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company.shape;
- abstract class Shape {
- protected String color = "red";
- protected boolean filled = true;
- public Shape(String color, boolean filled) {
- this.color = color;
- this.filled = filled;
- }
- public Shape() {
- }
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- public boolean isFilled() {
- return filled;
- }
- public void setFilled(boolean filled) {
- this.filled = filled;
- }
- @Override
- public String toString() {
- return "Shape{" +
- "color='" + color + '\'' +
- ", filled=" + filled +
- '}';
- }
- }
- class Circle extends Shape {
- protected double radius = 1.0;
- public Circle() {
- }
- public Circle(double radius) {
- this.radius = radius;
- }
- public Circle(String color, boolean filled, double radius) {
- this.radius = radius;
- setColor(color);
- setFilled(filled);
- }
- public double getRadius() {
- return this.radius;
- }
- public void setRadius(double p) {
- this.radius = p;
- }
- public double getArea() {
- return Math.PI * radius * radius;
- }
- public double getPerimeter() {
- return 2 * Math.PI * radius;
- }
- @Override
- public String toString() {
- return "Circle{" +
- "Shape{color='" + getColor() + '\'' +
- ", filled=" + isFilled() +
- "} radius=" + radius +
- '}';
- }
- }
- class Rectangle extends Shape {
- protected double width = 1;
- protected double length = 1;
- public Rectangle() {
- }
- public Rectangle(double width, double lenght) {
- this.width = width;
- this.length = lenght;
- }
- public Rectangle(double width, double lenght, String color, boolean filled) {
- this.width = width;
- this.length = lenght;
- setColor(color);
- setFilled(filled);
- }
- public double getWidth() {
- return width;
- }
- public void setWidth(double width) {
- this.width = width;
- }
- public double getLength() {
- return length;
- }
- public void setLenght(double lenght) {
- this.length = lenght;
- }
- public double getArea() {
- return width * length;
- }
- public double getPerimeter() {
- return 2 * width + 2 * length;
- }
- @Override
- public String toString() {
- return "Rectangle{" +
- "Shape{color='" + getColor() + '\'' +
- ", filled=" + isFilled() +
- "} width=" + width +
- ", length=" + length +
- '}';
- }
- }
- class Square extends Rectangle {
- public Square() {
- }
- public Square(double side) {
- setLenght(side);
- setWidth(side);
- }
- public Square(double side, String color, boolean filled) {
- setLenght(side);
- setWidth(side);
- setColor(color);
- setFilled(filled);
- }
- public double getSide() {
- return getWidth();
- }
- public void setSide(double side) {
- setLenght(side);
- setWidth(side);
- }
- @Override
- public String toString() {
- return "Square" +
- "{Rectangle{" +
- "Shape{color='" + getColor() + '\'' +
- ", filled=" + isFilled() +
- "} width=" + getWidth() +
- ", length=" + getLength() +
- '}' + '}';
- }
- }
- class Main {
- public static void main(String[] args) {
- Shape s1 = new Circle("RED", false, 5.5);
- System.out.println(s1);
- // get Area nie zadziała ponieważ bierzemy klasę absta
- // nie mozna wywoalac tych metod ponieważ Shape owych nie zawiera
- // System.out.println(s1.getArea());
- //System.out.println(s1.getPerimeter());
- System.out.println(s1.getColor());
- System.out.println(s1.isFilled());
- //System.out.println(s1.getRadius());
- Circle c1 = (Circle) s1;
- System.out.println(c1);
- System.out.println(c1.getArea());
- System.out.println(c1.getPerimeter());
- System.out.println(c1.getColor());
- System.out.println(c1.isFilled());
- System.out.println(c1.getRadius());
- // Klasa abstrakcyjna nie mozę być zaincjowana
- // Shape s2 = new Shape();
- Shape s3 = new Rectangle(1.0, 2.0, "RED", false);
- System.out.println(s3);
- // get Area nie zadziała ponieważ bierzemy klasę absta
- // nie mozna wywoalac tych metod ponieważ Shape owych nie zawiera
- // System.out.println(s3.getArea());
- /// System.out.println(s3.getPerimeter());
- System.out.println(s3.getColor());
- // System.out.println(s3.getLength());
- Rectangle r1 = (Rectangle) s3;
- System.out.println(r1);
- System.out.println(r1.getArea());
- System.out.println(r1.getColor());
- System.out.println(r1.getLength());
- // get Area nie zadziała ponieważ bierzemy klasę absta
- // nie mozna wywoalac tych metod ponieważ Shape owych nie zawiera
- Shape s4 = new Square(6.6);
- System.out.println(s4);
- // System.out.println(s4.getArea());
- System.out.println(s4.getColor());
- // System.out.println(s4.getSide());
- // get Area nie zadziała ponieważ bierzemy klasę absta
- // nie mozna wywoalac tych metod ponieważ Shape owych nie zawiera
- Rectangle r2 = (Rectangle) s4;
- System.out.println(r2);
- System.out.println(r2.getArea());
- System.out.println(r2.getColor());
- // System.out.println(r2.getSide());
- System.out.println(r2.getLength());
- Square sq1 = (Square) r2;
- System.out.println(sq1);
- System.out.println(sq1.getArea());
- System.out.println(sq1.getColor());
- System.out.println(sq1.getSide());
- System.out.println(sq1.getLength());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement