• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

7-6 设计风扇Fan类

武飞扬头像
aurora zero
帮助4

设计一个名为Fan的类表示一个风扇。这个类包括:

1.三个名为SlOW、MEDIUM和FAST,其值为1、2和3常量表示风扇的速度。

2.一个名为speed的int类型私有数据域表示风扇的速度(默认值为SLOW)。

3.一个名为on的boolean类型私有数据域表示风扇是否打开(默认值为false)。

4.一个名为radius的double类型私有数据域表示风扇的半径(默认值为5)。

5.一个名为color的string类型数据域表示风扇的颜色(默认值为white)。

6.这四个成员变量(数据域)的访问器和修改器。

7.一个无参构造方法;

8.一个有参构造方法 public Fan(int fanSpeed,boolean fanOn,double fanRadius,String fanColor) { ... },

9.一个名为toString()的方法返回描述风扇的字符串。如果风扇是打开的,那么该方法在一个组合的字符串中返回风扇的速度、颜色和半径。如果风扇没有打开,该方法就会返回一个由"fan is off"和风扇颜色以及半径组合成的字符串。

请在自己的草稿本上画出UML图,养成良好的设计习惯。

要求:创建两个Fan对象:

第一个对象利用无参构造方法构造默认风扇对象Fan1;

第二个对象依据输入的风扇属性,调用有参构造方法构造自定义风扇对象Fan2。

通过调用它们的toString方法显示这些对象。

输入格式:

第二个对象的属性

输出格式:

分别输出默认风扇和自定义风扇的速度、颜色、半径以及风扇是否打开

输入样例:

在这里给出一组输入。例如:

  1.  
    2
  2.  
    True
  3.  
    6
  4.  
    Red

输出样例:

在这里给出相应的输出。例如:

  1.  
    -------
  2.  
    Default
  3.  
    -------
  4.  
    speed 1
  5.  
    color white
  6.  
    radius 5.0
  7.  
    fan is off
  8.  
    -------
  9.  
    My Fan
  10.  
    -------
  11.  
    speed 2
  12.  
    color Red
  13.  
    radius 6.0
  14.  
    fan is on

代码如下:

  1.  
    import java.util.*;
  2.  
    class Fan{
  3.  
    public final int SlOW=1;
  4.  
    public final int MEDIUM=2;
  5.  
    public final int FAST=3;
  6.  
    private int speed=SlOW;
  7.  
    private boolean on=false;
  8.  
    private double radius=5;
  9.  
    private String color="white";
  10.  
    public Fan(){}
  11.  
    public void setSpeed(int speed) {
  12.  
    this.speed = speed;
  13.  
    }
  14.  
     
  15.  
    public void setOn(boolean on) {
  16.  
    this.on = on;
  17.  
    }
  18.  
     
  19.  
    public void setRadius(double radius) {
  20.  
    this.radius = radius;
  21.  
    }
  22.  
     
  23.  
    public void setColor(String color) {
  24.  
    this.color = color;
  25.  
    }
  26.  
     
  27.  
    public int getSpeed() {
  28.  
    return speed;
  29.  
    }
  30.  
     
  31.  
    public boolean isOn() {
  32.  
    return on;
  33.  
    }
  34.  
     
  35.  
    public double getRadius() {
  36.  
    return radius;
  37.  
    }
  38.  
     
  39.  
    public String getColor() {
  40.  
    return color;
  41.  
    }
  42.  
    public Fan(int fanSpeed,boolean fanOn,double fanRadius,String Color){
  43.  
    this.speed=fanSpeed;
  44.  
    this.on=fanOn;
  45.  
    this.radius=fanRadius;
  46.  
    this.color=Color;
  47.  
    }
  48.  
    public String toString(){
  49.  
    String s="speed " speed "\n" "color " color "\n" "radius " radius "\n";
  50.  
    if(on==false) s ="fan is off";
  51.  
    else s ="fan is on";
  52.  
    return s;
  53.  
    }
  54.  
     
  55.  
    }
  56.  
    public class Main{
  57.  
    public static void main(String[] args){
  58.  
    System.out.println("-------\n" "Default\n" "-------");
  59.  
    Fan fan1=new Fan();
  60.  
    System.out.println(fan1.toString());
  61.  
    System.out.println("-------\n" "My Fan\n" "-------");
  62.  
    Scanner in=new Scanner(System.in);
  63.  
    int fanSpeed=in.nextInt();
  64.  
    boolean fanOn=in.nextBoolean();
  65.  
    double fanRadius=in.nextDouble();
  66.  
    String Color=in.next();
  67.  
    Fan fan2=new Fan(fanSpeed,fanOn,fanRadius,Color);
  68.  
    System.out.println(fan2.toString());
  69.  
    }
  70.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjjckc
系列文章
更多 icon
同类精品
更多 icon
继续加载