JAVA day07: Code exercise (inheritance)

Posted by bluemonster on Fri, 24 Dec 2021 17:54:36 +0100

Topic 1: application of inheritance

Define Pet, Cat and Dog
1. Pets have attribute name s and ages
2. Pets have ways to eat, drink and shout
3. Cats have a way to climb trees
4. Dogs have police methods

//Pets
public class Pet {
	public String name;
	public int age;
	
	public void eat(){
		System.out.println(name+"Can eat");
	}
	public void drink(){
		System.out.println(name+"Can drink");
	}
	public void shout(){
		System.out.println(name+"Can call");
	}
}


//Cats
public class Cat extends Pet{
	public void climbTree(){
		System.out.println(super.name+"Can climb trees");
	}
}

//Dogs 
public class Dog extends Pet{
	public void police(){
		System.out.println(super.name+"Will alert");
	}
}

Topic 2: application of method rewriting

Define bank card and credit card classes
Bank cards
>There are balance attributes and deposit and withdrawal methods

Credit card
>Inherited bank cards
>Overdraft limit attribute (initial 1000)
>Rewrite withdrawal method
In the test class, two kinds of cards are used to complete the deposit and withdrawal operation.

//Bank cards
public class YinHangKa {
	public double yuE=500;// balance
	
	public void add(double cun){//deposit
		yuE+=cun;
		System.out.println("Deposit succeeded, and the balance is:"+yuE);
	}
	public void draw(double qu){//withdraw money
		if(yuE>=qu){
			yuE-=qu;
			System.out.println("Withdrawal succeeded, and the balance is:"+yuE);
		}else{
			System.out.println("Insufficient balance, withdrawal failed");
		}
	}
}

//Credit card
public class XinYongKa extends YinHangKa{
	public double touZiE=1000;//line of credit

	@Override
	public void draw(double qu) {
		if((super.yuE+touZiE)>=qu&&super.yuE>=qu){
			super.yuE-=qu;
			System.out.println("Withdrawal succeeded, and the balance is:"+super.yuE+"\t The overdraft amount is:"+touZiE);
		}else if((super.yuE+touZiE)>=qu&&super.yuE<qu){
			touZiE=super.yuE+touZiE-qu;
			super.yuE=0;
			System.out.println("Withdrawal succeeded, and the balance is:"+super.yuE+"\t The overdraft amount is:"+touZiE);
		}else{
			System.out.println("Insufficient balance, withdrawal failed");
		}
	}
}

//Test class
public class TestKa {
	public static void main(String[] args) {
		YinHangKa yhk=new YinHangKa();
		XinYongKa xyk=new XinYongKa();
		System.out.println("The initial balance is:"+yhk.yuE);
		System.out.println("Deposit 600 with bank card");
		yhk.add(600);
		System.out.println("Withdraw 700 with bank card");
		yhk.draw(700);
		System.out.println("Withdraw 1000 with bank card");
		yhk.draw(1000);
		System.out.println("Deposit 100 with a credit card");
		xyk.add(100);
		System.out.println("Withdraw 200 with credit card");
		xyk.draw(200);
		System.out.println("Withdraw 1000 with credit card");
		xyk.draw(1000);
	}
}

Operation results:

(Advanced) topic 3: define bank cards and credit cards to complete the following functions

1. Complete the deposit and withdrawal method of bank card
Bank cards have balance, card number, account owner and other attributes
There are methods of deposit and withdrawal (prompt when the balance is insufficient)
2. Complete the withdrawal method of credit card
The credit card has an additional overdraft limit attribute
Rewrite the withdrawal method of credit card (prompt if the balance is insufficient)
3. Write test cases to test the deposit and withdrawal function of bank card and credit card respectively (the balance is displayed after deposit and withdrawal)

//Bank cards
public class YinHangKa {
	public double yuE;// balance
	public String kaHao;//Card number
	public String huZhu;//a householder
	public void add(double cun){//deposit
		yuE+=cun;
		System.out.println("Card number:"+kaHao+"\t The head of household is:"+huZhu);
		System.out.println("Deposit succeeded, balance:"+yuE);
	}
	public void draw(double qu){//withdraw money
		if(yuE>=qu){
			yuE-=qu;
			System.out.println("Card number:"+kaHao+"\t The head of household is:"+huZhu);
			System.out.println("Withdrawal succeeded, and the balance is:"+yuE);
		}else{
			System.out.println("Insufficient balance, withdrawal failed");
		}
	}
}

//Credit card
public class XinYongKa extends YinHangKa{
	public double touZiE=1000;//line of credit

	@Override
	public void draw(double qu) {
		if((super.yuE+touZiE)>=qu&&super.yuE>=qu){
			super.yuE-=qu;
			System.out.println("Card number:"+super.kaHao+"\t The head of household is:"+super.huZhu);
			System.out.println("Withdrawal succeeded, and the balance is:"+super.yuE+"\t The overdraft amount is:"+touZiE);
		}else if((super.yuE+touZiE)>=qu&&super.yuE<qu){
			touZiE=super.yuE+touZiE-qu;
			super.yuE=0;
			System.out.println("Card number:"+super.kaHao+"\t The head of household is:"+super.huZhu);
			System.out.println("Withdrawal succeeded, and the balance is:"+super.yuE+"\t The overdraft amount is:"+touZiE);
		}else{
			System.out.println("Insufficient balance, withdrawal failed");
		}
	}
}

//Test class
public class TestKa {
	public static void main(String[] args) {
		YinHangKa yhk=new YinHangKa();
		yhk.yuE=500;
		yhk.kaHao="12345678";
		yhk.huZhu="Chun Chun";
		XinYongKa xyk=new XinYongKa();
		xyk.yuE=1000;
		xyk.kaHao="87654321";
		xyk.huZhu="Liu Liu";
		System.out.println("The initial bank card balance is:"+yhk.yuE);
		System.out.println("Deposit 600 with bank card");
		yhk.add(600);
		System.out.println("Withdraw 700 with bank card");
		yhk.draw(700);
		System.out.println("Withdraw 1000 with bank card");
		yhk.draw(1000);
		System.out.println("The initial bank card balance is:"+yhk.yuE);
		System.out.println("Deposit 100 with a credit card");
		xyk.add(100);
		System.out.println("Withdraw 200 with credit card");
		xyk.draw(200);
		System.out.println("Withdraw 1000 with credit card");
		xyk.draw(1000);
	}
}

Operation results:

(Advanced) topic 4: realizing the printing function of employee salary accounting through inheritance

1. There are employee class (parent class), general employee class (subclass of Employee class) and department manager class (subclass of Employee class)
Ordinary employee salary = single day salary * days * grade (1); department manager salary = 1000 + single day salary * days * grade (1.2).
2. Employee class properties and methods
Attribute: single day salary, working days, salary grade, static constant
Method: print salary method
3. Subclass rewrites the method of printing salary according to the characteristics of this class
4. Write test classes and output all kinds of wages (at the same time, verify the rewriting rules of the code)

//Employee category
public class Staff {
	//Attribute: single day salary, working days, salary grade, static constant
	public double dailyWage;//Daily wage
	public int workingDays;//Working days
	public double wageBracket;//Wage scale
	//Method: print salary method
	public void printWages(){
		double wage=dailyWage*workingDays*wageBracket;
		System.out.println("The employee's salary is:"+wage);
	}
}

//Ordinary employees
public class GeneralStaff extends Staff{

	@Override
	public void printWages() {
		super.wageBracket=1;
		super.printWages();
	}
}

//Project Manager
public class Manager extends Staff{

	@Override
	public void printWages() {
		wageBracket=1.2;
		double wage=1000+dailyWage*workingDays*wageBracket;
		System.out.println("The salary of the project manager is:"+wage);
	}
}

//Test class
public class Test {
	public static void main(String[] args) {
		GeneralStaff g=new GeneralStaff();
		Manager m=new Manager();
		
		g.dailyWage=100;
		m.dailyWage=500;
		
		g.workingDays=10;
		m.workingDays=20;
		
		g.printWages();
		m.printWages();
	}
}

Operation results:

(elevation) topic 5: comprehensive application 1

1. Mrs. Zhang has two cats: one is Xiaohua, 3 years old, white. The other is Xiaobai, 3 years old this year. Please write a program to display the name, age and color of the kitten when the user enters the kitten's name. If the kitten name entered is wrong, it will be displayed: Mrs. Zhang doesn't have this cat.
Implementation idea:
1) Create a cat class and define three variables: name, age, color, and all variables are private
2) Provide set and get methods for these three variables
3) Define how to print information
4) Create test class
5) Create 2 cats and assign values in set mode. The information is consistent with the meaning of the question
6) Create a keyboard entry object and enter the cat's name
7) Judge whether the names entered on the keyboard are consistent with the names of the two cats. If they are consistent, print the information of the cat,
If there is no cat in the inconsistent prompt, please re-enter
1. Scoring criteria:
(1) Create cats correctly (10 points)
(2) Correctly define private attributes and methods (10 points)
(3) Correctly define the printing method (10 points)
(4) Create a test class and create two objects (10 points)
(5) Correctly input cat information (10 points)
(6) Judge whether two cats are the same cat (10 points)
(7) Printout information (10 points)
(8) Call methods correctly and output information (10 points)
(9) Add notes (5 points)

//Cats
public class Cat {
	private String name;
	private int age;
	private String colour;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getColour() {
		return colour;
	}
	public void setColour(String colour) {
		this.colour = colour;
	}
	public Cat() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Cat(String name, int age, String colour) {
		super();
		this.name = name;
		this.age = age;
		this.colour = colour;
	}
	public void print(){
		System.out.println("full name:"+name+"\t Age:"+age+"\t Color:"+colour);
	}
}

//Test class
public class TestCat {
	public static void main(String[] args) {
		Cat cat1=new Cat();
		cat1.setName("floret");
		cat1.setAge(3);
		cat1.setColour("Decor");
		Cat cat2=new Cat();
		cat2.setName("Xiaobai");
		cat2.setAge(3);
		cat2.setColour("white");

		Scanner sc=new Scanner(System.in);
		while(true){
			System.out.println("Please enter the name of the cat:");
			String name=sc.next();
			if(cat1.getName().equals(name)){
				cat1.print();
				break;
			}else if(cat2.getName().equals(name)){
				cat2.print();
				break;
			}else{
				System.out.println("There is no cat, please re-enter");
			}
		}
	}
}

Operation results:

(elevation) topic 6: comprehensive application 2

1. Complete the following topics as required:
Case rendering:

 

Case Title Description:
A. Write two classes to inherit the transportation tool class: Truck class and train class
requirement:
(1) Add two variables and encapsulate them.
(2) Add nonparametric construction method and parametric construction method, and assign values to two variables of weight and distance through parametric construction
(3) The method of overriding the parent class to calculate freight is as follows:
a. Truck: Freight = weight x distance x120. When the distance is greater than 1000 (km) or the weight is greater than 60(t), the rejection time method returns - 1.
b. Train: when the distance is within 900(km) (including 900), freight = weight x distance X250; when the distance is greater than 900(km), freight = weight x distance x300.
B. Write test classes, create truck and train class objects, and call the freight calculation method
1. Scoring criteria:
(1) Correctly define tool classes (10 points)
(2) Correctly define the train category (10 points)
(3) Correctly define the truck category (10 points)
(4) Truck override parent method (10 points)
(5) Train override parent method (10 points)
(6) Correctly define various methods (10 points)
(7) Create test classes, create objects and call methods (10 points)
(8) Output all variable information (10 points)
(9) Add notes (5 points)

Topics: Java Eclipse