【发布时间】:2019-03-06 00:38:59
【问题描述】:
所以这是我的大学作业,我几乎完成了所有工作。它应该使用方法覆盖,我必须从我的教授的课程(我不能改变任何东西)RidingMower 扩展我的课程 SafeRidingMower(将在解释后插入代码)。
RidingMower 类:
import java.util.*;
/**
* The riding mower class developed by the engineering department. Contains all the functionality needed
* for a working riding mower.
*
*/
public class RidingMower {
private boolean rider_in_seat = false;
private boolean is_engine_on = false;
private boolean is_mower_enabled = false;
private Random rn = new Random();
/**
* Constructor for Riding Mower which automatically launches the main menu.
*/
public RidingMower()
{
mainMenu();
}
/** Starts the engine of the riding mower (ignition start) */
public void start_engine()
{
System.out.println("<<< Starting riding mower >>>");
is_engine_on = true;
}
/** Method that randomly seats and unseats a driver */
private void sit_rider()
{
int random = rn.nextInt(5);
if ( random<=1 )
rider_in_seat = true;
else rider_in_seat = false;
System.out.print("\t=>Seated? " + rider_in_seat + "\n");
}
/** Seat sensor. Returns true if there is a driver seated on the mower or false
* if no driver is sitting on the mower.
* @return
*/
public boolean check_seat_sensor()
{
return rider_in_seat;
}
/** Method that engages the mower blades and begins mowing if the ignition is turned on */
public void mow()
{
start_mowing();
}
/** Primary method that handles the mowing. Checks to see if the ignition is on then the
mowing starts otherwise the cutting blades are lowered and ready to mow upon engine start
*/
private void start_mowing()
{
is_mower_enabled = true;
System.out.println("Mower blade ready to mow");
if ( !is_engine_on ) return;
int i=1;
while (true)
{
mowing(i++);
}
}
/** Mower fully functioning here and blades are spinning. Simulates a rider being thrown off
the mower at step 50,000 */
private void mowing(int i)
{
System.out.println( "Mowing..." + i);
if ( i>50000 ) rider_in_seat = false;
if ( rider_in_seat == false )
System.out.println("\tOMG Mower is moving without a driver?!");
mowing_hook();
}
/** Hook/Stub function called while the mower is mowing. Other departments divisions can override */
public void mowing_hook()
{
}
/** Turns off the ignition. Stops the mower if running. */
public void stop_mower()
{
System.out.println("Stopping the mower.");
System.exit(0);
}
/** Main menu of the mower simulation. Asks user to check/seat a driver, start the mower,
* begn mowing the lawn, or exiting the simulation. Method is private and can not be
* overriden.
*/
private void mainMenu()
{
Scanner sc = new Scanner( System.in );
int option;
do
{
System.out.println();
System.out.println("1. Is driver seated? (" + rider_in_seat +")" );
System.out.println("2. Start mower");
System.out.println("3. Mow the lawn");
System.out.println("4. Stop mower (if running) and Exit.");
System.out.println();
System.out.println("CHOOSE 1-4");
option = sc.nextInt();
switch ( option )
{
case 1: sit_rider();
break;
case 2: start_engine();
if ( is_mower_enabled ) mow();
break;
case 3: mow();
break;
case 4: stop_mower();
break;
}
} while ( true ); // Infinite loop here. Select option #4 to exit the program.
}
}
SafeRidingMower 类:
public class SafeRidingMower extends RidingMower
{
@Override
public void start_engine() {
if (check_seat_sensor() == true)
super.start_engine();
else
System.out.println("Seat sensor does not detect a driver."
+ "Engine will not start.");
}
@Override
public boolean check_seat_sensor() {
return super.check_seat_sensor();
}
@Override
public void mow() {
if (check_seat_sensor() == true)
super.mow();
else if (check_seat_sensor() == false)
System.out.println("Seat sensor does not detect a driver."
+ " Mower will not start.");
else
System.out.println("Engine is not on. Cannot start mowing.");
}
@Override
public void mowing_hook() {
}
}
还有驱动程序,我认为它与我遇到的特定问题无关,但无论如何都是这样。
public class TheLawnMowerManDriver {
public static void main(String[] args)
{
RidingMower mower = new SafeRidingMower();
}
}
我需要做的:在 RidingMower 类中,有一个我无法覆盖的方法 mowing(),但我可以覆盖在方法 mowing() 结束时调用的方法 mowing_hook()。我应该做的是在变量 i = 50000 时停止割草机运行,但我不知道该怎么做。我尝试将 super 关键字与变量一起使用,但它不起作用(我不知道它是否应该起作用,我只是在几节课前才接触到 super 关键字)。
有谁知道我可以停止割草机/停止变量 i 从覆盖 mowing_hook() 方法计数的方法?
【问题讨论】:
-
没有意义,
mowing_hook没有i的可见性 -
@ScaryWombat 没错,但我必须想办法让它在 50000 之后停止割草机。RidingMower 上提供的代码是我教授的代码,我没有按照他的要求更改任何东西,所以我也不明白。
-
我假设当
check_seat_sensor()返回 false 时,您应该从mowing_hook覆盖中抛出异常。没有其他方法可以停止 start_mowing 中的无限循环。 (另外,有人需要打你的教授,告诉他/她 Java 不是 C,在 Java 程序中编写类似 C 的标识符是不可接受的。) -
start_mowing 方法包含
while (true) { mowing(i++); }。除了抛出异常或调用 System.exit 之外,没有办法停止该循环。 -
我不知道你们说 C 风格命名不好是什么意思(再说一次,编程新手,除了 java 没接触过其他东西)哈哈,但我想我只是手在我这样的作业中,因为我认为他不会扣很多分,所以我会问他解决问题的方法,我会在这里发布,因为我也很困惑和好奇
标签: java inheritance polymorphism overriding super