Java2 实用教材第三版例题程序第四章完整版.docx
- 文档编号:29431658
- 上传时间:2023-07-23
- 格式:DOCX
- 页数:40
- 大小:22.38KB
Java2 实用教材第三版例题程序第四章完整版.docx
《Java2 实用教材第三版例题程序第四章完整版.docx》由会员分享,可在线阅读,更多相关《Java2 实用教材第三版例题程序第四章完整版.docx(40页珍藏版)》请在冰豆网上搜索。
Java2实用教材第三版例题程序第四章完整版
例4.1
classXiyoujiRenwu
{floatheight,weight;
Stringhead,ear,hand,foot,mouth;
voidspeak(Strings)
{System.out.println(s);
}
}
classA
{publicstaticvoidmain(Stringargs[])
{XiyoujiRenwuzhubajie;//声明对象
zhubajie=newXiyoujiRenwu();//为对象分配内存,使用new运算符和默认的构造方法
}
}
例4.2
classPoint
{intx,y;
Point(inta,intb)
{x=a;
y=b;
}
}
publicclassA
{publicstaticvoidmain(Stringargs[])
{Pointp1,p2;//声明对象p1和p2
p1=newPoint(10,10);//为对象分配内存,使用new和类中的构造方法
p2=newPoint(23,35);//为对象分配内存,使用new和类中的构造方法
}
}
例4.3
classXiyoujiRenwu
{floatheight,weight;
Stringhead,ear,hand,foot,mouth;
voidspeak(Strings)
{head="歪着头";
System.out.println(s);
}
}
classExample4_3
{publicstaticvoidmain(Stringargs[])
{XiyoujiRenwuzhubajie,sunwukong;//声明对象
zhubajie=newXiyoujiRenwu();//为对象分配内存
sunwukong=newXiyoujiRenwu();
zhubajie.height=1.80f;//对象给自己的变量赋值
zhubajie.head="大头";
zhubajie.ear="一双大耳朵";
sunwukong.height=1.62f;//对象给自己的变量赋值
sunwukong.weight=1000f;
sunwukong.head="绣发飘飘";
System.out.println("zhubajie的身高:
"+zhubajie.height);
System.out.println("zhubajie的头:
"+zhubajie.head);
System.out.println("sunwukong的重量:
"+sunwukong.weight);
System.out.println("sunwukong的头:
"+sunwukong.head);
zhubajie.speak("俺老猪我想娶媳妇");//对象调用方法
System.out.println("zhubajie现在的头:
"+zhubajie.head);
sunwukong.speak("老孙我重1000斤,我想骗八戒背我");//对象调用方法
System.out.println("sunwukong现在的头:
"+sunwukong.head);
}
}
例4.4
class梯形
{float上底,下底,高,面积;
梯形(floatx,floaty,floath)
{上底=x;
下底=y;
高=h;
}
float计算面积()
{面积=(上底+下底)*高/2.0f;
return面积;
}
void修改高(floatheight)
{高=height;
}
float获取高()
{return高;
}
}
publicclassExample4_4
{publicstaticvoidmain(Stringargs[])
{梯形laderOne=new梯形(12.0f,3.5f,50),laderTwo=new梯形(2.67f,3.0f,10);
System.out.println("laderOne的高是:
"+laderOne.获取高());
System.out.println("laderTwo的高是:
"+laderTwo.获取高());
System.out.println("laderOne的面积是:
"+laderOne.计算面积());
System.out.println("laderTwo的面积是:
"+laderTwo.计算面积());
laderOne.修改高(10);
floath=laderOne.获取高();
laderTwo.修改高(h*2);
System.out.println("laderOne现在的高是:
"+laderOne.获取高());
System.out.println("laderTwo现在的高是:
"+laderTwo.获取高());
System.out.println("laderOne现在的面积是:
"+laderOne.计算面积());
System.out.println("laderTwo现在的面积是:
"+laderTwo.计算面积());
}
}
例4.5
classPeople
{Stringface;
voidsetFace(Strings)
{face=s;
}
}
classA
{voidf(intx,doubley,Peoplep)
{x=x+1;
y=y+1;
p.setFace("笑脸");
System.out.println("参数x和y的值分别是:
"+x+","+y);
System.out.println("参数对象p的face是:
"+p.face);
}
}
publicclassExample4_5
{publicstaticvoidmain(Stringargs[])
{intx=100;
doubley=100.88;
Peoplezhang=newPeople();
zhang.setFace("很严肃的样子");
Aa=newA();
a.f(x,y,zhang);
System.out.println("main方法中x和y的值仍然分别是:
"+x+","+y);
System.out.println("main方法中对象zhang的face是:
"+zhang.face);
}
}
例4.6
class圆
{double半径;
圆(doubler)
{半径=r;
}
double计算面积()
{return3.14*半径*半径;
}
void修改半径(double新半径)
{半径=新半径;
}
double获取半径()
{return半径;
}
}
class圆锥
{圆底圆;
double高;
圆锥(圆circle,doubleh)
{this.底圆=circle;
this.高=h;
}
double计算体积()
{doublevolume;
volume=底圆.计算面积()*高/3.0;
returnvolume;
}
void修改底圆半径(doubler)
{底圆.修改半径(r);
}
double获取底圆半径()
{return底圆.获取半径();
}
}
classExample4_6
{publicstaticvoidmain(Stringargs[])
{圆circle=new圆(10);
圆锥circular=new圆锥(circle,20);
System.out.println("圆锥底圆半径:
"+circular.获取底圆半径());
System.out.println("圆锥的体积:
"+circular.计算体积());
circular.修改底圆半径(100);
System.out.println("圆锥底圆半径:
"+circular.获取底圆半径());
System.out.println("圆锥的体积:
"+circular.计算体积());
}
}
例4.7
class梯形
{float上底,高;
staticfloat下底;
梯形(floatx,floaty,floath)
{上底=x;下底=y;高=h;
}
float获取下底()
{return下底;
}
void修改下底(floatb)
{下底=b;
}
}
classExample4_7
{publicstaticvoidmain(Stringargs[])
{梯形laderOne=new梯形(3.0f,10.0f,20),laderTwo=new梯形(2.0f,3.0f,10);
梯形.下底=200;//通过类名操作类变量
System.out.println("laderOne的下底:
"+laderOne.获取下底());
System.out.println("laderTwo的下底:
"+laderTwo.获取下底());
laderTwo.修改下底(60);//通过对象操作类变量
System.out.println("laderOne的下底:
"+laderOne.获取下底());
System.out.println("laderTwo的下底:
"+laderTwo.获取下底());
}
}
例4.8
classFibi
{publicstaticlongfibinacii(intn)
{longc=0;
if(n==1||n==2)
c=1;
else
c=fibinacii(n-1)+fibinacii(n-2);
returnc;
}
}
publicclassExample4_8
{publicstaticvoidmain(Stringargs[])
{System.out.println(Fibi.fibinacii(7));
}
}
例4.9
packagetom.jiafei;
publicclassPrimNumber
{publicvoidgetPrimnumber(intn)
{intsum=0,i,j;
for(i=1;i<=n;i++)
{for(j=2;j<=i/2;j++)
{if(i%j==0)
break;
}
if(j>i/2)
System.out.print(""+i);
}
}
publicstaticvoidmain(Stringargs[])
{PrimNumberp=newPrimNumber();
p.getPrimnumber(20);
}
}
例4.10
importjava.applet.Applet;
importjava.awt.*;
publicclassExample4_10extendsApplet
{Buttonredbutton;
publicvoidinit()
{redbutton=newButton("我是一个红色的按钮");
redbutton.setBackground(Color.red);
redbutton.setForeground(Color.white);
add(redbutton);
}
}
例4.11
importtom.jiafei.*;//引入包tom.jiafei中的类
publicclassExample4_11
{publicstaticvoidmain(Stringargs[])
{PrimNumbernum=newPrimNumber();//用包tom.jiafei中的类创建对象
num.getPrimnumber(30);
}
}
例4.12
publicclassExample4_12
{publicstaticvoidmain(Stringargs[])
{PrimNumbernum=newPrimNumber();//要保证PrimNuber类和Example4_12类在同一目录中
num.getPrimnumber(120);
}
}
例4.13
Trangel.java
packagetom.jiafei;
publicclassTrangle
{doublesideA,sideB,sideC;
booleanboo;
publicTrangle(doublea,doubleb,doublec)
{sideA=a;
sideB=b;
sideC=c;
if(a+b>c&&a+c>b&&c+b>a)
{System.out.println("我是一个三角形");
boo=true;
}
else
{System.out.println("我不是一个三角形");
boo=false;
}
}
publicvoid计算面积()
{if(boo)
{doublep=(sideA+sideB+sideC)/2.0;
doublearea=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
System.out.println("面积是:
"+area);
}
else
{System.out.println("不是一个三角形,不能计算面积");
}
}
publicvoid修改三边(doublea,doubleb,doublec)
{sideA=a;
sideB=b;
sideC=c;
if(a+b>c&&a+c>b&&c+b>a)
{boo=true;
}
else
{boo=false;
}
}
}
---------------------------------------------------------------------------------------
importtom.jiafei.Trangle;
classExample4_13
{publicstaticvoidmain(Stringargs[])
{Trangletrangle=newTrangle(12,3,1);
trangle.计算面积();
trangle.修改三边(3,4,5);
trangle.计算面积();
}
}
例4.14
classExample4_14
{privateintmoney;
Example4_14()
{money=2000;
}
privateintgetMoney()
{returnmoney;
}
publicstaticvoidmain(Stringargs[])
{Example4_14exa=newExample4_14();
exa.money=3000;
intm=exa.getMoney();
System.out.println("money="+m);
}
}
例4.15
classFather
{privateintmoney;
floatweight,height;
Stringhead;
voidspeak(Strings)
{System.out.println(s);
}
}
classSonextendsFather
{Stringhand,foot;
}
publicclassExample4_15
{publicstaticvoidmain(Stringargs[])
{Sonboy;
boy=newSon();
boy.weight=1.80f;
boy.height=120f;
boy.head="一个头";
boy.hand="两只手";
boy.foot="两只脚";
boy.speak("我是儿子");
System.out.println(boy.hand+boy.foot+boy.head+boy.weight+boy.height);
}
}
例4.16
classChengji
{floatf(floatx,floaty)
{returnx*y;
}
}
classXiangjiaextendsChengji
{floatf(floatx,floaty)
{returnx+y;
}
}
publicclassExample4_16
{publicstaticvoidmain(Stringargs[])
{Xiangjiasum;
sum=newXiangjia();
floatc=sum.f(4,6);
System.out.println(c);
}
}
例4.17
classArea
{floatf(floatr)
{return3.14159f*r*r;
}
floatg(floatx,floaty)
{returnx+y;
}
}
classCircleextendsArea
{floatf(floatr)
{return3.14159f*2.0f*r;
}
}
publicclassExample4_17
{publicstaticvoidmain(Stringargs[])
{Circleyuan;
yuan=newCircle();
floatlength=yuan.f(5.0f);
floatsum=yuan.g(232.645f,418.567f);
System.out.println(length);
System.out.println(sum);
}
}
例4.18
classA
{
finaldoublePI=3.1415926;
publicdoublegetArea(finaldoubler)
{
returnPI*r*r;
}
}
publicclassExample4_18
{publicstaticvoidmain(Stringargs[])
{Aa=newA();
System.out.println("面积:
"+a.getArea(100));
}
}
例4.19
class类人猿
{privateintn=100;
voidcrySpeak(Strings)
{System.out.println(s);
}
}
classPeopleextends类人猿
{voidcomputer(inta,intb)
{intc=a*b;
System.out.println(c);
}
voidcrySpeak(Strings)
{System.out.println("**"+s+"**");
}
}
classExample4_19
{publicstaticvoidmain(Stringargs[])
{类人猿monkey=newPeople();
monkey.crySpeak("Ilovethisgame");
Peoplepeople=(People)monkey;
puter(10,10);
}
}
例4.20
class动物
{voidcry()
{
}
}
class狗extends动物
{voidcry()
{System.out.println("汪汪.....");
}
}
class猫extends动物
{voidcry()
{System.out.println("喵喵.....");
}
}
classExample4_20
{publicstaticvoidmain(Stringargs[])
{动物dongwu;
dongwu=new狗();
dongwu.cry();
dongwu=new猫();
dongwu.cry();
}
}
例4.21
abstractclassA
{abstractintmin(intx,inty);
intmax(intx,inty)
{returnx>y?
x:
y;
}
}
classBextendsA
{intmin(intx,inty)
{returnx x: y; } } publicclassExample4_21 {publicstaticvoidmain(Stringargs[]) {Aa; Bb=newB(); intmax=b.max(12,34); intmin=b.min(12,34); System.out.println("max="+max+"min="+min); a=b; max=a.max(12,34); System.out.println("max="+max); } } 例4.22 abstractclass图形 {publicabstractdouble求面积(); } class梯形extends图形 {doublea,b,h; 梯形(doublea,doubleb,doubleh) {this.a=a; this.b=b;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java2 实用教材第三版例题程序第四章完整版 实用 教材 第三 例题 程序 第四 完整版