一、题目
1.编写java程序实现如下功能:声明定义一个People类(属性:身份证号,姓名,性别,体重和身高。方法:设置身份证号,姓名,性别,体重和身高,并且输出这些信息);声明定义Worker类,该类继承people类,但还具有自身的一个属性:工作时间,自身的一个方法:设置工作时间,分别生成People类的一个对象和Worker类的一个对象,输出每个对象信息。
2.设计类Rectangle,它具有两个私有属性:长和宽。请定义相关成员使该类可以计算长方形的周长和面积。并进行测试:从键盘上输入长和宽,输出相应长方形的周长和面积(使用scanner类来获取用户在键盘上的输入,通过“import Java.util.*”语句将该类导入到相应的程序)。
二、程序代码
class People {
private String id;
private String name;
private String gender;
private double weight;
private double height;
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void setHeight(double height) {
this.height = height;
}
public void printInfo() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Gender: " + gender);
System.out.println("Weight: " + weight);
System.out.println("Height: " + height);
}
}
class Worker extends People {
private int workTime;
public void setWorkTime(int workTime) {
this.workTime = workTime;
}
public void printInfo() {
super.printInfo();
System.out.println("Work Time: " + workTime);
}
}
public class Main {
public static void main(String[] args) {
People p = new People();
p.setId("123456789");
p.setName("Tom");
p.setGender("Male");
p.setWeight(70.5);
p.setHeight(180.0);
p.printInfo();
Worker w = new Worker();
w.setId("987654321");
w.setName("Jerry");
w.setGender("Male");
w.setWeight(65.0);
w.setHeight(175.0);
w.setWorkTime(8);
w.printInfo();
}
}