Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.work;
- public abstract class Shape {
- protected String color;
- protected boolean filled;
- public Shape(){
- }
- public Shape(String color, boolean filled){
- setColor(color);
- setFilled(filled);
- }
- 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;
- }
- public abstract double getArea();
- public abstract double getPerimeter();
- public abstract String ToString();
- }
- /////////////////////////////////////////////////////////////////////////////////
- package com.work;
- public class Circle extends Shape{
- protected double radius;
- public Circle(){}
- public Circle(double radius){
- super();
- setRadius(radius);
- }
- public Circle(double radius, String color, boolean filled){
- super(color, filled);
- setRadius(radius);
- }
- public double getRadius(){
- return radius;
- }
- public void setRadius(double radius) {
- this.radius = radius;
- }
- @Override
- public double getArea() {
- return Math.PI * radius * radius;
- }
- @Override
- public double getPerimeter() {
- return 2 * Math.PI * radius;
- }
- @Override
- public String ToString() {
- return "Radius: " + radius + "\n" + "Area: " + this.getArea() + "\n" + "Perimeter: " + this.getPerimeter();
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- package com.work;
- public class Rectangle extends Shape{
- protected double width;
- protected double length;
- public Rectangle(){
- }
- public Rectangle(double width, double length){
- setWidth(width);
- setLength(length);
- }
- public Rectangle(double width, double length, String color, boolean filled){
- super(color, filled);
- setWidth(width);
- setLength(length);
- }
- 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;
- }
- @Override
- public double getArea() {
- return width * length;
- }
- @Override
- public double getPerimeter() {
- return 2 * (width + length);
- }
- @Override
- public String ToString() {
- return "Width: " + this.getWidth() + "\n" + "Length: " + this.getLength() + "\n" + "Area: " + this.getArea() + "\n" + "Perimeter: " + this.getPerimeter();
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- package com.work;
- public class Square extends Rectangle{
- protected double side;
- public Square(){}
- public Square(double side){
- super(side, side);
- }
- public Square(double side, String color, boolean filled){
- super(side, side, color, filled);
- }
- public double getSide(){
- return length;
- }
- public void setSide(double side){
- length = side;
- width = side;
- }
- @Override
- public void setWidth(double side) {
- setSide(side);
- }
- @Override
- public void setLength(double side) {
- setSide(side);
- }
- @Override
- public String ToString() {
- return "Side: " + this.getSide() + "\n" + "Area: " + this.getArea() + "\n" + "Perimeter: " + this.getPerimeter();
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- package com.work;
- public class Main {
- public static void main(String[] args) {
- Shape s1 = new Circle(5.5, "RED", false);
- System.out.println(s1);
- System.out.println(s1.getArea());
- System.out.println(s1.getPerimeter());
- System.out.println(s1.getColor());
- System.out.println(s1.isFilled());
- System.out.println(s1.ToString());
- // Ошибка: у класса Shape нет метода getRadius()
- //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());
- // у класса Circle есть метод getRadius()
- System.out.println(c1.getRadius());
- // Ошибка: нельзя создать экземпляр абстрактного класса
- //Shape s2 = new Shape();
- Shape s3 = new Rectangle(1.0, 2.0, "RED", false);
- System.out.println(s3);
- System.out.println(s3.getArea());
- System.out.println(s3.getPerimeter());
- System.out.println(s3.getColor());
- // Ошибка: у класса Shape не метода getLength()
- //System.out.println(s3.getLength());
- Rectangle r1 = (Rectangle)s3;
- System.out.println(r1);
- System.out.println(r1.getArea());
- System.out.println(r1.getColor());
- // у класса Rectangle есть метод getLength()
- System.out.println(r1.getLength());
- Shape s4 = new Square(6.6, "blue",true );
- System.out.println(s4);
- System.out.println(s4.getArea());
- System.out.println(s4.getColor());
- // Ошибка: у класса Shape нет метода getSide()
- //System.out.println(s4.getSide());
- Rectangle r2 = (Rectangle)s4;
- System.out.println(r2);
- System.out.println(r2.getArea());
- System.out.println(r2.getColor());
- // Ошибка: у класса Rectangle тоже нет метода getSide()
- //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());
- // у класса Square есть метод getSide()
- System.out.println(sq1.getSide());
- System.out.println(sq1.getLength());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement