




已阅读5页,还剩27页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
6定义一个计算n!的类fact,然后主类中创建一个对象求解4!的值。7在applet界面中显示当前文件夹下名为pica.jpg的图像。8定义一个计算xn的类,然后用该类求103的值。9在applet界面中显示一些由直线、矩形框、椭圆框和文字组成的图形。10求两个数的最大值。6import java.io.*;public class class1 public static void main( string args ) fact n = new fact( 4 ); system.out.println( n.fact( ) ); class fact int n ; fact( int nn ) n = nn; int fact( ) int i , f = 1; for ( i=1; i=n; i+ ) f = f*i; return f; 7import java.awt.*;import java.applet.applet;public class drawmyimage extends appletimage myimage; / 定义一个图像类image的对象myimagepublic void init( )myimage = getimage(getdocumentbase( ),pica.jpg);public void paint(graphics g)g.drawimage(myimage,0,0,this);8 import java.io.*;public class class1 public static void main( string args ) subclass a = new subclass( 10,3 ); system.out.println( a.exp( ); class superclass float x; int n; superclass( float xx , int nn ) x = xx ; n = nn; class subclass extends superclass subclass( float xx , int nn ) super( xx , nn ); float exp( ) float s = 1; for ( int i = 1; ib)?a:b); 1 编写一个字符界面的java application 程序,接受用户从键盘输入的一个正整数,然后统计并输出从1到这个正整数的累加和。2 编写一个字符界面的java application 程序,接受用户输入的10个整数,并输出这10个整数的最大值和最小值。3 编写一个完整的java applet 程序使用复数类complex验证两个复数 1+2i 和3+4i 相加产生一个新的复数 4+6i 。 复数类complex必须满足如下要求:(1)复数类complex 的属性有:realpart : int型,代表复数的实数部分imaginpart : int型,代表复数的虚数部分(2)复数类complex 的方法有:complex( ) : 构造函数,将复数的实部和虚部都置0complex( int r , int i ) : 构造函数,形参 r 为实部的初值,i为虚部的初值。complex complexadd(complex a) : 将当前复数对象与形参复数对象相加,所得的结果仍是一个复数值,返回给此方法的调用者。string tostring( ) : 把当前复数对象的实部、虚部组合成 a+bi 的字符串形式,其中a 和 b分别为实部和虚部的数据。4 编写一个学生类 student ,要求:(1) 学生类 student 属性有:id : long型,代表学号name : string类对象,代表姓名age : int型,代表年龄sex : boolen型,代表性别(其中:true表示男,false表示女)phone : string类对象,代表联系电话(2) 学生类 student的方法有:student(long i , string n , int a , boolean s , long p) : 有参构造函数,形参表中的参数分别初始化学号、姓名、年龄、性别和联系电话。int getage() ( ) : 获取年龄作为方法的返回值。boolean getsex( ) ( ) : 获取性别作为方法的返回值。long getphone ( ) : 获取联系电话作为方法的返回值。public string tostring( ) : 以 姓名:联系电话 的形式作为方法的返回值。5 编写图形界面下的java applet程序,接受用户输入的三个数据,输出最小的一个数。6 编写图形界面下的java applet程序,接受用户输入的两个数据为上、下限,然后10个一行输出上、下限之间的所有素数。7编写一个java applet程序响应用户的鼠标动作:以鼠标单击处为圆心,画一个随机大小,随机颜色的圆,当鼠标双击时则清空屏幕。8. 编写一个applet响应鼠标事件程序,其功能是用户可以通过拖动鼠标在applet中画出矩形,并在状态条显示鼠标当前的位置。9. 下图中给出某应用程序的图形界面,试写出构建该应用程序的界面程序。 10.下图中给出了某应用程序的图形界面,试写出构建该应用程序的界面程序。 1import java.io.*;public class sumpublic static void main (string args) int i , n=-1 , sum=0 ; while ( n1) try bufferedreader br =new bufferedreader( new inputstreamreader(system.in); n = integer.parseint(br.readline( ); catch ( ioexception e ) ; for ( i =1 ; i=n; i+ ) sum += i; system.out.println(sum);2import java.io.* ;public class abc public static void main(string args ) int i , n = 10 , max = 0 , min = 0 , temp = 0; try bufferedreader br = new bufferedreader( new inputstreamreader(system.in); max = min = integer.parseint(br.readline( ); catch ( ioexception e ) ; for ( i = 2 ; i max ) max=temp; if (temp min) min=temp; catch ( ioexception e ) ; system.out.println(max=+max+nmin=+min); 3import java.applet.* ; import java.awt.* ;public class abc extends applet complex a,b,c ; public void init( ) a = new complex(1,2); b = new complex(3,4); c = new complex(); public void paint(graphics g) c=plexadd(b); g.drawstring(第一个复数:+a.tostring(),10,50); g.drawstring(第二个复数:+b.tostring(),10,70); g.drawstring(两复数之和:+c.tostring(),10,90); class complex int realpart ; / 复数的实部 int imaginpart ; / 复数的虚部 complex() realpart = 0 ; imaginpart = 0 ; complex(int r , int i) realpart = r ; imaginpart = i ; complex complexadd(complex a) complex temp = new complex( ); / 临时复数对象 temp.realpart=realpart+a.realpart; temp.imaginpart=imaginpart+a.imaginpart; return temp; public string tostring( ) return ( realpart+ + +imaginpart+ i ); 4class student long id; string name;int age;boolean sex;long phone;student( long i , string n , int a , boolean s , long p ) id = i; name = n;age = a;sex = s;phone = p;int getage() return age; boolean getsex() return sex; long getphone() return phone; public string tostring() return name+ : +phone; 5import java.applet.*;import java.awt.*;import java.awt.event.*;public class findminextendsappletimplementsactionlistenerlabelresult;textfieldin1,in2,in3;buttonbtn1,btn2;int a=0,b=0,c=0,min;public void init()result=new label(请先输入三个待比较的整数);in1=new textfield(5);in2=new textfield(5);in3=new textfield(5);btn1=new button(比较);btn2=new button(关闭);add(in1);add(in2);add(in3);add(btn1);add(btn2);add(result);btn1.addactionlistener(this);btn2.addactionlistener(this);public void actionperformed(actionevente)if(e.getsource()=btn1) a=integer.parseint(in1.gettext(); b=integer.parseint(in2.gettext(); c=integer.parseint(in3.gettext(); min=a; if (bmin) min=b; if(cmin) min=c; result.settext(三数中最小值是:+min); else system.exit(0);6/输入上限与下限,输出范围内的素数import java.applet.*;import java.awt.*;import java.awt.event.*;public class sushu extendsappletimplementsactionlistenerlabellb1,lb2,result;textfieldin1,in2;textarea out1;buttonbtn1,btn2;int a=0,b=0;string s=;public void init()result=new label(输出框);lb1=new label(上限);lb2=new label(下限);in1=new textfield(5);in2=new textfield(5);out1=new textarea();out1.seteditable(false);/设为不可编辑btn1=new button(输出素数);btn2=new button(关闭);add(lb1);add(in1);add(lb2);add(in2);add(btn1);add(btn2);add(result);add(out1);btn1.addactionlistener(this);btn2.addactionlistener(this);public void actionperformed(actionevente)int k=0;/设计数器if(e.getsource()=btn1)/响应按钮输出素数的动作out1.settext(n);/新行输出a=integer.parseint(in1.gettext();b=integer.parseint(in2.gettext();/判断是否素数,是则计数并加入输出子串sloop:for(int i=b;ia;i+) for(int j=2;ji;j+)if(i%j=0)continueloop;k+;if(k%10=0)/每10个数加个回车s=s+i+,+n;else s=s+i+,;result.settext(b+与+a+之间共有素数+k+个,输出如下:);out1.settext(s);else system.exit(0);/点击关闭退出程序7import java.applet.*;import java.awt.*;import java.awt.event.*;public class mouselistenertest extends applet int xpos=-1,ypos=-1;color color;int radius;int mouseflag;public void paint(graphics g)if(mouseflag= =1)radius = (int)(50*math.random();color = new color(int)(255*math.random(),(int)(255*math.random(),(int) (255*math.random();g.setcolor(color);g.drawstring(radius is +radius,20,20);g.drawoval(xpos,ypos,radius,radius);else if(mouseflag= =2) g.clearrect(0,0,getwidth(),getheight(); public void init() addmouselistener(new mouseadapter() public void mouseclicked(mouseevent e) if(e.getclickcount() =1) mouseflag = 1; else if (e.getclickcount() =2) mouseflag = 2; xpos = e.getx() ypos = e.gety(); repaint(); ); 8. import java.applet.*;import java.awt.*;import java.awt.event.*;public class mousedrawrect extends applet implements mousemotionlistener int xstart=-1,ystart=-1; int xend,yend; textfield showposition; public void init() showposition = new textfield(20); add(showposition); addmousemotionlistener(this); addmouselistener(new mouseadapter() public void mousepressed(mouseevent event) xstart = event.getx(); ystart = event.gety(); ); public void paint(graphics g) if(xstart!=-1 & ystart!=-1) g.drawrect(xstart,ystart,xend-xstart,yend-ystart); showposition.settext(the x is +xend+ the y is +yend); public void mousedragged(mouseevent e) xend = e.getx(); yend = e.gety(); repaint(); public void mousemoved(mouseevent e) 9.import javax.swing.*;import java.awt.*;class speakphone extends jframejtextarea speak;jbutton numbers = new jbutton12;string numberstring = 0,1,2,3,4,5,6,7,8,9,*,#;jbutton enter,cancel;font f1 = new font(arial,font.bold,20);speakphone()setsize(200,400);container con = getcontentpane();speak = new jtextarea(4,6);jpanel p1 = new jpanel();p1.setlayout(new gridlayout(2,1);label lab =new label(connet number); lab.setfont(f1);p1.add(lab);p1.add(speak);jpanel p2 = new jpanel();p2.setlayout(new gridlayout(3,4);for(int i=0;i12;i+)numbersi=new jbutton(numberstringi);numbersi.setfont(f1);p2.add(numbersi);jpanel p3 = new jpanel();p3.setlayout(new gridlayout(1,2);enter = new jbutton(确定);cancel = new jbutton(取消);p3.add(enter);p3.add(cancel);con.add(p1,borderlayout.north);con.add(p2,borderlayout.center);con.add(p3,borderlayout.south);setvisible(true);public static void main(string args)speakphone mainfrm = new speakphone();10. import java.awt.*;class mypanel1 extends panelcheckbox box1,box2,box3;checkboxgroup sex;mypanel1() sex = new checkboxgroup(); box1 = new checkbox(男,true,sex); box2 = new checkbox(女,false,sex); setlayout(new gridlayout(3,1); add(new label(性别); add(box1); add(box2);class mypanel2 extends panellist list;mypanel2() list = new list(4,false); list.add(学生); list.add(教师); list.add(高工); setlayout(new gridlayout(4,1); add(new label(职业); add(list);class mypanel3 extends panelcheckbox box1,box2,box3;mypanel3() box1 = new checkbox(读书); box2 = new checkbox(电脑); box3 = new checkbox(电影); setlayout(new gridlayout(4,1); add(new label(爱好); add(box1); add(box2); add(box3);class choicelike extends frame public static void main(string args) mypanel1 panel1 = new mypanel1(); mypanel2 panel2 = new mypanel2(); mypanel3 panel3 = new mypanel3(); choicelike mainframe = new choicelike(); mainframe.add(panel1,borderlayout.west); mainframe.add(panel2,borderlayout.center); mainframe.add(panel3,borderlayout.east); mainframe.setvisible(true); 1、设计一个图形用户界面的颜色合成程序,界面如下图所示。要求改变任一颜色值时,合成颜色实时变化。import java.awt.*;import java.awt.event.*;public class rgbcolor extends windowadapter implements textlistenerframe f;textfield tf1,tf2,tf3;panel p2;public rgbcolor()f=new frame(合成颜色);f.setsize(500,200);panel p1=new panel();p2=new panel();f.add(p1,north);f.add(p2);p1.add(new label(red);tf1=new textfield(255,10);p1.add(tf1);p1.add(new label(green);tf2=new textfield(0,10);p1.add(tf2);p1.add(new label(blue);tf3=new textfield(0,10);p1.add(tf3);tf1.addtextlistener(this);tf2.addtextlistener(this);tf3.addtextlistener(this);p2.setbackground(new color(255,0,0);f.setvisible(true);f.addwindowlistener(this);public void textvaluechanged(textevent e)int r=(new integer(tf1.gettext().intvalue();int g=(new integer(tf2.gettext().intvalue();int b=(new integer(tf3.gettext().intvalue();p2.setbackground(new color(r,g,b); public void windowclosing(windowevent e)system.exit(0);public static void main(string k)new rgbcolor();2、设计一个密码验证程序,如下图所示。要求:当输入正确的密码“123456”时,验证文本域显示“you pass!”,否则显示密码错误的信息“your password error!”。import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class login1 public static void main(string args) loginframe frame = new loginframe();frame.setdefaultcloseoperation(jframe.exit_on_close);frame.show();class loginframe extends jframepublic loginframe()settitle(login);setsize(width, height); container contentpane = getcontentpane();/建立容器面板jpanel textpanel = new jpanel();/新建输入密码标签jlabel inputpassword = new jlabel(input password: , jlabel.center);mypassword = new jpasswordfield(20);textpanel.add(inputpassword);textpanel.add(mypassword);/新建密码验证标签jlabel checkedpassword = new jlabel(checked password: , jlabel.left);checkpassword = new jtextfield(20);checkpassword.seteditable(false);textpanel.add(checkedpassword);textpanel.add(checkpassword);loginbutton = new jbutton(登录, new imageicon(hlpcd.gif);/注册事件监听器loginbutton.addactionlistener(new loginaction();textpanel.add(loginbutton);contentpane.add(textpanel);/实现事件监听器private class loginactionimplements actionlistenerpublic void actionperformed(actionevent event)/获取密码域的内容char s = mypassword.getpassword();string gets = new string(s);if(gets.equals(checkpassword)checkpassword.settext(pass!);elsecheckpassword.settext(wrong password!);public static final int width = 280;public static final int height = 180;public static final string checkpassword = password;private jpasswordfield mypassword;private jtextfield checkpassword;private jbutton loginbutton;3、编写一个application程序,求1!+2!+3!+20!。把最后的结果在屏幕显示出来。public class jiechenghepublic static void main(string s)long i=1;long k=0;for(int n=1;n=20;n+)i=i*n;k=k+i;system.out.println(k);4、用switch语句编写程序。判断输入的某个月属于一年中的哪个季节。2月、3月、4月为春季;5月、6月、7月为夏季;8月、9月、10月为秋季;11月、12月、1月为冬季。 要求:1、月份采用键盘输入;2、在屏幕上显示季节结果。3、只写出1个case语句即可,其它省略。import java.io.*;class myswitchpublic static void main(string j)int x=0;trybufferedreader stdin=new bufferedreader(new inputstreamreader(system.in);system.out.print(请输入一个月份: );x=integer.parseint(stdin.readline();catch(ioexception e)system.err.println(ioexception);switch(x)case 2:system.out.println(这个月属于春季);break;case 3:system.out.println(这个月属于春季);break;case 4:system.out.println(这个月属于春季);break;case 5:system.out.println(这个月属于夏季);break;case 6:system.out.println(这个月属于夏季);break;case 7:system.out.println(这个月属于夏季);break;case 8:system.out.println(这个月属于秋季);break;case 9:system.out.println(这个月属于秋季);break;case 10:system.out.println(这个月属于秋季);break;case 11:system.out.println(这个月属于冬季);break;case 12:system.out.println(这个月属于冬季);break;case 1:system.out.println(这个月属于冬季);break;default:system.out.println(你输入数据有误!);5、编写一个计算“两数相加”的程序。要求:1、界面布置如右图所示;2、当点击按钮“计算”时,实现两个数的相加,把结果放入第三个文本框中;同时按钮的背景色变为红色。import java.awt.event.*;import java.awt.*;import javax.swing.*;import javax.swing.event.*;public class myaddpublic static void main(string k)myframe f =new myframe();f.show();class myframe extends jframepublic myframe()settitle(两数相加);setsize(300,150);container aaa=getcontentpane();mypanel p=new mypanel();aaa.add(p);class mypanel extends jpanel public mypanel()b1=new jbutton(计算); origintext = new jtextfield( , 20); addtext=new jtextfield( , 20);sumtext=new jtextfield( , 20);add(orig
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 九年级化学上册 第1单元《课题1 物质的变化和性质》教学设计 (新版)新人教版
- 五年级信息技术上册 孙悟空变变变教学设计 冀教版
- 上册教案(教案)-2024-2025学年三年级上册劳动浙教版
- 人教部编版九年级下册12 词四首综合与测试教学设计
- 初中主题班会“文明礼仪伴我行”教学设计
- 防性侵安全教育主题
- 五年级品德与社会上册 请你相信我 1教学设计 人教新课标版
- 财务咨询公司业务培训
- 2024中铝智能科技发展有限公司面向社会公开招聘5人(第十五批)笔试参考题库附带答案详解
- 2024中铁大桥局集团武汉置业发展有限公司春季校园招聘笔试参考题库附带答案详解
- CNAS-TRL-022:2023《实验室风险管理指南》
- 2024年河南轻工职业学院高职单招语文历年参考题库含答案解析
- 第19课 资本主义国家的新变化 说课稿-2024-2025学年高一统编版2019必修中外历史纲要下册
- 即时通讯系统建设方案
- 2025年中国人保股份有限公司招聘笔试参考题库含答案解析
- 土石方施工合同协议书
- 《nike的品牌发展史》课件
- 胎盘植入课件讲义版
- 口腔门诊接待流程
- 2025年上半年下半年中国南水北调集团东线限公司招聘工作人员拟聘人员易考易错模拟试题(共500题)试卷后附参考答案
- 2025年江苏盐城东方集团招聘笔试参考题库含答案解析
评论
0/150
提交评论