Java习题(三)

1.根据下面的要求,编写Java应用程序实现:

    编写程序模拟中国人、美国人是人;北京人是中国人。除主类外,程序中还有4个类:People、ChinaPeople、AmericanPeople和BeijingPeople类。要求如下:

l People类有权限是protected的double型成员变量height和weight,以及public void speakHello()、public void averageHeight()和public void averageWeight()方法。

l ChinaPeople类是People的子类,新增了public void chinaGongfu()方法。要求ChinaPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。

l AmericanPeople类是People的子类,新增public void americanBoxing()方法。要求AmericanPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。

l BeijingPeople类是ChinaPeople的子类,新增public void beijingOpera()方法。要求ChinaPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。

在主类中定义各类的对象并调用其方法输出相应信息。

public class People {
	protected double height;
	protected double weight;
	public void speakHello(){
		System.out.print("People说:"+"Are you OK? Hello! Thank you!\n");
	}
	public void averageHeight(){
		System.out.print("People平均身高:"+"170cm\n");
	}
	public void averageWeight(){
		System.out.print("People平均体重:"+"65kg\n");
	}

}
public class ChinaPeople extends People{
	public void chinaGongfu(){
		System.out.print("这是中国功夫~pia,pia,阿达~\n");
	}
	public void speakHello(){
		System.out.print("中国人说: "+"你好呀\n");
	}
	public void averageHeight(){
		System.out.print("中国人平均身高: "+"180cm\n");
	}
	public void averageWeight(){
		System.out.print("中国人平均体重: "+"65kg\n");
	}
}
public class AmericanPeople extends People{
	public void americanBoxing(){
		System.out.print("come on,朴茨大慈朴茨大慈嘟嘟嘟嘟嘟~\n");
	}
	public void speakHello(){
		System.out.print("美国人说: "+"Hello\n");
	}
	public void averageHeight(){
		System.out.print("美国人平均身高: "+"150cm\n");
	}
	public void averageWeight(){
		System.out.print("美国人平均体重: "+"100kg\n");
	}
}
public class BeijingPeople extends ChinaPeople{
	public void beijingOpera(){
		System.out.print("您倒是真入了化境,连雌雄都不分了。\n");
	}
	public void speakHello(){
		System.out.print("老北京人说: "+"这题写的不得劲啊\n");
	}
	public void averageHeight(){
		System.out.print("北京人平均身高: "+"185cm\n");
	}
	public void averageWeight(){
		System.out.print("北京人平均体重: "+"70kg\n");
	}
}
import my_work.AmericanPeople;
import my_work.ChinaPeople;
import my_work.BeijingPeople;
public class main_1 {
	public static void main(String args[]){
		People p = new People();
		ChinaPeople Cp = new ChinaPeople();
		AmericanPeople Ap = new AmericanPeople();
		BeijingPeople C_Bp = new BeijingPeople();
		
		p.speakHello();
        p.averageHeight();
        p.averageWeight();
        
        Cp.speakHello();
        Cp.chinaGongfu();
        Cp.averageHeight();
        Cp.averageWeight();
        
        Ap.speakHello();
        Ap.americanBoxing();
        Ap.averageHeight();
        Ap.averageWeight();
        
        C_Bp.speakHello();
        C_Bp.beijingOpera();
        C_Bp.averageHeight();
        C_Bp.averageWeight();
	}
}

2.根据下面的描述,编写Java程序实现:

    假设银行Bank已经有了按整年year计算利息的一般方法,其中year只能取正整数。比如按整年计算的方法:

    double computerInterest() {

             interest=year*0.035*savedMoney;

             return interest;

    }

  建设银行ConstructionBank是Bank的子类,准备隐藏继承的成员变量year,并重写计算利息的方法,即自己声明一个double型的year变量,比如,当year取值为5.216时,表示要计算5年零216天的利息,但希望首先按银行Bank的方法computerInterest()计算出5整年的利息,然后再自己计算216天的利息。那么,建设银行就必须把5.216的整数部分赋给隐藏的year,并让super调用隐藏的、按整年计算利息的方法。

    要求ConstructionBank和BankOfQingdao类是Bank类的子类,ConstructionBank和BankOfQingdao都使用super调用隐藏的成员变量和方法。

    计算5万元存5年零216天,在建设银行和青岛银行存的话,利息差额是多少?

    ConstructionBank、BankOfQingdao和Bank类的UML图如下所示:

注:整年利率:0.035 ,建设银行按天计算利率:0.0001,青岛银行按天计算利率:0.00015

功能扩展:

参照建设银行或青岛银行,再编写一个商业银行,让程序输出8000元存在商业银行8年零236天的利息。商业银行按天计算利率是0.00012

public class Bank {
	int savedMoney;
	int year;  
	double interest;
	Bank(){}
	
	public double computerInterest() {
        interest=year*0.035*savedMoney;
        return interest;
	}
}
public class ConstructionBank extends Bank{
	double year;
	ConstructionBank(double year,int savedMoney){
		super();
		this.year=year;
		super.year=(int)year;
		this.savedMoney=savedMoney;
	}
	public double computerInterest() {
        interest=super.computerInterest()+(year-(int)year)*0.1*savedMoney;  //year-(int)year里面包含三位小数
        return interest;
	}
}
public class BankOfQingdao extends Bank{
	double year;
	BankOfQingdao(double year,int savedMoney){
		super();
		this.year=year;
		super.year=(int)year;
		this.savedMoney=savedMoney;
	}
	public double computerInterest() {
        interest=super.computerInterest()+(year-(int)year)*0.15*savedMoney;   //year-(int)year里面包含三位小数
        return interest;
	}
}
public class CommercialBank extends Bank {
	double year;
	CommercialBank(double year,int savedMoney){
		super();
		this.year=year;
		super.year=(int)year;
		this.savedMoney=savedMoney;
	}
	public double computerInterest() {
        interest=super.computerInterest()+(year-(int)year)*0.12*savedMoney;   //year-(int)year里面包含三位小数
        return interest;
	}
}
public class main_2 {
	public static void main(String args[]){
		ConstructionBank jsyh = new ConstructionBank(5.216,50000);
		System.out.printf("建设银行的利息为%f元\n",jsyh.computerInterest());
		
		BankOfQingdao qdyh = new BankOfQingdao(5.216,50000);
		System.out.printf("青岛银行的利息为%f元\n",qdyh.computerInterest());
		
		CommercialBank syyh = new CommercialBank(8.236,8000);
		System.out.printf("商业银行的利息为%f元",syyh.computerInterest());
	}
}

3.根据下面要求,编写一个Java应用程序:

    用类封装手机的基本属性和功能,要求手机即可以使用移动公司的SIM卡也可以使用联通公司SIM卡(可以使用任何公司提供的SIM卡)。

①.设计抽象类:设计一个抽象类SIM,该类有三个抽象方法:giveNumber()、setNumber()和giveCorpName()

②.设计手机类:设计MobileTelephone,该类有useSIM(SIM card)方法

③.各公司手机卡类:设计SIMOfChinaMobile、SIMOfChinaUnicom类

各类之间的关系如下:

编程定义各类,并在主类中进行测试。

public class MobileTelephone {
	SIM card;
	public void useSIM(SIM card){
		this.card=card;
	}
	public void showMess(){
		System.out.print(card.giveCorepName());
	}

}
public abstract class SIM{
	
	public abstract void setNumber(String n);
	public abstract String giveNumber();
	public abstract String giveCorepName();
}
public class SIMOfChinaMobile extends SIM{
	String number;
	public void setNumber(String n){
		number=n;
	}
	public String giveNumber(){
		return number;
	}
	public String giveCorepName(){
		return "此卡为中国移动\n\n";
	}
}
public class SIMOfChinaUnicom extends SIM {
	String number;
	public void setNumber(String n){
		number=n;
	}
	public String giveNumber(){
		return number;
	}
	public String giveCorepName(){
		return "此卡为中国联通";
	}
}
public class main_3 {
	public static void main(String args[]){
		MobileTelephone mp = new MobileTelephone();
		SIM sm = new SIMOfChinaMobile();
		SIM su = new SIMOfChinaUnicom();
		
		sm.setNumber("12345");
		System.out.print("手机号为 "+sm.giveNumber()+'\n');
		mp.useSIM(sm);
		mp.showMess();
		
		su.setNumber("54321");
		System.out.print("手机号为 "+su.giveNumber()+'\n');
		mp.useSIM(su);
		mp.showMess();
	}
}

4.根据下面要求,编写Java应用程序实现:

    要求有一个abstract类,类名为Employee。Employee的子类有YearWorker、MonthWorker、WeekWorker。YearWorker对象按年领取薪水,MonthWorker按月领取薪水,WeekWorker按周领取薪水。Employee类有一个abstract方法:

    public  abstract  earnings();

子类必须重写父类的earnings()方法,给出各自领取报酬的具体方式。

    有一个Company类,该类用Employee对象数组作为成员,Employee对象数组的元素可以是YearWorker对象的上转型对象、MonthWorker对象的上转型对象或WeekWorker对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。
public abstract class Employee{
	public abstract int earnings();
}
public class YearWorker extends Employee{
	int year;
	int yearSalary;
	
	YearWorker(int y,int s){
		year=y;
		yearSalary=s;
	}
	
	public int earnings(){
	   return year*yearSalary;
   }
}
public class MonthWorker extends Employee {
	int month;
	int monthSalary;
	
	MonthWorker(int m,int s){
		month=m;
		monthSalary=s;
	}
	
	public int earnings(){
	   return month*monthSalary;
   }
}
public class WeekWorker extends Employee{
	int week;
	int weekSalary;
	
	WeekWorker(int w,int s){
		week=w;
		weekSalary=s;
	}
	
	public int earnings(){
	   return week*weekSalary;
   }
}
public class Company{ 
	Employee[] emp;
	
	Company(Employee[] e) {
	    emp = e;
	}
}
public class main_4 {
	public static void main(String args[]){
		int sumSalary;
		Employee[] employee=new Employee[3];		
		employee[0]=new YearWorker(1,100000); 		
		employee[1]=new MonthWorker(12,8000);
		employee[2]=new WeekWorker(52,1400);
		
		sumSalary=employee[0].earnings()+employee[1].earnings()+employee[2].earnings();
		System.out.println("公司一年支出的总薪水为:"+sumSalary);
	}
}

5.对于各种几何图形,一般都有求图形的面积、周长等方法。现在有圆、矩形和三角形三种图形,要求通过类来实现求三种图形的面积。

问题分析:

    三种图形都有相同的方法,因此可以抽象出一个抽象类:图形类,该类有抽象的求面积方法。然后由该抽象类生成3个子类:圆类、矩形类、三角形类。在主类中实现求各类图形的面积。

各类之间的关系如下:

在子类中,定义构造方法,实现对子类成员变量的初始化。

编程定义各类,并在主类中进行测试。

public abstract class Figure{
	public abstract double area();
}
public class Circle extends Figure {
	double r;
	double pi=3.1415926;
	
	Circle(double r){
		this.r=r;
	}
	
	public double area(){
	   return pi*r*r;
   }
}
public class Rectangle extends Figure{
	double length;
	double width;
	
	Rectangle(double l,double w){
		length=l;
		width=w;
	}
	
	public double area(){
	   return length*width;
   }
}
public class Triangle extends Figure{
	double high;
	double bottom;
	
	Triangle(double h,double b){
		high=h;
		bottom=b;
	}
	
	public double area(){
	   return 0.5*high*bottom;
   }
}
public class main_5 {
	public static void main(String args[]){				
		Circle cir=new Circle(2.0);
		Rectangle rec=new Rectangle(4.0,5.5);
		Triangle tri=new Triangle(3.0,4.0); 
		
		System.out.println("圆面积为:"+cir.area());
		System.out.println("矩形面积为:"+rec.area());
		System.out.println("三角形面积为:"+tri.area());
	}
}

6.设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下:

l 编写抽象类Animal

    Animal抽象类有2个抽象方法cry()和getAnimalName(),即要求各种具体的动物给出自己的叫声和种类名称。

l 编写模拟器类Simulator

    该类有一个playSound(Animal animal)方法,该方法的参数是Animal类型。即参数animal可以调用Animal的子类重写的cry()方法播放具体动物的声音,调用子类重写的getAnimalName()方法显示动物种类的名称。

l 编写Animal类的子类:Dog和Cat类

各类的UML图如下所示:

image.png

在各子类中通过构造方法实现对子类成员变量的初始化。

l 编写主类Application(用户程序)

    在主类Application的main方法中至少包含如下代码。

    Simulator simulator = new Simulator();

    simulator.playSound(new Dog(“藏獒”));

    simulator.playSound(new Cat(“加菲猫”));
public abstract class Animal{
	public abstract void cry();
	public abstract String getAnimalName();
}
public class Dog extends Animal {
	String name;
	
	Dog(String n){
		name=n;
	}
	
	public void cry(){
		System.out.println("汪汪~汪汪~");
	}
	public String getAnimalName(){
		return name;
	}
}
public class Cat extends Animal{
	String name;
	
	Cat(String n){
		name=n;
	}
	
	public void cry(){
	    System.out.println("喵呜~喵呜~");
    }
	public String getAnimalName(){
		return name;
	}
}
public class Simulator{ 
	public void playSound(Animal a){
		a.cry();
		System.out.println("你好呀,我是:"+a.getAnimalName());
	}
}
public class main_6 {
	public static void main(String args[]){				
		Simulator simulator = new Simulator();
        simulator.playSound(new Dog("藏獒"));
        simulator.playSound(new Cat("加菲猫")); 
	}
}
  • 15
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值