1、编写一个人类person,其中包含姓名、性别和年龄的属性,包含构造方法以及显示姓名、性别和年龄的方法。再编写一个学生类student,它继承person类,其中包含学号属性,包含构造方法以及显示学号的方法。最后编写一个主类teststudent,包含main()方法,在main()方法中定义两个学生s1和s2并给他们赋值,最后显示他们的学号、姓名、性别以及年龄。 2、编一个程序,包含以下文件。 (1)shape.java文件,在该文件中定义接口shape。 属性:pi。 方法:求面积的方法area()。 (2)circle.java文件,在该文件中定义圆类circle,实现shape接口。 属性:圆半径radius。 方法:构造方法;实现接口中求面积方法area();求周长方法perimeter()。 (3)“cylinder.java”文件,在该文件中定义圆柱体类cylinder,继承圆类。 属性:圆柱体高度height。 方法:构造方法;求表面积方法area();求体积方法volume()。 (4)test.java文件,在该文件中定义主类test,其中包含主方法main(),在主方法中创建两个圆类对象cir1和cir2,具体尺寸自己确定,并显示圆的面积和周长;再创建两个圆柱体类的对象cy1和cy2,具体尺寸自己确定,然后分别显示圆柱体cy1和cy2的底圆的面积和周长以及它们各自的体积和表面积。
时间: 2023-04-28 21:02:15 浏览: 78
1、人类person类的代码:
public class Person {
private String name;
private String gender;
private int age;
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public void displayInfo() {
System.out.println("姓名:" + name);
System.out.println("性别:" + gender);
System.out.println("年龄:" + age);
}
}
学生类student类的代码:
public class Student extends Person {
private String studentId;
public Student(String name, String gender, int age, String studentId) {
super(name, gender, age);
this.studentId = studentId;
}
public void displayStudentId() {
System.out.println("学号:" + studentId);
}
}
主类teststudent类的代码:
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student("张三", "男", 18, "2018001");
Student s2 = new Student("李四", "女", 19, "2018002");
s1.displayStudentId();
s1.displayInfo();
s2.displayStudentId();
s2.displayInfo();
}
}
2、shape接口的代码:
public interface Shape {
double pi = 3.14;
double area();
}
圆类circle的代码:
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double area() {
return pi * radius * radius;
}
public double perimeter() {
return 2 * pi * radius;
}
}
圆柱体类cylinder的代码:
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double area() {
return 2 * super.area() + 2 * pi * radius * height;
}
public double volume() {
return super.area() * height;
}
}
主类test的代码:
public class Test {
public static void main(String[] args) {
Circle cir1 = new Circle(5);
Circle cir2 = new Circle(10);
System.out.println("圆1的面积:" + cir1.area());
System.out.println("圆1的周长:" + cir1.perimeter());
System.out.println("圆2的面积:" + cir2.area());
System.out.println("圆2的周长:" + cir2.perimeter());
Cylinder cy1 = new Cylinder(5, 10);
Cylinder cy2 = new Cylinder(10, 20);
System.out.println("圆柱体1的底圆面积:" + cy1.area());
System.out.println("圆柱体1的底圆周长:" + cy1.perimeter());
System.out.println("圆柱体1的体积:" + cy1.volume());
System.out.println("圆柱体1的表面积:" + cy1.area() + 2 * pi * radius * height);
System.out.println("圆柱体2的底圆面积:" + cy2.area());
System.out.println("圆柱体2的底圆周长:" + cy2.perimeter());
System.out.println("圆柱体2的体积:" + cy2.volume());
System.out.println("圆柱体2的表面积:" + cy2.area() + 2 * pi * radius * height);
}
}