2017-02-23 98 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