Java Zero Basic Introduction: Practical Tutorial (2)

Blogger profile:
Blog homepage: Java knowledge sharing blogger
Java Zero Basic Introduction Column: Java Zero Basic Introduction Column
Java Communication Community: Flying Bird Community
Welcome to read, if the article is helpful to you, like it and support it!

Recommended reading
Java Zero Basic Introduction: Introduction (1)
Java Zero Basic Introduction: Practical Tutorial (2)

Problem Description:

Define an abstract graphic class, which includes abstract methods for finding perimeter and area. The graphic class derives two subclasses: circle and square. And write test classes.

Define an abstract type shape, define the class name circular, define the radius, constructor, computer area formula and perimeter, the same is true for square, use c2.print() to call the method to output the result

package S3;

public class S1 {
    
    

	public static void main(String[] args) {
    
    
		
		circular c1 =new circular(4);//传递实参
		c1.print();
		Square c2 = new Square(5);
		c2.print();//调用方法
		

	}

}
abstract class Shape
{
    
    
	abstract double getArea();
	abstract double getperimeter();
}
class circular extends Shape //圆形
{
    
    
	 double rear;
	 
	public circular(double r)//构造函数
	{
    
    
		rear=r;
		
	}
	public double getArea() //面积
	{
    
    
		
		return 3.14*rear*rear;
		
	}
	public double getperimeter() //周长
	{
    
    
		
		return 2*3.14*rear;
		
	}
	public void print()//输出圆形的面积和周长
	{
    
    
		System.out.println("面积是:"+getArea());
		System.out.println("周长是:"+getperimeter());
	}
}
class Square extends Shape  //正方形
{
    
    
	double leght;
	 
	public Square(double l)
	{
    
    
		leght=l;
		
	}
	public double getArea() //面积
	{
    
    
		
		return leght*leght;
		
	}
	public double getperimeter() //周长
	{
    
    
		
		return 2*(leght+leght);
		
	}
	
	public void print()
	{
    
    
		System.out.println("面积是:"+getArea());
		System.out.println("周长是:"+getperimeter());
	}
}

insert image description here

Problem Description:

2. Suppose the Bank already has a general method of calculating interest by the whole year, in which year can only be a positive integer, such as the method of calculating by the whole year:
double comperterInterest()
{ interest=year 0.35 savedMoney; return interest } Construction Bank ConstructionBank is The subclass of Bank is going to overwrite the inherited member variable year, and overwrite the method of calculating interest, that is, declare a double-type year variable by itself. For example, when the value of year is 5.216, it means that the interest will be calculated for 5 years and 216 days. But I hope to first calculate the interest for 5 whole years according to Bank's method comperterInterest(), and then calculate the interest for 216 days by myself. Then, CCB must assign the integer part of 5.216 to the hidden year, and let super call the hidden method of calculating interest by the whole year. Suppose the daily interest is 0.0001 when calculating interest by day.




Construction Bank ConstructionBank is a subclass of Bank, ready to override the inherited member variable year, and override the method of calculating interest. The same constructor, passing parameters, calculating interest, and calling methods.

package S3;

public class S2 {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Bank C1 =new ConstructionBank();
		C1.dispoit(10000.75, 8);
		//C1.computerInterest();
		System.out.println(C1.computerInterest());
		
		

	}

}
class Bank{
    
    
	int year;//年份
	double interest;//利息
	double saveMoney;//保存货币

void dispoit(double money,int year) {
    
    
	saveMoney=money;
	this.year=year;

}
double computerInterest(){
    
    
	
    interest=year*0.35*saveMoney;
    return interest;
}

}
class ConstructionBank extends Bank{
    
    
	double year;
	double price;
	double computerInterest()
	{
    
    
		price=(year-(int)year)*0.0001*saveMoney+super.computerInterest();
		return  price;
		
	
	}
	
}

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/A6_107/article/details/124064735