Java语言程序设计 例题9.8(Fan类)

9.8 (The Fan class) Design a class named Fan to represent a fan. The class contains: 
 ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to 
denote the fan speed.
 ■ A private int data field named speed that specifies the speed of the fan (the 
default is SLOW).
 ■ A private boolean data field named on that specifies whether the fan is on (the 
default is false).
 ■ A private double data field named radius that specifies the radius of the fan 
(the default is 5).
 ■ A string data field named color that specifies the color of the fan (the default 
is blue).
 ■ The accessor and mutator methods for all four data fields.
 ■ A no-arg constructor that creates a default fan.
 ■ A method named toString() that returns a string description for the fan. If 
the fan is on, the method returns the fan speed, color, and radius in one combined
string. If the fan is not on, the method returns the fan color and radius 
along with the string “fan is off” in one combined string.
Draw the UML diagram for the class and then implement the class. Write a test 
program that creates two Fan objects. Assign maximum speed, radius 10, color 
yellow, and turn it on to the first object. Assign medium speed, radius 5, color 
blue, and turn it off to the second object. Display the objects by invoking their 
toString method. 

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

三个名为 SLOW、MEDIUM 和 FAST 而值为 1、2 和 3 的常量,表示风扇的速度。
一个名为 speed 的 int 类型私有数据域,表示风扇的速度(默认值为 SLOW)。
一个名为 on 的 boolean 类型私有数据域,表示风扇是否打开(默认值为 false)。
一个名为 radius 的double 类型私有数据域,表示风扇的半径(默认值为 5)。
一个名为 color 的 String 类型数据域,表示风扇的颜色(默认值为 blue)。
这四个数据域的访问器和修改器。
一个创建默认风扇的无参构造方法。
一个名为 toString() 的方法返回描述风扇的字符串。如果风扇是打开的,那么该方法在一个组合的字符串中返回风扇的速度、颜色和半径。如果风扇没有打开,该方法就返回一个由 “fan is off” 和风扇颜色及半径组合的字符串。
画出该类的UML图并实现这个类。编写一个测试程序,创建两个 Fan 对象。将第一个对象设置为最大速度、半径为 10、颜色为 yellow 、状态为打开。将第二个对象设置为中等速度、半径为 5、颜色为 blue 、状态为关闭。通过调用它们的 toString 方法显示这些对象。
 

代码如下:

import java.util.*;
public class Unite9Test8 
{	
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		Fan f1=new Fan();
		Fan f2=new Fan();
		f1.setSpeed(3);
		f1.setRadius(10);
		f1.color="yellow";
		f1.setOn(true);
		System.out.println(f1.toString());
		f2.setSpeed(2);
		f2.setRadius(5);
		f2.color="blue";
		f2.setOn(false);
		System.out.println(f2.toString());

	}

}
class Fan
{
	int SLOW=1;
	int MEDIUM=2;
	int FAST=3;
	private int speed=SLOW;
	private boolean on =false;
	private double radius=5;
	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 Fan() 
	{
		
	}
	public String toString() 
	{
		if(this.on==true) 
		{
			String a="The speed is "+this.speed+" ,the color is "+this.color+" ,the radius is "+this.radius;
			return a;
		}else 
		{
			String a="The color is "+this.color+",the radius is "+this.radius+" ,fan is off";
			return a;
		}
	}
	
}

结果如下:

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

差劲的厉害了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值