MyDogSex0.docx

上传人:b****6 文档编号:4332372 上传时间:2022-11-29 格式:DOCX 页数:8 大小:17.30KB
下载 相关 举报
MyDogSex0.docx_第1页
第1页 / 共8页
MyDogSex0.docx_第2页
第2页 / 共8页
MyDogSex0.docx_第3页
第3页 / 共8页
MyDogSex0.docx_第4页
第4页 / 共8页
MyDogSex0.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

MyDogSex0.docx

《MyDogSex0.docx》由会员分享,可在线阅读,更多相关《MyDogSex0.docx(8页珍藏版)》请在冰豆网上搜索。

MyDogSex0.docx

MyDogSex0

My,Dog,Sex

篇一:

解析JAVA程序设计第三章课后答案

第3章习题解答

1.如何定义方法?

在面向对象程序设计中方法有什么作用?

答:

方法的定义包括方法名、方法形参、方法的返回值类型和方法体四部分,方法只能在类中定义。

方法是对象的动态特征的描述,对象通过方法操作属性,进而改变对象的状态,完成程序所预期的功能。

2.定义一个Dog类,有名字、颜色、年龄等属性,定义构造方法用来初始化类的这些属性,定义方法输出Dog的信息。

编写应用程序使用Dog。

答:

publicclassDog{

privatestringname;

privatestringcolor;

privatestringage;

Dog(stringn,stringc,stringa){

name=n;color=c;age=a;

}

publicstringtostring(){

returnname+","+color+","+age;

}

publicstaticvoidmain(stringargs[]){

Dogdog=newDog("小白","白色","2岁");

system.out.println(dog.tostring());

}

}

3.什么是访问控制修饰符?

修饰符有哪些种类?

它们各有何作用?

答:

访问控制修饰符是对类、属性和方法的访问权限的一种限制,不同的修饰符决定了不同的访问权限。

访问控制修饰符有

3个:

private、protected、public,另外还有一种默认访问权限。

各个修饰符的作用如下表所示:

b:

包中的类

c:

所有子类

D:

本类

A:

所有类

4.阅读程序,写出程序的输出结果

classA{

privateintprivateVar;

A(int_privateVar){

privateVar=_privateVar;

}

booleanisequalTo(AanotherA){

if(this.privateVar==anotherA.privateVar)

returntrue;

else

returnfalse;

}

}

publicclassb{

publicstaticvoidmain(stringargs[]){

Aa=newA

(1);

Ab=newA

(2);

system.out.println(a.isequalTo(b));

}

}

程序的输出结果为:

false

5.阅读程序,写出程序的输出结果

publicclassTest{

publicstaticvoidmain(string[]args){

intx;

inta[]={0,0,0,0,0,0};

calculate(a,a[5]);

system.out.println("thevalueofa[0]is"+a[0]);

system.out.println("thevalueisa[5]is"+a[5]);

}

staticintcalculate(intx[],inty){

for(inti=1;iif(yx[i]=x[i-1]+1;

returnx[0];

}

}

程序的输出结果为:

thevalueofa[0]is0

thevalueisa[5]is5

6.阅读程序,写出程序的输出结果

publicclassTest{

publicstaticvoidmain(string[]args){

stringstr1=newstring("Java");

stringstr2=newstring("Java");

system.out.println(str1==str2);

}

}

程序的输出结果为:

false

7.阅读下列程序,程序中已经指明错误位置,请说出错误原因。

1.

packagesample;

classA{

privateintnum;

A(){

num=0;

}

intget(){returnnum;}

}

classZ{

publicstaticvoidmain(string[]args){

Aa1=newA();

intt=a1.get();

ints=a1.num;//此处有错误

}

}

错误原因:

私有变量只能在其所在类中直接使用,在其它类中不可以直接使用。

8.阅读下列程序,程序中已经指明错误位置,请说出错误原因。

其中,方法m的功能是把形参的值赋给类的成员变量x。

classAlpha{

privateintx;

publicvoidm(intx){

x=x;//此处有错误

}

}

应该修改为:

this.x=x;

9.下面定义了一个完整的类,包括有构造方法。

阅读这段程序,程序中已经指明错误位置,请说出错误原因。

classAlpha{

privateintx;

voidAlpha(){//此处有错误

x=0;

}

publicvoidgetx(){

returnx;

}

}

错误原因:

构造方法不能有返回类型,也不能以void作为它的返回类型。

10.定义一个名字为myRectangle的矩形类,类中有4个私有的整型成员变量,分别是矩形的左上角坐标(xup,yup)和右下角坐标(xDown,yDown);类中定义了无参数的构造方法和有4个int参数的构造方法,用来初始化类对象。

类中还有以下方法:

getw()-计算矩形的宽度;

geth()-计算矩形的高度;

area()-计算矩形的面积;

tostring()-把矩形的宽、高和面积等信息作为一个字符串返回。

编写应用程序使用myRectangle类。

答:

publicclassmyRectangle{

privateintxup,yup,xDown,yDown;

myRectangle(){

xup=0;yup=0;xDown=0;yDown=0;

}

myRectangle(x1,y1,x2,y2){

xup=x1;yup=y1;xDown=x2;yDown=y2;

}

publicintgetw(){

returnxDown-xup;

}

publicintgeth(){

returnyDown-yup;

}

publicintarea(){

returngetw()*geth();

}

publicstringtostring(){

return"矩形宽:

"+getw()+"矩形高:

"+geth()+"矩形面积:

"+area();}

publicstaticvoidmain(stringargs[]){

myRectanglerectangle=newmyRectangle(1,2,7,8);

system.out.println(rectangle.tostring());

}

}

11.定义一个表示学生的类student,包括的成员变量有:

学号、姓名、性别、年龄;成员方法有:

获得学号、姓名、性别、年龄;修改年龄。

并书写Java程序创建student类的对象及测试其方法的功能。

答:

publicclassstudent{

privatestringnumber,name;

privatebooleansex;//true表示“男”,false表示“女”

privateintage;

student(){

number="";;sex=true;age=0

}

student(stringnum,stringna,booleans,inta){

number=num;sex=s;age=a;

}

publicstringgetnumber(){

returnnumber;

}

publicstringgetname(){

returnname;

}

publicbooleangetsex(){

returnsex;

}

publicintgetAge(){

returnage;

}

publicvoidsetAge(inta){

age=a;

}

publicstringtostring(){

return"学号:

"+number+"姓名:

"+name+"性别:

"+sex+"年龄:

"+age;

}

篇二:

mydog

mydog

whatanimaldoyoulike?

Ilikethedog.Ihaveone.hernameiscici.herbodyisbrown.shehasbigears,roundeyesandashorttail.everyonewillhavetemper,thedogisnoexcepion.ciciis.Ifsomeonemethertail,shewillopenhermouthtobitehim.butshesometimesisverylovely.Aslongassomeonegavehertoeat,shewillnotonlylickinghishand,willheshooktail.how,Doescicicute?

篇三:

myfavouritepet(dog)

英语大作战

英语大作战之作文《myfavouritepet》制作人:

痴人笑看事无常编号:

20XX510160213myfavouritepetIhaveadog.heismyfavoritepet.heisverylovely.hisnameismikeandheisoneyearold.hisfurislongandwhite.hehasbigblackeyes.hisnoseisverygood.hecansmellverywell.heisquitesmall.heweighsabouttwokilograms.mike’sfavoritefoodismeat.healsolikesbones.

mikeisveryfriendly.Ifeedhimeveryday.heneverbarksorbites.mikelikeslotsofexercise.Itisnecessarytowalkthedogintheparkeverydayifyouwantittobehealthy.soIplaywithhimeverydayinthepark.mikelikestoruninthepark.heoftenchasescatsandbirds.Itisveryinteresting.mikecanfindthewaybackeasily.Ithinkheisthecleverestanimalofall.

Ilikemydogandhelovesmetoo.heisveryhealthy.Allmyfamilylikehim.welookafterhimverycarefully.I’llmakeasmallandlovelyhouseforhim.Ithinkhewillbehappytolivethere.Doyoulikemydog?

IhavealovelylttledognamedDion.helooksprettywithshortlegs,bigearsandshorttail.heismygoodfriendandheisalsoeasytotakecareof.Iwalkhimatleasttwiceaday,feedhimandspendtimewithhim.healsogiveshislovetomeinreturn.heisalwaystheretolickmeandlieonme.Ilikeplayingwithhim.IthinkhecantellwhenIamhappy,sad,angryortoubled.sometimeshecanbenoisyandrunaroundtheroom.

Inaword,heisnotonlymydog,butalsomyfriend.

manypeoplelikekeepingananimalasapet.,Ilikesmallanimalsverymuch,soIhavealovelylittledog,hisnameisDion.(IhavealovelylittledognamedDion).Thedogisbrown,hehasfourshortlegs,sohelooksverypretty(漂亮).hehastwobigearsandashorttail.heismygoodfriendandheisalsoeasytotakecareof.heisloyaltomyfamiliesandme,sowealllikehim.IoftentakesawalkwithhimafterIfinishmywork,andIlikeplayingwithhim,feedinghimandspendingtimewithhim.healsogiveshislovetomeinreturn.hecanhelpmyfamilylookafterourhouse.IthinkhecantellwhenIamhappy,sad,angryorintouble.butsometimeshecanbenoisyandrunaroundtheroom.Atnightwhenhemakesmuchnoise,Ican’tsleepwell.Andhelikesholdingthingsinhismouthhereandthere,InthemorningwhenIgetup,Idon’toftenfindmyshoes,Thatmakesmeunhappy.

Inaword,heisnotonlymydog,butalsomyfriend.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 初中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1