实验目的与要求:
实验目的:熟悉面向对象编程中类的编写。
实验要求:
(1).“自行车”、“小汽车”、“火车”都可以称之为“车”。请通过分析,抽象它们所共有的性质,定义一个抽象类Vehicle。在报告中附上程序截图、运行结果和详细的文字说明。(20分)
(2).编写一个Person类。该类包含两个成员变量weight和height,分别代表一个人的身高和体重。该类重写了Object类的toString方法。当调用它重写的toString方法时,输出这个人的身高和体重。在报告中附上程序截图、运行结果和详细的文字说明。(20分)
(3).编写一个抽象类Animal,它具有两个抽象方法run和eat。分别实现这个抽象类的三个子类Elephant、Lion和Zebra。实现一个测试类,在测试类的main方法中分别使用这三个子类创建对象实体,然后通过它们的上转型对象变量调用run方法和eat方法。在报告中附上程序截图、运行结果和详细的文字说明。(20分)
(4).一个二维向量由两个分量组成。二维向量的相加和相减等价于对应两个分量的相加和相减。比如两个二维向量(5,2)和(3,-1),它们的和为(8,1),它们的差为(2,3)。编写一个接口Computable,它具有两个抽象方法add和minus。编写一个Vector类,通过Computable接口实现了二维向量的相加和相减。在报告中附上程序截图、运行结果和详细的文字说明。(20分)
(5).进一步扩展例4-5,创建一个继承了Son类的GrandSon类。有没有办法在GrandSon类中调用它的祖父类Father中被Son类所覆盖的print_house_area方法?在报告中附上程序截图、运行结果和详细的文字说明。(20分)
(1).“自行车”、“小汽车”、“火车”都可以称之为“车”。请通过分析,抽象它们所共有的性质,定义一个抽象类Vehicle。在报告中附上程序截图、运行结果和详细的文字说明。(20分)
设计:对于三种载具,抽象出种类,速度,载客上限三个成员变量,实现对应的构造器,getter和setter方法,然后预留一个抽象方法toString,最后以在main方法中实现抽象方法的方式进行测试
package Hw4;
public abstract class Vehicle {
private String type;
private double speed;
private double passenger;
public Vehicle(String type, double speed, double passenger) {
this.type = type;
this.speed = speed;
this.passenger = passenger;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public double getPassenger() {
return passenger;
}
public void setPassenger(double passenger) {
this.passenger = passenger;
}
public abstract String toString();
public static void main(String[] args) {
Vehicle bike = new Vehicle("bike",20,1) {
@Override
public String toString() {
String temp = "";
temp += "This is bike! Type:"+getType()+" Speed:"+getSpeed()+" Passenger:"+getPassenger();
return temp;
}
};
Vehicle car = new Vehicle("car",80,4) {
@Override
public String toString() {
String temp = "";
temp += "This is car! Type:"+getType()+" Speed:"+getSpeed()+" Passenger:"+getPassenger();
return temp;
}
};
Vehicle plane = new Vehicle("plane",400,50) {
@Override
public String toString() {
String temp = "";
temp += "This is plane! Type:"+getType()+" Speed:"+getSpeed()+" Passenger:"+getPassenger();
return temp;
}
};
System.out.println(bike);
System.out.println(car);
System.out.println(plane);
}
}
(2).编写一个Person类。该类包含两个成员变量weight和height,分别代表一个人的身高和体重。该类重写了Object类的toString方法。当调用它重写的toString方法时,输出这个人的身高和体重。在报告中附上程序截图、运行结果和详细的文字说明。(20分)
设计:额外添加对应的setter,getter方法
package Hw4;
public class Person {
private double weight;
private double height;
public Person(double weight, double height) {
this.weight = weight;
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "Person{" +
"weight=" + weight +
", height=" + height +
'}';
}
public static void main(String[] args) {
Person person = new Person(160,180);
System.out.println(person);
}
}
(3).编写一个抽象类Animal,它具有两个抽象方法run和eat。分别实现这个抽象类的三个子类Elephant、Lion和Zebra。实现一个测试类,在测试类的main方法中分别使用这三个子类创建对象实体,然后通过它们的上转型对象变量调用run方法和eat方法。在报告中附上程序截图、运行结果和详细的文字说明。(20分)
设计:在run和eat方法中输出相关信息来区分不同的方法。
package Hw4;
abstract class Animal {
abstract public void run();
abstract public void eat();
}
class Elephant extends Animal{
@Override
public void run() {
System.out.println("Elephant is running");
}
@Override
public void eat() {
System.out.println("Elephant is eating");
}
}
class Lion extends Animal{
@Override
public void run() {
System.out.println("Lion is running");
}
@Override
public void eat() {
System.out.println("Lion is eating");
}
}
class Zebra extends Animal{
@Override
public void run() {
System.out.println("Zebra is running");
}
@Override
public void eat() {
System.out.println("Zebra is eating");
}
}
class Test{
public static void main(String[] args) {
Animal elephant = new Elephant();
Animal lion = new Lion();
Animal zebra = new Zebra();
elephant.eat();
elephant.run();
lion.eat();
lion.run();
zebra.eat();
zebra.run();
}
}
(4).一个二维向量由两个分量组成。二维向量的相加和相减等价于对应两个分量的相加和相减。比如两个二维向量(5,2)和(3,-1),它们的和为(8,1),它们的差为(2,3)。编写一个接口Computable,它具有两个抽象方法add和minus。编写一个Vector类,通过Computable接口实现了二维向量的相加和相减。在报告中附上程序截图、运行结果和详细的文字说明。(20分)
设计:将题目中的二维向量拓展到n维向量,其中Vector类中有两个成员变量分别表示向量的维度和向量。
package Hw4;
import java.util.Arrays;
import java.util.Scanner;
interface Computable {
Vector add(Vector addElement);
Vector minus(Vector addElement);
}
public class Vector implements Computable {
int dimension;
int[] element;
public Vector(int dimension) {
this.dimension = dimension;
this.element = new int[dimension];
}
public Vector(int dimension, int[] element) {
this.dimension = dimension;
this.element = new int[dimension];
for (int i = 0; i < dimension; i++) {
this.element[i] = element[i];
}
}
@Override
public Vector add(Vector addElement) {
Vector temp = new Vector(dimension);
for (int i = 0; i < dimension; i++) {
temp.element[i] = this.element[i] + addElement.element[i];
}
return temp;
}
@Override
public Vector minus(Vector addElement) {
Vector temp = new Vector(dimension);
for (int i = 0; i < dimension; i++) {
temp.element[i] = this.element[i] - addElement.element[i];
}
return temp;
}
@Override
public String toString() {
return "Vector{" +
"dimension=" + dimension +
", element=" + Arrays.toString(element) +
'}';
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
System.out.println("请输入维度:");
n = sc.nextInt();
int[] arr1 = new int[n];
int[] arr2 = new int[n];
for (int i = 0; i < n; i++) {
arr1[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
arr2[i] = sc.nextInt();
}
Vector vector1 = new Vector(n,arr1);
Vector vector2 = new Vector(n,arr2);
System.out.println(vector1);
System.out.println(vector2);
System.out.println("两者之和为:");
System.out.println(vector1.add(vector2));
System.out.println("两者之差为:");
System.out.println(vector1.minus(vector2));
}
}
(5).进一步扩展例4-5,创建一个继承了Son类的GrandSon类。有没有办法在GrandSon类中调用它的祖父类Father中被Son类所覆盖的print_house_area方法?在报告中附上程序截图、运行结果和详细的文字说明。(20分)
设计:给父类增加一个调用祖父类的方法,然后子类通过super关键字调用父类的调用父类的父类的方法。
package Hw4;
public class Son extends Father{
double house_area = 120.3;
@Override
public void print_house_area(){
System.out.println("The area of son's house is " + house_area);
super.house_area = 160;
super.print_house_area();
}
public void print_father_house_area(){
super.print_house_area();
}
}
package Hw4;
public class GrandSon extends Son{
public static void main(String[] args) {
GrandSon grandSon = new GrandSon();
grandSon.print_father_house_area();
}
}
package Hw4;
public class Father {
int house_area = 100;
public void print_house_area(){
System.out.println("The area of father's house is " + house_area);
}
}