《Java程序设计》实验4
本次实验考查知识点:
(1)Java语言中的类继承(extends)
(2)Java语言中的类封装(private)
(3)Java语言中的构造方法重写(override)
实验题目:
(1)定义一个名为Student的类,它继承Person类(Person类中包含String类型的name和int类型的age),其中定义sno(表示学号)和major(表示专业)两个成员变量和封装这两个变量的方法。编写主程序,检查新建类中的所有变量和方法。
public class Person {
String name;
int age;
private int sno;
private String major;
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public void show() {
System.out.println("学号:"+sno);
System.out.println("专业:"+major);
}
}
public class Student extends Person {
public void printName() {
System.out.println("name="+name);
}
public void printAge() {
System.out.println("age="+age);
}
}
public class Testfirst {
public static void main(String[] args) {
Student student = new Student();
student.name="张三";
student.printName();
student.age=20;
student.printAge();
Person person = new Person();
person.setSno(117);
person.setMajor("计科");
person.show();
}
}
(2)设计一个汽车类Auto,其中包含一个表示速度的double型的成员变量speed和表示启动的start()方法、表示加速的speedUp()方法以及表示停止的stop()方法。再设计一个Auto类的子类Bus表示公共汽车,在Bus类中定义一个int型的、表示乘客数量的成员变量passenger,另外定义两个方法gotOn()和gotOff()表示乘客上车和下车。编写一个应用程序,测试Bus类的使用。
public class Auto {
double speed;
public void start(){
System.out.println("始站发车");
}
public void speedUp(){
System.out.println("加速前行");
}
public void stop(){
System.out.println("到站停车");
}
}
public class Bus extends Auto {
int passenger;
public void gotOn(){
passenger = 5;
System.out.println("本站有"+passenger+"人上车");
}
public void gotOff()
{
passenger = 3;
System.out.println("本站有"+passenger+"人下车");
}
}
public class BusTest {
public static void main(String[] args) {
Bus bus = new Bus();
bus.gotOn();
bus.gotOff();
}
}
(3)定义一个名为Cuboid的长方体类,使其继承Rectangle类(Rectangle类中包含double类型的length和width),其中包含一个表示高度的double型成员变量height,定义一个构造方法Cuboid(double length,double width,double height)和一个求长方体体积的volume()方法。编写一个应用程序,在其中求一个长、宽、高分别为10、5、2的长方体的体积。
package homework.test;
public class Rectangle {
double length;
double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public String toString() {
return "Rectangle [length=" + length + ", width=" + width + "]";
}
}
package homework.test;
public class Cuboid extends Rectangle {
double height;
public Cuboid() {
this(0,0,0);
}
public Cuboid(double length,double width,double height)
{
super(length,width);
this.height = height;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double volume()
{
return length * width * height;
}
}
package homework.test;
public class CuboidTest {
public static void main(String[] args) {
Cuboid c = new Cuboid();
c.setLength(10);
c.setWidth(5);
c.setHeight(2);
System.out.println("长方体的体积为:"+c.volume());
}
}