




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、6定義一個計算n!的類fact,然后主類中創(chuàng)建一個對象求解4!的值。7在applet界面中顯示當前文件夾下名為pica.jpg的圖像。8定義一個計算xn的類,然后用該類求103的值。9在applet界面中顯示一些由直線、矩形框、橢圓框和文字組成的圖形。10求兩個數(shù)的最大值。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
2、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
3、.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( fl
4、oat xx , int nn ) super( xx , nn ); float exp( ) float s = 1; for ( int i = 1; ib)?a:b); 1 編寫一個字符界面的java application 程序,接受用戶從鍵盤輸入的一個正整數(shù),然后統(tǒng)計并輸出從1到這個正整數(shù)的累加和。2 編寫一個字符界面的java application 程序,接受用戶輸入的10個整數(shù),并輸出這10個整數(shù)的最大值和最小值。3 編寫一個完整的java applet 程序使用復數(shù)類complex驗證兩個復數(shù) 1+2i 和3+4i 相加產(chǎn)生一個新的復數(shù) 4+6i 。 復數(shù)類complex必
5、須滿足如下要求:(1)復數(shù)類complex 的屬性有:realpart : int型,代表復數(shù)的實數(shù)部分imaginpart : int型,代表復數(shù)的虛數(shù)部分(2)復數(shù)類complex 的方法有:complex( ) : 構(gòu)造函數(shù),將復數(shù)的實部和虛部都置0complex( int r , int i ) : 構(gòu)造函數(shù),形參 r 為實部的初值,i為虛部的初值。complex complexadd(complex a) : 將當前復數(shù)對象與形參復數(shù)對象相加,所得的結(jié)果仍是一個復數(shù)值,返回給此方法的調(diào)用者。string tostring( ) : 把當前復數(shù)對象的實部、虛部組合成 a+bi 的字符串
6、形式,其中a 和 b分別為實部和虛部的數(shù)據(jù)。4 編寫一個學生類 student ,要求:(1) 學生類 student 屬性有:id : long型,代表學號name : string類對象,代表姓名age : int型,代表年齡sex : boolen型,代表性別(其中:true表示男,false表示女)phone : string類對象,代表聯(lián)系電話(2) 學生類 student的方法有:student(long i , string n , int a , boolean s , long p) : 有參構(gòu)造函數(shù),形參表中的參數(shù)分別初始化學號、姓名、年齡、性別和聯(lián)系電話。int geta
7、ge() ( ) : 獲取年齡作為方法的返回值。boolean getsex( ) ( ) : 獲取性別作為方法的返回值。long getphone ( ) : 獲取聯(lián)系電話作為方法的返回值。public string tostring( ) : 以 姓名:聯(lián)系電話 的形式作為方法的返回值。5 編寫圖形界面下的java applet程序,接受用戶輸入的三個數(shù)據(jù),輸出最小的一個數(shù)。6 編寫圖形界面下的java applet程序,接受用戶輸入的兩個數(shù)據(jù)為上、下限,然后10個一行輸出上、下限之間的所有素數(shù)。7編寫一個java applet程序響應用戶的鼠標動作:以鼠標單擊處為圓心,畫一個隨機大小,隨
8、機顏色的圓,當鼠標雙擊時則清空屏幕。8. 編寫一個applet響應鼠標事件程序,其功能是用戶可以通過拖動鼠標在applet中畫出矩形,并在狀態(tài)條顯示鼠標當前的位置。9. 下圖中給出某應用程序的圖形界面,試寫出構(gòu)建該應用程序的界面程序。 10.下圖中給出了某應用程序的圖形界面,試寫出構(gòu)建該應用程序的界面程序。 1import java.io.*;public class sumpublic static void main (string args) int i , n=-1 , sum=0 ; while ( n1) try bufferedreader br =new bufferedrea
9、der( 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 b
10、r = 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
11、 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(第一個復數(shù):+a.tostring(),10,50); g.drawstring(第二個復數(shù):+b.tostring(),10,70); g.drawstring(兩復數(shù)之和:+c.tostring(),10,90); class co
12、mplex int realpart ; / 復數(shù)的實部 int imaginpart ; / 復數(shù)的虛部 complex() realpart = 0 ; imaginpart = 0 ; complex(int r , int i) realpart = r ; imaginpart = i ; complex complexadd(complex a) complex temp = new complex( ); / 臨時復數(shù)對象 temp.realpart=realpart+a.realpart; temp.imaginpart=imaginpart+a.imaginpart; ret
13、urn 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; lon
14、g 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(
15、請先輸入三個待比較的整數(shù));in1=new textfield(5);in2=new textfield(5);in3=new textfield(5);btn1=new button(比較);btn2=new button(關(guān)閉);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=
16、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(三數(shù)中最小值是:+min); else system.exit(0);6/輸入上限與下限,輸出范圍內(nèi)的素數(shù)import java.applet.*;import java.awt.*;import java.awt.event.*;public class sushu extendsapp
17、letimplementsactionlistenerlabellb1,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);/設(shè)為不可編輯btn1=new button
18、(輸出素數(shù));btn2=new button(關(guān)閉);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;/設(shè)計數(shù)器if(e.getsource()=btn1)/響應按鈕輸出素數(shù)的動作out1.settext(n);/新行輸出a=integer.parseint(in1.gette
19、xt();b=integer.parseint(in2.gettext();/判斷是否素數(shù),是則計數(shù)并加入輸出子串sloop:for(int i=b;ia;i+) for(int j=2;ji;j+)if(i%j=0)continueloop;k+;if(k%10=0)/每10個數(shù)加個回車s=s+i+,+n;else s=s+i+,;result.settext(b+與+a+之間共有素數(shù)+k+個,輸出如下:);out1.settext(s);else system.exit(0);/點擊關(guān)閉退出程序7import java.applet.*;import java.awt.*;import j
20、ava.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(co
21、lor);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
22、() =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() showposit
23、ion = 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,yen
24、d-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 jbut
25、ton12;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.
26、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.a
27、dd(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 se
28、x;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(高工); setla
29、yout(new gridlayout(4,1); add(new label(職業(yè)); 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
30、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,borderlay
31、out.east); mainframe.setvisible(true); 1、設(shè)計一個圖形用戶界面的顏色合成程序,界面如下圖所示。要求改變?nèi)我活伾禃r,合成顏色實時變化。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=ne
32、w 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.addtextlistene
33、r(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); pub
34、lic void windowclosing(windowevent e)system.exit(0);public static void main(string k)new rgbcolor();2、設(shè)計一個密碼驗證程序,如下圖所示。要求:當輸入正確的密碼“123456”時,驗證文本域顯示“you pass!”,否則顯示密碼錯誤的信息“your password error!”。import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class logi
35、n1 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 = n
36、ew 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);checkpas
37、sword.seteditable(false);textpanel.add(checkedpassword);textpanel.add(checkpassword);loginbutton = new jbutton(登錄, new imageicon(hlpcd.gif);/注冊事件監(jiān)聽器loginbutton.addactionlistener(new loginaction();textpanel.add(loginbutton);contentpane.add(textpanel);/實現(xiàn)事件監(jiān)聽器private class loginactionimplements action
38、listenerpublic void actionperformed(actionevent event)/獲取密碼域的內(nèi)容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;p
39、ublic static final string checkpassword = password;private jpasswordfield mypassword;private jtextfield checkpassword;private jbutton loginbutton;3、編寫一個application程序,求1!+2!+3!+20!。把最后的結(jié)果在屏幕顯示出來。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;
40、system.out.println(k);4、用switch語句編寫程序。判斷輸入的某個月屬于一年中的哪個季節(jié)。2月、3月、4月為春季;5月、6月、7月為夏季;8月、9月、10月為秋季;11月、12月、1月為冬季。 要求:1、月份采用鍵盤輸入;2、在屏幕上顯示季節(jié)結(jié)果。3、只寫出1個case語句即可,其它省略。import java.io.*;class myswitchpublic static void main(string j)int x=0;trybufferedreader stdin=new bufferedreader(new inputstreamreader(system
41、.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
42、: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.ou
43、t.println(這個月屬于冬季);break;default:system.out.println(你輸入數(shù)據(jù)有誤!);5、編寫一個計算“兩數(shù)相加”的程序。要求:1、界面布置如右圖所示;2、當點擊按鈕“計算”時,實現(xiàn)兩個數(shù)的相加,把結(jié)果放入第三個文本框中;同時按鈕的背景色變?yōu)榧t色。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 m
44、yframe();f.show();class myframe extends jframepublic myframe()settitle(兩數(shù)相加);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(origi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 企業(yè)辦公空間裝修合同
- 設(shè)備租賃與售后服務合同全新版
- 工程車輛采購合同樣本
- 有限合伙投資入股合同模板
- 服裝公司供應商合同管理范本
- 度安全生產(chǎn)目標責任合同范本
- 企業(yè)年會承辦合同范本(專業(yè)版)
- 美團押金合同范本
- 20《金字塔:金字塔夕照》教學設(shè)計-2023-2024學年五年級下冊語文統(tǒng)編版
- 15《真理誕生于一百個問號之后》教學設(shè)計-2023-2024學年六年級下冊語文統(tǒng)編版
- 民政局業(yè)務工作流程圖
- 《現(xiàn)代管理學》全套課件
- 《生物化學與分子生物學》(第9版)配套題庫
- 【部編版】六年級道德與法治下冊全冊課件
- 《殯葬文化學》課程教學大綱
- DBJ51T 198-2022 四川省既有民用建筑結(jié)構(gòu)安全隱患排查技術(shù)標準
- 退役軍人基層服務平臺
- DB15T 2427-2021 公路水泥穩(wěn)定鋼渣混合料設(shè)計與施工技術(shù)規(guī)范
- 稅法最全課件完整版ppt教程整套教學講義(最新)
- 廣東佛山祖廟導游詞
- 硬筆書法紙可打印
評論
0/150
提交評論