Exercises on the inheritance of the three characteristics of Java

Keywords: Attribute Java

[exercises] 1. Inheritance:
Define a base class (parent class) named Vehicles, which should contain the member properties brand and color of String type, the member methods run (driving, display "I have started" in the console) and showInfo (display information, display trademark and color in the console), and write a construction method to initialize its member properties Sex.
Write Car class to inherit from Vehicles class, add int member attribute seats, add member method showCar (display Car information on console), and write construction method. The Truck class inherits from the Vehicles class, increases the float type member attribute load, and adds the member method showTruck (displaying the Truck information in the console), and writes the construction method. Test the above categories in the main method.
Code implementation:
1. Vehicles class:

package home.work;

public class T1_Vehicles {
	String brand;// trademark
	String color;

	public T1_Vehicles() {
	}

	public T1_Vehicles(String brand, String color) {
		this.brand = brand;
		this.color = color;
	}

	public void run() {
		System.out.println("I've started!");
	}

	public void showInfo() {
		System.out.println("The trademark is:" + brand + ";The color is:" + color);
	}
}

2. Car:

package home.work;

public class T1_Car extends T1_Vehicles {
	int seats;// seat

	public T1_Car() {
	}

	public T1_Car(int seats, String brand, String color) {
		super.brand = brand;
		super.color = color;
		this.seats = seats;
	}

	public void showCar() {
		System.out.println("The trademark is:" + brand + ";The color is:" + color + ";Seats are" + seats + "individual");
	}
}

3. Truch class:

package home.work;

public class T1_Truck extends T1_Vehicles {
	float load;// load

	public T1_Truck(float load, String brand, String color) {
		super.brand = brand;
		super.color = color;
		this.load = load;
	}

	public void showTruck() {
		System.out.println("The trademark is:" + brand + ";The color is:" + color + ";The load is:" + load + "ton");
	}
}

4. Text test class:

package home.work;

public class T1_Text {
	public static void main(String[] args) {
		T1_Vehicles v = new T1_Vehicles("Audi", "white");
		v.showInfo();
		T1_Car c = new T1_Car(5, "Benz", "black");
		c.showCar();
		T1_Truck t = new T1_Truck(20.5f, "Flanders", "blue");
		t.showTruck();
	}
}

Operation result display:

[exercises] 2. Inheritance:
Write a class Calculate1 to implement two operations: addition and subtraction, and then write another derived class Calculate2 to implement two operations: multiplication and division.
Code implementation:
1. Calculate1 class:

	package home.work;
	public class T2_Calculate1 {
		public double add(double a, double b) {
			System.out.println(a + "+" + b + "=" + (a + b));
			return a + b;
		}
	
		public double app(double a, double b) {
			System.out.println(a + "-" + b + "=" + (a - b));
			return a - b;
		}
	}

2. Calculate2 class:

package home.work;

public class T2_Calculate2 extends T2_Calculate1 {
	public double cheng(double a, double b) {
		System.out.println(a + "*" + b + "=" + (a * b));
		return a * b;
	}

	public double chu(double a, double b) {
		System.out.println(a + "/" + b + "=" + (a / b));
		return a / b;
	}
}

3. Text test class:

package home.work;

public class T2_Text {
	public static void main(String[] args) {
		T2_Calculate2 c = new T2_Calculate2();
		c.add(10.1, 9.9);
		c.app(10.1, 5.1);
		c.cheng(5, 5);
		c.chu(10, 2);
	}
}

Operation result display:

[exercises] 3. Inheritance:
Establish three categories: residents, adult adult, Official. Residents include ID card number, name and date of birth, while adults inherit from residents, mostly including education background and occupation; officials inherit from adults, mostly including parties and positions. The fields of each class are required to provide the function of data input and output in the form of properties. Setter getter method
Code implementation:
1. Residents of JuMin class:

package home.work;

public class T4_JuMin {
	private String id;
	private String name;
	private String date;

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getDate() {
		return date;
	}
}

2. Adult adult class:

package home.work;

public class T4_Adult extends T4_JuMin {
	private String xunLi;
	private String zhiYe;

	public void setXunLi(String xunLi) {
		this.xunLi = xunLi;
	}

	public String getXunLi() {
		return xunLi;
	}

	public void setZhiYe(String zhiYe) {
		this.zhiYe = zhiYe;
	}

	public String getZhiYe() {
		return zhiYe;
	}
}

3. Official class:

package home.work;

public class T4_Official extends T4_Adult {
	private String dangPai;
	private String zhiWu;

	public void setDangPai(String dangPai) {
		this.dangPai = dangPai;
	}

	public String getDangPai() {
		return dangPai;
	}

	public void setZhiWu(String zhiWu) {
		this.zhiWu = zhiWu;
	}

	public String getZhiWu() {
		return zhiWu;
	}
}

4. Test class Text class:

package home.work;

public class T4_Text {
	public static void main(String[] args) {
		T4_JuMin j = new T4_JuMin();
		j.setName("Zhang San");
		j.setId("230125198812210124");
		j.setDate("1988 December 21, 2010");
		System.out.println(j.getName() + "\t" + j.getId() + "\t" + j.getDate());
		T4_Adult a = new T4_Adult();
		a.setXunLi("Undergraduate");
		a.setZhiYe("Teacher");
		a.setName("Zhang San");
		a.setId("230125198812210124");
		a.setDate("1988 December 21, 2010");
		System.out.println(
				a.getName() + "\t" + a.getId() + "\t" + a.getDate() + "\t" + a.getXunLi() + "\t" + a.getZhiYe());
		T4_Official o = new T4_Official();
		o.setDangPai("The Communist Party");
		o.setZhiWu("hand over Java");
		o.setXunLi("Undergraduate");
		o.setZhiYe("Teacher");
		o.setName("Zhang San");
		o.setId("230125198812210124");
		o.setDate("1988 December 21, 2010");
		System.out.println(o.getName() + "\t" + o.getId() + "\t" + o.getDate() + "\t" + o.getXunLi() + "\t"
				+ o.getZhiYe() + "\t" + o.getDangPai() + "\t" + o.getZhiWu());
	}
}

Operation result display:

[exercises] 4. Overwrite (rewrite @ Override), inherit
Build an Auto class, including the number of tires, Car color, body weight, speed and other member variables. And create instances through different construction methods. At least: the Car can accelerate, decelerate and stop. Then define a Car class Car, inherit Auto, add air conditioning, CD and other member variables, override (rewrite) the acceleration and deceleration methods.
Code implementation:
1. Auto class:

package home.work;

public class T5_Auto {
	int wheel;
	String color;
	double weight;// Unit: ton
	double speed;// Unit: km / h

	public T5_Auto() {
	}

	public T5_Auto(int wheel, String color, double weight, double speed) {
		this.color = color;
		this.speed = speed;
		this.weight = weight;
		this.wheel = wheel;
	}

	public void showInfo() {
		System.out.println("The car has" + wheel + "A wheel," + "Color is" + color + ",Weight is" + weight + "ton,The current speed is" + speed + "Kilometer/Hour.");
	}

	public void speedAdd(double speedAdd) {
		if (speed + speedAdd <= 120) {
			speed += speedAdd;
			System.out.println("Accelerate success! accelerate" + speedAdd + "Kilometer/hour" + ",The current speed is:" + speed + "Kilometer/hour");
		} else {
			System.out.println("Maximum acceleration exceeded,Acceleration failed!");
		}
	}

	public void speedApp(double speedAdd) {
		if (speed - speedAdd >= 0) {
			speed -= speedAdd;
			System.out.println("Decelerate successfully! Slow down" + speedAdd + "Kilometer/hour" + ",The current speed is:" + speed + "Kilometer/hour");
		} else {
			System.out.println("Maximum deceleration exceeded,Deceleration failed!");
		}
	}

	public void park() {
		speed = 0;
		System.out.println("Current speed: 0 km/hour,Parking successful!");
	}
}

2. Car:

package home.work;

public class T5_Car extends T5_Auto {
	String kongTiao;
	String CD;

	public T5_Car() {
	}

	public T5_Car(String kongTiao, String CD, int wheel, String color, double weight, double speed) {
		super.color = color;
		super.speed = speed;
		super.weight = weight;
		super.wheel = wheel;
		this.CD = CD;
		this.kongTiao = kongTiao;
	}

	public void showInfo() {
		System.out.println("The car has" + wheel + "A wheel," + "Color is" + color + ",Weight is" + weight + "ton,The current speed is" + speed + "Kilometer/hour,"
				+ "Air conditioning in" + kongTiao + "state," + "CD Be in" + CD + "Status.");
	}

	@Override
	public void speedAdd(double speedAdd) {
		if (speed + speedAdd <= 200) {
			speed += speedAdd;
			System.out.println("Accelerate success! accelerate" + speedAdd + "Kilometer/hour" + ",The current speed is:" + speed + "Kilometer/hour");
		} else {
			System.out.println("Maximum acceleration exceeded,Acceleration failed!");
		}
	}

	@Override
	public void speedApp(double speedAdd) {
		if (speed - speedAdd >= 0) {
			speed -= speedAdd;
			System.out.println("Decelerate successfully! Slow down" + speedAdd + "Kilometer/hour" + ",The current speed is:" + speed + "Kilometer/hour");
		} else {
			System.out.println("Maximum deceleration exceeded,Deceleration failed!");
		}
	}
}

3. Test class Text class:

package home.work;

public class T5_Text {
	public static void main(String[] args) {
		T5_Auto a = new T5_Auto(10, "blue", 10.5, 50.5);
		a.showInfo();
		a.speedAdd(30.5);
		a.speedApp(30.5);
		a.speedAdd(100);
		a.speedApp(100);
		a.park();
		System.out.println("=====================================");
		T5_Car c = new T5_Car("work", "Close", 4, "Red", 2.5, 120);
		c.showInfo();
		c.speedAdd(50.5);
		c.speedApp(50.5);
		c.speedAdd(80.1);
		c.speedApp(120.1);
		c.park();
	}
}

Operation result display:

[exercises] 5. Class continuation, super
1) Write a class simulation Account named Account. The properties and methods of this class are shown in the following figure. This class includes the following attributes: Account id, balance, annual interest rate; methods included: accessor method (getter and setter methods), return monthly interest rate method getMonthlyInterest(), withdraw method withdraw(), deposit method deposit().
Account
private int id
private double balance
private double annualInterestRate
public Account (int id, double balance, double annualInterestRate )
public int getId()
public double getBalance()
public double getAnnualInterestRate()
public void setId( int id)
public void setBalance(double balance)
public void setAnnualInterestRate(double annualInterestRate)
public double getMonthlyInterest()
public void withdraw (double amount)
public void deposit (double amount)
Write a user program to test the Account class. In the user program, create an Account object with Account 1122, balance 20000 and annual interest rate of 4.5%. Withdraw 30000 yuan using the method of withdraw and print the balance.
Then withdraw 2500 yuan with the method of withdraw, deposit 3000 yuan with the method of deposit, and print the balance and monthly interest rate.

Prompt: in the withdrawal method, it is necessary to judge whether the user balance can meet the withdrawal amount requirements, and if not, give a prompt.
The operation results are shown as follows:

2) , create a subclass of Account class CheckAccount to represent overdrawable Account, in which an attribute overdraft is defined to represent overdrawable limit. Rewrite the withdraw method in the CheckAccount class. The algorithm is as follows:
If (withdrawal amount < account balance),
Direct withdrawal
If (withdrawal amount > account balance),
Calculate the amount to be overdrawn
Determine whether overdraft is enough to meet the overdraft needs, if it can
Change the account balance to 0 to offset the overdraft amount
If not
Prompt the user to exceed the limit of the permeable amount

Requirement: write a user program to test the CheckAccount class. In the user program, create a CheckAccount object with an account number of 1122, a balance of 20000, an annual interest rate of 4.5%, and an overdraft limit of 5000 yuan.
Withdraw 5000 yuan by using the method of withdraw, and print the account balance and the permeable amount.
Then withdraw 18000 yuan by using the method of withdraw, and print the account balance and the permeable amount.
Then use the method of withdraw to withdraw 3000 yuan, and print the account balance and the permeable amount.

Tips:
(1) The construction method of the subclass CheckAccount needs to initialize all the three properties inherited from the parent class and the properties of the subclass itself.
(2) the attribute balance of parent class Account is set to private, but in the withdraw method of subclass CheckAccount, its value needs to be modified. Therefore, super.setBalance(super.getBalance() - amount) is well understood. The meaning of this sentence uses getter and setter methods to call parent class member variables in the subclass coverage method.
1. Account class:

package home.work;

public class T6_Account {
	private int id;
	private double balance;
	private double annualInterestRate;

	public T6_Account() {

	}

	public T6_Account(int id, double balance, double annualInterestRate) {
		this.id = id;
		this.balance = balance;
		this.annualInterestRate = annualInterestRate;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = annualInterestRate;
	}

	public int getId() {
		return id;
	}

	public double getBalance() {
		return balance;
	}

	public double getAnnualInterestRate() {
		return annualInterestRate;
	}

	public double getMonthlyInterest() {
		return annualInterestRate / 12;
	}

	public void withdraw(double amount) {
		if (amount <= balance) {
			balance -= amount;
			System.out.println("Successful withdrawals,Withdrawal amount " + amount + " element");
			System.out.println("The balance is:" + balance + "element");
		} else {
			System.out.println("Sorry, your credit is running low,Withdraw money" + amount + "Yuan failed!");
			System.out.println("The balance is:" + balance + "element");
		}
	}

	public void deposit(double amount) {
		balance += amount;
		System.out.println("Deposit success,Deposit amount " + amount + " element");
		System.out.println("The balance is:" + balance + "element");
	}
}

2. Test class Text1 class:

package home.work;

public class T6_Text1 {
	public static void main(String[] args) {
		T6_Account a = new T6_Account();
		a.setId(1122);
		a.setBalance(20000);
		a.setAnnualInterestRate(0.045);
		a.withdraw(30000);
		System.out.println("==================");
		a.withdraw(2500);
		System.out.println("==================");
		a.deposit(3000);
		System.out.println("==================");
		double monthlyInterest = a.getMonthlyInterest();
		System.out.println("The monthly interest rate is:"+monthlyInterest);
	}
}

3. CheckAccount class:

package home.work;

public class T6_CheckAccount extends T6_Account {
	private double overdraft;// Overdraft limit

	public T6_CheckAccount() {
	}
	
	public void setOverdraft(double overdraft) {
		this.overdraft = overdraft;
	}

	public double getOverdraft() {
		return overdraft;
	}

	public void withdraw(double amount) {
		if (amount <= super.getBalance()) {
			super.setBalance(super.getBalance() - amount);
			System.out.println("Successful withdrawals,Withdrawal amount " + amount + " element");
			System.out.println("The balance is:" + super.getBalance() + "element," + "The overdraft balance is:" + overdraft + "Yuan.");
		} else {
			if (amount - overdraft <= super.getBalance()) {
				overdraft -= (amount - super.getBalance());
				System.out.println("Successful withdrawals,Withdrawal amount " + amount + " element");
				System.out.println("The account is overdrawn and the overdraft amount is:" + (amount - super.getBalance()) + "element," + "The overdraft balance of the account is:" + overdraft + "Yuan.");
				super.setBalance(0); 
			} else {
				if (super.getBalance() > 0) {
					System.out.println("Sorry, your credit is running low,Withdraw money" + amount + "Yuan failed!");
					System.out.println("The balance is:" + super.getBalance() + "element," + "The overdraft balance is:" + overdraft + "element," + "Total:"
							+ (overdraft + super.getBalance()) + "Yuan.");
				} else {
					System.out.println("Balance: 0,Insufficient overdraft balance,Withdraw money" + amount + "Yuan failed!");
					System.out.println("The overdraft balance is" + overdraft + "Yuan.");
				}
			}
		}
	}
}

4. Test class Text2:

package home.work;

public class T6_Text2 {
	public static void main(String[] args) {
		T6_CheckAccount c = new T6_CheckAccount();
		c.setId(1122);
		c.setBalance(20000);
		c.setAnnualInterestRate(0.045);
		c.setOverdraft(5000);
		c.withdraw(5000);
		System.out.println("======================");
		c.withdraw(18000);
		System.out.println("======================");
		c.withdraw(3000);
	}
}

The operation results are as follows:
Text1 running results show:

Text2 operation results show:

Published 10 original articles, praised 0, visited 91
Private letter follow

Posted by en on Fri, 07 Feb 2020 04:39:29 -0800