/* 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?
謝謝大家對你的幫助。這個網站真棒!
main()應該做什麼? – Jan
在我看來你是在方法中聲明方法。此代碼不會編譯,更不用說運行 – Stultuske