永发信息网

作业来的,设计一个名为Fan的类模拟风扇。

答案:4  悬赏:0  手机版
解决时间 2021-01-29 15:39
  • 提问者网友:雾里闻花香
  • 2021-01-29 04:18
作业来的,设计一个名为Fan的类模拟风扇。
最佳答案
  • 五星知识达人网友:大漠
  • 2021-01-29 05:50
public class Fan {

private final int SLOW = 1;
private final int MEDIUM = 1;
private final int FAST = 1;
private int speed = SLOW;
private boolean on = false;
private double radius = 5;
private String color = "blue";

public int getSpeed() {
return speed;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public boolean isOn() {
return on;
}

public void setOn(boolean on) {
this.on = on;
}

public double getRadius() {
return radius;
}

public void setRadius(double radius) {
this.radius = radius;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public Fan(){};

public Fan(int speed,boolean on,double radius,String color){
this.speed = speed;
this.on = on;
this.radius = radius;
this.color = color;
}

@Override
public String toString() {
if(on==true){
return "该风扇的速度为:" + speed +";颜色是:"+color+";半径是:"+radius;
}else{
return "fan is off;"+"该风扇的颜色是:"+color+";半径是:"+radius;
}
}

}

uml图自己画,呵呵
全部回答
  • 1楼网友:像个废品
  • 2021-01-29 07:32

UML如果需要的话稍后给你画吧,吃饭去啦 呵呵 这个程序仅供参考
public class Fan {
 private final int SLOW = 1;
 private final int MEDIUM = 2;
 private final int FAST = 3;
 private int speed = SLOW;
 private boolean isOn  = false;
 private double radius =5.0;
 private String color = "blue";
 public Fan()
 {
  
 }
 public int getSpeed()
 {
  return speed;
 }
 public void setSpeed(int _speed)
 {
  if(_speed>=3)
  speed = FAST;
  else if(_speed<=1)
  speed = SLOW;
  else 
  speed = MEDIUM;
  
 }
 
  public double getRadius()
 {
  return radius;
 }
 public void setRadius(int _radius)
 {
  radius = radius;
 }
 
 public void setColor(String _color)
 {
  color = _color;
 }
 
 public String getColor()
 {
  return color;
 }
 
 public boolean isOn()
 {
  return isOn;
 }
  public void run()
  {
   if(!isOn())
   isOn = true;
  }
  
  //@Overwrite
  public String toString()
  {
   if(isOn())
   return "Fan : speed= "+speed+" color= "+color+" radius = "+radius;
   else
   return "This Fan is off! And the color of this Fan is "+color+" ,the radius of this Fan is "+radius;
  }
  
  public static void main(String []args)
  {
   Fan f1=new Fan();
   f1.setColor("yellow");
   f1.setRadius(10);
   f1.setSpeed(3);
   f1.run();
   Fan f2 = new Fan();
   f2.setSpeed(2);
   f2.setRadius(5);
   System.out.println(f1);
   System.out.println(f2);
   
  }
 }
  • 2楼网友:杯酒困英雄
  • 2021-01-29 07:14
你拿200分就要回答这些问题,,,,直接送我得了~~~~~~~
  • 3楼网友:胯下狙击手
  • 2021-01-29 06:38
1000分待考虑
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯