Java考试参考_第1页
Java考试参考_第2页
Java考试参考_第3页
Java考试参考_第4页
全文预览已结束

下载本文档

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

文档简介

1、1.编写一个类,该类创建的对象可以计算等差数列的和。代码:class leisum static int qsz,bcz,xs,sum;leisum(int a,int b,int c)qsz=a; bcz=b; xs=c;int sum() return sum=xs*qsz+xs*(xs-1)/2*bcz; class examplepublic static void main(string args) int sum=0; leisum leisum=new leisum(1,1,100);sum= leisum.sum();system.out.println("等差数列的

2、和为:"+sum);2.编写一个类,该类封装了一元二次方程共有的属性和功能,既该类有刻画方程系数的3个成员变量以及计算实根的方法。要求:该类的所有对象共享常数项。代码:class laderdouble a,b; static double c; double x1,x2; boolean boo; public lader(double a,double b,double c) this.a=a; this.b=b; lader.c=c;public void getroots() if(a!=0) system.out.println("一元2次方程:");

3、double d=b*b-4*a*c; if(d>=0)x1=(-b+math.sqrt(d)/(2*a);x2=(-b-math.sqrt(d)/(2*a); system.out.printf("方程的根:x1=%f,x2=%fn",x1,x2);else system.out.printf("方程没有实根n"); else system.out.println("不是一元2次方程"); public class liti2_5public static void main(string args )lader reluct

4、ance1=new lader(2,5,1); lader reluctance2=new lader(3,2,2);reluctance1.getroots(); reluctance2.getroots(); 3.编写一个应用程序,用户从键盘输入一行字符串,程序输出该字符串中与模式”24680a135792”匹配的子字符串。代码:import java.util.scanner;import java.util.regex.*;public class one public static void main(string args)system.out.println("请输入一

5、行字符串:"); scanner reader=new scanner(system.in);string str=reader.nextline();pattern p=ppile("24680a135792");matcher m=p.matcher(str);while(m.find() string s=m.group();system.out.print("从"+m.start()+"到"+m.end()+"匹配模式子序列:");system.out.println(s); 4. 编写一个应用程

6、序,将用户从键盘输入的10行文字存入文件。代码:import java.io.*;import java.util.*;public class one public static void main(string args)scanner reader=new scanner(system.in);try fileoutputstream writefile=new fileoutputstream("hello.txt");int line=1,n=10;system.out.println("输入"+n+"行文本,并存入磁盘:"

7、);while(line<=n)string s=reader.nextline();byte buffer=s.getbytes();writefile.write(buffer,0,buffer.length); line+;writefile.close();catch(ioexception e)system.out.println("error "+e);file f=new file("hello.txt");try int i=-1; randomaccessfile random=new randomaccessfile(f,&qu

8、ot;rw");random.seek(0);long m=random.length();while(i!=m) i+; random.seek(i); int c=random.readbyte();if(c<=255&&c>=0) system.out.print(char)c); else m-;random.seek(m); byte cc=new byte2;random.readfully(cc); system.out.print(new string(cc); random.close();catch(ioexception ee)5.用

9、randomaccessfile流将一个文本文件倒置读出。代码:import java.io.*;public class animator public static void main(string args) file f=new file("hello.txt");tryrandomaccessfile random=new randomaccessfile(f,"rw");random.seek(0);long m=random.length();while(m>=0)m=m-1;random.seek(m);int c=random.r

10、eadbyte(); if(c<=255&&c>=0) system.out.print(char)c);elsem=m-1; random.seek(m); byte cc=new byte2;random.readfully(cc);system.out.print(new string(cc);random.close();catch(ioexception ee)6.图形用户设计用jframe创建一个带有菜单的窗口。代码:import javax.swing.*;public class animentpublic static void main(stri

11、ng args)firstwindow win= new firstwindow("菜单练习");class firstwindow extends jframejmenubar menubar=new jmenubar();menu menu1=new menu("文件");menu menu2=new menu("编辑");jmenuitem itema=new jmenuitem("打开");jmenuitem item=new jmenuitem("退出");jmenuitem item

12、c=new jmenuitem("剪切");jmenuitem item=new jmenuitem("复制");firstwindow(string s)set title(s); set size(160,170);set location(120,120); set visible(true); menu1.add(itema);menu1.add(item); menu2.add(itemc); menu2.add(item); menubar.add(menu1);menubar.add(menu2); setjmenubar(menubar)

13、;validate(); setdefaultcloseoperation(jframe.dispose_on_close);7.选项卡窗格添加6个按钮,在单击按钮时在窗体中分别显示6幅图片。代码:import javax.swing.*;import java.awt.*;public class animentpublic static void main(string args)new mycard();class mycard extends jframe jtabbedpane p; icon icon;string imagename="a.jpg","

14、;b.jpg","c.jpg","d.jpg","e.jpg","f.jpg"public mycard() setbounds(100,100,500,300); set visible(true);icon =new iconimagename.length;for(int i=0;i<icon.length;i+)iconi=new imageicon(imagenamei);p=new jtabbedpane(jtabbedpane.left);for(int i=0;i<icon.l

15、ength;i+) int m=i+1; p.add("第"+m+"张图片",new jbutton(iconi);p.validate(); add(p,borderlayout.center); validate();setdefaultcloseoperation(jframe.dispose_on_close);8.设置一个窗口,里面有两个文本框,如果在第一个文本框输入的全部是数字字符,计算它的平方并显示在第二个文本框中,如果包括非数字字符,就将整个字符串显示在第二个文本框中,并且在命令行输出其字符串的长度。代码:import java.awt.

16、*;import java.awt.event.*;import javax.swing.*;import java.math.*;public class onepublic static void main(string args)mathwindow win=new mathwindow();class mathwindow extends jframejtextfield inputtext,showtext;mathwindow()inputtext=new jtextfield(10); showtext=new jtextfield(10);inputtext.addaction

17、listener(new actionlistener()public void actionperformed(actionevent e)string s=inputtext.gettext();trybiginteger n=new biginteger(s);n=n.pow(2);showtext.settext(n.tostring();catch(numberformatexception ee)jtextfield t1=(jtextfield) (e.getsource();string str=t1.gettext();showtext.settext(str);system

18、.out.println(str.length(););setlayout(new flowlayout(); add(inputtext);add(showtext);setbounds(100,100,200,120); set visible(true); validate(); setdefaultcloseoperation(jframe.dispose_on_close);9. 编写一个应用程序,设计4个按钮,分别命为:“加”、“差”、“乘”、“除”;还有3个文本框,单击相应的按钮,将两个文本框的数字做运算,在第三个文本框中显示结果。import java.awt.*;import

19、 java.awt.event.*;import javax.swing.*;public class one public static void main(string args) computerframe one=new computerframe();one.settitle("计算"); class computerframe extends jframe implements actionlistener jtextfield texta,textb,textshow;jbutton button1,button2,button3,button4;jlabel

20、 label;public computerframe()setlayout(new flowlayout(); texta=new jtextfield(10);textb=new jtextfield(10); textshow=new jtextfield(10); label=new jlabel(" ",jlabel.center);label.setbackground(color.green); add(texta); add(label); add(textb);add(textshow); button1=new jbutton("加"

21、); button2=new jbutton("减"); button3=new jbutton("乘"); button4=new jbutton("除"); add(button1); add(button2);add(button3); add(button4); button1.addactionlistener(this); button2.addactionlistener(this); button3.addactionlistener(this); button4.addactionlistener(this); se

22、t size(420,120); set visible(true); addwindowlistener(new windowadapter()public void windowclosing(windowevent e)system.exit(0););validate();public void actionperformed(actionevent e) double n; if(e.getsource()=button1)double n1,n2;try n1=double.parsedouble(texta.gettext(); n2=double.parsedouble(tex

23、tb.gettext(); n=n1+n2;textshow.settext(string.valueof(n); label.settext("+");catch(numberformatexception ee) textshow.settext("请输入数字字符");else if(e.getsource()=button2) double n1,n2; try n1=double.parsedouble(texta.gettext();n2=double.parsedouble(textb.gettext(); n=n1-n2;textshow.

24、settext(string.valueof(n); label.settext("-");catch(numberformatexception ee) textshow.settext("请输入数字字符");else if(e.getsource()=button3)double n1,n2;try n1=double.parsedouble(texta.gettext(); n2=double.parsedouble(textb.gettext(); n=n1*n2; textshow.settext(string.valueof(n);label

25、.settext("*"); catch(numberformatexception ee)textshow.settext("请输入数字字符");else if(e.getsource()=button4)double n1,n2; try n1=double.parsedouble(texta.gettext(); n2=double.parsedouble(textb.gettext(); n=n1/n2; textshow.settext(string.valueof(n); label.settext("/");catch(

26、numberformatexception ee) textshow.settext("请输入数字字符");validate();10.分别用三个单选按钮,控制三张不同的图片import javax.swing.*;import java.awt.*;import java.awt.event.*;public class anepublic static void main(string args)new rbuttonwindow(); class rbuttonwindow extends jframe implements itemlistenerjradiobut

27、ton rbuttona,rbuttonb,rbuttonc;buttongroup g; panel p; jlabel imagelabel; icon pic1,pic2,pic3;rbuttonwindow()g=new buttongroup();rbuttona=new jradiobutton("图片1");rbuttonb=new jradiobutton("图片2");rbuttonc=new jradiobutton("图片3");g.add(rbuttona);g.add(rbuttonb);g.add(rbut

28、tonc);p=new jpanel();p.add(rbuttona);p.add(rbuttonb);p.add(rbuttonc);imagelabel=new jlabel();pic1=new imageicon("a.jpg"); pic2=new imageicon("b.jpg");pic3=new imageicon("c.jpg");add(p,borderlayout.south);add(imagelabel,borderlayout.center);validate();rbuttona.additemlis

29、tener(this);rbuttonb.additemlistener(this);rbuttonc.additemlistener(this);setbounds(280,80,500,600);set visible(true);setdefaultcloseoperation(jframe.dispose_on_close);public void itemstatechanged(itemevent e)try if(rbuttona.isselected()imagelabel.seticon(pic1); catch(numberformatexception ee) try i

30、f(rbuttonb.isselected()imagelabel.seticon(pic2); catch(numberformatexception ee) try if(rbuttonc.isselected()imagelabel.seticon(pic3); catch(numberformatexception ee) 11.编写一个应用程序,使用弹出式菜单在两个文本框中实现“剪切”,“复制”,“粘贴”的操作。代码:import javax.swing.*;import java.awt.event.*;import java.awt.*;public class com publ

31、ic static void main(string args)editwindow win=new editwindow("窗口");class editwindow extends jframe implements actionlistenerjpopupmenu menu; jsplitpane splitpane;jmenuitem itemcopy, itemcut, itempaste;jtextarea inputtext, showtext;editwindow(string s)set title("弹出式菜单");set size(

32、260,270);set location(120,120); set visible(true);menu=new jpopupmenu();itemcut=new jmenuitem("剪切");itemcopy=new jmenuitem("复制");itempaste=new jmenuitem("粘贴");menu.add(itemcut);menu.add(itemcopy);menu.add(itempaste);inputtext=new jtextarea(); showtext=new jtextarea();jp

33、anel panel=new jpanel();panel.setlayout(new gridlayout(1,2);panel.add(new jscrollpane(inputtext);panel.add(new jscrollpane(showtext);inputtext.addmouselistener(new mouseadapter()public void mousepressed(mouseevent e)if(e.getmodifiers()=inputevent.button3_mask)menu.show(inputtext,e.getx(),e.gety(););

34、showtext.addmouselistener(new mouseadapter()public void mousepressed(mouseevent e)if(e.getmodifiers()=inputevent.button3_mask)menu.show(showtext,e.getx(),e.gety(););splitpane=new jsplitpane(jsplitpane.horizontal_split,inputtext,showtext);splitpane.setdividerlocation(120);add(splitpane,borderlayout.c

35、enter);itemcopy.addactionlistener(this);itemcut.addactionlistener(this);itempaste.addactionlistener(this); validate();setdefaultcloseoperation(jframe.dispose_on_close);public void actionperformed(actionevent e)if(e.getsource()=itemcopy)inputtext.copy();else if(e.getsource()=itemcut)inputtext.cut();e

36、lse if(e.getsource()=itempaste)showtext.paste();12. 编写一个应用程序,有2个按钮,用户通过按动键盘上的方向键移动这些按钮。代码:import javax.swing.*;import java.awt.*;import java.awt.event.*;public class compublic static void main(string args)new win();class win extends jframe implements keylistener,focuslistenerjbutton button1,button2; int x1=0,x2=

温馨提示

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

评论

0/150

提交评论