/* 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外部工具在textpad中编码。 它适用于主类和StackFlow的方法,但不是没有主类 - 仍然说它无法找到类。我删除了"私人"来自实例变量。它在没有它的情况下成功编译,但仍然存在上述运行时错误。 这个文件并没有真正运行,但是它将与另一个为我编码的框架一起使用。这只是第1部分;第2部分的基础。
对于其他任何追问我的人,我确实相信它只适用于java 1.6或更低版本" How do Java programs run without defining the main method?
再次感谢大家的帮助。这个网站太棒了!
答案 0 :(得分: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这样的东西。这将为你指出这些错误。
答案 1 :(得分:0)
你做不到
public static void main(String[] args)
{
private String airportCode;
private int gates;
}
您的变量不在main函数之外或将它们声明为local。 在开始其他功能之前,您还需要关闭main。
您更正的代码如下:
/* 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
}
//机场类定义的结束 }
答案 2 :(得分:0)
这可能会有所帮助!!!如上所述,
您的变量不在main函数之外或将它们声明为local。在开始其他功能之前,您还需要关闭main。
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());
}
}
答案 3 :(得分:0)
您不需要主方法。如果删除这些行:
public static void main(String[] args)
{
并在底部注释掉这一行:
enter code here
它应该编译:{{1}}。这个机场对象听起来像你没有运行的东西...只是建立它,它将表现得像一个机场。我猜你以后会在课程中制作一些使用机场的东西......比如AirportSimulator.java。它可能有一个主要的方法,看起来像这样:
javac Airport.java
我的观点是,模拟器就像一个动词,更有意义的是你可以使用main运行它,而机场就像一个名词。