2015-12-01 30 views
-2
/* Airport class 
    Anderson, Franceschi 
*/ 


public class Airport 
{ 
    public static void main(String[] args) 
    { 
// 1. ***** Define the instance variables ***** 
// airportCode is a String 
// gates is an integer 
// Part 1 student code starts here: 


    private String airportCode; 
    private int gates; 


// Part 1 student code ends here. 

// 2. ***** Write this method ***** 
// Default constructor: 
// method name: Airport 
// return value: none 
// parameters: none 
// function: sets the airportCode to an empty String 
// Part 2 student code starts here: 
    public Airport() 
    { 
     airportCode = ""; 
    } 


// Part 2 student code ends here. 

// 3. ***** Write this method ***** 
// Overloaded constructor: 
// method name: Airport 
// return value: none 
// parameters: a String startAirportCode and an int startGates 
// function: 
//  calls the the setAirportCode method, 
//     passing startAirportCode parameter; 
//  calls the setGates method, passing the startGates parameter 
// Part 3 student code starts here: 
    public Airport(String startAirportCode, int startGates) 
    { 
     setAirportCode(startAirportCode); 
     setGates(startGates); 

    } 

// Part 3 student code ends here. 

// 4. ***** Write this method ***** 
// Accessor method for the airportCode instance variable 
// method name: getAirportCode 
// return value: String 
// parameters: none 
// function: returns airportCode 
// Part 4 student code starts here: 
    public String getAirportCode() 
    { 
     return airportCode; 
    } 


// Part 4 student code ends here. 

// 5. ***** Write this method ***** 
// Accessor method for the gates instance variable 
// method name: getGates 
// return value: int 
// parameters: none 
// function: returns gates 
// Part 5 student code starts here: 
    public int getGates() 
    { 
     return gates; 
    } 


// Part 5 student code ends here. 

// 6. ***** Write this method ***** 
// Mutator method for the airportCode instance variable 
// method name: setAirportCode 
// return value: void 
// parameters: String newAirportCode 
// function: assigns airportCode the value of the 
//     newAirportCode parameter 
// Part 6 student code starts here: 
    public void setAirportCode(String newAirportCode) 
    { 
     airportCode = newAirportCode; 
    } 


// Part 6 student code ends here. 

// 7. ***** Write this method ***** 
// Mutator method for the gates instance variable 
// method name: setGates 
// return value: void 
// parameters: int newGates 
// function: validates the newGates parameter. 
// if newGates is greater than or equal to 0, 
//  sets gates to newGates; 
//  otherwise, prints an error message to System.err 
//  and does not change value of gates 
// Part 7 student code starts here: 
    public void setGates(int newGates) 
    { 
     if (newGates >= 0) 
     gates = newGates; 
    else 
     { 
      System.err.println("Gates must be at least 0."); 
      System.err.println("Value of gates unchanged."); 
      return; 
     } 





    /*public void setGates(int newGates) 
    { 
     newGates = gates; 
    } 

*/ 
// Part 7 student code ends here. 

    enter code here 

} // end of Airport class definition 
} 

//本書中的代碼不包含public static void main(String [] args)。無論如何,一旦我搜索了該網站,我就添加了它,它說你不能用最近的java更新來做到這一點。沒有主語句,我得到它成功編譯,但有一個運行時錯誤,說「在機場班級找不到主要方法」。我添加它,並得到一些編譯器錯誤。我用大括號玩弄,最終破壞了它......現在我無法弄清楚爲什麼我會這樣,23個錯誤。JAVA ---編譯器和運行時錯誤。 Class main

我使用Java外部工具在textpad中進行編碼。 它適用於主類和StackFlow的方法,但不是沒有主類 - 仍然說它找不到類。我從實例變量中刪除了「private」。它沒有它編譯成功,但仍然有上述運行時錯誤。 這個文件並沒有真正運行,但它是與另一個爲我編碼的框架一起使用的。這只是第1部分;對於第2部分

的基礎,不管是誰在我詢問,我相信它再次「只有Java的工作1.6或更低版本」 How do Java programs run without defining the main method?

謝謝大家對你的幫助。這個網站真棒!

+0

main()應該做什麼? – Jan

+0

在我看來你是在方法中聲明方法。此代碼不會編譯,更不用說運行 – Stultuske

回答

0

你不能這樣做

public static void main(String[] args) 
{ 
    private String airportCode; 
    private int gates; 
} 

你的變量去外面的主要功能或把他們定義爲本地。 在開始其他功能之前,您還需要關閉主體。

您正確的代碼是這樣:

/* Airport class 
    Anderson, Franceschi 
*/ 


public class Airport 
{ 
    public static void main(String[] args) 
    { 

    } 
// 1. ***** Define the instance variables ***** 
// airportCode is a String 
// gates is an integer 
// Part 1 student code starts here: 
    private String airportCode; 
    private int gates; 


// Part 1 student code ends here. 

// 2. ***** Write this method ***** 
// Default constructor: 
// method name: Airport 
// return value: none 
// parameters: none 
// function: sets the airportCode to an empty String 
// Part 2 student code starts here: 
    public Airport() 
    { 
     airportCode = ""; 
    } 


// Part 2 student code ends here. 

// 3. ***** Write this method ***** 
// Overloaded constructor: 
// method name: Airport 
// return value: none 
// parameters: a String startAirportCode and an int startGates 
// function: 
//  calls the the setAirportCode method, 
//     passing startAirportCode parameter; 
//  calls the setGates method, passing the startGates parameter 
// Part 3 student code starts here: 
    public Airport(String startAirportCode, int startGates) 
    { 
     setAirportCode(startAirportCode); 
     setGates(startGates); 

    } 

// Part 3 student code ends here. 

// 4. ***** Write this method ***** 
// Accessor method for the airportCode instance variable 
// method name: getAirportCode 
// return value: String 
// parameters: none 
// function: returns airportCode 
// Part 4 student code starts here: 
    public String getAirportCode() 
    { 
     return airportCode; 
    } 


// Part 4 student code ends here. 

// 5. ***** Write this method ***** 
// Accessor method for the gates instance variable 
// method name: getGates 
// return value: int 
// parameters: none 
// function: returns gates 
// Part 5 student code starts here: 
    public int getGates() 
    { 
     return gates; 
    } 


// Part 5 student code ends here. 

// 6. ***** Write this method ***** 
// Mutator method for the airportCode instance variable 
// method name: setAirportCode 
// return value: void 
// parameters: String newAirportCode 
// function: assigns airportCode the value of the 
//     newAirportCode parameter 
// Part 6 student code starts here: 
    public void setAirportCode(String newAirportCode) 
    { 
     airportCode = newAirportCode; 
    } 


// Part 6 student code ends here. 

// 7. ***** Write this method ***** 
// Mutator method for the gates instance variable 
// method name: setGates 
// return value: void 
// parameters: int newGates 
// function: validates the newGates parameter. 
// if newGates is greater than or equal to 0, 
//  sets gates to newGates; 
//  otherwise, prints an error message to System.err 
//  and does not change value of gates 
// Part 7 student code starts here: 
    public void setGates(int newGates) 
    { 
     if (newGates >= 0) 
     gates = newGates; 
    else 
     { 
      System.err.println("Gates must be at least 0."); 
      System.err.println("Value of gates unchanged."); 
      return; 
     } 





    /*public void setGates(int newGates) 
    { 
     newGates = gates; 
    } 

*/ 
// Part 7 student code ends here. 

    enter code here 

} 
機場類定義的

//結束 }

+0

謝謝。我已經嘗試過,但是我得到了這個。 Bulldogs \ 1.CIS210 \ week7 \ Airport.java:14:錯誤:表達式的非法開始 private String airportCode; ^ 下載\新文件夾\ GMC Bulldogs \ 1.CIS210 \ week7 \ Airport.java:15:錯誤:表達式的非法開始 private int gates; ^ 2錯誤 工具完成退出代碼1 – CoderChic

+0

這!非常感謝!!它現在有效。我仍然不明白爲什麼框架沒有促使我添加主體,或者如果確實有必要(另一位評論者說不是),但我完全學到了一些新東西! – CoderChic

+0

Main可以是任何想要開始執行的類的一部分。因此,框架不會強制您將其添加到特定的類。很高興這有助於。請忘記將問題標記爲答案來解決問題。 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – StackFlowed

1

這別讓應該(但不是修復)這個:

public class Airport 
{ 
public static void main(String[] args) 
{ 
    System.out.println("Running. But what do I have to do?"); 
} //CLOSE THAT MAIN <- need to close that method. 

[... all you code ...] 

} // end of Airport class definition 
//Remove this. Class already ended. } 

從那裏你應該做兩件事:找出main()中應該發生的事情,並安裝一個IDE語法高亮和eclipse等東西。這會爲你指出這些錯誤。

+0

哈!我有文本板。完全應該注意到這一點!謝謝你的幫助!! – CoderChic

0

這可能有幫助!正如上面有人說,

Your variables go outside main function or declare them as local. You also need to close out main before you start an other functions.

public class Airport 
{ 
String airportCode; 
int gates; 

public Airport() 
{ 
    airportCode = ""; 
} 

public Airport(String startAirportCode, int startGates) 
{ 
    setAirportCode(startAirportCode); 
    setGates(startGates); 

} 

public String getAirportCode() 
{ 
    return airportCode; 
} 


public int getGates() 
{ 
    return gates; 
} 


public void setAirportCode(String newAirportCode) 
{ 
    airportCode = newAirportCode; 
} 

public void setGates(int newGates) 
{ 
    if (newGates >= 0) 
    gates = newGates; 
else 
    { 
     System.err.println("Gates must be at least 0."); 
     System.err.println("Value of gates unchanged."); 
     return; 
    } 
} 
public static void main(String[] args) { 
    Airport r = new Airport("Hello", 1); 
    r.setAirportCode("Abcd"); 
    System.out.println("" + r.getAirportCode()); 
    r.setGates(3); 
    System.out.println("" + r.getGates()); 
} 
} 
0

你不需要一個主要方法。如果您刪除這些行:

public static void main(String[] args) 
    { 

,並在底部註釋掉該行:

enter code here 

應該編譯:javac Airport.java。這個機場對象聽起來像是你不運行的東西......只是建造它,它會像機場一樣。我猜測在本課的後面你會做一些使用機場的東西......比如AirportSimulator.java。它可能有一個主要方法是這個樣子:

public class AirportSimulator 
{ 
    public static void main(String[] args) 
    { 
     AirportSimulator airsim = new AirportSimulator(); 
     airsim.simulate(); 
    } 

    public void simulate() 
    { 
     Airport air = new Airport(); 
     System.out.println("Setting gates to 1"); 
     air.setGates(1); 
     System.out.println(String.format("Gates set to %d", air.getGates()); 
     System.out.println("Setting airport code to IAD"); 
     air.setAirportCode("IAD"); 
     System.out.println(String.format("Airport code is %s", air.getAirportCode()); 
    } 
} 

我的觀點是,模擬器就像是一個動詞,使更多,你會與主運行它的意義,而一個機場就像是一個名詞。