创建两个Shape子类

huangapple go评论68阅读模式
英文:

create two Shape subclasses

问题

import java.util.Scanner;

abstract class Shape {
    int width;
    abstract void area();
}

class Square extends Shape {
    Square(int x) {
        width = x;
    }

    void area() {
        int area = width * width;
        System.out.println(area);
    }
}

class Circle extends Shape {
    Circle(int y) {
        width = y;
    }

    void area() {
        double area = Math.PI * width * width;
        System.out.println(area);
    }
}

public class Program {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();

        Square a = new Square(x);
        Circle b = new Circle(y);
        a.area();
        b.area();
    }
}

以上是修复后的代码,已经可以正常工作。

英文:

> You are working on a graphical app, which includes multiple different shapes.
The given code declares a base Shape class with an abstract area() method and a width attribute.
You need to create two Shape subclasses, Square and Circle, which initialize the width attribute using their constructor, and define their area() methods.
The area() for the Square class should output the area of the square (the square of the width), while for the Circle, it should output the area of the given circle (PI*width*width).
The code in main creates two objects with the given user input and calls the area() methods.

I already started writing some code, but it doesn't work and I think I made more than just one mistake.


import java.util.Scanner;

abstract class Shape {
    int width;
    abstract void area();
}
//your code goes here

class Square extends Shape {

    int width = x;
    
    Square() {

    int area(){
    area = x*x;
    }
}
}

class Circle extends Shape {
    
    int width = (double) y;

    Circle(){
    
    double area() {
    area = Math.PI*y*y;
    }
}
}

public class Program {
    public static void main(String[ ] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        
        Square a = new Square(x);
        Circle b = new Circle(y);
        a.area();
        b.area();
    }
}


答案1

得分: 1

您在构造函数内编写了一个函数。这是无效的,您应该会看到一个语法错误...

我假设您也想要构造函数中的参数,就像在主方法中给出的那样,并且您需要从您的area函数中返回值,因为它不是void。至少,可能不应该是;您需要在抽象类中更改abstract void area();

class Square extends Shape {
    
    public Square(int x) { 
        super.width = x;  // 设置父类的字段 
    }

    @Overrides
    public double area(){
        return 1.0 * super.width * super.width;
    }
}

然后在主方法中您想要 System.out.println(a.area())

实现Circle我留给您自己处理。

更多信息 -

英文:

You've written a function within a constructor. That's not valid, and you should be seeing a syntax error...

I assume you also will want parameters to constructors as given in the main method, and you need to return from your area function since it isn't void. At least, probably shouldn't be ; you'll want to change abstract void area(); in the abstract class

class Square extends Shape {
    
    public Square(int x) { 
        super.width = x;  // sets field of parent class 
    }

    @Overrides
    public double area(){
        return 1.0 * super.width * super.width;
    }
}

Then you want System.out.println (a.area()) in the main method.

Implementing Circle I'll leave up to you.

More info -

huangapple
  • 本文由 发表于 2023年1月9日 02:01:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定