我有一个我正在处理的应用程序,我想创建一个代表具有这些方法的小型电池供电电机的类:
drain()
- 以恒定量排出电池runTIme()
- 显示电机运行的时间,并在电池电量降至90%时发出警告start()
- 启动电机并显示电池的运行时间和剩余容量。 (如果电机无法启动导致容量降低,则会显示警告信息)stop()
- 停止马达电机的电池容量是每个电池容量的总和,在这种情况下是两节电池。电机启动后,电机将持续显示其状态,直至电机停止或电池容量过低。
到目前为止,我所知道的是相当低级的,但我无法弄清楚如何正确编码并得到我想要的结果。
这是我的代码:
public class DCMotor {
double capacity = 7000.0;
public DCMotor(Battery bat1, Battery bat2)
{
bat1 = new Battery(3500.0);
bat2 = new Battery(3500.0);
}
public void drain() //drains the battery by a constant amount
{
do
{
capacity -= 50.0;
}while(capacity <= 7000.0 && capacity > 0);
}
public String runTime() //shows how long the battery has been running
{
if (capacity == 6300.0)
System.out.println("90% battery remaining.");
return "90% battery remaining.";
}
public void start() //starts the motor and displays the running time and available capacity
{
start();
//try{ Thread.sleep(1000);} catch (InterruptedException ex) {}
System.out.println("Remaining Capacity: " + capacity);
}
public void stop() //stops the motor
{
stop();
}
}