Java Experiment 7, Interface and Polymorphism

Keywords: Java Programming

I. Experimental purposes

1. Understanding interface and polymorphism;

2. Master interface callback technology;

3. Master the idea of Interface-oriented programming.

II. Contents and requirements of the experiment

The truck loads a batch of goods, which are made up of TV sets, computers and washing machines. The truck needs to calculate the weight of the whole batch of goods. Write programs that meet the following requirements:

1. Define an interface that contains an abstract method for calculating the weight of goods.

2. Define the classes for computing televisions, computers and washing machines that implement the above interfaces.

3. Define a truck class, in which an array member variable is defined to represent the goods it loads, and provide a method for calculating the weight of the whole shipment.

4. In the main category, the truck is simulated to load N cargo, each cargo is randomly classified and the total weight of the cargo is exported.

III. Experimental process and results

 Goods.java
public interface Goods {
	double cal_weight();//An abstract method for calculating the weight of goods
}
 TV.java
public class TV implements Goods{
	double singleTVWeight;//Single TV weight
	double single_weight[] = {5.5, 3.1, 4.1, 6};//Optional TV weight

	public TV(int randomInt) {
		this.singleTVWeight = single_weight[randomInt];
	}//Construct method, randomly select a weight from single_weight [] and set it

	public double cal_weight() {
		return singleTVWeight;
	}//The method of calculating the weight of TV set and returning the weight of TV set
}
 Computer.java
public class Computer implements Goods{
	double singleComputerWeight;//Single computer weight
	double single_weight[] = {1.5, 1.9, 2.0, 2.4};//Optional computer weight

	public Computer(int randomInt) {
		this.singleComputerWeight = single_weight[randomInt];
	}//Construct method, randomly select a weight from single_weight [] and set it

	public double cal_weight() {
		return singleComputerWeight;
	}//The method of calculating the weight of a computer and returning it to the computer
}
 WashingMachine.java
public class WashingMachine implements Goods{
	double singleWMWeight;//Single washing machine weight
	double single_weight[] = {13.5, 11.9, 20, 12.4};//Optional washing machine weight

	public WashingMachine(int randomInt) {
		this.singleWMWeight = single_weight[randomInt];
	}//Construct method, randomly select a weight from single_weight [] and set it

	public double cal_weight() {
		return singleWMWeight;
	}//Method of calculating the weight of the washing machine, returning the weight of the washing machine
}
 Truck.java
import java.util.Random;

public class Truck {
	static Goods[] goods;//Group membership variable, representing the goods it loads
	
	static void show_weight(Goods[] goods) {
		double sum = 0;//Total cargo weight, initial 0

		for (Goods g: goods) {
			sum += g.cal_weight();
		}//Calculate the total weight of the goods
		
		System.out.printf("The total weight of the goods loaded by the truck is:%.2fkg\n", sum);
	}
	
	public static void main(String[] args) {
		Random random = new Random();//Creating random Number Generator
		Truck.goods = new Goods[50];//Set N to 50.

		//Calculate the total number of computers, washing machines and TV sets
		int countTV = 0, countWM = 0, countCom = 0;
		//Calculate the total weight of computers, washing machines and TV sets
		double TVWeight = 0, WMWeight = 0, ComWeight = 0;
		
		for (int i = 0; i < Truck.goods.length; i++) {
			int selection = random.nextInt(3);
			int index = random.nextInt(4);
			
			if (selection == 0) {//If the random number is 0, the type of goods is "computer"
				Truck.goods[i] = new Computer(index);
//Set the first item in the array to "Computer"
				countCom++;//Total number of computers plus 1
				ComWeight += Truck.goods[i].cal_weight();
			} else if (selection == 1) {//If the random number is 1, the type of goods is "washing machine"
				Truck.goods[i] = new WashingMachine(index);
//Set the first item in the array to "washing machine"
				countWM++;//Total number of washing machines plus 1
				WMWeight += Truck.goods[i].cal_weight();
			} else if (selection == 2) {//If the random number is 2, the type of goods is "TV set".
				Truck.goods[i] = new TV(index);
//Set the first item in the array to "TV"
				countTV++;//Total TV set plus 1
				TVWeight += Truck.goods[i].cal_weight();
			} 
		}
		
		System.out.printf("Total number of computers:%d, Gross weight:%.2fkg\n", countCom, ComWeight);
		System.out.printf("Total number of washing machines:%d, Gross weight:%.2fkg\n", countWM, WMWeight);
		System.out.printf("Total number of TV sets:%d, Gross weight:%.2fkg\n", countTV, TVWeight);
		System.out.println("The total number of goods loaded by trucks is:" + Truck.goods.length);
		Truck.show_weight(Truck.goods);
	}
}	

IV. Problems and Experiences in the Experiments

1. Through this experiment, I have a deeper understanding of Interface-oriented programming.

(1) Interface-oriented programming should follow the open-close principle, so that the modification of the interface can be "closed", otherwise once the interface is modified (such as adding an abstract method), the class that implements the interface should be modified; and the class that implements the interface should be opened, so that the function of the program can be easily expanded.

(2) java only allows single inheritance, and each subclass can only have one parent class; with interfaces, each class can implement multiple interfaces.

2. In this experiment, both interface callbacks and object arrays are used, and I have a deeper understanding of object arrays. For example:

(1)A[] a;

(2)a = new A[10];

(3)a[i] = new A();

The first two steps only define that array a has 10 elements, each of which is an object of class A. These objects are currently empty objects. After the third step, each element in the array is the real object.

Posted by ICKelly on Wed, 15 May 2019 17:40:38 -0700