首页 > 解决方案 > 测试类未与其他类(形状、正方形、矩形、圆形)集成

问题描述

我正在开发一个程序来分析给定的形状并打印它们的颜色和面积。我正在编写 5 个不同的类(测试、形状、正方形、矩形和圆形)。我无法获得我想要的输出。我相信唯一有问题的类是“TestShapes”。它不与其他类集成以实现正确的输出。我正在为该程序找到正确的区域,但其余部分与课程中的内容无关。

下面我提供了我的代码以及我的输出和所需的输出。

预先感谢您的帮助。

package Test;

public class Test {


    public static void main(String[] args){
           
           
                 Shape s1 = new Square("Green", 12);
                 System.out.println(s1);
                 System.out.println("Area is " + s1.area());
             
                 Shape s2 = new Circle("Purple", 3);
                 System.out.println(s2);
                 System.out.println("Area is " + s2.area());
             
                 Shape s3 = new Rectangle("Blue", 5, 9);
                 System.out.println(s2);
                 System.out.println("Area is " + s3.area());    
           
    }
    }

        class Shape {

             String color;
             double area;
        
            public Shape(){ // default constructor
                color = "red";
            }
        
            public Shape(String c){ // constructor
                color =c;
            }
        
            public String getColor(){ //accessor
                return color;
            }
        
            public void setColor(String c){//mutators
                color=c;
            }
            //print method
            public void print()
            {
                System.out.println("Color: "+ getColor());
            }
             public double area(){
                return area;
    }
    }

        class Square extends Shape{ // inherits from the shape class

            double sideLength;
        
            public Square(){//default constructor
                super();
                sideLength = 1;
            }
        
            public Square(String c, double s){ //constructor
                super(c);
                sideLength = s;
            }
        
            public double getSideLength(){//accessor
                return sideLength;
            }
        
            public void setSideLength(double s){//mutator
                sideLength = s;
            }
        
            public double area(){
                return sideLength * sideLength; //calculates the area of the square
            }
        
            public void print()
            {
                super.print();
                System.out.println("Side length: " + getSideLength() + "\nArea: " + area());
    }
    }
        
        class Circle extends Shape{// inherits from the shape class
            
            double radius;
            
            public Circle(){
                super();
                radius = 1;
            }

            public Circle(String color,double radius){
                super(color);
                this.radius = radius;
            }

            public Circle(double r){ //constructor
                super();
                radius = r;
            }
        
            public double getRadius(){//accessor
                return radius;
            }
        
            public void setRadius(double r){//mutator
                radius = r;
            }
        
            public void print()
            { // prints out the information
                super.print();
                System.out.printf("Radius: " + getRadius() + "\nArea:"+ "%.2f\n", + area());
        
            }
            public double area(){
            return 3.14159 * radius * radius; //calculates the area of the circle
            
    }   
    }

        class Rectangle extends Shape{// inherits from the shape class
            double length;
            double width;

        public Rectangle(){//default constructor
            super();
            length = 1;
            width = 1;
        }

        public Rectangle(String c, double l, double w){ //constructor
            super(c);
            length = l;
            width = w;
            }
        
            public double getLength(){//accessor
                return length;
            }
        
            public double getWidth(){//accessor
                return width;
            }
        
            public void setLength(double l){//mutator
                length = l;
            }
            public void setSideLength(double w){//mutator
                width = w;
            }
        
            public double area(){
                return length * width; //calculates the area of the rectangle
            }
        
            public void print()
            { // prints out the information
                super.print();
                System.out.println("Length: " + getLength() + "\nWidth:"+ getWidth() + "\nArea: "+ area());

        }

        }

输出:

test.Square@6ff3c5b5
Area is 144.0
test.Circle@4b1210ee
Area is 28.274309999999996
test.Circle@4b1210ee
Area is 45.0

期望的输出:

Shape 1:
Color: Green
Side Length: 12
Area: 144

Shape 2:
Color: Purple
Radius: 3
Area: 28.27

Shape 3:
Color: Blue 
Length: 5
Width: 9
Area: 45

标签: javaswing

解决方案


我认为您应该像下面的代码一样初始化对象并使用类中的打印函数,例如:

` public static void main(String[] args){

    Square s1 = new Square("Green", 12);
    s1.print();

    Shape s2 = new Circle("Purple", 3);
    s2.print();

}`

推荐阅读