全国Java语言程序设计自考试题.docx

上传人:b****3 文档编号:11629865 上传时间:2023-06-01 格式:DOCX 页数:66 大小:992.59KB
下载 相关 举报
全国Java语言程序设计自考试题.docx_第1页
第1页 / 共66页
全国Java语言程序设计自考试题.docx_第2页
第2页 / 共66页
全国Java语言程序设计自考试题.docx_第3页
第3页 / 共66页
全国Java语言程序设计自考试题.docx_第4页
第4页 / 共66页
全国Java语言程序设计自考试题.docx_第5页
第5页 / 共66页
全国Java语言程序设计自考试题.docx_第6页
第6页 / 共66页
全国Java语言程序设计自考试题.docx_第7页
第7页 / 共66页
全国Java语言程序设计自考试题.docx_第8页
第8页 / 共66页
全国Java语言程序设计自考试题.docx_第9页
第9页 / 共66页
全国Java语言程序设计自考试题.docx_第10页
第10页 / 共66页
全国Java语言程序设计自考试题.docx_第11页
第11页 / 共66页
全国Java语言程序设计自考试题.docx_第12页
第12页 / 共66页
全国Java语言程序设计自考试题.docx_第13页
第13页 / 共66页
全国Java语言程序设计自考试题.docx_第14页
第14页 / 共66页
全国Java语言程序设计自考试题.docx_第15页
第15页 / 共66页
全国Java语言程序设计自考试题.docx_第16页
第16页 / 共66页
全国Java语言程序设计自考试题.docx_第17页
第17页 / 共66页
全国Java语言程序设计自考试题.docx_第18页
第18页 / 共66页
全国Java语言程序设计自考试题.docx_第19页
第19页 / 共66页
全国Java语言程序设计自考试题.docx_第20页
第20页 / 共66页
亲,该文档总共66页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

全国Java语言程序设计自考试题.docx

《全国Java语言程序设计自考试题.docx》由会员分享,可在线阅读,更多相关《全国Java语言程序设计自考试题.docx(66页珍藏版)》请在冰点文库上搜索。

全国Java语言程序设计自考试题.docx

全国Java语言程序设计自考试题

全国2011年1月Java语言程序设计自考试题

实验指导书。

运行结果:

使用switch语句

1)编写程序用swith语句实现从键盘读如1,2,3时,屏幕提示不同信息。

代码见实验指导书。

运行结果:

3、使用循环结构

使用for语句

1)程序源代码见实验指导书。

2)改正源程序中错误:

publicstaticvoidmain(Stringargs[])throwsjava.io.IOException

 

运行结果:

使用while语句

1)程序源代码实验指导书:

运行结果:

多重循环,输出九九乘法表的程序。

运行结果:

4、编程题,编写程序并写出运行结果

1)用分支语句编程,输入一个学生成绩,给出相应等级:

90~100优80~89良70~79中69~60及格0~59不及格

代码:

importjava.io.*;

classaaa{

publicstaticvoidmain(Stringargs[])throwsIOException

{

BufferedReaderstdin=newBufferedReader(newInputStreamReader(System.in));

System.out.println("Pleasepressthescorein:

");

intnum=Integer.parseInt(stdin.readLine());

num=(int)(num/10);

switch(num){

case10:

case9:

System.out.println("Thestudent'slevelis'A'!

");

break;

case8:

System.out.println("Thestudent'slevelis'B'!

");

break;

case7:

System.out.println("Thestudent'slevelis'C'!

");

break;

case6:

System.out.println("Thestudent'slevelis'D'!

");

break;

case5:

case4:

case3:

case2:

case1:

case0:

System.out.println("Thestudent'slevelis'E'!

");

break;

default:

System.out.println("Thekeypressiswrong!

");

}

}

}

结果:

2)采用for循环求1至1000之内的所有“完全数”。

所谓“完全数”是指一个数,恰好等于它的因子之和。

例如,6是一个完全数,因为6的因子为1、2、3,而6=1十2十30

代码:

classaaa{

publicstaticvoidmain(Stringargs[])

{

System.out.println("Nowlookingforthenumberbetween1to1000:

");

for(inti=1;i<=1000;i++)

{

intsum=0;

for(intj=1;j<=i/2;j++)

{

if(i%j==0)sum+=j;

}

if(sum==i)

System.out.print("\t"+i+"\t");

}

System.out.println();

System.out.println("selectover!

");

}

}结果:

3)一个整数的各位数字之和能被9整除,则该数也能被9整除。

编程验证给定的整数能否被9整除。

代码:

importjava.io.*;

classsss

{

publicstaticvoidmain(Stringarg[])throwsIOException

{

BufferedReaderstdin=newBufferedReader(newInputStreamReader(System.in));

intnum=0;

intNO;

System.out.print("请输入给定的整数:

");

Stringstr=stdin.readLine();

NO=Integer.parseInt(str);

for(inti=0;i

{

num+=NO%10;

NO=(int)(NO/10);

}

if(num%9==0)

System.out.println(str+"可以被9整除。

");

else

System.out.println(str+"不可以被9整除。

");

}

}

结果:

4)已知XYZ+YZZ=532,其中X、Y和Z为数字,编程求出X,Y和Z的值。

代码:

classsss{

publicstaticvoidmain(Stringarg[])

{

intX,Y,Z;

for(X=1;X<=4;X++)

for(Y=1;Y<=4;Y++)

for(Z=0;Z<=2;Z++)

{

intsum=(100*X+10*Y+Z)+(100*Y+10*Z+Z);

if(sum==532)

System.out.println("X="+X+",Y="+Y+",Z="+Z);

}

}}

结果:

实验二:

面向对象编程

实验目的:

通过编程和上机实验理解Java语言是如何体现面向对象编程基本思想,熟悉类的封装方法以及如何创建类和对象,熟悉成员变量和成员方法的特性,熟悉类的继承性和多态性的作用,熟悉包、接口的使用方法,掌握OOP方式进行程序设计的方法。

实验要求:

1、编写程序实现类的定义和使用。

2、编写不同成员和不同成员方法修饰方法的程序。

3、编写体现类的继承性(成员变量、成员方法、成员变量隐藏)的程序和多态性(成员方法重载、构造方法重载)的程序。

4、编写接口的定义和使用的程序。

5、编写包的定义和使用的程序。

实验内容:

1、类的定义和使用

代码:

classTable{

publicStringname;

publicintweight,X,Y,high;

publicTable(Stringnwname,intnwweight,intnwX,intnwY,intnwhigh)

{

this.name=nwname;

this.weight=nwweight;

this.X=nwX;

this.Y=nwY;

this.high=nwhigh;

}

publicintArea(intX,intY)

{

intArea=X*Y;

returnArea;

}

publicvoidChangeWeight(intNewWeight)

{

weight=NewWeight;

}

publicvoidDisplay()

{

System.out.println("TheTable'sName:

Name="+name);

System.out.println("TheTable'sSize:

X="+X+",Y="+Y);

System.out.println("TheTable'sArea:

Area="+Area(X,Y));

System.out.println("TheTable'sWeight:

Weight="+weight);

System.out.println("TheTable'sHigh:

High="+high);

}

publicstaticvoidmain(Stringarg[])

{

System.out.println("NowDisplayMyTable'sInformation:

");

Tabletb=newTable("Mytable",170,10,15,75);

tb.Display();

System.out.println("NowChangeMyTable'sWeightto200!

");

tb.ChangeWeight(200);

System.out.println("NowDisplayMyTable'sNewInformation:

");

tb.Display();

}

}

结果:

2、修饰符的使用

程序功能:

通过两个类StaticDemo、TestDemo说明静态变量/方法与实例变量/方法的区别,程序源代码见实验指导书。

对源程序进行编译,查错并运行。

静态方法不能访问实例变量。

3、继承和多态的作用

代码:

abstractclassRodent

{

Stringname;

publicStringgetName()

{

returnname;

}

publicvoidsetName(Stringname)

{

this.name=name;

}

publicabstractvoidrun();

}

classMouseextendsRodent

{

publicvoidrun(){}

}

classGerbilextendsMouse{}

classHamsterextendsMouse{}

4、接口的定义和使用

5、包的定义和使用

􀁺创建自定义包Mypackage

􀁺在包中创建类,编译Test_YMD.java文件,分析运行结果。

􀁺编写使用包Mypackage中Test_YMD类的程序,编译并运行程序,分析程序的运行结果。

6、编程题,编写程序并写出运行结果

(1)创建一个名称为Pay的类,该类包括工作小时、每小时工资、扣缴率、应得工资总额和实付工资等5个双精度型的成员变量。

创建3个重载的应得工资computeNetPay()方法。

应得工资是工时乘以每小时工资的计算结果。

当computeNetPay()接收代表小时、扣缴率和工资率的数值时,计算出应得工资=工作小时*每小时工资*(1—扣缴率)*(1—工资率)。

当computeNetPay()接收两个参数时,扣缴率假定为15%,计算出应得工资=工作小时*每小时工资*(1—0.15)*(1—工资率)

当computeNetPay()接收一个参数时,扣缴率假定为15%,每小时工资率为4.65%。

同时编写一个测试类,该测试类的main方法测试所有3个重载的方法。

代码:

classPay

{

doublework_hour,price=100,deduct_rate;

doubleshould_pay,real_pay=0;

publicdoublecomputeNetPay(doublework_hour)

{

should_pay=work_hour*this.price*(1-0.15)*(1-0.0465);

returnshould_pay;

}

publicdoublecomputeNetPay(doublework_hour,doubleprice_rate)

{

should_pay=work_hour*this.price*(1-0.15)*(1-price_rate);

returnshould_pay;

}

publicdoublecomputeNetPay(doublework_hour,doubleprice_rate,doublededuct_rate)

{

should_pay=work_hour*this.price*(1-deduct_rate)*(1-price_rate);

returnshould_pay;

}

publicvoiddisplay()

{

System.out.println("Theemployeeshouldbepay$"+should_pay);

}

publicstaticvoidmain(Stringargs[])

{

Paypay=newPay();

System.out.println("Thefirstemployeeworked8hour");

puteNetPay(8.0);

pay.display();

System.out.println("Thefirstemployeeworked8hourandprice_rate8%");

puteNetPay(8.0,0.08);

pay.display();

System.out.println("Thefirstemployeeworked8hourandprice_rate10%anddeduct_rateis18%");

puteNetPay(8.0,0.1,0.18);

pay.display();

}

}

结果:

(2)商店销售某一件商品,商店每天公布统一的折扣(discount)。

同时允许销售人员在销售时灵活掌握价格(price),在统一折扣的基础上,对一次购入10件以上者,还可以销售9.5折优惠。

现已知当天5名售货员的销售情况为:

售货员编号(num)销售件数(quantity)销售单价(price)

1013126.8

2218125.6

32510124.8

10845123.4

901100121.5

编写销售商品类Sale和含有main方法的公共类Test,计算当天此商品的总销售额sum,以及每件商品的平均售价,并在显示器上显示。

代码:

importjava.io.*;

classSale

{

doublediscount,price;

intsale_sum=0;

doublesum=0.0,price_avg=0.0;

publicvoidchange_discount(doublediscount)

{

this.discount=discount;

}

publicvoidSaleCount(intperson_sale_num,doubleprice)

{

if(person_sale_num<10)

{

sum+=price*discount*person_sale_num;

}

else

{

sum+=0.95*price*discount*person_sale_num;

}

sale_sum+=person_sale_num;

price_avg=sum/sale_sum;

//returnsale_sum;

}

publicvoiddisplay()

{

System.out.println("Thetotalsalenamberis:

"+sale_sum);

System.out.println("Thetotalsalemoneyis:

"+sum);

System.out.println("Theavgofthepriceis:

"+price_avg);

}

}

classTest

{

publicstaticvoidmain(Stringargs[])throwsIOException

{

Salesale=newSale();

System.out.println("Pressthediscount:

");

BufferedReaderstdin=newBufferedReader(newInputStreamReader(System.in));

doubledisc=Double.parseDouble(stdin.readLine());

sale.change_discount(disc);

System.out.println("Thesaler101saled3goodsatprice126.8;");

sale.SaleCount(3,126.8);

System.out.println("Thesaler221saled8goodsatprice125.6;");

sale.SaleCount(8,125.6);

System.out.println("Thesaler325saled10goodsatprice124.8;");

sale.SaleCount(10,124.8);

System.out.println("Thesaler108saled45goodsatprice123.4;");

sale.SaleCount(45,123.4);

System.out.println("Thesaler901saled100goodsatprice121.5;");

sale.SaleCount(100,121.5);

sale.display();

}

}

结果:

(3)定义接口Shape及其抽象方法getArea()和getPerimeter()用于计算图形和面积和周长。

定义类Rectangle(矩形)、类Circle(圆形)、类Triangle(三角形),要求这些类继承点类Coordinates()并实现接口的抽象方法。

代码:

classTest

{

publicstaticvoidmain(Stringargs[])

{

circlec=newcircle();

c.display();

rectangler=newrectangle();

r.display();

trianglet=newtriangle();

t.display();

}

}

interfacecal_Shape

{

finaldoublePI=3.14;

abstractdoublegetArea(doubler);

abstractdoublegetArea(doublea,doubleb);

abstractdoublegetArea(doublea,doubleb,doublec);

abstractdoublegetPerimeter(doubler);

abstractdoublegetPerimeter(doublea,doubleb);

abstractdoublegetPerimeter(doublea,doubleb,doublec);

}

classCoordinatesimplementscal_Shape

{

publicdoublegetArea(doubler)

{

returnPI*r*r;

}

publicdoublegetPerimeter(doubler)

{

return2*PI*r;

}

publicdoublegetArea(doublea,doubleb)

{

returna*b;

}

publicdoublegetPerimeter(doublea,doubleb)

{

return2*(a+b);

}

publicdoublegetArea(doublea,doubleb,doublec)

{

doubles=(a+b+c)/2;

returnMath.sqrt(s*(s-a)*(s-b)*(s-c));

}

publicdoublegetPerimeter(doublea,doubleb,doublec)

{

returna+b+c;

}

}

classcircleextendsCoordinates

{

publicvoiddisplay()

{

doublecircle_area,circle_perimeter;

Coordinatesx=newCoordinates();

circle_area=x.getArea(2.0);

circle_perimeter=x.getPerimeter(2.0);

System.out.println("半径为2的圆的面积为:

"+circle_area+",周长为:

"+circle_perimeter);

}

}

classrectangleextendsCoordinates

{

publicvoiddisplay()

{

doublerectangle_area,rectangle_perimeter;

Coordinatesy=newCoordinates();

rectangle_area=y.getArea(2.0,3.0);

rectangle_perimeter=y.getPerimeter(2.0,3.0);

System.out.println("长,宽为2,3的矩形的面积为:

"+rectangle_area+",周长为:

"+rectangle_perimeter);

}}

classtriangleextendsCoordinates

{

publicvoiddisplay()

{

doubletriangle_area,triangle_perimeter;

Coordinatesz=newCoordinates();

triangle_area=z.getArea(3.0,4.0,5.0);

triangle_perimeter=z.getPerimeter(3.0,4.0,5.0);

System.out.println("三边长为3,4,5的三角形的面积为:

"+

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

当前位置:首页 > IT计算机 > 电脑基础知识

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

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