1, Height and weight problems of different nationalities
Write a program to simulate that Chinese and Americans are human; Beijingers are Chinese. In addition to the main class, there are four classes in the program: People, ChinaPeople, American People and Beijing People. The requirements are as follows:
(1) The People class has protected double member variables height and weight, and public void speakHello(), public void averageHeight() and public void averageWeight() methods.
(2) ChinaPeople class is a subclass of People, and the public void chinaGongfu() method is added. ChinaPeople is required to override the public void speakHello(), public void averageHeight() and public void averageWeight() methods of the parent class.
(3) AmericanPeople class is a subclass of People, and the public void americanBoxing() method is added. Americanpipe is required to override the public void speakHello(), public void averageHeight() and public void averageWeight() methods of the parent class.
(4) The BeijingPeople class is a subclass of ChinaPeople. A new public void beijingOpera() method is added. ChinaPeople is required to override the public void speakHello(), public void averageHeight() and public void averageWeight() methods of the parent class.
The UML diagrams of People, ChinaPeople, AmericanPeople and Beijing People are shown in the following figure:
People class:
public class People { protected double height; protected double weight; public void speakHello(){ System.out.printf("people say hello"+"\n"); } public void averageHeight(){ System.out.printf("Average height of ordinary people:"+this.height+"\n"); } public void averageWeight(){ System.out.printf("Average weight of ordinary people:"+this.weight+"\n"); } }
ChinaPeople class:
public class ChinaPeople extends People { //New method public void chinaGongfu(){ System.out.printf("chinaGongfu yyds!"+"\n"); } //override method public void speakHello(){ System.out.printf("chinese people say 'nihao'."+"\n"); } //override method public void averageHeight(){ System.out.printf("Average height of Chinese people:"+this.height+"\n"); } //override method public void averageWeight(){ System.out.printf("Average weight of Chinese:"+this.weight+"\n"); } }
Americanpipe class:
public class AmericanPeople extends People { //New method public void americanBoxing(){ System.out.printf("American Boxing"+"\n"); } //override method public void speakHello(){ System.out.printf("American people say 'hello'."+"\n"); } //override method public void averageHeight(){ System.out.printf("Average American height:"+this.height+"\n"); } //override method public void averageWeight(){ System.out.printf("Average American weight:"+this.weight+"\n"); } }
BeijingPeople class:
public class BeijingPeople extends ChinaPeople { //New method public void beijingOpera(){ System.out.printf("Beijing Opera"+"\n"); } //override method public void speakHello(){ System.out.printf("Beijingers say hello:'Did you eat'"+"\n"); } //override method public void averageHeight(){ System.out.printf("Average height of Beijingers:"+this.height+"\n"); } //override method public void averageWeight(){ System.out.printf("Average weight of Beijingers:"+this.weight+"\n"); } }
Text test main class:
public class Text { public static void main(String args[]){ People people1=new People(); people1.height=170; people1.weight=60; people1.speakHello(); people1.averageHeight(); people1.averageWeight(); ChinaPeople chinapeople1=new ChinaPeople(); chinapeople1.height=165; chinapeople1.weight=55; chinapeople1.speakHello(); chinapeople1.chinaGongfu(); chinapeople1.averageHeight(); chinapeople1.averageWeight(); BeijingPeople beijingpeople1=new BeijingPeople(); beijingpeople1.height=165; beijingpeople1.weight=55; beijingpeople1.speakHello(); beijingpeople1.beijingOpera(); beijingpeople1.averageHeight(); beijingpeople1.averageWeight(); AmericanPeople americanPeople1=new AmericanPeople(); americanPeople1.height=165; americanPeople1.weight=55; americanPeople1.speakHello(); americanPeople1.americanBoxing(); americanPeople1.averageHeight(); americanPeople1.averageWeight(); } }
Operation results:
2, Interest on bank deposits
Suppose that the Bank already has a general method of calculating interest by year, where year can only take a positive integer. For example, the calculation method based on the whole year:
double computerInterest() { interest=year*0.035*savedMoney; return interest; }
China Construction Bank is a subclass of bank. It plans to hide the inherited member variable year and rewrite the method of calculating interest, that is, declare a double year variable. For example, when the value of year is 5.216, it means that the interest of 5 years and 216 days will be calculated, but you want to calculate the interest of 5 whole years first according to the bank's method computerInterest(), Then calculate the interest for 216 days. Then, China Construction Bank 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.
Requirements: ConstructionBank and BankOfQingdao are subclasses of Bank class. Both ConstructionBank and BankOfQingdao use super to call hidden member variables and methods.
UML diagrams of ConstructionBank, BankOfQingdao and Bank classes are as follows:
Note: the annual interest rate is 0.035, the daily interest rate of China Construction Bank is 0.0001, and the daily interest rate of Bank of Qingdao is 0.00015
(1) If 50000 yuan is deposited in China Construction Bank and Bank of Qingdao for 5 years and 216 days, what is the interest difference?
(2) With reference to China Construction Bank or bank of Qingdao, write a commercial bank and let the program output 8000 yuan of interest for 8 years and 236 days in the commercial bank. The daily interest rate of commercial banks is 0.00012.
Bank class:
public class Bank { int savedMoney; int year; double interest; double computerInterest() { this.interest = this.year*0.035*this.savedMoney; return interest; } }
ConstructionBank class:
import java.lang.Math; public class ConstructionBank extends Bank { double year; public double computerInterest() { super.year=(int)Math.floor(this.year); double r=this.year-(int)Math.floor(this.year); int day=(int)(r*1000);//Days double yearInterest=super.computerInterest(); double dayInterest=day*0.0001*savedMoney; interest=yearInterest+dayInterest; System.out.printf("%d Yuan exists in China Construction Bank%d Year zero%d Interest for days:%f element\n",savedMoney,super.year,day,interest); return interest; } }
BankOfQingdao class:
import java.lang.Math; public class BankOfQingdao extends Bank { double year; public double computerInterest() { super.year=(int)Math.floor(this.year); double r=this.year-(int)Math.floor(this.year); int day=(int)(r*1000);//Days double yearInterest=super.computerInterest(); double dayInterest=day*0.00015*savedMoney; interest=yearInterest+dayInterest; System.out.printf("%d RMB in Bank of Qingdao%d Year zero%d Interest for days:%f element\n",savedMoney,super.year,day,interest); return interest; } }
Commercial Bank class:
public class CommercialBank extends Bank { double year; public double computerInterest() { super.year=(int)Math.floor(this.year); double r=this.year-(int)Math.floor(this.year); int day=(int)(r*1000);//Days double yearInterest=super.computerInterest(); double dayInterest=day*0.00012*savedMoney; interest=yearInterest+dayInterest; System.out.printf("%d Yuan exists in commercial banks%d Year zero%d Interest for days:%f element\n",savedMoney,super.year,day,interest); return interest; } }
Text test main class:
public class Text { public static void main(String args[]){ //50000 yuan with interest of 5 years and 216 days in China Construction Bank. ConstructionBank bank1=new ConstructionBank(); bank1.year=5.216; bank1.savedMoney=50000; bank1.computerInterest(); //50000 yuan with interest of 5 years and 216 days in Bank of Qingdao. BankOfQingdao bank2=new BankOfQingdao(); bank2.year=5.216; bank2.savedMoney=50000; bank2.computerInterest(); //Interest difference System.out.printf("Interest difference:"+(bank1.interest-bank2.interest)+"\n\n"); //8000 yuan of interest in commercial banks for 8 years and 236 days CommercialBank bank3=new CommercialBank(); bank3.year=8.236; bank3.savedMoney=80000; bank3.computerInterest(); } }
Operation results:
3, Encapsulate the basic properties and functions of mobile phones with classes
Class is used to encapsulate the basic properties and functions of the mobile phone. The mobile phone is required to use either the SIM card of the mobile company or the SIM card of China Unicom (the SIM card provided by any company can be used).
① . design abstract class: design an abstract class SIM, which has three abstract methods: giveNumber(), setNumber() and giveCorpName()
② . design mobile phone class: design MobileTelephone. This class has useSIM(SIM card) method
③ Mobile cards of all companies: designed SIMOfChinaMobile and simofchinunicom
The relationships between various types are as follows:
Program to define various types and test them in the main class.
SIM class:
public abstract class SIM { //Abstract class, abstract method abstract void setNumber(String s); abstract String giveNumber(); abstract String giveCorpName(); }
MobileTelephone class:
public class MobileTelephone{ SIM card; void useSIM(SIM card){ this.card=card; } void showMess(){ } }
SIMOfChinaMobile class:
public class SIMOfChinaMobile extends SIM { String number; @Override String giveNumber(){ return number; } @Override void setNumber(String number){ this.number=number; } @Override String giveCorpName(){ return "China Mobile"; } }
Simofchinacom class:
public class SIMOfChinaUnicom extends SIM { String number; @Override //This symbol represents rewriting, helps to understand when reading code, and can be used as an annotation. String giveNumber(){ return this.number; } @Override void setNumber(String number){ this.number=number; } @Override String giveCorpName(){ return "China Unicom"; } }
Text test main class:
public class Text { public static void main(String[] args) { //A mobile phone can use either the SIM card of the mobile company or the SIM card of China Unicom. MobileTelephone phone =new MobileTelephone(); SIMOfChinaUnicom sim1=new SIMOfChinaUnicom(); sim1.setNumber("123456789"); System.out.printf("SIM1 Card mobile number:"+sim1.giveNumber()+"\n"); System.out.printf("SIM1 Card communication company:"+sim1.giveCorpName()+"\n"); phone.useSIM(sim1); SIMOfChinaMobile sim2=new SIMOfChinaMobile(); sim2.setNumber("987654321"); System.out.printf("SIM2 Card mobile number:"+sim2.giveNumber()+"\n"); System.out.printf("SIM2 Card communication company:"+sim2.giveCorpName()+"\n"); phone.useSIM(sim2); } }
Operation results:
4, Total salary paid by the company
An abstract class named employee is required. Subclasses of employee include YearWorker, MonthWorker and WeekWorker. YearWorker objects are paid annually, MonthWorker is paid monthly, and WeekWorker is paid weekly. The Employee class has an abstract method:
public abstract earnings();
The subclass must override the methods of the parent class, giving the specific ways to get paid.
There is a Company class that uses the Employee object array as a member. The elements of the Employee object array can be the upper transformation object of the YearWorker object, the upper transformation object of the MonthWorker object, or the upper transformation object of the WeekWorker object. The program can output the total salary that the Company object needs to pay in a year.
This topic examines: the object of transformation.
Employee class:
public abstract class Employee { public abstract double earnings(); }
WeekWorker class:
public class WeekWorker extends Employee { int week; double weekSalary; WeekWorker(int w,double s){ week=w; weekSalary=s; } public double earnings(){ return this.week*this.weekSalary; } }
MonthWorker class:
public class MonthWorker extends Employee { int month; double monthSalary; MonthWorker(int m,double s){ month=m; monthSalary=s; } public double earnings(){ return this.month*this.monthSalary; } }
YearWorker class:
public class YearWorker extends Employee { int year; double yearSalary; YearWorker(int y,double s){ year=y; yearSalary=s; } public double earnings(){ return this.year*this.yearSalary; } }
Company class:
public class Company { //Employee object array as a member Employee[] employee; //Constructor Company(Employee[] employee) { this.employee=employee; } }
CompanySalary class:
public class CompanySalary { public static void main(String[] args) { //Define an array of employee class objects Employee[] employee=new Employee[3]; //employee[0] is the transformation object of YearWorker employee[0]=new YearWorker(1,48000.0); //employee[1] is the transformation object of MonthWorker employee[1]=new MonthWorker(12,4000.0); //employee[2] is the transformation object of WeekWorker employee[2]=new WeekWorker(48,1000.0); Company com=new Company(employee); double sumSalary; sumSalary=employee[0].earnings()+employee[1].earnings()+employee[2].earnings(); System.out.println("The total salary paid by the company is:"+sumSalary); } }
5, Through the class to find the area of three kinds of graphics
For various geometric figures, there are generally methods to calculate the area and perimeter of the figure. Now there are three kinds of graphics: circle, rectangle and triangle. It is required to calculate the area of the three graphics through classes.
The three kinds of graphics have the same method, so an abstract class can be abstracted: graphics class, which has an abstract area method. Then three subclasses are generated from the abstract class: circle class, rectangle class and triangle class. The area of various graphics is calculated in the main class.
The relationships between various types are as follows:
In the subclass, the construction method is defined to initialize the subclass member variables.
Program to define various types and test them in the main class.
Figure class:
public abstract class Figure { abstract double area(); }
Circle class:
public class Circle extends Figure { double r; Circle(double r){ this.r=r; } double area(){ return Math.pow(this.r, 2)*Math.PI; } }
Rectangle class:
public class Rectangle extends Figure { double lengh; double weigh; Rectangle(double lengh,double weigh){ this.lengh=lengh; this.weigh=weigh; } double area(){ return this.lengh*this.weigh; } }
Triangle class:
public class Triangle extends Figure { double high; double bottom; Triangle(double high,double bottom){ this.high=high; this.bottom=bottom; } double area(){ return 0.5*this.high*this.bottom; } }
Text class:
public class Text { public static void main(String[] args) { Circle circle1=new Circle(5); Rectangle rectangle1=new Rectangle(10,5); Triangle triangle1=new Triangle(10,5); System.out.println("Area of circle:"+circle1.area()); System.out.println("Rectangular area:"+rectangle1.area()); System.out.println("Triangle area:"+triangle1.area()); } }
6, Animal sound simulator
An animal sound "Simulator" is designed. It is hoped that the simulator can simulate the cry of many animals. The requirements are as follows:
(1) Write abstract class Animal
The Animal abstract class has two abstract methods cry() and getAnimalName(), that is, it requires various specific animals to give their own calls and species names.
(2) Write Simulator class Simulator
This class has a playsound (animal) method whose parameters are of type animal. That is, the parameter animal can call the cry() method overridden by the subclass of animal to play the sound of a specific animal, and call the getAnimalName() method overridden by the subclass to display the name of the animal species.
(3) Write subclasses of the Animal class: Dog and Cat classes
In each subclass, the initialization of subclass member variables is realized by construction methods.
(4) Write the main class Application (user program)
The main method of the main class Application contains at least the following code.
Simulator simulator = new Simulator(); simulator.playSound(new Dog("Tibetan Mastiff)); simulator.playSound(new Cat("Garfield cat));
UML diagrams of various types are as follows:
Animal class:
public abstract class Animal { abstract void cry(); abstract String getAnimalName(); }
Cat class:
public class Cat extends Animal { String name; Cat(String name){ this.name=name; } void cry() { System.out.println("cat ~"); } String getAnimalName() { return name; } }
Dog class:
public class Dog extends Animal { String name; Dog(String name){ this.name=name; } void cry() { System.out.println("Woof, woof~"); } String getAnimalName() { return name; } }
Simulator class:
public class Simulator { void playSound(Animal animal){ System.out.println( animal.getAnimalName() + " may cry like this: "); animal.cry(); } }
Text class:
public class Text { public static void main(String[] args) { Simulator simulator = new Simulator(); simulator.playSound(new Dog("Tibetan Mastiff")); simulator.playSound(new Cat("Garfield")); } }
Operation results: