Advertisement
Toxotsist

T3

Sep 2nd, 2021 (edited)
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.53 KB | None | 0 0
  1. public abstract class Shape {
  2.     protected String color;
  3.     protected boolean filled;
  4.  
  5.     public Shape(String c, boolean f){
  6.         this.color = c;
  7.         this.filled = f;
  8.     }
  9.  
  10.     public Shape(){
  11.         this("None", false);
  12.     }
  13.  
  14.     public void setColor(String color) {
  15.         this.color = color;
  16.     }
  17.  
  18.     public String getColor() {
  19.         return color;
  20.     }
  21.  
  22.     public boolean isFilled() {
  23.         return filled;
  24.     }
  25.  
  26.     public void setFilled(boolean filled) {
  27.         this.filled = filled;
  28.     }
  29.  
  30.     public abstract double getArea();
  31.     public abstract double getPerimeter();
  32.     public abstract String ToString();
  33. }
  34.  
  35. ////////////////////////
  36. public class Circle extends Shape {
  37.     protected double radius;
  38.     public Circle(double radius, String color, boolean filled){
  39.         super(color, filled);
  40.         this.radius = radius;
  41.     }
  42.  
  43.     public double getRadius() {
  44.         return radius;
  45.     }
  46.  
  47.     public void setRadius(double radius) {
  48.         this.radius = radius;
  49.     }
  50.  
  51.     @Override
  52.     public double getArea() {
  53.         return 2*Math.PI*(radius*radius);
  54.     }
  55.  
  56.     @Override
  57.     public double getPerimeter() {
  58.         return 2*Math.PI*radius;
  59.     }
  60.  
  61.     @Override
  62.     public String ToString() {
  63.         return "Радиус: " + String.valueOf(this.getRadius()) + "\nПлощадь: " + String.valueOf(this.getArea()) + "\nПериметр: " + String.valueOf(this.getPerimeter())
  64.                 + "\nЦвет: " + color + "\nПрозрачность: " + filled;
  65.     }
  66. }
  67.  
  68. /////////////////////////
  69. public class Rectangle extends Shape {
  70.     protected double width;
  71.     protected double length;
  72.  
  73.     public Rectangle(double w, double l){
  74.         super();
  75.         width = w;
  76.         length = l;
  77.     }
  78.  
  79.     public Rectangle(double w, double l, String color, boolean fill){
  80.         super(color, fill);
  81.         width = w;
  82.         length = l;
  83.     }
  84.  
  85.     public Rectangle(){
  86.         width = 1;
  87.         length = 1;
  88.     }
  89.  
  90.     public double getLength() {
  91.         return length;
  92.     }
  93.  
  94.     public double getWidth() {
  95.         return width;
  96.     }
  97.  
  98.     public void setLength(double length) {
  99.         this.length = length;
  100.     }
  101.  
  102.     public void setWidth(double width) {
  103.         this.width = width;
  104.     }
  105.  
  106.     @Override
  107.     public double getArea(){
  108.         return width*length;
  109.     }
  110.  
  111.     @Override
  112.     public double getPerimeter(){
  113.         return (2*width)+(2*length);
  114.     }
  115.  
  116.     @Override
  117.     public String ToString(){
  118.        return "Длина: " + length + "\nШирина: " + width + "\nПлощадь: " +this.getArea() + "\nПериметр: " + this.getPerimeter();
  119.     }
  120. }
  121.  
  122. ///////////////////////////
  123. public class Square extends Rectangle {
  124.     protected double side;
  125.     public Square(double si) {super(si, si); side = si;}
  126.  
  127.     @Override
  128.     public double getArea(){
  129.         return width*length;
  130.     }
  131.  
  132.     @Override
  133.     public double getPerimeter(){
  134.         return (2*width)+(2*length);
  135.     }
  136.  
  137.     @Override
  138.     public double getWidth() {
  139.         return side;
  140.     }
  141.     public double getSide() {
  142.         return side;
  143.     }
  144.  
  145.     @Override
  146.     public String ToString(){
  147.         return "Длина: " + length + "\nШирина: " + width + "\nПлощадь: " +String.valueOf(this.getArea()) + "\nПериметр: " + String.valueOf(this.getPerimeter());
  148.     }
  149. }
  150.  
  151. /////////////////////////
  152. public class Main {
  153.     public static void main(String[] args){
  154.         Shape s1 = new Circle(5.5, "RED", false); // Upcast Circle to Shape
  155.         System.out.println(s1); // which version?
  156.         System.out.println(s1.getArea()); // which version?
  157.         System.out.println(s1.getPerimeter()); // which version?
  158.         System.out.println(s1.getColor());
  159.         System.out.println(s1.isFilled());
  160.         //System.out.println(s1.getRadius());
  161.  
  162.         Circle c1 = (Circle)s1; // Downcast back to Circle
  163.         System.out.println(c1);
  164.         System.out.println(c1.getArea());
  165.         System.out.println(c1.getPerimeter());
  166.         System.out.println(c1.getColor());
  167.         System.out.println(c1.isFilled());
  168.         System.out.println(c1.getRadius());
  169.  
  170.         //Shape s2 = new Shape();
  171.  
  172.         Shape s3 = new Rectangle(1.0, 2.0, "RED", false); // Upcast
  173.         System.out.println(s3);
  174.         System.out.println(s3.getArea());
  175.         System.out.println(s3.getPerimeter());
  176.         System.out.println(s3.getColor());
  177.         //System.out.println(s3.getLength());
  178.  
  179.         Rectangle r1 = (Rectangle)s3; // downcast
  180.         System.out.println(r1);
  181.         System.out.println(r1.getArea());
  182.         System.out.println(r1.getColor());
  183.         System.out.println(r1.getLength());
  184.  
  185.         Shape s4 = new Square(6.6); // Upcast
  186.         System.out.println(s4);
  187.         System.out.println(s4.getArea());
  188.         System.out.println(s4.getColor());
  189.         //System.out.println(s4.getSide());
  190.  
  191. // Take note that we downcast Shape s4 to Rectangle,
  192. // which is a superclass of Square, instead of Square
  193.         Rectangle r2 = (Rectangle)s4;
  194.         System.out.println(r2);
  195.         System.out.println(r2.getArea());
  196.         System.out.println(r2.getColor());
  197.         //System.out.println(r2.getSide());
  198.         System.out.println(r2.getLength());
  199.  
  200. // Downcast Rectangle r2 to Square
  201.         Square sq1 = (Square)r2;
  202.         System.out.println(sq1);
  203.         System.out.println(sq1.getArea());
  204.         System.out.println(sq1.getColor());
  205.         System.out.println(sq1.getSide());
  206.         System.out.println(sq1.getLength());
  207.     }
  208. }
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement