java課后習(xí)題答案1_第1頁
java課后習(xí)題答案1_第2頁
java課后習(xí)題答案1_第3頁
java課后習(xí)題答案1_第4頁
java課后習(xí)題答案1_第5頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第一章

07

publicclasshomework_l_07{

publicstaticvoidmain(String[]args)

{Systemoutprintln(H***************************************"),

System.out.println("**Practicemakesperfect");

Systemoutprintln("***************************************"),)

08

importjava.awt.Graphics;

importjavax.swing.JApplet;

publicclasshomework_l_08extendsJApplet{

publicvoidpaint(Graphicsg){

g.clearRect(O,0,getWidthO,getHeight());

gdrswStringf11***********************************'2075),

g.drawstring,**Practicemakesperfect",20,90);

gdrawString("**********************************"20105)'}

第二章

03

publicclasshomework_2_03{

publicstaticvoidmain(String[]args){}}

06

publicclasshomework_2_06{

publicstaticvoidmain(String[]args)

{//TODOAuto-generatedmethodstub

for(inta=l;a<5;a++)

{spacenum(4-a);

starnum(a);}

for(inta=0;a<4;a++)

{spacenum(a+l);

starnum(3-a);}}

/**輸出每一行星星的個數(shù)*/

publicstaticintstarnum(inta)〃輸出每?行的星星個數(shù)

{intm=num(a);

for(inti=0;i<m;i++)

{System.out.print("*");}

System.out.println();

return0;}

/**算出每一行星星的個數(shù)*/

publicstaticintnum(inta)〃計算每行星星個數(shù)

{returna*2-l;}

/**輸出每一行的空格數(shù)*/

publicstaticintspacenum(int輸出每行星星個數(shù)

{for(inti=0;i<a;i++)

{System.out.print("");}

return0;}}

09

publicclasshomework_2_09{

publicstaticvoidmain(String[]args)

{//TODOAuto-generatedmethodstub

for(inta=l;a<5;a++)

{if(a?=4){

spacenum(4-a);

starnum(a);}

if(a==4){

for(intb=l;b<4;b++){

System.out.p^nt("*,,);}

System.out.print("+H);

for(intb=l;b<3;b++){

System.out.print("*");}

System.out.println("*");}}

for(inta=0;a<4;a++)

{spacenum(a+l);

starnum(3-a);}}

/**輸出每一行星星的個數(shù)*/

publicstaticintstarnum(inta)〃輸出每一行的星星個數(shù)

{intm=num(a);

for(inti=0;i<m;i++)

{System.out.print("*");}

System.out.println();

return0;}

/**算出每一行星星的個數(shù)*/

publicstaticintnum(inta)〃計算每行星星個數(shù)

{returna*2-l;}

/**輸出每一行的空格數(shù)*/

publicstaticintspacenum(inta)〃輸出每行星星個數(shù)

{for(inti=0;i<a;i++)

{System.out.print("");}

return0;}}

10

publicclasshomework_2_10{

publicstaticvoidmain(String[]args){

booleanflag=false;

for(intn=50;n<=100;n++)

{flag=WeatherPrime(n);

if(flag==true){

System.out.println(n);}}}

publicstaticbooleanWeatherPrime(intnum)

{for(inti=2;i<num;i++)

{if(num%i!=0){continue;}

else{returnfalse;}}

returntrue;}}

11

publicclasshomework__2_ll{

publicstaticvoidmain(String[]args)

{longresult=0;

for(intn=l;n<=20;n++)

{result+=num(n);}

System.out.print(result);}

publicstaticintnum(inti){

return(int)Math.pow(i,i);}}

第三章:

06

publicclasshomework_3_06{

publicstaticvoidmain(String[]args){

inti=56;

if(i<0)i=(~Math.abs(i))+1;

System.out.println("Decimal:"+i);

System.out.println("Bits:"+Integer.toBinaryString(i));

System.out.println("Oct:"+Integer.toOctalString(i));

System.out.println("Hex:"+Integer.toHexString(i));}}

07

publicclasshomework_3_07{

intnumber;〃成員變量

publichomework_3__07(intn)〃構(gòu)造方法

{number=n;}

publicbooleanisPrime()

{booleanflag=true;

inti;for(i=2;i<number;i++)

{if(number%i==O)

{flag=false;

break;}}

returnflag;}

publicintgetNumber()

{returnnumber;}

publicstaticvoidmain(String[]args)

{homework_3_07ispl=newhomework_3_07(ll);〃第——個實例對象

homework__3_07isp2=newhomework_3_07(12);〃第二個實例對象

booleanb;

b=ispl.isPrime();〃判別11是否為素數(shù)

if(b==true)

System.out.println(ispl.getNumber()+”是素數(shù)!");

else

System.out.println(ispl.getNumber()+”不是素數(shù)!");

bnisp2.isPrime();〃河別12是否為素數(shù)

if(b==true)

System.out.println(isp2.getNumber()+”是素數(shù)!");

else

System.out.println(isp2.getNumber()+”不是素數(shù)!");}}

08

publicclasshomework_3_08{

publicstaticvoidmain(String[]args)

{inta=l/b=3,c=2;

doublexl,x2;

doublen=(b*b)-(4*a*c);

if(n>=0){

xl=(-b+Math.sqrt(n))/(2*a);

x2=(-b-Math.sqrt(n))/(2*a);

System.out.printin("方程的兩個根分別是:xl="+xl+"x2="+x2);}

else{

System.out.printin("無解!");}}}

10

publicclasshomework_3__10{

/**

*兩個數(shù)不能同時為素數(shù)

*做不出來。*/

privatestaticintcount[]/count2[],count3[],setl[]zset2[];

privatestaticintnotPrimer[];

privatestaticintprimer[],nl/n2J/j;

publicstaticvoidmain(String[]args){

nl=n2=0;

setl=newint[200];

set2=newint[10000];

count=newint[200];

count2=newint[10000];

counts=newint[10000];

notPrimer=newint[80];

primer=newint[30];

for(i=0;i<198;i++){count[i]=0;}

for(i=0;i<10000;i++){count2[i]=0;}

for(i=0;i<10000;i++){count3[i]=0;}

for(i=2;i<100;i++){

for(j=2;j<i;j++)

if(i%j==O)break;

if(j<i)notPrimer[nl++]=i;

else

primer[n2++]=i;}

for(i=0;i<n2;i++)

for(j=0;j<n2;j++){

if(primer[j]!=primer[i]){

count[primer[i]+primer[j]]++;

count2[primer[i]*primer[j]]++;}}

j=0;for(i=5;i<198;i++){

if(count[i]==0)

setl[j++]=i;}

for(i=2;i<100;i++)

for(j=2;j<100;j++){

if(count2[i*j]==0&&i!=j)

count3[i*j]++;}}}

第四章

02

publicclassJ_4__02{

publicstaticvoidmb_swap(String[Js){

if(s.length<2)

return;

Stringt=s[O];

s[O]=s[l];

s[l]=t;}〃方法mb__swap結(jié)束

publicstaticvoidmain(String[]args){

String[]s={"r';'2");

mb_swap(s);

System.out.print(s[O]+s[l]);

}〃方法main結(jié)束}

03

publicclasshomework_4_03{

publicstaticvoidmain(String[]args){

booleanflag=false;

for(intn=0;n<=100;n++)

{flag=WeatherPrime(n);

if(flag==true){System.out.println(n);}}}

publicstaticbooleanWeatherPrime(intnum)

{for(inti=2;i<num;i++)

{if(num%i!=0){

continue;

}else{

returnfalse;}}

returntrue;}}

04

publicclasshomework_4_04{

publicstaticvoidmain(Stringargs[])

{inti,day=l,a=2j=0,c;

c=day%7;

Stringb=,,H;

for(i=l;i<13;i++)

{switch(i)

{case1:

case3:

case5:

case7:

case8:

case10:

case12:

{for(day=l;day<32;day++)

{switch(a)

{case1:b=''星期一";break;

case2:b="星期二";break;

case3:b="星期三”;break;

case4:b二"星期四「break;

case5:b二"星期五”;break;

case6:b二"星期六";break;

case0:b="星期B";break;}

System.out.println("2008年“+i+“月"+day+"曰”+b);

a++;a=a%7;if(a%10==c)j++;}}

break;

case4:

case6:

case9:

case11:

{for(day=l;day<31;day++)

{switch(a)

{case1:b="星期一";break;

case2:b="星期二”;break;

case3:b二"星期三”;break;

case4:b二"星斯四";break;

case5:b二"星期五”;break;

case6:b="星期六”;break;

case0:b="星期H";break;}

System.out.println("2008年”+i+“月”+day+”日,,+b);a++;a=a%7;if(a%10==c)j++;}}

break;

case2:

{for(day=l;day<30;day++)

{switch(a)

{case1:b="星期一二break;

case2:b="星期二";break;

case3:b二“星期三”;break;

case4:b="星期四”;break;

case5:b二"星期五”;break;

case6:b二"星期六”;break;

case0:b二"星期日";break;}

System.out.println("2008"+day+"H"+b);a++;a=a%7;if(a%10==c)j++;}}

break;

default:

System.out.printin(“該算法有誤”);}}

System.out.println("2008年Fl期的個位數(shù)與相應(yīng)的星期恰好相同的總天數(shù)為:"+j);}

05

publicclasshomework_4_05{

publicstaticvoidmain(String[]args)

{for(intn=0;n<100;n++){

//(int)(Math.random()*20+0.5);}}}

06

publicclassJ_4_06{

privatestaticchardata[];

publicstaticvoidmain(String[]args){

inti,j;

data=newchar[10];

for(i=0;i<10;i++){

data[i]=(char)(,a'+(int)(Math.random()*26));}

System.out.printing排序前:");

for(i=0;i<10;i++){

System.out.print(data[i]+"");}

System.out.println();

chartemp;

for(i=l;i<10;i++)

for(j=0;j<10-i;j++)

if(data[j]>data[j+l]){

temp=data[j];

data[j]=data[j+l];

data[j+l]=temp;}

System.out.printin("排序后

for(i=0;i<10;i++){

System.out.print(data[i]+"");}

System.out.println();}}

08

publicclasshomework_4_08{

publicintbf_value=10000;

publicintaf_value;

publicStringbf_string;

publicStringaf_string;

publicString[]num=J9”};

publichomework_4_08(){

for(inti=0;i<69914;i++){

bf_value++;

af_value=bf_value+20085;

bf_string=newString(bf_value+'*");

af_string=newString(af_value+,n,);

if(comp(bf_string,af_string)){

System.out.print(bf_string+"");

System.out.println(af_string);

//return;

}})

publicbooleancomp(Stringa,Stringb){

for(inti=0;i<10;i++){

if(a.indexOf(num[i])<0){

if(b.indexOf(num[i])<0){

returnfalse;}}}

returntrue;}

publicstaticvoidmain(String[]args){

newhomework_4_08();}}

第五章

02

interfaceShape{

publicdoublegetArea();//計算并返回形狀的面積}

classCircleimplementsShape{

publicdoubleradius=3.0;//年徑

//計算并返回形狀的面枳

publicdoublegetArea(){

returnMath.PI*radius*radius;}}

classSquareimplementsShape{

publicdoublex=3.0;//半徑

//計算并返回形狀的面積

publicdoublegetArea(){

returnx*x;}}

publicclassJ_5_02<TextendsShape>{

publicdoubleget(Tf){

doublefb=f.getArea();

returnfb;}

publicstaticvoidmain(Stringargs[]){

J_5_02<Circle>circle=newJ_5_02<Circle>();

System.out.printin("半徑為3.0的圓的面稹是:"+circle.get(newCircle。));

J_5__02<Square>square=newJ_5__02<Square>();

System.out.printing邊長為3.0的正方形的面積是:"+square.get(newSquare()));}}

04

publicclassJ_5_04{

enumE_SEASON{

一月,三月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月"/枚舉

publicstaticvoidmain(Stringargs[]){

E_SEASON[]sa=E_SEASON.values();

for(inti=0;i<sa.length;i++){

switch(sa[i]){

case一月:

System.out.println("January-31天”);break;

case二月:

System.out.println("February-29天”);break;

case三月:

System.out.println("March-31天”);break;

case四月:

System.out.println("April-30天break;

case五月:

System.out.println("May-31天");break;

case六月:

System.out.println("June-30天”);break;

case七月:

System.out.println("July-31天");break;

case八月:

System.out.println("August-31天”);break;

case九月:

System.out.println("September-30天”);break;

case十月:

System.out.println(nOctober-31天”);break;

caseH—1月:

System.out.println("November-30天”);break;

case十二月:

System.out.println(HDecember-31天”);break;

}//switch結(jié)構(gòu)結(jié)束

}〃for循環(huán)結(jié)束

}//方法main結(jié)束}

第六章

04

publicclassJ_6_04{

publicstaticvoidmb_createException()

{thrownewArraylndexOutOfBoundsException();}

publicstaticvoidmb_method(){

try{mb_createException();

System.out.print("a");

}catch(ArithmeticExceptione){System.err.print("b");

}finally{

System.out.print("c");}}

publicstaticvoidmain(String[]args){

try{mb_method();

}catch(Exceptione){

System.err.print("m");}

System.out.print("n");}}

06

publicclasshomework_6_06{

publicstaticvoidmain(String[]args){

System.out.println(mb_calculate(10));}

staticintx=0;

publicstaticvoidmb_hanoi(intn,charstart,chartemp,charend){

if(n<=l)

{System.out.println("將盤從“+start+“移到"+end);

X++;)

else{

mb_hanoi(n-l,start,end,temp);

System.out.printin("將盤從"+start+“移到"+end);

mb_hanoi(n-l,temp,start,end);

x++;}}

publicstaticintmb_calculate(intn){

mb_hanoi(n/S7T'/E');

returnx;}}

07

importjava.util.Scanner;

publicclasshomework_6__07

{publicstaticintinput(){-

inta=0;

Scannerx=newScanner(System.in);〃輸入要求階乘的數(shù)

System.out.printin(“請輸入一個數(shù):”);

Stringy=x.next();

try(

a=lnteger.parselnt(y);

}catch(Exceptioni){

System.out.printin(“錯誤!請輸入數(shù)字!");}

returna;}

publicstaticvoidtestl(intn){

intresult=l;

inty=n;

do{〃求階乘

result=n*result;n--;

}while(n>=l);

System.out.printlnC1非遞歸求法:,,+y+,,!="+result);}

publicstaticinttest2(intn){

if(n==l11n==0)

returnn;

else

returnn*test2(n-l);}

publicstaticvoidmain(String[]args){

intn=input();

testl(n);

System.out.println(“遞歸求法:“+n+“!=”+test2(n));}}

08

publicclassJ_6_08{

privatestaticintcount=2;

privateJ_6_08(){}

publicstaticJ_6_08getJ08(){

if(count>0){

count-;

returnnewJ_6_08();}

returnnull;}

publicstaticvoidmain(String[]args){

J_6_08jl=getJ_08();

J_6_08j2=getJ_08();

J_6_08j3=getJ_08();

if(jl!=null){

System.out.println("jl");}

if(j2!=null){

System.out.println("j2");}

if(j3!=null){

System.out.println(nj3");}}}

第七章

03

publicclasshomework_7_03{

publicstaticvoidmain(String[]args){

intx=lnteger.parselnt(lnput.infln("請輸入第一個整數(shù):"輸入不能為空!"));

inty=Intege卬arselntjmput.inflnj”請輸入第二個整數(shù):"輸入木能為空!"));

System.out.printin("請輸入第一個整數(shù):"+x);

System.out.printin(”請輸入第二個整數(shù):"+y);

System.out.println("計算結(jié)果:“+x+"+“+y+“=”+(x+y));}}

04

importjava.io.BufferedReader;

importjava.io.BufferedWriter;

importjava.io.FilelnputStream;

importjava.io.FileOutputStream;

importjava.io.FileWriter;

importjava.io.lOException;

importjava.io.InputStreamReader;

publicclasshomework_7_04{

publicstaticvoidmain(String[]args){

try{adder();}

catch(lOExceptionel){

el.printStackTrace();}

try{read();}

catch(lOExceptione){

e.printStackTrace();}}

publicstaticvoidadder()throwsIOException{

FileOutputStreamtestfile=newFileOutputStreamC'CrWdata.txt");

testfile.write(newString("").getBytes());〃清空文術(shù)

for(intn=l;n<=5;n++){

Stringa=Input.infln(“請輸入第”+n+”位學(xué)生成績“J輸入不能為空!");

BufferedWriterbw=newBufferedWriter(newFileWriterC'Ci/data.txt'^true));

bw.newUne();

bw.append(a);

bw.close();}}

publicstaticvoidread()throwsIOException{

Fileinputstreamfr=newFilelnputStream("C:/data.txt");

InputStreamReaderir=newInputStreamReader(fr);

BufferedReaderbr=newBufferedReader(ir);

Strings=br.readLine();

int[]x={0,0,0,0,01;

inta=0;

while(br.ready()){

s=br.readUne();

x[a]=lnteger.parselnt(s);

a++;}

for(inty=0;y<4;y++){

for(intm=0;m<4;m++){

intnuml=x[m];

intnum2=x[m+l];

if(numl>num2){

intn=x[m];

x[m]=x[m+l];

x[m+l]=n;}}}

System.out.println("五位學(xué)生的平均成績?yōu)椋骸?((x[0]+x[l]+x⑵+x[3]+x[4])/5));

System.out.print("五位學(xué)生成績的排序為:”);

for(intm=0;m<5;m++){

System.out.print(x[m]+"");}}}

06

importjava.io.File;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.lOException;

publicclasshomework_7_06{

publicstaticString[]data;

publicstaticFileOutputStreamfos;

publicstaticFilefile=null;

publicstaticvoidmain(String[]args){

data=newString[10];

data[O]="*******

data[l]=II******u.

data[2]=?1**

11*************”.

data[3]=/

data[4]=”*************”.

data[5]=”**

”**

data[6]=/

data[7]=???I.

data[8]=

try{file=newFile("data2.txt");

fos=newFileOutputStream(file);

for(inti=0;i<9;i++){data[i]+=System.getProperty("line.separator*');

fos.write(data[i].getBytes());}

System.out.printin("寫入成功!”);

}catch(FileNotFoundExceptione)

{e.printStackTrace();

}catch(lOExceptione){

e.printStackTrace();}}}

input

importjava.util.Scanner;

publicclassInput{

publicstaticStringinf;

publicstaticStringinfln(Stringa,Stringb){

booleanflag=true;

do{flag=rein3(a,b);

}while(!flag);

returninf;}

publicstaticbooleanrein3(Stringa,Stringb){

Scannerin=newScanner(System.in);

System.out.println(a);

StringBufferstr=newStringBuffer();

str.append(in.nextLine());

inf=str.toString().trim();

if(inf==null11inf.equals(H")){

System.out.println(b);

returnfalse;}

returntrue;}}

第八章

06

importjava.awt.event.MouseAdapter;

importjava.awt.event.MouseEvent;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JPanel;

importjavax.swing.JTextField;

publicclasshomework_8_06{

publicstaticJFramejf;

publicstaticJPaneljp;

publicstaticJTextFieldjtfl,jtf2,jtf3;

publicstaticJLabeljllzjl2;

publicvoidinit(){

jf=newJFrame("Sum");

jp=newJPanel();

jp.setLayout(null);

jtfl=newJTextField();

jtfl.setBounds(Oz10,50,20);

jp.add(jtfl);

jll=newJLabel("+");

jll.setBounds(55,10,20,20);

jp.add(jll);

jtf2=newJTextField();

jtf2.setBounds(70,10,50,20);

jp.add(jtf2);

jl2=newJLabel("=");

jl2.setBounds(125,10,20,20);

jl2.addMouseListener(newMyMouseListener());

jp.add(jl2);

jtf3=newJTextField();

jtf3.setBounds(140,10,50,20);

jp.add(jtf3);

jf.add(jp);

jf.setBounds(100,100,215,70);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setVisible(true);}

publicstaticvoidmain(String[]args){

newhomework_8_06().init();}

publicclassMyMouseListenerextendsMouseAdapter{

publicvoidmouseClicked(MouseEvente){

if(e.getClickCount()==1){

if(e.getSource()==jl2){jtf3.setText(String.valueOf(lnteger.parselnt(jtfl.getText())

+Integer.parselnt(jtf2.getlext())));}}}}}

07

importjava.awt.Container;

importjava.awt.FlowLayout;

importjavax.swing.BoxLayout;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JPanel;

publicclasshomework_8_07{

publicstaticvoidmain(Stringargs[])

{JFrameapp=newJFrame("Ex08-07");

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

app.setSize(215,150);

Containerc=app.getContentPane();

c.setLayout(newFlowLayout());

JPanel[]p=newJPanel[3];

inti;

for(i=0;i<3;i++)

{p[i]=newJPanel();

p[ij.setLayout(newBoxLayout(p[i],BoxLayout.X_AXIS));

c.add(p[i]);}

Strings;

JButtonb;

int[]pj={0,0,0,1,2,2};

for(i=0;i<6;i++)

{s="按鈕“+(i+1);

b=newJButton(s);

p[pj[i]].add(b);)

app.setVisible(true);}}

08

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.io.File;

importjava.io.FilelnputStream;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.FileWriter;

importjava.io.lOException;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importjavax.swing.JTextArea;

importjavax.swing.JTextField;

publicclasshomework__8_08{

publicstaticJFramejf;

publicstaticJPaneljp;

publicstaticJTextFieldreadPath,savePath;

publicstaticJButtonreadButton,saveButton;

publicstaticJTextAreatextcontent;

publicstaticFileinputstreamfis;

publicstaticFileOutputStreamfos;

publicstaticFilefile;

publicstaticbyte[]data;

publicvoidinit(){

jf=newJFrame("多文檔文本編輯器)

jp=newJPanel();

jp.setLayout(null);

readPath=newJTextField();

readPath.setBounds(5z5,300,25);

jp.add(readPath);

readButton=newJButton("讀取”);

readButton.setBounds(310,5,60,25);

readButton.addActionListener(newButtonListener());

jp.add(readButton);

savePath=newJTextField();

savePath.setBounds(5,40,300,25);

jp.add(savePath);

saveButton=newJButton(“保存");

saveButton.setBounds(310,40,60,25);

saveButton.addActionListener(newButtonListener());

jp.add(saveButton);

textcontent=newJTextArea();

textContent.setBounds(0,75,400,400);

jp.add(textContent);

jf.add(jp);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setBounds(100,100,400,400);

jf.setVisible(true);}

publicstaticvoidmain(String[]args){

newhomework_8_08().init();}

publicclassButtonListenerimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==readButton){

file=newFile(readPath.getText());

try{fis=newFilelnputStream(file);

data=newbyte[2000];

intch,index=0;

while((ch=fis.read())!=-1){

data[index++]=(byte)ch;}

Stringstr=newString(data,0,index);

textContent.setText(str);

JOptionPane.showMessageDialog(null,"恭喜您,讀取成功!");}

catch(FileNotFoundExceptionel)

{el.printStackTrace();

}catch(lOExceptionel){

el.printStackTrace();}}

elseif(e.getSource()==saveButton){

try{FileWriterwriter=newFileWriter(savePath.getText(),false);

writer.write(textContent.getText());

writer.close();

JOptionPane.showMessageDialog(nulL"恭喜您,保存成功!”);

}catch(lOExceptionel){

el.printStackTrace();}}}}}

09

importjava.awt.event.*;

publicclasshomework_8_09implementsWindowListener{

publicvoidwindowOpened(WindowEvente)

()

publicvoidwindowClosing(WindowEvente)

{System.exit(O);}

publicvoidwindowClosed(WindowEvente)

()

publicvoidwindowlconified(WindowEvente)

publicvoidwindowDeiconified(WindowEvente){}

publicvoidwindowActivated(WindowEvente)

()

publicvoidwindowDeactivated(WindowEvente){}}

11

importjava.awt.*;

importjava.awt.event.WindowAdapter;

importjava.awt.event.WindowEvent;

publicclasshomework_8_ll{

publicstaticvoidmain(String[]args){

Framef=newFrame("xjfshuaige");

f.setSize(400,500);

f.setLocation(100,100);

f.setBackground(Color.orange);

//f.setLayout(newBorderLayout(10,10));

//f.setLayout(newFlowLayout(FlowLayout.LEFT));

f.setLayout(newGridLayout(3210,l。,);

Buttonbtl=newButton("xihuanl");

Buttonbt2=newButton("xihuan2");

Buttonbt3=newButtonf'xihuanS");

Buttonbt4=newButton("xihuan4");

Buttonbt5=newButton("xihuan5");

f.add(btl,"North");

f.addjbtZ/'South");

f.add(bt3,"West");

f.add(bt4;'East");

f.addjbtS/'Center");

//f.add(bt);

//f.addWindowListener(new

MyWindowListener());

//f.addWindowListener(newYourWindowListener());

//f.addWindowListener(newHisWindowListener());

f.addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente)

{System.exit(O);}});

f.show();}

第九章

03

〃用小應(yīng)用程序J_Digit來驗證小應(yīng)用程序執(zhí)行過程的生命周期

importjava.awt.BorderLayout;

importjava.awt.Container;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.JApplet;

importjavax.swing.JButton;

importjavax.swing.JTextField;

publicclasshomework_9_03extendsJAppletimplementsActionListener

{privateJTextFieldm__textField=newJIextField();

publicvoidstart()

{System.out.println("start:啟動)}

publicvoidpaint()

{System.out.println("paint:繪制");}

publicvoidstop()

{System.out.println("stop:停止");}

publicvoiddestroy()

{System.out.println("destroy:關(guān)閉");}

publicvoidinit()

{System.out.println("init:初始化”);

Containerc=getContentPane();

JButtonb;

String[]s={BorderLayout.SOUTH,BorderLayout.EASl^

BorderLayout.NORTH,BorderLayout.WEST};

for(inti=0;i<4;i++)

{b=newJButton(,n,+i);

c.add(b,s[i]);

b.addActionListener(this);}//for循環(huán)結(jié)束

c.add(m_textField,BorderLayout.CENTER);

}//方法init結(jié)束

publicvoidactionPerformed(ActionEvente)

彳System.out.println("事件處理");

Strings=m_textField.getText()+e.getActionCommand();

m_textField.setText(s);}//方法actionPerformed結(jié)束

}/7類J_Digit結(jié)束

05

〃在網(wǎng)頁中用字母繪制一個漢字一一”大“

importjava.awt.Graphics;

importjavax.swing.JApplet;

publicclasshomework_9_05extendsJApplet{

publicvoidpaint(Graphicsg){

intx=20,y=40;

inti,b;

g.drawString("B",x+40,y);

g.drawStringf'B",x+40,y+20);

for(i=10;i<80;){

g.drawStnng("B",x+i,y+40);

i=i+10;}

g.drawString("B"zx4-40,y4-60);

g.drawString("B",x+30,y+80);

g.drawStringj"B”,x+20,y+100);

g.drawStringC'B'^x+lO^+lZO);

g.drawString("B",x+50,y+80);

g.drawStringf'B'^x+GO^+lOO);

g.drawString("B",x+70,y+120);}}

06

〃單位換算

importjava.awt.Container;

importjava.awt.FlowLayout;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.io.File;

importjava.io.Fileinputstream;

importjava.io.FileNotFoundException;

importjava.io.FileWriter;

importjava.io.lOException;

importjava.math.BigDecimal;

importjavax.swingJApplet;

importjavax.swing.JButton;

importjavax.swing.JLabel;

importjavax.swingJOptionPane;

importjavax.swing.JTextField;

publicclasshomework__9_06extendsJApplet{

privateContainerc;

privateJLabeltitle;

privateJButton[]jb;

privateJLabel[]jl;

privateJTextField[]jtf;

privateString[]text={"盎司=,"克丁'磅”克丁,千克"磅”,“噸”公噸,

“磅“千克,廣短噸叫"磅”);

privateBigDecimalresult;

publicvoidinit(){

setSize(280,180);

c=getContentPane();

c.setLayout(newFlowLayout());

title=newJLabel("單位換算”);

jb=newJButton[5];

jl=newJLabel[10];

jtf=newJTextField[10];

for(inti=0;i<5;i++){

jtf[2*i]=newJTextFieid(5);

c.add(jtf[2*i]);

jl[2*i]=newJLabel(text[2*i]);

c.add(jl[2*i]);

jtf[2*i+1]=newJTextField(5);

c.add(jtf[2*i+1]);

jl[2*i+1]=newJLabel(text[2*i+1]);

c.add(jl[2*i+1]);

jb[i]=newJButton("itM");

jb[i].addActionListener(newButtonListener());

c.add(jb[i]);}}

publicclassButtonListenerimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==jb[O]){

result=newBigDecimal(jtf[O].getText());

result=result.multiply(newBigDecimal(28.350));

jtf[l].setText(result.toString());}

if(e.getSource()==jb[l]){

result=newBigDecimal(jtf[2].getText());

result=result.multiply(newBigDecimal(454));

jtf[3].setText(result.toString());}

if(e.getSource()==jb[2]){

result=newBigDecimaI(jtf[4].getText());

result=result.multiply(newBigDecimal(2.205));

jtf[5].setlext(result.toString());}

if(e.getSource()==jb[3]){

result=newBigDecimal(jtf[6].getText());

result=result.multiply(newBigDecimal(lOOO));

jtf[7].setText(result.toString());}

if(e.getSource()==jb[4]){

result=newBigDecimal(jtf[8].getText());

result=result.multiply(newBigDecimal(2000));

jtf[9].setlext(result.toString());}}}}

08

〃在小應(yīng)用程序上顯示一個愛心

importjava.awt.Graphics;

importjavax.swing.JApplet;

publicclasshomework_9_08extendsJApplet{

publicvoidpaint(Graphicsg){

intx,y;intij;

for(i=0;i<27;i++){}

/*g.drawString("------******-------******------",30,20);

g.drawString("--********************30,40);

g.drawString("-*************_*************30,60);

gdrawString("—***************************__??gQj.

g.drawString("—************************—",30,100)/

g.drawString("------******************------”,30,120);

g.drawString("---------*************---------",10,140);

g.drawString("------------*******------------"z10,160);

g.drawString("---------------**----------------",10,180);*/}}

第十一章

04

〃加法程序

importjava.util.Date;

publicclasshomework_ll_04implementsRunnable{

privatestaticintn,sl;

privatestaticint[]num;

privatestaticdoubletimeStart,timeEnd4l,t2;

privatestatichomework_ll_04ctl,ct2;

intstart,end;

staticints2=0;

staticintcount=2;

publichomework_ll_04(ints,inte){

start=s;end=e;}

publicstaticvoidmain(String[]args){

timeStart=System.currentTimeMillis();

n=10+(int)(Math.random()*100);

num=newint[n];

for(inti=0;i<n;i++){

num[i]=(int)(Math.random()*100);

;)

for(inti=0;i<n;i++){

sl+=num[i];}

timeEnd=System.currentTimeMillis();

tl=timeEnd-timeStart;

System.out.println("sl="+sl);

System.out.println("tl="+tl+"ms");

timeStart=System.currentTimeMillis();

ctl=newhomework__ll_04(0/n/2-l);

newThread(ctl).start();

ct2=newhomework_ll_04(n/2,n-l);

newThread(ct2).start();}

publicvoidrun(){

for(inti=start;i<=end;i++){

s2+=num[i];}

count-;

if(count<=0){

timeEnd=System.currentTimeMi

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論