java试题08285资料.docx
《java试题08285资料.docx》由会员分享,可在线阅读,更多相关《java试题08285资料.docx(9页珍藏版)》请在冰点文库上搜索。
java试题08285资料
java试题08285
1.随机产生20个50~100之间的整数,输出这20个数并找出最大数及最小数输出
publicclassTest1{
publicstaticvoidmain(String[]args){
intmath[]=newint[20];
intmax=0;intmin=100;
for(inti=0;i<20;i++){
math[i]=(int)(Math.random()*50+50);
System.out.print(math[i]+"");
}
System.out.println();
if(maxif(min>math[i])min=math[i];}System.out.println("max:"+max);System.out.println("min:"+min);}}2.创建一个图书类,类中包含的属性有:书名、作者、出版社;包含的方法有:构造方法,设置书籍状态,查看书籍状态。书籍状态有在馆和外借两种。publicclassTest2{publicstaticvoidmain(String[]args){Bookbook=newBook("Java程序设计","李伟","清华大学出版社");book.setzt(true);book.getzt();}}classBook{privateStringBname;privateStringAname;privateStringBaddress;Book(StringBname,StringAname,StringBaddress){this.Bname=Bname;this.Aname=Aname;this.Baddress=Baddress;}privatebooleanzt;publicvoidsetzt(booleanzt){this.zt=zt;}publicvoidgetzt(){if(zt==true)System.out.println("在馆");elseSystem.out.println("外借");}}3.设计一个Birthday类,其成员变量有:year,month,day;提供构造方法、输出Birthday对象值的方法和计算年龄的方法。编写程序测试这个类。publicclassTest3{publicstaticvoidmain(String[]args){Birthdayb=newBirthday(2010,6,8);b.printBirthDay();System.out.println(b.printAge());}}classBirthday{privateintyear;privateintmonth;privateintday;publicBirthday(intyear,intmonth,intday){this.year=year;this.month=month;this.day=day;}publicvoidprintBirthDay(){System.out.println(year+"-"+month+"-"+day);}publicintprintAge(){return2017-year;}}4.编写一个类,描述汽车,其中用字符型描述车的牌号,用浮点型描述车的价格。编写一个测试类,其中有一个修改价格的方法,对汽车对象进行操作,根据折扣数修改汽车的价格,最后在main()方法中输出修改后的汽车信息publicclassTest4{publicstaticvoidmain(String[]args){Carc=newCar("奔驰S6OO",50000);c.dismessage();}}classCar{StringchePai;floatprice;floatprice1;Car(StringchePai,floatprice){this.chePai=chePai;this.price1=price*4/5;this.price=price;}voiddismessage(){System.out.println("这辆车的品牌是"+chePai+"原价是"+price+"打折后为"+price1);}}5.编写一个异常类MyException,再编写一个类Student,该类有一个产生异常的方法speak(intm)。要求参数m的值大于1000时,方法抛出一个MyException对象。最后编写主类,在主方法中创建Student对象,让该对象调用speak()方法。classMyExceptionextendsException{privateintm;MyException(intm){this.m=m;}publicStringgetMessage(){return"出现异常:参数"+this.m+"大于1000";}}classStudent{publicintspeak(intm)throwsMyException{if(m>1000)thrownewMyException(m);returnm;}}publicclassTest5{publicstaticvoidmain(String[]args){try{Students=newStudent();System.out.println("输出的结果是:"+s.speak(5));System.out.println("输出的结果是:"+s.speak(5000));}catch(MyExceptione){System.out.println(e.getMessage());}}}6.单击窗体的关闭按钮时,跳出如下对话框,选择“是”窗体关闭,选择“否”,窗体不关闭importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;classJFrame6{JFrameframe=newJFrame();JFrame15(){frame.setTitle("关闭窗体时,问一声");frame.setBounds(100,100,300,200);frame.setVisible(true);frame.addWindowListener(newmyWindowListener());}classmyWindowListenerextendsWindowAdapter{publicvoidwindowClosing(WindowEvente){intresult=JOptionPane.showConfirmDialog(frame,"你确定要关闭窗体?","确认对话框",JOptionPane.YES_NO_OPTION);if(result==JOptionPane.OK_OPTION){System.exit(0);}elseif(result==JOptionPane.NO_OPTION){frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);}}}}publicclassText6{publicstaticvoidmain(String[]args){newJFrame6();}}7.创建一个File类的对象,首先判断此配置文件是否存在,如果不存在,则调用createNewFile方法创建一个文件,然后从键盘输入字符存入数组里,创建文件输出流,把数组里的字符写入到文件中,最终保存在“Example7.txt”文件中importjava.io.*;publicclassTest7{publicstaticvoidmain(String[]args){Filefile=newFile("D:\\","Example7.txt");byte[]b=newbyte[1000];intn;try{if(!file.exists())file.createNewFile();FileOutputStreamfos=newFileOutputStream(file,true);n=System.in.read(b);fos.write(b,0,n);fos.close();}catch(Exceptione){e.getMessage();}}}9.编写MyThread线程类,在该类中实现九九乘法表的动态输出,每隔1秒输出乘法表中的一个运算结果。publicclassTest9{publicstaticvoidmain(String[]args){Threadt=newMyThread();t.start();}}classMyThreadextendsThread{publicvoidrun(){inti,j;try{for(i=1;i<=9;i++){for(j=1;j<=i;j++){System.out.print(j+"*"+i+"="+i*j+"\t");sleep(1000);}System.out.println();}}catch(Exceptione){e.toString();}}}9.编写类OverLoading,在该类中定义3个方法:一个info()方法是没有参数的,一个info()方法需要使用一个整形参数,一个info()方法需要使用一个String类型参数。在main方法中进行测试。运行结果如下:publicclassTest9{publicstaticvoidmain(String[]args){OverLoadingol=newOverLoading();ol.info();ol.info(5);ol.info("HelloWorld");}}classOverLoading{publicvoidinfo(){System.out.println("您调用的是无参数的方法");}publicvoidinfo(intn){System.out.println("您调用的是整形类型参数的方法,参数是:"+n);}publicvoidinfo(Strings){System.out.println("您调用的是String类型参数的方法,参数是:"+s);}}10.编写类Shape,该类是一个抽象类。在该类中定义一个抽象方法:getArea()。编写类Circle,该类继承自Shape并实现了其抽象方法getArea()。在该类的构造方法中,获得了圆形的半径,以此在getArea()中计算面积。abstractclassShape{publicabstractdoublegetArea();}classCircleextendsShape{privatedoubler;publicCircle(doubler){this.r=r;}publicdoublegetArea(){//returnMath.PI*Math.pow(r,2);return3.14*r*r;}}publicclassTest10{publicstaticvoidmain(String[]args){Circlec=newCircle(3);System.out.println(图形的面积是:"+c.getArea());}}
if(min>math[i])min=math[i];}
System.out.println("max:
"+max);
System.out.println("min:
"+min);}}
2.创建一个图书类,类中包含的属性有:
书名、作者、出版社;包含的方法有:
构造方法,设置书籍状态,查看书籍状态。
书籍状态有在馆和外借两种。
publicclassTest2{
Bookbook=newBook("Java程序设计","李伟","清华大学出版社");
book.setzt(true);
book.getzt();}}
classBook{
privateStringBname;
privateStringAname;
privateStringBaddress;
Book(StringBname,StringAname,StringBaddress){
this.Bname=Bname;
this.Aname=Aname;
this.Baddress=Baddress;}
privatebooleanzt;
publicvoidsetzt(booleanzt){
this.zt=zt;}
publicvoidgetzt(){
if(zt==true)
System.out.println("在馆");
else
System.out.println("外借");}}
3.设计一个Birthday类,其成员变量有:
year,month,day;提供构造方法、输出Birthday对象值的方法和计算年龄的方法。
编写程序测试这个类。
publicclassTest3{
Birthdayb=newBirthday(2010,6,8);
b.printBirthDay();
System.out.println(b.printAge());}}
classBirthday{
privateintyear;
privateintmonth;
privateintday;
publicBirthday(intyear,intmonth,intday){
this.year=year;
this.month=month;
this.day=day;}
publicvoidprintBirthDay(){
System.out.println(year+"-"+month+"-"+day);}
publicintprintAge(){
return2017-year;}}
4.编写一个类,描述汽车,其中用字符型描述车的牌号,用浮点型描述车的价格。
编写一个测试类,其中有一个修改价格的方法,对汽车对象进行操作,根据折扣数修改汽车的价格,最后在main()方法中输出修改后的汽车信息
publicclassTest4{
Carc=newCar("奔驰S6OO",50000);
c.dismessage();}}
classCar{
StringchePai;
floatprice;
floatprice1;
Car(StringchePai,floatprice){
this.chePai=chePai;
this.price1=price*4/5;
this.price=price;}
voiddismessage(){
System.out.println("这辆车的品牌是"+chePai+"原价是"+price+"打折后为"+price1);}}
5.编写一个异常类MyException,再编写一个类Student,该类有一个产生异常的方法speak(intm)。
要求参数m的值大于1000时,方法抛出一个MyException对象。
最后编写主类,在主方法中创建Student对象,让该对象调用speak()方法。
classMyExceptionextendsException{
privateintm;
MyException(intm){
this.m=m;}
publicStringgetMessage(){
return"出现异常:
参数"+this.m+"大于1000";}}
classStudent{
publicintspeak(intm)throwsMyException{
if(m>1000)thrownewMyException(m);
returnm;}}
publicclassTest5{
try{
Students=newStudent();
System.out.println("输出的结果是:
"+s.speak(5));
"+s.speak(5000));
catch(MyExceptione){
System.out.println(e.getMessage());}}}
6.单击窗体的关闭按钮时,跳出如下对话框,选择“是”窗体关闭,选择“否”,窗体不关闭
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
classJFrame6{
JFrameframe=newJFrame();
JFrame15(){
frame.setTitle("关闭窗体时,问一声");
frame.setBounds(100,100,300,200);
frame.setVisible(true);
frame.addWindowListener(newmyWindowListener());}
classmyWindowListenerextendsWindowAdapter{
publicvoidwindowClosing(WindowEvente){
intresult=JOptionPane.showConfirmDialog(frame,"你确定要关闭窗体?
","确认对话框",JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.OK_OPTION){
System.exit(0);}
elseif(result==JOptionPane.NO_OPTION){
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}}}}
publicclassText6{
newJFrame6();}}
7.创建一个File类的对象,首先判断此配置文件是否存在,如果不存在,则调用createNewFile方法创建一个文件,然后从键盘输入字符存入数组里,创建文件输出流,把数组里的字符写入到文件中,
最终保存在“Example7.txt”文件中
importjava.io.*;
publicclassTest7{
Filefile=newFile("D:
\\","Example7.txt");
byte[]b=newbyte[1000];
intn;
if(!
file.exists())
file.createNewFile();
FileOutputStreamfos=newFileOutputStream(file,true);
n=System.in.read(b);
fos.write(b,0,n);
fos.close();}
catch(Exceptione){
e.getMessage();}}}
9.编写MyThread线程类,在该类中实现九九乘法表的动态输出,每隔1秒输出乘法表中的一个运算结果。
publicclassTest9{
Threadt=newMyThread();
t.start();}}
classMyThreadextendsThread{
publicvoidrun(){
inti,j;
for(i=1;i<=9;i++){
for(j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+i*j+"\t");
sleep(1000);}
System.out.println();}}
e.toString();}}}
9.编写类OverLoading,在该类中定义3个方法:
一个info()方法是没有参数的,一个info()方法需要使用一个整形参数,一个info()方法需要使用一个String类型参数。
在main方法中进行测试。
运行结果如下:
OverLoadingol=newOverLoading();
ol.info();
ol.info(5);
ol.info("HelloWorld");
classOverLoading{
publicvoidinfo(){
System.out.println("您调用的是无参数的方法");
publicvoidinfo(intn){
System.out.println("您调用的是整形类型参数的方法,参数是:
"+n);
publicvoidinfo(Strings){
System.out.println("您调用的是String类型参数的方法,参数是:
"+s);
10.编写类Shape,该类是一个抽象类。
在该类中定义一个抽象方法:
getArea()。
编写类Circle,该类继承自Shape并实现了其抽象方法getArea()。
在该类的构造方法中,
获得了圆形的半径,以此在getArea()中计算面积。
abstractclassShape{
publicabstractdoublegetArea();
classCircleextendsShape{
privatedoubler;
publicCircle(doubler){
this.r=r;
publicdoublegetArea(){
//returnMath.PI*Math.pow(r,2);
return3.14*r*r;
publicclassTest10{
Circlec=newCircle(3);
System.out.println(图形的面积是:
"+c.getArea());
}}
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2