
1.中国人,北京人和美国人继承
public class People {
protected double weight,height;
public void speakHello() {
System.out.println("yayaya");}
public void averageHeight() {
height =173;
System.out.println("average height:"+height);}
public void averageWeight() {
weight =70;
System.out.println("average weight:"+weight);}}
public class ChinaPeople extends People{
public void speakHello() {
System.out.println("你好");
}public void averageHeight() {
height=168.78;
System.out.println("中国人的平均身高:"+height+"厘米")}
public void averageWeight() {
weight=60;
System.out.println("中国人的平均身高:"+weight+"厘米");}
public void chinaGongfu() {
System.out.println("坐如钟,站如松,睡如弓");}}
public class BeijingPeople extends ChinaPeople{
public void averageHeight() {
height=167;
System.out.println("中国人的平均身高:"+height+"厘米");}
public void averageWeight() {
weight=61;
System.out.println("中国人的平均身高:"+weight+"厘米");
}
public void beijingOpera() {
System.out.println("花脸,青衣,花旦和老生");}}
public class AmericanPeople extends People {
public void averageWeight() {
weight=75;
System.out.println("American is average weight:"+weight+"kg");}
public void americanBoxing() {
System.out.println("直拳,勾拳,组合拳");}}
public class Example {
public static void main(String[] args) {
ChinaPeople chinaPeople=new ChinaPeople();
AmericanPeople americanPeople=new AmericanPeople();
BeijingPeople beijingPeople=new BeijingPeople();
chinaPeople.speakHello();
americanPeople.speakHello();
beijingPeople.speakHello();
chinaPeople.averageHeight();
americanPeople.averageHeight();
beijingPeople.averageHeight();
chinaPeople.averageWeight();
americanPeople.averageWeight();
beijingPeople.averageWeight();
chinaPeople.chinaGongfu();
americanPeople.americanBoxing();
beijingPeople.beijingOpera() ;
beijingPeople.chinaGongfu();}}
2.一个圆包含圆心和半径两个属性,定义一个圆,要求:
- 提供合理的构造器
- 提供方法,计算当前图形的面积
- 计算当前图形是否包含指定的点,方法名
contains(Point) - 计算当前图形是否包含坐标为x,y的点,重载方法
public class Point {
int x;
int y;
public Point (int x){
this(x,x);}
public Point(int x,int y){
this.x=x;
this.y=y; }
public double distance(){
return Math.sqrt(this.x*this.x+this.y*this.y); }
public double distance(int x,int y){
return Math.sqrt((this.x-x)*(this.x-x)+(this.y-y)*(this.y-y)); }
public double distance(Point other){
return distance(other.x,other.y); }
public void distance(int x){ }}
public class Circle {
Point center;
double r;
//构造方法
public Circle(Point center,double r){
this.center=center;
this.r=r;
}
public Circle(int x,int y,double r){
this(new Point(x,y),r);
}
public double area(Circle s){
return Math.PI*(s.r*s.r);
}
public boolean contain(Point p){
double d=this.center.distance(p);
return d<=r; }
public boolean contain(int x,int y){
double d=this.center.distance(x,y);
return d<=r; }
public static void main(String[] args) {
Circle c=new Circle(3,4,5);
Circle c1=new Circle(new Point(3),9);
System.out.println(c.area(c));
System.out.println(c1.area(c1));
boolean flag =c.contain(3, 5);
boolean flag1=c1.contain(new Point(5));
System.out.println(flag);
System.out.println(flag1);}}
- 编写一个Java应用程序,模拟家庭买一台电视,即家庭将电视作为自己的一个成员,即通过调用一个方法将某个电视的引用传递给自己的电视成员。具体要求如下:
1.有三个源文件:TV.java、Family.java、MainClass.java,其中,TV.java中的TV类负责创建“电视”对象,Family.java中的Family类负责创建“家庭”对象,MainClass.java是主类。
2.在主类的main()方法中首先使用TV类创建一个对象haierTV,然后使用Family类再创建一个对象zhangSanFamily,并将先前TV类的实例haierTV的引用传递给zhangSanFamily对象的成员变量homeTV。
public class TV {
String channel;//电视频道
int TVnum;
void setChannel(int num) {
switch(num) {
case 1:channel="综合频道";
TVnum=1;
break;
case 2:channel="经济频道";
TVnum=2;
break;
case 3:channel="文艺频道";
TVnum=3;
break;
case 4:channel="国际频道";
TVnum=4;
break;
case 5:channel="体育频道";
TVnum=5;
break;}}
String getTV(String channel) {
return channel;}
int GetTVnum() {
return TVnum;}}
public class Family {
TV homeTV;
void setHomeTV(TV tv) {
homeTV=tv;
}
String getHomeTV() {
return homeTV.channel;
}
int GetTVNum() {
return homeTV.TVnum;
}
}
public class MainClass {
public static void main(String[] args) {
TV haierTV = new TV();
haierTV.setChannel(4);
Family zhangSanFamily = new Family();
zhangSanFamily.setHomeTV(haierTV);
System.out.println("haier TV的频道是"+zhangSanFamily.GetTVNum());
System.out.println("haier TV的频道是"+zhangSanFamily.getHomeTV());
int m=2;
System.out.println("hangSanFamily将电视更换到"+m+"频道");
haierTV.setChannel(m);
System.out.println("haier TV的频道是"+zhangSanFamily.GetTVNum());
System.out.println("haier TV的频道是"+zhangSanFamily.getHomeTV());}}
- 公司支出的总薪水
要求有一个abstract类,类名为Employee. Employee的子类有YearWorker、MonthWorker、WeekWorker按周领取薪水。Employee类有一个abstract方法:
public abstract earnings();子类必须重写父类的earnings()方法,给出各自领取报酬的具体方法。
有一个Company类,该类用Employee对象数组作为成员变量,Employee对象数组的元素可以是YearWorker对象的上转型对象、MonkeyWorker对象的上转型对象或WeekWorker对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。
再增加一种雇员,并计算公司的一年的总薪水。
abstract class Employee {
public abstract double earnings();
}
class YearWorker extends Employee {
@Override
public double earnings() {
// TODO Auto-generated method stub
return 60000;}}
class MonthWorker extends Employee {
@Override
public double earnings() {
// TODO Auto-generated method stub
return 5000;}}
class WeekWorker extends Employee {
@Override
public double earnings() {
// TODO Auto-generated method stub
return 1250;}}
class DayWorker extends Employee {
@Override
public double earnings() {
// TODO Auto-generated method stub
return 178;}}
class Company {
Employee[] employee;
double salaries=0;
Company(Employee[] employee) {
this.employee=employee;
}
public double salariesPay() {
salaries=0;
for(int i=0;i
5.银行计算利息
银行Bank已经有了按整年year计算利息的一般方法,其中year只能取正整数。比如按整年计算的方法:
Double computerInterest(){
interest=year0.35savedMonney;
Return interest;
}
建设银行ConstructionBank是Bank的子类,贮备隐藏继承的成员变量year,并重写计算利息的方法,即自己声明一个double型的year变量,比如,当year取值5.216时,表示要计算5年零216天的利息,但希望首先按银行Bank的方法computerInterest()的计算出5整年的利息,然后再自己计算216天的利息。那么,建设银行就必须把5.216的整数部分赋给隐藏的year,并让super调用隐藏的、按整年计算利息的方法。
要求ConstructionBank和BankOfDalian类是Bank类的子类,ConstructionBank和BankOfDalian都使用super调用隐藏的成员变量和方法。
参照建设银行或大连银行,再编写一个商业银行,让程序输出8000元存在商业银行8年零236天的利息。
public class Bank {
int savedMoney;
int year;
double interest;
double interestRate=0.29;
public double computerInterest(){
interest=year*interestRate*savedMoney;
return interest;
}public void setInterestRate(double rate){
interestRate=rate; }}
public class BankOfDalian extends Bank{
double year;
public double computerInterest(){
super.year=(int)year;
double r=year-(int)year;
int day=(int)(r*1000);
double yearInterest =super.computerInterest();
double dayInterest=day*0.00012*savedMoney;
interest=yearInterest+dayInterest;
System.out.printf("%d元存在大连银行%d年零%d天的利息:%f元n",savedMoney,super.year,day,interest);
return interest;}}
public class ConstructionBank extends Bank{
double year;
public double computerInterest(){
super.year=(int)year;
double r=year-(int)year;
int day=(int)(r*1000);
double yearInterest =super.computerInterest();
double dayInterest=day*0.0001*savedMoney;
interest=yearInterest+dayInterest;
System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元n",savedMoney,super.year,day,interest);
return interest; }}
public class shangyeBank extends Bank{
double year;
public double computerInterest(){
super.year=(int)year;
double r=year-(int)year;
int day=(int)(r*1000);
double yearInterest =super.computerInterest();
double dayInterest=day*0.00015*savedMoney;
interest=yearInterest+dayInterest;
System.out.printf("%d元存在商业银行%d年零%d天的利息:%f元n",savedMoney,super.year,day,interest);
return interest;}}
public class SaveMoney {
public static void main(String[] args) {
int amount=8000;
ConstructionBank bank1=new ConstructionBank();
bank1.savedMoney=amount;
bank1.year=8.236;
bank1.setInterestRate(0.035);
double interest1=bank1.computerInterest();
BankOfDalian bank2=new BankOfDalian();
bank2.savedMoney=amount;
bank2.year=8.236;
bank2.setInterestRate(0.035);
double interest2=bank2.computerInterest();
shangyeBank bank3=new shangyeBank();
bank3.savedMoney=amount;
bank3.year=8.236;
bank3.setInterestRate(0.035);
double interest3=bank3.computerInterest();
System.out.printf("两个银行利息相差%f元n",interest2-interest1);}}
6.编写程序模拟司令部通过下达作战命令指挥作战。 (1)编写一个接口Commander,该接口的方法是void battle(String mess)。 (2)编写一个LeaderHeader类,该类负责创建司令部。LeaderHeader类有方法: giveCommand(Commander com); 在该方法中的参数是Commander接口类型,该方法让参数com回调接口方法battle。 (3)编写ShiZhang类和LvZhang类,实现Commander接口。 (4)在主类中创建一个名字为leader的司令部和名字是oneShi和oneLv的师长和旅长,leader调用giveCommand指挥师长和旅长作战
public interface Commander {
public void battle(String mess);}
public class LeaderHeader {
String battleContent;
public void giveCommand(Commander com){
com.battle(battleContent);
}
public void setBattleContent(String s){
battleContent=s;}}
public class ShiZhang implements Commander{
final int MAXSoldierNumber=1000;
int soldierNumber=1;
String name;
ShiZhang(String s){
name=s; }
public void battle(String mess){
System.out.println(name+"接到作战命令:"+mess);
System.out.println(name+"指派"+soldierNumber+"人参与作战");
System.out.println(name+"保证完成任务");}
void setSoldierNumber(int m){
if(m>MAXSoldierNumber){
soldierNumber=MAXSoldierNumber;}
else if(m0){
soldierNumber=m;}}}
public class LvZhang implements Commander {
final int MAXSoldierNumber=800;//最大人数
int soldierNumber=1;//人数
String name;
LvZhang(String s){//构造方法
name =s;
}
public void battle(String mess){//重写接口方法
System.out.println(name+"接到作战命令:"+mess);
System.out.println(name+"指派"+soldierNumber+"人参与作战");
System.out.println(name+"保证完成任务");
}
void setSoldierNumber(int m){//设置人数
if(m>MAXSoldierNumber){
soldierNumber=MAXSoldierNumber;
}
else if(m0){
soldierNumber=m;}}}
public class ddd {
public static void main(String[] args) {//主类
LeaderHeader leader=new LeaderHeader();
ShiZhang oneShi=new ShiZhang("师长");//实例化对象
oneShi.setSoldierNumber(800);
leader.setBattleContent("进攻北城");
leader.giveCommand(oneShi);
LvZhang oneLv=new LvZhang("旅长");
oneLv.setSoldierNumber(567);
leader.setBattleContent("在2号公路阻击敌人");
leader.giveCommand(oneLv);}}
7.要求:体 *** 比赛计算选手成绩的办法是去掉一个最高分和最低分再计算平均分。而学校考察一个班级的某门课的考试成绩时,是计算全班同学的平均成绩。Gymnastics和School类都实现了CompurerAverage接口,但实现方式不同。
interface CompurerAverage {
public double average(double x[]);
}
class Gymnastics implements CompurerAverage {
public double average(double x[]) {
int count=x.length;
double aver=0,temp=0;
for(int i=0;i2)
aver=aver/(count-2);
else
aver=0;
return aver;
}
}
class School implements CompurerAverage {
@Override
public double average(double[] x) {
int count=x.length;
double aver=0,temp=0;
for(int i=0;i
8.小狗在不同环境条件下可能出现不同的状态表现,要求用接口封装小狗的状态。具体要求如下。
(1)编写一个接口DogState,该接口有一个名字为void showState()的方法。
(2)编写若干个实现DogState接口的类,负责刻画小狗的各种状态。
(3)编写Dog类,该类中有一个DogState接口声明的变量state。另外,该类有一个show(DogState s)方法,在该方法中让接口state回调showState()方法。
(4)编写主类,在主类中测试小狗的各种状态。
interface DogState {
public void showState();
}
class SoftlyState implements DogState {
public void showState(){
System.out.println("听主人的命令!");
}
}
class MeetEnemyState implements DogState {
public void showState(){
System.out.println("上去咬一口!");
}
}
class MeetFriendState implements DogState {
public void showState(){
System.out.println("晃动尾巴,以示友好!");
}
}
class MeetAnotherDog implements DogState {
public void showState(){
System.out.println("hello!");
}
}
class Dog{
DogState state;
public void show(){
state.showState();
}
public void setState(DogState s){
state=s;
}}
public class CheckDogState {
public static void main(String[] args) {
Dog yellowDog=new Dog();
System.out.println("狗在主人面前:");
yellowDog.setState(new SoftlyState());
yellowDog.show();
System.out.println("狗遇到敌人:");
yellowDog.setState(new MeetEnemyState());
yellowDog.show();
System.out.println("狗遇到朋友:");
yellowDog.setState(new MeetFriendState());
yellowDog.show();
System.out.println("狗遇到同伴:");
yellowDog.setState(new MeetAnotherDog());
yellowDog.show();}}
9.手机专卖店为了促销自己的产品,决定发行内部购物券,但其他商场不能发行该购物券。
编写一个MobileShop类(模拟手机专卖店),该类中有一个名字为InnerPurchaseMoney的内部类(模拟内部购物券)
class MobileShop {
InnerPurchaseMoney purchaseMoney1;//用内部类InnerPurchaseMoney声明对象purchaseMoney1
InnerPurchaseMoney purchaseMoney2; //用内部类InnerPurchaseMoney声明对象purchaseMoney2
private int mobileAmount; //手机的数量
MobileShop(){
purchaseMoney1=new InnerPurchaseMoney(20000);//创建价值为20000的purchaseMoney1
purchaseMoney2=new InnerPurchaseMoney(10000);//创建价值为10000的purchaseMoney2
}
void setMobileAmount(int m) {
mobileAmount = m;
}
int getMobileAmount() {
return mobileAmount;
}
class InnerPurchaseMoney {
int moneyValue;
InnerPurchaseMoney(int m) {
moneyValue = m;
}
void buyMobile() {
if(moneyValue>=20000) {
mobileAmount = mobileAmount-6;
System.out.println("用价值"+moneyValue+"的内部购物卷买了6部手机");
}
else if(moneyValue<20000&&moneyValue>=10000) {
mobileAmount = mobileAmount-3;
System.out.println("用价值"+moneyValue+"的内部购物卷买了3部手机");}}}}
public class NewYear
{public static void main(String args[]) {
MobileShop shop = new MobileShop();
shop.setMobileAmount(30);
System.out.println("手机专卖店目前有"+shop.getMobileAmount()+"部手机");
shop.purchaseMoney1.buyMobile();
shop.purchaseMoney2.buyMobile();
System.out.println("手机专卖店目前有"+shop.getMobileAmount()+"部手机");}}
10.和接口有关的匿名类
输出两种打招呼的方式:hello.welcome!
你好,欢迎光临!
interface SpeakHello {
void speak();
}
class HelloMachine {
public void turnOn(SpeakHello hello) {
hello.speak();
}
}
public class Example7_3 {
public static void main(String args[]) {
HelloMachine machine = new HelloMachine();
machine.turnOn(new SpeakHello(){
@Override
public void speak() {
// TODO Auto-generated method stub
System.out.println("hello.welcome!");
}
});
machine.turnOn( new SpeakHello(){
public void speak() {
System.out.println("你好,欢迎光临!");
}
}
);
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)