java试题(最新整理)_第1页
java试题(最新整理)_第2页
java试题(最新整理)_第3页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1. 随机产生 20 个 50100 之间的整数,输出这 20 个数并找出最大数及最小数输出public class test1 public static void main(string args) int math = new int20;int max = 0;int min = 100; for(int i = 0;i20;i+)mathi = (int)(math.random()*50+50); system.out.print(mathi+ );system.out.println(); for(int i = 0;i20;i+)if(maxmathi) min=mathi;s

2、ystem.out.println(max:+max); system.out.println(min:+min);2. 创建一个图书类,类中包含的属性有:书名、作者、出版社;包含的方法有:构造方法,设置书籍状态,查看书籍状态。书籍状态有在馆和外借两种。public class test2 public static void main(string args)book book=new book(java 程序设计,李伟,清华大学出版社);book.setzt(true); book.getzt();class bookprivate string bname; private string

3、 aname; private string baddress;book(string bname,stringaname,stringbaddress) this.bname=bname;this.aname=aname; this.baddress=baddress;privatebooleanzt;public void setzt(booleanzt) this.zt=zt;public void getzt() if(zt=true)system.out.println(在馆); elsesystem.out.println(外借);3. 设计一个 birthday 类,其成员变量有

4、:year,month,day;提供构造方法、输出 birthday 对象值的方法和计算年龄的方法。编写程序测试这个类。public class test3 public static void main(string args) birthday b=new birthday(2010,6,8); b.printbirthday(); system.out.println(b.printage();class birthday privateint year; privateint month; privateint day;public birthday(intyear,intmonth,

5、int day) this.year=year;this.month=month; this.day=day;public void printbirthday() system.out.println(year+-+month+-+day);publicintprintage() return 2017-year;4. 编写一个类,描述汽车,其中用字符型描述车的牌号,用浮点型描述车的价格。编写一个测试类,其中有一个修改价格的方法,对汽车对象进行操作,根据折扣数修改汽车的价格,最后在 main()方法中输出修改后的汽车信息public class test4 public static voi

6、d main(string args)car c=new car(奔驰 s6oo,50000); c.dismessage();class carstring chepai; float price; float price1;car(string chepai,float price) this.chepai=chepai; this.price1=price*4/5; this.price=price;void dismessage()system.out.println(这辆车的品牌是+chepai+原价是+price+打折后为+price1);5. 编写一个异常类 myexceptio

7、n,再编写一个类 student,该类有一个产生异常的方法 speak(int m)。要求参数 m 的值大于 1000 时,方法抛出一个 myexception 对象。最后编写主类,在主方法中创建 student 对象,让该对象调用 speak()方法。classmyexception extends exception privateint m;myexception(int m) this.m=m; public string getmessage()return 出现异常:参数+this.m+大于 1000; class studentpublicint speak(int m)thro

8、wsmyexception if(m1000) throw new myexception(m); return m; public class test5 publicstatic void main(string args) trystudent s=new student();system.out.println(输出的结果是:+s.speak(5); system.out.println(输出的结果是:+s.speak(5000);catch(myexception e) system.out.println(e.getmessage(); 6. 单击窗体的关闭按钮时,跳出如下对话框,

9、选择“是”窗体关闭, 选择“否”,窗体不关闭importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; class jframe6jframe frame=new jframe(); jframe15()frame.settitle(关闭窗体时,问一声); frame.setbounds(100,100,300,200); frame.setvisible(true);frame.addwindowlistener(new mywindowlistener(); classmywindowlistener extends win

10、dowadapterpublic void windowclosing(windowevent e)int result=joptionpane.showconfirmdialog(frame,你确定要关闭窗体?,确认对话框,joptionpane.yes_no_option);if(result=joptionpane.ok_option) system.exit(0);else if(result=joptionpane.no_option) frame.setdefaultcloseoperation(jframe.do_nothing_on_close);public class te

11、xt6 public static void main(string args) new jframe6();7. 创建一个 file 类的对象,首先判断此配置文件是否存在,如果不存在,则调用 createnewfile 方法创建一个文件,然后从键盘输入字符存入数组里,创建文件输出流,把数组里的字符写入到文件中, 最终保存在“example7.txt”文件中import java.io.*; public class test7 public static void main(string args) file file=new file(d:,example7.txt); byte b=ne

12、w byte1000;int n; tryif(!file.exists() file.createnewfile();fileoutputstreamfos=new fileoutputstream(file,true); n=system.in.read(b);fos.write(b,0,n);fos.close(); catch(exception e)e.getmessage();9. 编写 mythread 线程类,在该类中实现九九乘法表的动态输出,每隔 1 秒输出乘法表中的一个运算结果。public class test9 public static void main(strin

13、g args) thread t=new mythread(); t.start();classmythread extends thread public void run()inti,j; tryfor(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(exception e)e.tostring();9. 编写类 overloading,在该类中定义 3 个方法:一个 info()方法是没有参数的,一个 info()方法需要使用一个整形参数

14、,一个 info()方法需要使用一个 string 类型参数。在 main 方法中进行测试。运行结果如下:public class test9 public static void main(string args) overloadingol=new overloading(); ();(5); (helloworld);classoverloading public void info()system.out.println(您调用的是无参数的方法);public void info(int n)system.out.println(您调用的是整形

15、类型参数的方法,参数是:+n);public void info(string s)system.out.println(您调用的是string 类型参数的方法,参数是:+s);10. 编写类 shape,该类是一个抽象类。在该类中定义一个抽象方法: getarea()。编写类 circle,该类继承自 shape 并实现了其抽象方法 getarea()。在该类的构造方法中,获得了圆形的半径,以此在 getarea()中计算面积。abstract class shapepublic abstract double getarea();class circle extends shape pri

16、vate double r; public circle(double r)this.r=r;public double getarea()/return math.pi*math.pow(r,2); return 3.14*r*r;public class test10 public static void main(string args) circle c=new circle(3);system.out.println(图形的面积是:+c.getarea();“”“”at the end, xiao bian gives you a passage. minand once said,

17、 people who learn to learn are very happy people. in every wonderful life, learning is an eternal theme. as a professional clerical and teaching position, i understand the importance of continuous learning, life is diligent, nothing can be gained, only continuous learning can achieve better self. only by constantly learning and mastering the latest relevant knowledge,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论