本帖最后由 三生石 于 2014-2-26 11:33 编辑
/**需求:老师使用电脑上课
* 对象:电脑:正常运行
* 重新启动
* 老师:姓名、拥有电脑、可以上课
* 故障:电脑蓝屏
* 电脑冒烟
* 老师无法上课
* 情况分析:1.电脑正常运行后,老师正常上课
* 2.电脑蓝屏后,重新启动即恢复正常
* 3.电脑冒烟后,老师1无法上课,调换老师2上课
* @sknishy
*/
class BluePrintException extends Exception
{
BluePrintException(String Message)
{
super(Message);
}
}
class SmokeRuntimeException extends RuntimeException
{
SmokeRuntimeException(String Message)
{
super(Message);
}
}
class NoTeachException extends Exception
{
NoTeachException(String Message)
{
super(Message);
}
}
class Computer
{
private int state;//使用state表示电脑运行的状态:1为正常运行,0为蓝屏,其它数字表示冒烟了。
public void setState(int state)
{
this.state=state;
}
public int getState()
{
return state;
}
public void run()throws BluePrintException,SmokeRuntimeException
{
if(state==1)
System.out.print("正常运行,");
else if(state==0)
throw new BluePrintException("蓝屏,");
else
throw new SmokeRuntimeException("出现损坏,");
}
public void reset()
{
System.out.println("重新启动");
state++;//如何定义函数使电脑在蓝屏重新启动后恢复正常,并且让老师开始讲课即使state=1
}
}
class Teacher
{
private String name;
private Computer cp;
Teacher(String name,int k)
{
this.name=name;
cp=new Computer();
cp.setState(int k);
}
public void prelect()throws BluePrintException,NoTeachException
{
System.out.print(name+"的电脑");
try
{
cp.run();
}
catch(BluePrintException e)
{
System.out.print(e.getMessage());
cp.reset();
cp.run();
}
catch(SmokeRuntimeException e)
{
System.out.print(e.getMessage());
throw new NoTeachException("不上课");
}
System.out.println("开始上课。");
}
public void chance()
{
System.out.println("导致其无法正常上课,正在调新老师前来……请稍等……");
}
}
class School
{
public static void main(String[] args)throws BluePrintException,NoTeachException
{
Teacher t=new Teacher("穆贞贤",0);
try
{
t.prelect();
}
catch (NoTeachException e)
{
t.chance();
}
}
}
编译一直无法通过,提示需要“.class”和“;”,求高手指教,并说明原因…… |