Java Exercise 14.2 Inheritance

Java Exercise 14.2 Inheritance


Follow the public account "Ambition and Home", reply "11.19" to get the source file

1. Design a student class Student and one of its subclasses Undergraduate, the requirements are as follows:

1. The Student class has name (name) and age (age) attributes, a construction method containing two parameters, used to assign values ​​to the name and age attributes, and a show method to print Student's attribute information.

2. Undergraduate adds a speciality attribute. There is a three-parameter construction method. The first two parameters are used to assign values ​​to the inherited name and age attributes, the third parameter is assigned to speciality, and a show method is used to print Undergraduate attribute information.

3. Create Student and Undergraduate objects in the test class, and call their show methods.

package com.shangjiti.aoian;
public class No1 {
    
    
	public static void main(String[] args) {
    
    
		Student s=new Student("张三",18);
		Undergraduate u=new Undergraduate("李四",25,"计算机软件");
		s.show();
		u.show();
	}
}
class Student{
    
    
	String name;
	int age;
	public Student() {
    
    
		
	}
	public Student(String xm,int nl) {
    
    
		this.name=xm;
		this.age=nl;
	}
	public void show() {
    
    
		System.out.println("我是一名学生,我叫"+name+",我今年"+age+"岁");
	}
}
class Undergraduate extends Student{
    
    
	private String speciality;
	public Undergraduate(){
    
    
	}
	public Undergraduate(String xm,int nl,String zy){
    
    
		super(xm,nl);
		this.speciality=zy;	
	}
	public void show() {
    
    
		System.out.println("我是一名本科生,我叫"+name+",我今年"+age+"岁,我的专业是"+speciality);
	}
}

2. Write a Java application as required:

1. Write a rectangle class Rectangle, including:

(1) Two attributes: the width of the rectangle; the height of the rectangle.

(2) Two construction methods:
a construction method with two parameters, used to
initialize the width and height attributes; a construction method without parameters, the rectangle is initialized to both width and height 10.
(3) Two methods:
area() to find the area of ​​a rectangle

​ The method to find the perimeter of a rectangle perimeter()

2. Write a rectangular class PlainRect with a certain position by inheriting the Rectangle class. The certain position is identified by the coordinates of the upper left corner of the rectangle, including:

(1) Add two attributes: the coordinates startX and startY of the upper left corner of the rectangle.

(2) Two construction methods: the construction method
with 4 parameters is used to initialize the startX, startY, width and height attributes;
the construction method without parameters, the rectangle is initialized to the upper left corner coordinates, length and width are all 0 rectangular;
(3) a method of adding:
determining whether a point is inside the rectangle method isInside (double x, double y) . If it is inside the rectangle, it returns true, otherwise, it returns false.

3. Write a test program for the PlainRect class

(1) Create a rectangular object with coordinates (10, 10) at the upper left corner, length 20, and width 10;
(2) Calculate and print out the area and perimeter of the rectangle;
(3) Determine the point (25.5, 13) Whether it is in the rectangle, and print out relevant information.

package com.shangjiti.aoian;
public class No2 {
    
    
	public static void main(String[] args) {
    
    
		PlainRect r = new PlainRect(20,10,10,10);
		r.area(20,10);
		r.perimeter(20, 10);
		r.isInside(25.5, 13);
	}
}
class Rectangle{
    
    
	double width;
	double height;
	public Rectangle() {
    
    
		this(10,10);
//		width=10;
//		height=10;
	}
	public Rectangle(double width,double height) {
    
    
		this.width=width;
		this.height=height;
	}
	public void area(double width,double height) {
    
    
		double area;
		area=width*height;
		System.out.println("面积:"+area);
	}
	public void perimeter(double width,double height) {
    
    
		double per;
		per=width*2+height*2;
		System.out.println("周长:"+per);
	}
}
class PlainRect extends Rectangle{
    
    
	double startX;
	double startY;
	public PlainRect(double width,double height,double startX,double startY){
    
    	
		super(width,height);
		this.startX=startX;
		this.startY=startY;
	}
	public PlainRect(){
    
    
		this(0,0,0,0);
//	或	super();
//		this.startX=0;
//		this.startY=0;
	}
	public Boolean isInside(double x,double y) {
    
    
		if(x>=startX&&x<=(startX+width)&&y<=startY&&y>(startY-height)){
    
    
			System.out.println("该点在矩形内");
			return true;
		}	
		else {
    
    
			System.out.println("该点不在矩形内");
			return false;
		}
	}
}

1. Define two classes Monkey class and People class. Claim:

1. There is a public void speak() method in the Monkey class, which outputs the message "babble babble..." in the speak method.
2. The People class is a subclass of the Monkey class. The method speak() is overridden in the People class, and the message "Small sample, not bad! I can speak!" is output in the speak method.
3. Add a new method void think() in the People class, and output the message "Don't talk! Think hard!" in the think method.
4. Create object classes of Monkey and People in the main method of the test class to test the functions of these two classes.

package com.shangjiti.aoian;
public class No3 {
    
    
	public static void main(String[] args) {
    
    
		Monkey m=new Monkey();
		People p=new People();
		m.speak();
		p.speak();
		p.think();
	}
}
class Monkey{
    
    
	public void speak() {
    
    
		System.out.println("咿呀咿呀。。。");
	}
}
class People extends Monkey{
    
    
	public void speak() {
    
    
		System.out.println("小样的,不错嘛!会说话了!");
	}
	public void think() {
    
    
		System.out.println("别说话!认真思考!");
	}
}

2. Create classes as required:

1. Create a general bank account account:

(1) Contains attributes:
account name, password, balance.

(2) Included methods:
an account opening method with 3 parameters (user name, account password, account opening amount), account name, password and balance are set at the same time as the account is opened;
one with two parameters (password and transaction amount) Withdrawal method withdraw();
a deposit method with one parameter (transaction amount) deposit();
requires operation information and balance to be displayed after each deposit and withdrawal.

2. Create a VipAccount class:

VipAccount account has one more attribute: card number, which is between 10000-99999; in
addition, VipAccount account can overdraft up to 2,000 yuan (including 2,000) when withdrawing money.

package com.shangjiti.aoian;
public class No4 {
    
    
	public static void main(String[] args) {
    
    
		VipAccount vip=new VipAccount("张三","123",990,77177);
		vip.deposit(10000.0);
		vip.withdraw("123", 12000.0);
	}
}
class Account{
    
    
	String name;
	String password;
	double balance;
	public Account() {
    
    	
	}
	public Account(String xm,String mm,double m) {
    
    
		this.name=xm;
		this.balance=m;
		this.password=mm;	
	}
	public void deposit(double money) 
	{
    
    
		balance=balance+money;
		System.out.println("您好,您的账户已存入"+money+"元,您的账户余额:"+balance+"元。");
	}
	public void withdraw(String mm,double money)
	{
    
    
		if(this.password==mm)
		{
    
    
			if(this.balance>=money)
			{
    
    
				this.balance=this.balance-money;
				System.out.println("您好,您的账户已取出"+money+"元,您的账户余额:"+this.balance+"元。");
			}
			else {
    
    
				balance=balance-money;
				if(balance>=-2000) {
    
    
				System.out.println("您好,您的账户已取出"+money+"元,您的账户透支:"+this.balance+"元。");
				System.out.println("您的账户余额不足!");
				}
				else
					System.out.println("您的账户透支额度不足!");			
			}
		}
		else
			System.out.println("密码错误");
	}
}
class VipAccount extends Account{
    
    
	int id;
	public VipAccount() {
    
    
		
	}
	public VipAccount(String xm, String mm, double m,int id) {
    
    
		super(xm,mm,m);
		this.id=id;
		System.out.println("Vip账户----------");
		System.out.println("开户成功 ! 卡号:"+id+",余额:"+balance);
	}
	public void deposit(double money) {
    
    
		super.deposit(money);
	}
	public void withdraw(String mm,double money) {
    
    
		super.withdraw(mm, money);
	}
}

Guess you like

Origin blog.csdn.net/m0_46653702/article/details/109801418