2017-02-23 50 views
-3

所以这种情况是有点令人沮丧的我,因为我的在线老师喜欢我们只是通过自己独立学习在线课程和我到处都看上去还是我找不到如何使用无参数完全无参数的构造

以下是我正在创建的程序,但是因为我知道它不能在没有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); 

    } 
} 
+2

'main'方法是一个静态方法,没有构造函数被调用。如果你需要调用默认的构造函数,你必须明确地做。如果你的教授拒绝解释他的学生不理解的东西,那么......不是一个好的IMO教授。 – BackSlash

+0

如果你想运行“Week07_Assignment”的构造函数,那么你必须创建它的对象。 –

回答

0

你好@BackSlash提到,你可以使用下面的代码。

public static void main (String[] args) 
{ 
    Week07_Assignment object = new Week07_Assignment(); 

    object.createrectangle(); 

} 
+0

你已经救了我一周最大的头痛,非常感谢你! – zenon

+0

@zenon不用担心,你可以在一个叫做program的独立类中使用main方法。这样你就可以把你的逻辑分成类 – hoChay

0

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


从头开始(并根据您的问题),你需要创建一个类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; 
    } 
} 

的InputReader

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); 
    } 
} 

注意InputReaderRectangle类在一个单独的包中,以便保留您的任务所需的任何命名约定。

+0

我不明白'InputReader'是什么。它只是一个'扫描仪'的包装纸。 –

+0

的确,这是它唯一的目的。这就是为什么我提到这是一个矫枉过正的问题 - 这意味着没有必要。但是,我认为分配可能不仅仅是给定的描述,或者用作后续分配​​的基础。因此,如果'InputReader'得到扩展,它不会混淆'Main'类。 – mcuenez