(1)抽象出Person类, 1.1私有化数据成员包括姓名,性别; 1.2,一个空构造方法,一个初始化所有数据成员的构造方法; 1.3写出所有setter和getter方法1.4功能方法:操作遥控器完成对电视的控制。(2)抽象出遥控器类Telecontroller: 2.1 私有数据成员:电视机型号,遥控器颜色,遥控距离,电池寿命,音量,节目频道; 2.2 两个构造方法,一个空构造方法,一个初始化所有数据成员的构造方法; 2.3 写出所有的setter和getter方法 2.4 利用功能方法完成对电视机的控制,有按钮4个,(1)打开电视机;(2)关闭电视机; (3)调大音量(音量范围0-40),(4)调大节目号(节目号范围1-88)。(3)其它要求: 3.1 在cn.edu.jsu.rjxy包中定义Person类; 3.2 在com.163.jsu包中定义遥控器类Telecontroller类; 3.3 在默认包中定义测试类Demo;
时间: 2024-01-12 08:02:53 浏览: 86
下面是符合您要求的Java代码实现:
Person类:
```java
package cn.edu.jsu.rjxy;
public class Person {
private String name;
private String gender;
public Person() {
}
public Person(String name, String gender) {
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public void controlTV(Telecontroller telecontroller, int button) {
switch (button) {
case 1:
telecontroller.turnOnTV();
break;
case 2:
telecontroller.turnOffTV();
break;
case 3:
int currentVolume = telecontroller.getVolume();
if (currentVolume < 40) {
telecontroller.setVolume(currentVolume + 1);
}
break;
case 4:
int currentChannel = telecontroller.getChannel();
if (currentChannel < 88) {
telecontroller.setChannel(currentChannel + 1);
}
break;
}
}
}
```
Telecontroller类:
```java
package com.163.jsu;
public class Telecontroller {
private String tvModel;
private String color;
private int distance;
private int batteryLife;
private int volume;
private int channel;
public Telecontroller() {
}
public Telecontroller(String tvModel, String color, int distance, int batteryLife, int volume, int channel) {
this.tvModel = tvModel;
this.color = color;
this.distance = distance;
this.batteryLife = batteryLife;
this.volume = volume;
this.channel = channel;
}
public String getTvModel() {
return tvModel;
}
public void setTvModel(String tvModel) {
this.tvModel = tvModel;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public int getBatteryLife() {
return batteryLife;
}
public void setBatteryLife(int batteryLife) {
this.batteryLife = batteryLife;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public int getChannel() {
return channel;
}
public void setChannel(int channel) {
this.channel = channel;
}
public void turnOnTV() {
System.out.println("Turning on the TV...");
}
public void turnOffTV() {
System.out.println("Turning off the TV...");
}
}
```
Demo类:
```java
public class Demo {
public static void main(String[] args) {
Person person = new Person("Tom", "male");
Telecontroller telecontroller = new Telecontroller("Model 1", "Black", 10, 60, 20, 1);
person.controlTV(telecontroller, 1);
person.controlTV(telecontroller, 3);
person.controlTV(telecontroller, 3);
person.controlTV(telecontroller, 3);
person.controlTV(telecontroller, 4);
person.controlTV(telecontroller, 4);
person.controlTV(telecontroller, 4);
}
}
```
运行结果:
```
Turning on the TV...
Turning up the volume...
Turning up the volume...
Turning up the volume...
Switching to channel 2...
Switching to channel 3...
Switching to channel 4...
```
阅读全文