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()方法。
People、ChinaPeople、AmericanPeople和BeijingPeople类的UML图如下图所示:
在主类中定义各类的对象并调用其方法输出相应信息。
package major;
public class major {
public static void main(String[] args)
{
People people=new People();
people.speakHello();
people.averageHeight();
people.averageWeight();
ChinaPeople chinapeople=new ChinaPeople();
chinapeople.speakHello();
chinapeople.averageHeight();
chinapeople.averageWeight();
chinapeople.chinaGongfu();
AmericanPeople ampeople=new AmericanPeople();
ampeople.speakHello();
ampeople.averageHeight();
ampeople.averageWeight();
ampeople.americanBoxing();
BeijingPeople bepeople=new BeijingPeople();
bepeople.speakHello();
bepeople.averageHeight();
bepeople.averageWeight();
bepeople.beijingOpera();
}
}
class People{
protected double height,weight;
public void speakHello(){
System.out.println("hello");
};
public void averageHeight(){
height=1.7;
System.out.println(height);
};
public void averageWeight(){
weight=60;
System.out.println(weight);
};
}
class ChinaPeople extends People{
public void chinaGongfu(){
System.out.println("中国功夫");
};
//重写父类的三个方法
public void speakHello(){
System.out.println("你好");
};
public void averageHeight(){
height=1.75;
System.out.println(height);
};
public void averageWeight(){
weight=53;
System.out.println(weight);
};
}
class AmericanPeople extends People{
public void americanBoxing(){
System.out.println("美国box");
};
//重写父类的三个方法
public void speakHello(){
System.out.println("Hi!");
};
public void averageHeight(){
height=1.8;
System.out.println(height);
};
public void averageWeight(){
weight=65;
System.out.println(weight);
};
}
class BeijingPeople extends ChinaPeople{
public void beijingOpera(){
System.out.println("京剧");
};
//重写父类的三个方法
public void speakHello(){
System.out.println("好呀!");
};
public void averageHeight(){
height=1.71;
System.out.println(height);
};
public void averageWeight(){
weight=55;
System.out.println(weight);
};
}
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图如下所示:
package major;
public class major {
public static void main(String[] args)
{
double c_interest,q_interest,com_interest,cha;
Bank bank=new Bank();
bank.savedMoney=50000;
Construction con=new Construction();
con.year=5.216;
c_interest=con.computerInterest();
BankOfQingdao qing=new BankOfQingdao();
qing.year=5.216;
q_interest=qing.computerInterest();
cha=q_interest-c_interest;
System.out.println(cha);
CommercialBank com=new CommercialBank();
com.year=8.236;
com.savedMoney=8000;
com_interest=com.computerInterest();
System.out.println(com_interest);
}
}
class Bank{
static int savedMoney;
int year;
double interest;
public double computerInterest(){
interest=year*0.035*savedMoney;
return interest;
};
}
class Construction extends Bank{
double year;//隐藏继承变量
public double computerInterest(){
int y =(int)year;
super.year=y;
interest=super.computerInterest();
interest=interest+(year-y)*0.0001*savedMoney;
return interest;
};
}
class BankOfQingdao extends Bank{
double year;//隐藏继承变量
public double computerInterest(){
int y =(int)year;
super.year=y;
interest=super.computerInterest();
interest=interest+(year-y)*0.00015*savedMoney;
return interest;
};
}
class CommercialBank extends Bank{
double year;//隐藏继承变量
public double computerInterest(){
int y =(int)year;
super.year=y;
interest=super.computerInterest();
interest=interest+(year-y)*0.00012*savedMoney;
return interest;
};
}
3.根据下面要求,编写一个Java应用程序:
用类封装手机的基本属性和功能,要求手机即可以使用移动公司的SIM卡也可以使用联通公司SIM卡(可以使用任何公司提供的SIM卡)。
①.设计抽象类:设计一个抽象类SIM,该类有三个抽象方法:giveNumber()、setNumber()和giveCorpName()
②.设计手机类:设计MobileTelephone,该类有useSIM(SIM card)方法
③.各公司手机卡类:设计SIMOfChinaMobile、SIMOfChinaUnicom类
各类之间的关系如下:
package major;
public class major {
public static void main(String[] args)
{
MobileTelephone phone = new MobileTelephone();
phone.card= new SIMOfChinaMobile();
phone.card.setNumber("12345678988");
phone.useSIM(phone.card);
phone.showMess();
}
}
abstract class SIM{
abstract void setNumber(String s);
abstract String giveNumber();
abstract String giveCorpName();
}
class SIMOfChinaMobile extends SIM{
String number;
void setNumber(String s){
number=s;
};
String giveNumber(){
return number;
};
String giveCorpName(){
return "SIMOfChinaMobile";
};
}
class SIMOfChinaUnicom extends SIM{
String number;
void setNumber(String s){
number=s;
};
String giveNumber(){
return number;
};
String giveCorpName(){
return "SIMOfChinaUnicom";
};
}
class MobileTelephone {
SIM card;String s;
void useSIM(SIM card){
s=card.giveNumber();
card.giveCorpName();
};
void showMess(){
System.out.println(s);
};
}
4.根据下面要求,编写Java应用程序实现:
要求有一个abstract类,类名为Employee。Employee的子类有YearWorker、MonthWorker、WeekWorker。YearWorker对象按年领取薪水,MonthWorker按月领取薪水,WeekWorker按周领取薪水。Employee类有一个abstract方法:
public abstract earnings();
子类必须重写父类的earnings()方法,给出各自领取报酬的具体方式。
有一个Company类,该类用Employee对象数组作为成员,Employee对象数组的元素可以是YearWorker对象的上转型对象、MonthWorker对象的上转型对象或WeekWorker对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。
(这个答案有误,l5和l12报错了,但还不知道为什么)
package major;
public class major {
public static void main(String[] args)
{
Company com = new Company();
System.out.println(com.ex);
}
}
class Company{
Employee b[]= new YearWorker[10];
int ex = b[0].earnings();
}
abstract class Employee{
public abstract int earnings();
}
class YearWorker extends Employee{
public int earnings(){
return 365*1;
};
}
class MonthWorker extends Employee{
public int earnings(){
return 26*12;
};
}
class WeekWorker extends Employee{
public int earnings(){
return 5*50;
};
}
5.对于各种几何图形,一般都有求图形的面积、周长等方法。现在有圆、矩形和三角形三种图形,要求通过类来实现求三种图形的面积。
问题分析:
三种图形都有相同的方法,因此可以抽象出一个抽象类:图形类,该类有抽象的求面积方法。然后由该抽象类生成3个子类:圆类、矩形类、三角形类。在主类中实现求各类图形的面积。各类之间的关系如下:
在子类中,定义构造方法,实现对子类成员变量的初始化。
编程定义各类,并在主类中进行测试。
package major;
import java.lang.Math;
public class major {
public static void main(String[] args)
{
Circle c =new Circle();
System.out.println(c.area());
Rectangle r =new Rectangle();
System.out.println(r.area());
Triangle t =new Triangle();
System.out.println(t.area());
}
}
abstract class Figure{
public abstract double area();
}
class Circle extends Figure{
double r;
Circle(){
r=1.2;
};
public double area(){
return Math.PI*(r*r);
};
}
class Rectangle extends Figure{
double length;
double width;
Rectangle(){
length=1.5;
width=1.6;
};
public double area(){
return length*width;
};
}
class Triangle extends Figure{
double high;
double bottom;
Triangle(){
high=2.0;
bottom=3.0;
};
public double area(){
return high*bottom*1/2;
};
}
6.设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下:
l 编写抽象类Animal
Animal抽象类有2个抽象方法cry()和getAnimalName(),即要求各种具体的动物给出自己的叫声和种类名称。
l 编写模拟器类Simulator
该类有一个playSound(Animal animal)方法,该方法的参数是Animal类型。即参数animal可以调用Animal的子类重写的cry()方法播放具体动物的声音,调用子类重写的getAnimalName()方法显示动物种类的名称。
l 编写Animal类的子类:Dog和Cat类
各类的UML图如下所示:
在各子类中通过构造方法实现对子类成员变量的初始化。
l 编写主类Application(用户程序)
在主类Application的main方法中至少包含如下代码。
Simulator simulator = new Simulator();
simulator.playSound(new Dog(“藏獒”));
simulator.playSound(new Cat(“加菲猫”));
package major;
import java.lang.Math;
public class major {
public static void main(String[] args)
{
Simulator simulator = new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
}
}
abstract class Animal{
public abstract void cry();
public abstract String getAnimalName();
}
class Dog extends Animal{
String name;
public void cry(){
System.out.println("汪汪");
};
public String getAnimalName(){
return "藏獒";
};
}
class Cat extends Animal{
String name;
public void cry(){
System.out.println("喵喵");
};
public String getAnimalName(){
return "加菲猫";
};
}
class Simulator{
void playSound(Animal animal){
System.out.print(animal.getAnimalName());
animal.cry();
};
}