No-args构造函数

时间:2017-02-23 11:26:45

标签: java args

所以这种情况对我来说有点令人沮丧,因为我的网上老师更喜欢我们自己独立在线学习课程,而且我看到的任何地方我仍然无法找到如何完全使用no-args

以下是我正在创建的程序,但是据我所知,我不能在没有main的情况下运行它,但每当我输入main时它都不会运行no-args构造而我一般都不知道我在哪里我搞砸了。

import java.util.Scanner;
public class Week07_Assignment {

    Scanner input = new Scanner(System.in);

    private double height = 1;
    private double width=1;
    private double h;
    private double w;


    public void createrectangle(){
        System.out.println("Give me both the height and width of this rectangle");
        height = heightgetter(height);
        width = heightgetter(width);
        area(height, width);
        perimeter(height, width);
    }

    public double heightgetter(double a){
        a = input.nextDouble();
        return a;

    }

    public double widthgetter(double a){
        a = input.nextDouble();
        return a;
    }

    public void area(double a, double b){
        double area = a * b;
        System.out.println("This is the area: " +area);

    }

    public void perimeter(double a, double b){
        double perimeter = 2 * (a + b);
        System.out.println("This is the area: " +perimeter);

    }
}

1 个答案:

答案 0 :(得分:0)

hoChay 的答案是正确的。除了他的评论之外,这里还有一种将你的逻辑分成不同类的方法。

从头开始(并根据您的问题),您需要创建一个类setInterval,它应该只有与矩形相关的所有方法,例如计算周长区域

接下来,你应该通过阅读用户的输入创建一个关注自己的类(虽然它对你的作业来说太过分了),即Rectangle

最后但并非最不重要的是创建您的InputReader类,它将在执行代码时充当起点。

矩形

Main

输入阅读器

package assignment_07;

public class Rectangle {
    private double height;
    private double width;

    private double area;
    private double perimeter;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public void create() {
        area(this.height, this.width);
        perimeter(this.height, this.width);
    }

    private double area(double width, double height) {
        this.area = width * height;
        return this.area;
    }

    private double perimeter(double width, double height) {
        this.perimeter = 2 * (width + height);
        return this.perimeter;
    }

    public double getPerimeter(){
        return this.perimeter;
    }

    public double getArea(){
        return this.area;
    }

    @Override
    public String toString() {
        return "Perimeter: " + this.perimeter + "\nArea: " + this.area;
    }
}

起点(主要)

package assignment_07;
import java.util.Scanner;

public class InputReader {
    private Scanner scanner;

    public InputReader() {
        scanner = new Scanner(System.in);
    }

    public double readDouble(){
        return scanner.nextDouble();
    }
}

注意import assignment_07.InputReader; import assignment_07.Rectangle; public class Main { public static void main(String[] args) { InputReader inputReader = new InputReader(); // Get measurements from user double width = inputReader.readDouble(); double height = inputReader.readDouble(); // Create rectangle Rectangle rectangle = new Rectangle(width, height); rectangle.create(); // Print System.out.println(rectangle); } } InputReader类位于单独的包中,以保留您的作业所需的任何命名约定。