Java练习题附答案.docx

上传人:b****1 文档编号:14008668 上传时间:2023-06-20 格式:DOCX 页数:16 大小:18.88KB
下载 相关 举报
Java练习题附答案.docx_第1页
第1页 / 共16页
Java练习题附答案.docx_第2页
第2页 / 共16页
Java练习题附答案.docx_第3页
第3页 / 共16页
Java练习题附答案.docx_第4页
第4页 / 共16页
Java练习题附答案.docx_第5页
第5页 / 共16页
Java练习题附答案.docx_第6页
第6页 / 共16页
Java练习题附答案.docx_第7页
第7页 / 共16页
Java练习题附答案.docx_第8页
第8页 / 共16页
Java练习题附答案.docx_第9页
第9页 / 共16页
Java练习题附答案.docx_第10页
第10页 / 共16页
Java练习题附答案.docx_第11页
第11页 / 共16页
Java练习题附答案.docx_第12页
第12页 / 共16页
Java练习题附答案.docx_第13页
第13页 / 共16页
Java练习题附答案.docx_第14页
第14页 / 共16页
Java练习题附答案.docx_第15页
第15页 / 共16页
Java练习题附答案.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Java练习题附答案.docx

《Java练习题附答案.docx》由会员分享,可在线阅读,更多相关《Java练习题附答案.docx(16页珍藏版)》请在冰点文库上搜索。

Java练习题附答案.docx

Java练习题附答案

练习题:

一、读程序,写出和程序输出格式一致的输出结果。

1、

classJ_SuperClass{

voidmb_method(){

System.out.println("J_SuperClass:

:

mb_method");

}

staticvoidmb_methodStatic(){

System.out.println("J_SuperClass:

:

mb_methodStatic");}

}

publicclassJ_TestextendsJ_SuperClass{

voidmb_method(){

System.out.println("J_Test:

:

mb_method");

}

staticvoidmb_methodStatic(){

System.out.println("J_Test:

:

mb_methodStatic");

}

publicstaticvoidmain(String[]args){

J_SuperClassa=newJ_Test();

a.mb_method();

a.mb_methodStatic();

J_Testb=newJ_Test();

b.mb_method();

b.mb_methodStatic();

}

}

输出结果:

3

2

3

4

2、

classValHold{

publicinti=10;

}

publicclassObParm{

publicstaticvoidmain(String[]args){

ObParmo=newObParm();

o.amethod();

}

publicvoidamethod(){

inti=99;

ValHoldv=newValHold();

v.i=30;

another(v,i);

System.out.println(v.i+""+i);

}

publicvoidanother(ValHoldv,inti){

i=0;

v.i=20;

ValHoldvh=newValHold();

v=vh;

System.out.println(v.i+""+i);

}

}

输出结果:

100

2099

3、

classJ_SuperClass{

intm_data;

J_SuperClass(){

mb_add

(1);

}

publicvoidmb_add(inti){

m_data+=i;

}

publicvoidmb_print(){System.out.println(m_data);

}

}

classJ_SubClassextendsJ_SuperClass{J_SubClass(){

mb_add

(2);

}

publicvoidmb_add(inti){

m_data+=i*2;

}

}

publicclassJ_Test{

publicstaticvoidmb_method(J_SuperClassa){a.mb_add(6);

a.mb_print();

}

publicstaticvoidmain(String[]args){

mb_method(newJ_SubClass());

}

}

输出结果:

18

 

4、

publicclassTwo{

publicstaticvoidmain(String[]args){

Oneone=newOne("Hello");

one.getString("Xiaoming!

");

}

}

classOne{

privateStringmyStr;

publicOne(Stringstr){

myStr=str;

}

publicvoidgetString(Stringstr){System.out.println(myStr+""+str);

}

}

输出结果:

HelloXiaoming!

5、

publicclassJ_Test{

Stringm_kobe="1";

String[]m_king={"2"};

publicstaticvoidmain(String[]args){

J_Testapp=newJ_Test();

app.mb_operate(app.m_kobe,app.m_king);System.out.println(app.m_kobe+app.m_king[0]);}

staticvoidmb_operate(Stringkobe,String[]king){kobe=newString("3");

king[0]=newString("4");

}

}

输出结果:

14

1.某公司正进行招聘工作,被招聘人员需要填写个人信息,编写“个人简历”的封装类。

包括如下属性和对属性进行操作的方法。

Stringxm;//姓名

Stringxb;//性别

intnl;//年龄

Stringjtzz;//家庭住址

Stringxl;//学历

publicclasstest4_17

{

privateStringxm;//姓名

privateStringxb;//性别

privateintnl;//年龄

privateStringjtzz;//家庭住址

privateString;//学历

publicStringgetXm(){

returnxm;

}

publicvoidsetXm(Stringxm){

this.xm=xm;

}

publicStringgetXb(){

returnxb;

}

publicvoidsetXb(Stringxb){

this.xb=xb;

}

publicintgetNl(){

returnnl;

}

publicvoidsetNl(intnl){

this.nl=nl;

}

publicStringgetJtzz(){

returnjtzz;

}

publicvoidsetJtzz(Stringjtzz){

this.jtzz=jtzz;

}

publicStringget(){

return;

}

publicvoidsetXl(Stringxl){

this.xl=xl;

}

publictest4_17(Stringxm,Stringxb,intnl,Stringjtzz,Stringxl){Super();

this.xm=xm;

this.xb=xb;

this.nl=nl;

this.jtzz=jtzz;

this.xl=xl;

}

publictest4_17(){

Super();

}}

publicstaticvoidmain(String[]args){

test4_17a=newtest4_17("张三","男",12,"上海","大学");

System.out.println(a.xm+""+a.jtzz+""+a.nl+""+a.xb+""+a.xl);}

2.定义一个动物类(Animal)为抽象类:

成员变量(weight、age)、成员函数(getAge()、getWeight()、setAge(int)、setWeight(int)、叫声meow())。

根据Animal类派生出猫(Cat)类,猫所具有的成员变量(weight、age、color),成员函数(getAge()、getWeight()、setAge(int)、setWeight(int)、setColor(char[])、getColor()、run()、meow())。

并在main函数中创建一只猫,输出它的年龄、毛色和叫声。

 

3.创建一个接口IShape,接口中有一个求取面积的抽象方法“publicdoublearea()”。

定义一个正方形类Square,该类实现了IShape接口。

Square类中有一个属性a表示正方形的边长,在构造方法中初始化该边长。

定义一个主类,在主类中,创建Square类的实例对象,求该正方形对象的面积。

 

4.定义一个人类,包括属性:

姓名、性别、年龄、国籍;包括方法:

吃饭、睡觉,工作。

(1)根据人类,派生一个学生类,增加属性:

学校、学号;重写工作方法(学生的工作是学习)。

(2)根据人类,派生一个工人类,增加属性:

单位、工龄;重写工作方法(工人的工作是„„自己想吧)。

(3)根据学生类,派生一个学生干部类,增加属性:

职务;增加方法:

开会。

(4)编写主函数分别对上述3类具体人物进行测试。

packagepr2;

publicclassPeople{

Stringxm;

Stringxb;

intnl;

Stringgj;

publicvoidchifan(){

System.out.println("正在吃饭");

}

publicvoidshuijiao(){

System.out.println("正在睡觉");

}

publicvoidgongzuo(){

System.out.println("正在工作");

}

}

(2)学生类:

packagepr2;

publicclassXueshengextendsPeople{

Stringxx;

intxh;

publicvoidgongzuo(){

System.out.println("正在学习");

}

}

(3)工人类:

packagepr2;

publicclassGongrenextendsPeople{

Stringdw;

intgl;

publicvoidgongzuo(){

System.out.println("正在生产汽车");

}

}

(4)干部类:

packagepr2;

publicclassGanbuextendsXuesheng{

Stringzw;

publicvoidkaihui(){

System.out.println("正在开会");

}

}

(5)主类:

packagepr2;

publicclasslx2_1_6{

publicstaticvoidmain(String[]args){

Xueshengxs=newXuesheng();

xs.chifan();xs.shuijiao();xs.gongzuo();

Gongrengr=newGongren();

gr.chifan();gr.shuijiao();

gr.gongzuo();

Ganbugb=newGanbu();

gb.chifan();gb.shuijiao();gb.gongzuo();

gb.kaihui();

}

}

5、根据软件设计需求,需要设计Dog、Cat、Bird、Butterfly等4个类,要求如下:

Dog类:

表示狗对象,提供speak方法。

Cat类:

表示猫对象,提供speak方法。

Bird类:

表示鸡对象,提供speak方法、fly方法。

Butterfly类:

表示蝴蝶对象,提供fly方法。

采用接口的回调技术实现要求。

 

6、市场上有很多种类型的门,比如普通木门、卷闸门、防火门、防盗门等。

为了更加安全发挥的功能,现对防火门和防盗门分别增加警报功能。

比如:

当有火灾发生时,防火门会发出警报响声;有人入室行窃时,防盗门会发出警报声。

而普通木门和卷闸门没有警报功能。

请用Java语言来设计这些门。

packagech06;

abstractclassDoor

{

protectedStringmetrial;

publicabstractvoidopen();

publicabstractvoidclose();

publicvoidsetMetrial(Stringm)

{

this.metrial=m;

}

interfaceCall{

voidcall();

}

classWoodDoorextendsDoor

{

publicvoidopen()

{

System.out.println("这是一个普通的木门,用简单的钥匙就能打开!

");

}

publicvoidclose()

{

System.out.println("这是一个普通的木门,用力一拉就可关上,但安全性不高!

");}

}

classRollDoorextendsDoor

{

publicvoidopen()

{

System.out.println("这是一个卷闸门,开门的时候打开锁,开启电动机,向上卷动!

");

}

publicvoidclose()

{

System.out.println("这是一个卷闸门,关门的时候开启电动机向下卷动,放下门!

");

}

}

classFireProofDoorextendsDoorimplementsCall{

publicvoidopen()

{

System.out.println("这是一个防火门,开门的时候要用力向外推动!

");

}

publicvoidclose()

{

System.out.println("防火门是常处于关闭状态的!

");

}

publicvoidcall()

{

System.out.println("起火了!

");

}

}

classDoor2extendsDoor

publicvoidopen()

{

System.out.println("这是一个卷闸门,开门的时候打开锁,开启电动机,向上卷动!

");

}

publicvoidclose()

{

System.out.println("这是一个卷闸门,关门的时候开启电动机向下卷动,放下门!

");

}

publicvoidcall()

{

System.out.println("失窃了!

");

}

}

publicclassUseDoor{

}

7.编写一个计算图形面积的程序,程序应当能够计算并输出矩形、圆的面积。

分析:

考虑到程序的未来扩展,设计一个图形抽象类:

Shape,在此基础上派生出图形类

Rectangle类和Circle类。

abstractclassShape{

publicabstractdoublegetArea();

}

classRectangleextendsShape{

privatedoublewidth;

privatedoubleheight;

publicRectangle(doublew,doubleh){

width=w;

height=h;

}

publicRectangle(){

this(0,0);

}

publicdoublegetArea(){

returnwidth*height;

publicdoublegetWidth(){

returnwidth;

}

publicvoidsetWidth(doublew){

width=w;

}

publicdoublegetHeight(){

returnheight;

}

publicvoidsetHeight(inth){

height=h;

}

publicStringtoString(){

return("Rectangle:

width="+width+",height="+height);}

}

classCircleextendsShape{

privatedoublex;

privatedoubley;

privatedoubleradius;

publicCircle(doublexx,doubleyy,doubler){

x=xx;

y=yy;

radius=r;

}

publicdoublegetArea(){

returnMath.PI*radius*radius;

}

publicdoublegetRadius(){

returnradius;

}

publicvoidsetRadius(doubler){

radius=r;

}

publicdoublegetX(){

returnx;

}

publicvoidsetX(doublexx){

x=xx;

}

publicdoublegetY(){

returny;

publicvoidsetY(doubleyy){

y=yy;

}

publicvoidsetXY(doublexx,doubleyy){

x=xx;

y=yy;

}

publicStringtoString(){

return("Circle:

("+x+","+y+"),radius="+radius);}

}

publicclassTest{

publicstaticvoidmain(String[]args){

Rectangler=newRectangle(10,10);

Circlec=newCircle(10,10,50);System.out.println(r);

System.out.println("area="+r.getArea());System.out.println(c);

System.out.println("area="+c.getArea());

}

}

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

当前位置:首页 > 外语学习 > 法语学习

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2