李尊朝java教材作业参考答案new.docx
《李尊朝java教材作业参考答案new.docx》由会员分享,可在线阅读,更多相关《李尊朝java教材作业参考答案new.docx(22页珍藏版)》请在冰豆网上搜索。
李尊朝java教材作业参考答案new
作业参考答案
习题一
4、如何建立和运行Java程序?
首先启动文本编辑器,如记事本、UltraEdit等,编辑程序代码,并以.Java作为文件扩展名保存程序源代码;然后进入dos环境利用javac编译源程序,生成扩展名为.class的字节码文件;再利用命令java运行字节码文件,得到程序的运行结果。
在集成开发环境Jbuilder、Eclipse下,可以完成程序的编辑、编译、调试及运行等所有任务。
5、
publicclassLikeJava
{
publicstaticvoidmain(String[]args)
System.out.println(“ILikeJavaVerymuch!
”);
}
习题二
(1)45
(2)false(3)14(4)14(5)-6(6)true(7)12
9、
publicclassVolume
doubler=0,v=0;
r=double.parseDouble(args[0]);
v=4*3.14159/3*r*r*r;//floatpi=3.14159f;
System.out.println(“球体积为:
”+v);
习题三
8、
publicclassFactorials{
publicstaticvoidmain(Stringargs[])
inti,j;
longs=0,k;
i=1;
do//外循环开始
k=1;
j=1;
do{//内循环开始
k=k*j;//内循环体
j++;
}while(j<=i);//内循环结束
System.out.println(i+"!
="+k);
s=s+k;
i++;
}while(i<=20);//外循环结束
System.out.println("Totalsum="+s);
10、
publicclassNum
inti,j,k,n;
for(n=100;n<1000;n++)
i=n/100;
j=(n-i*100)/10;
k=n%10;
if(i*i*i+j*j*j+k*k*k==n)
System.out.print(n+"");
习题四
importjava.util.Scanner;
classFactor{
longfac(intm)
{if(m==0||m==1)return1;
elsereturnm*fac(m-1);
{inti,n;
longsum=0;
Strings="";
Scannerinput=newScanner(System.in);
System.out.print("Pleaseinputn:
");
n=input.nextInt();
Factorf=newFactor();
for(i=1;i<=n;i++)
{System.out.println(f.fac(i));
sum=sum+f.fac(i);
s=s+i+"!
+";
System.out.println(s.substring(0,s.length()-1)+"="+sum);
习题五
2、
importjava.io.*;
publicclassYangHuiOk
publicstaticvoidmain(Stringargs[])throwsIOException
intmax,a[][],i,j;
charx;
System.out.print("请输入杨辉三角要显示的行数:
x=(char)System.in.read();
max=Integer.parseInt(String.valueOf(x));
a=newint[max][];//不要忘记初始化数组了
for(i=0;i{a[i]=newint[i+1];}a[0][0]=1;for(i=1;i{a[i][0]=1;a[i][a[i].length-1]=1;for(j=1;j{a[i][j]=a[i-1][j-1]+a[i-1][j];}}for(i=0;i{//for(j=0;j<=max-i;j++)System.out.print("");for(j=0;j<=a[i].length-1;j++)System.out.print(a[i][j]+"");System.out.println();}}} 5.//不能用t=a[i][j];a[i][j]=a[j][i];a[j][i]=t来实现,此法只适合方阵习题六9、publicclassVehicle{Stringcolor,kind;intspeed;Vehicle(){color=”Red”;kind=”卡车”;speed=0;}publicvoidsetColor(Stringcolor1){color=color1;}publicvoidsetSpeed(Stringspeed1){speed=speed1;}publicvoidsetKind(Stringkind1){kind=kind1;}publicStringgetColor(){returncolor;}publicStringgetKind(){returnkind;}publicintgetSpeed(){returnspeed;}publicstaticvoidmain(String[]args){Vehicleche=newVehicle();Che.setColor(“Blue”);Che.setSpeed(150);Che.setKind(“跑车”);System.out.println(“有一辆”+che.getColor()+”的”+che.getKind()+”行驶在高速公路上”);System.out.println(“时速”+che.getSpeed()+”km/h”);}}习题七7、publicclassVehicle{Stringcolor,kind;intspeed;Vehicle(){color=””;kind=””;speed=0;}publicvoidsetColor(Stringcolor1){color=color1;}publicvoidsetSpeed(Stringspeed1){speed=speed1;}publicvoidsetKind(Stringkind1){kind=kind1;}publicStringgetColor(){returncolor;}publicStringgetKind(){returnkind;}publicintgetSpeed(){returnspeed;}}publicclassCarextendsVehicle{intpassenger;publicCar(){super();passenger=0;}publicvoidsetPassenger(intpassenger){this.passenger=passenger;}publicintgetPassenger(){returnpassenger;}publicstaticvoidmain(String[]args){Carbenz=newCar();benz.setColor(“Yellow”);benz.setKind(“roadster”);benz.setSpeed(120);benz.setPassenger(4);System.out.println(“benz:“);System.out.println(“Color“+benz.getColor());System.out.print(“Speed(km/h)“);System.out.println(benz.getSpeed());System.out.println(“Kind:“+benz.getKind());System.out.print(“Passenger:“);System.out.println(benz.getPassenger());}}习题九4、importjava.io.*;publicclassUseException{publicstaticvoidmain(String[]args){System.out.println("请输入一个整数字符串");try{BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));inta=Integer.parseInt(in.readLine());System.out.println("您输入的整数是:"+a);}catch(IOExceptione){System.out.println("IO错误");}catch(NumberFormatExceptione1){System.out.println("您输入的不是一个整数字符串");}}}习题十7、importjava.io.*;publicclassSaveName{publicstaticvoidmain(String[]args){try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbw=newBufferedWriter(newFileWriter("name.txt"));Strings;while(true){System.out.println("请输入姓名:");s=br.readLine();if(s.length()==0)break;bw.write(s);bw.newLine();}br.close();bw.close();}catch(FileNotFoundExceptione){System.out.println(e.toString());}catch(IOExceptione1){System.out.println(e1.toString());}}}8、importjava.io.*;publicclassSaveGrade{publicstaticvoidmain(String[]args){try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbw=newBufferedWriter(newFileWriter("grade.txt"));Strings,ss;while(true){System.out.println("请输入姓名:");s=br.readLine();if(s.length()==0)break;bw.write(s);bw.newLine();System.out.println("请输入学号:");s=br.readLine();bw.write(s);bw.newLine();System.out.println("请输入成绩:");s=br.readLine();bw.write(s);bw.newLine();}br.close();bw.close();intmax=0,min=100,total=0,num=0;BufferedReaderbf=newBufferedReader(newFileReader("grade.txt"));while(true){ss=bf.readLine();if(ss==null)break;ss=bf.readLine();ss=bf.readLine();intgrade=Integer.parseInt(ss);total+=grade;num+=1;if(grade>max)max=grade;if(grade}System.out.println("学生成绩中最高为:"+max+",最低为:"+min+",平均分为:"+total*1.0/num);bf.close();}catch(FileNotFoundExceptione){System.out.println(e.toString());}catch(IOExceptione1){System.out.println(e1.toString());}}}习题十一6、importjava.awt.*;importjava.awt.event.*;publicclassChangeColorextendsFrame{privateButtonred=newButton("红");privateButtongreen=newButton("绿");privateButtonblue=newButton("蓝");privateTextFieldtext=newTextField();publicChangeColor(){super("改变颜色");this.setLayout(null);text.setBackground(Color.WHITE);red.setBounds(25,50,50,20);this.add(red);green.setBounds(125,50,50,20);this.add(green);blue.setBounds(225,50,50,20);this.add(blue);text.setBounds(25,100,250,30);this.add(text);red.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.RED);}});green.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.GREEN);}});blue.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.BLUE);}});addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setSize(300,200);setVisible(true);}publicstaticvoidmain(String[]args){ChangeColorcolor=newChangeColor();}}习题十二5、importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassGoodsextendsJFrame{privateJComboBoxlist;privateJTextAreainfo;privateStringnames[]={"请选择你要查询的商品","A商品","B商品","C商品","D商品","E商品","F商品"};privateStringgoods[][]={{"","",""},{"A商品","北京","¥300"},{"B商品","上海","¥400"},{"C商品","广州","¥500"},{"D商品","长沙","¥600"},{"E商品","武汉","¥700"},{"F商品","天津","¥800"}};publicGoods(){super("商品信息");Containerpane=this.getContentPane();pane.setLayout(newBorderLayout());list=newJComboBox(names);info=newJTextArea(5,20);pane.add(list,BorderLayout.NORTH);pane.add(info,BorderLayout.CENTER);list.addItemListener(newItemListener(){publicvoiditemStateChanged(ItemEvente){intindex=list.getSelectedIndex();info.setText("商品名:"+goods[index][0]+"\n");info.append("产地:"+goods[index][1]+"\n");info.append("价格:"+goods[index][2]+"\n");}});this.setSize(250,300);this.setVisible(true);}publicstaticvoidmain(String[]args){Goodsccc=newGoods();ccc.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}} 习题十三4、importjava.awt.*;importjavax.swing.JApplet;publicclassSimpleAppletextendsJAppletimplementsRunnable{intj=0;Threadthread;publicvoidinit(){thread=newThread(this);thread.start();}publicvoidstop(){thread.destroy();thread=null;}publicvoidrun(){while(true){repaint();j++;j=j%3;try{thread.sleep(4000);}catch(InterruptedExceptione){}}}publicvoidpaint(Graphicsg){while(true){g.setFont(newFont("黑体",Font.BOLD,24));switch(j){case0:g.setColor(Color.RED);break;case1:g.setColor(Color.BLUE);break;case2:g.setColor(Color.GREEN);}g.drawString("各位同学大家好,你们辛苦啦!",50,60);}}}习题十四4、publicclassUseThreadextendsThread{intst,en,count=0;UseThread(intm,intn){this.st=
a[i]=newint[i+1];
a[0][0]=1;
for(i=1;i{a[i][0]=1;a[i][a[i].length-1]=1;for(j=1;j{a[i][j]=a[i-1][j-1]+a[i-1][j];}}for(i=0;i{//for(j=0;j<=max-i;j++)System.out.print("");for(j=0;j<=a[i].length-1;j++)System.out.print(a[i][j]+"");System.out.println();}}} 5.//不能用t=a[i][j];a[i][j]=a[j][i];a[j][i]=t来实现,此法只适合方阵习题六9、publicclassVehicle{Stringcolor,kind;intspeed;Vehicle(){color=”Red”;kind=”卡车”;speed=0;}publicvoidsetColor(Stringcolor1){color=color1;}publicvoidsetSpeed(Stringspeed1){speed=speed1;}publicvoidsetKind(Stringkind1){kind=kind1;}publicStringgetColor(){returncolor;}publicStringgetKind(){returnkind;}publicintgetSpeed(){returnspeed;}publicstaticvoidmain(String[]args){Vehicleche=newVehicle();Che.setColor(“Blue”);Che.setSpeed(150);Che.setKind(“跑车”);System.out.println(“有一辆”+che.getColor()+”的”+che.getKind()+”行驶在高速公路上”);System.out.println(“时速”+che.getSpeed()+”km/h”);}}习题七7、publicclassVehicle{Stringcolor,kind;intspeed;Vehicle(){color=””;kind=””;speed=0;}publicvoidsetColor(Stringcolor1){color=color1;}publicvoidsetSpeed(Stringspeed1){speed=speed1;}publicvoidsetKind(Stringkind1){kind=kind1;}publicStringgetColor(){returncolor;}publicStringgetKind(){returnkind;}publicintgetSpeed(){returnspeed;}}publicclassCarextendsVehicle{intpassenger;publicCar(){super();passenger=0;}publicvoidsetPassenger(intpassenger){this.passenger=passenger;}publicintgetPassenger(){returnpassenger;}publicstaticvoidmain(String[]args){Carbenz=newCar();benz.setColor(“Yellow”);benz.setKind(“roadster”);benz.setSpeed(120);benz.setPassenger(4);System.out.println(“benz:“);System.out.println(“Color“+benz.getColor());System.out.print(“Speed(km/h)“);System.out.println(benz.getSpeed());System.out.println(“Kind:“+benz.getKind());System.out.print(“Passenger:“);System.out.println(benz.getPassenger());}}习题九4、importjava.io.*;publicclassUseException{publicstaticvoidmain(String[]args){System.out.println("请输入一个整数字符串");try{BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));inta=Integer.parseInt(in.readLine());System.out.println("您输入的整数是:"+a);}catch(IOExceptione){System.out.println("IO错误");}catch(NumberFormatExceptione1){System.out.println("您输入的不是一个整数字符串");}}}习题十7、importjava.io.*;publicclassSaveName{publicstaticvoidmain(String[]args){try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbw=newBufferedWriter(newFileWriter("name.txt"));Strings;while(true){System.out.println("请输入姓名:");s=br.readLine();if(s.length()==0)break;bw.write(s);bw.newLine();}br.close();bw.close();}catch(FileNotFoundExceptione){System.out.println(e.toString());}catch(IOExceptione1){System.out.println(e1.toString());}}}8、importjava.io.*;publicclassSaveGrade{publicstaticvoidmain(String[]args){try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbw=newBufferedWriter(newFileWriter("grade.txt"));Strings,ss;while(true){System.out.println("请输入姓名:");s=br.readLine();if(s.length()==0)break;bw.write(s);bw.newLine();System.out.println("请输入学号:");s=br.readLine();bw.write(s);bw.newLine();System.out.println("请输入成绩:");s=br.readLine();bw.write(s);bw.newLine();}br.close();bw.close();intmax=0,min=100,total=0,num=0;BufferedReaderbf=newBufferedReader(newFileReader("grade.txt"));while(true){ss=bf.readLine();if(ss==null)break;ss=bf.readLine();ss=bf.readLine();intgrade=Integer.parseInt(ss);total+=grade;num+=1;if(grade>max)max=grade;if(grade}System.out.println("学生成绩中最高为:"+max+",最低为:"+min+",平均分为:"+total*1.0/num);bf.close();}catch(FileNotFoundExceptione){System.out.println(e.toString());}catch(IOExceptione1){System.out.println(e1.toString());}}}习题十一6、importjava.awt.*;importjava.awt.event.*;publicclassChangeColorextendsFrame{privateButtonred=newButton("红");privateButtongreen=newButton("绿");privateButtonblue=newButton("蓝");privateTextFieldtext=newTextField();publicChangeColor(){super("改变颜色");this.setLayout(null);text.setBackground(Color.WHITE);red.setBounds(25,50,50,20);this.add(red);green.setBounds(125,50,50,20);this.add(green);blue.setBounds(225,50,50,20);this.add(blue);text.setBounds(25,100,250,30);this.add(text);red.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.RED);}});green.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.GREEN);}});blue.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.BLUE);}});addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setSize(300,200);setVisible(true);}publicstaticvoidmain(String[]args){ChangeColorcolor=newChangeColor();}}习题十二5、importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassGoodsextendsJFrame{privateJComboBoxlist;privateJTextAreainfo;privateStringnames[]={"请选择你要查询的商品","A商品","B商品","C商品","D商品","E商品","F商品"};privateStringgoods[][]={{"","",""},{"A商品","北京","¥300"},{"B商品","上海","¥400"},{"C商品","广州","¥500"},{"D商品","长沙","¥600"},{"E商品","武汉","¥700"},{"F商品","天津","¥800"}};publicGoods(){super("商品信息");Containerpane=this.getContentPane();pane.setLayout(newBorderLayout());list=newJComboBox(names);info=newJTextArea(5,20);pane.add(list,BorderLayout.NORTH);pane.add(info,BorderLayout.CENTER);list.addItemListener(newItemListener(){publicvoiditemStateChanged(ItemEvente){intindex=list.getSelectedIndex();info.setText("商品名:"+goods[index][0]+"\n");info.append("产地:"+goods[index][1]+"\n");info.append("价格:"+goods[index][2]+"\n");}});this.setSize(250,300);this.setVisible(true);}publicstaticvoidmain(String[]args){Goodsccc=newGoods();ccc.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}} 习题十三4、importjava.awt.*;importjavax.swing.JApplet;publicclassSimpleAppletextendsJAppletimplementsRunnable{intj=0;Threadthread;publicvoidinit(){thread=newThread(this);thread.start();}publicvoidstop(){thread.destroy();thread=null;}publicvoidrun(){while(true){repaint();j++;j=j%3;try{thread.sleep(4000);}catch(InterruptedExceptione){}}}publicvoidpaint(Graphicsg){while(true){g.setFont(newFont("黑体",Font.BOLD,24));switch(j){case0:g.setColor(Color.RED);break;case1:g.setColor(Color.BLUE);break;case2:g.setColor(Color.GREEN);}g.drawString("各位同学大家好,你们辛苦啦!",50,60);}}}习题十四4、publicclassUseThreadextendsThread{intst,en,count=0;UseThread(intm,intn){this.st=
a[i][0]=1;
a[i][a[i].length-1]=1;
for(j=1;j{a[i][j]=a[i-1][j-1]+a[i-1][j];}}for(i=0;i{//for(j=0;j<=max-i;j++)System.out.print("");for(j=0;j<=a[i].length-1;j++)System.out.print(a[i][j]+"");System.out.println();}}} 5.//不能用t=a[i][j];a[i][j]=a[j][i];a[j][i]=t来实现,此法只适合方阵习题六9、publicclassVehicle{Stringcolor,kind;intspeed;Vehicle(){color=”Red”;kind=”卡车”;speed=0;}publicvoidsetColor(Stringcolor1){color=color1;}publicvoidsetSpeed(Stringspeed1){speed=speed1;}publicvoidsetKind(Stringkind1){kind=kind1;}publicStringgetColor(){returncolor;}publicStringgetKind(){returnkind;}publicintgetSpeed(){returnspeed;}publicstaticvoidmain(String[]args){Vehicleche=newVehicle();Che.setColor(“Blue”);Che.setSpeed(150);Che.setKind(“跑车”);System.out.println(“有一辆”+che.getColor()+”的”+che.getKind()+”行驶在高速公路上”);System.out.println(“时速”+che.getSpeed()+”km/h”);}}习题七7、publicclassVehicle{Stringcolor,kind;intspeed;Vehicle(){color=””;kind=””;speed=0;}publicvoidsetColor(Stringcolor1){color=color1;}publicvoidsetSpeed(Stringspeed1){speed=speed1;}publicvoidsetKind(Stringkind1){kind=kind1;}publicStringgetColor(){returncolor;}publicStringgetKind(){returnkind;}publicintgetSpeed(){returnspeed;}}publicclassCarextendsVehicle{intpassenger;publicCar(){super();passenger=0;}publicvoidsetPassenger(intpassenger){this.passenger=passenger;}publicintgetPassenger(){returnpassenger;}publicstaticvoidmain(String[]args){Carbenz=newCar();benz.setColor(“Yellow”);benz.setKind(“roadster”);benz.setSpeed(120);benz.setPassenger(4);System.out.println(“benz:“);System.out.println(“Color“+benz.getColor());System.out.print(“Speed(km/h)“);System.out.println(benz.getSpeed());System.out.println(“Kind:“+benz.getKind());System.out.print(“Passenger:“);System.out.println(benz.getPassenger());}}习题九4、importjava.io.*;publicclassUseException{publicstaticvoidmain(String[]args){System.out.println("请输入一个整数字符串");try{BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));inta=Integer.parseInt(in.readLine());System.out.println("您输入的整数是:"+a);}catch(IOExceptione){System.out.println("IO错误");}catch(NumberFormatExceptione1){System.out.println("您输入的不是一个整数字符串");}}}习题十7、importjava.io.*;publicclassSaveName{publicstaticvoidmain(String[]args){try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbw=newBufferedWriter(newFileWriter("name.txt"));Strings;while(true){System.out.println("请输入姓名:");s=br.readLine();if(s.length()==0)break;bw.write(s);bw.newLine();}br.close();bw.close();}catch(FileNotFoundExceptione){System.out.println(e.toString());}catch(IOExceptione1){System.out.println(e1.toString());}}}8、importjava.io.*;publicclassSaveGrade{publicstaticvoidmain(String[]args){try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbw=newBufferedWriter(newFileWriter("grade.txt"));Strings,ss;while(true){System.out.println("请输入姓名:");s=br.readLine();if(s.length()==0)break;bw.write(s);bw.newLine();System.out.println("请输入学号:");s=br.readLine();bw.write(s);bw.newLine();System.out.println("请输入成绩:");s=br.readLine();bw.write(s);bw.newLine();}br.close();bw.close();intmax=0,min=100,total=0,num=0;BufferedReaderbf=newBufferedReader(newFileReader("grade.txt"));while(true){ss=bf.readLine();if(ss==null)break;ss=bf.readLine();ss=bf.readLine();intgrade=Integer.parseInt(ss);total+=grade;num+=1;if(grade>max)max=grade;if(grade}System.out.println("学生成绩中最高为:"+max+",最低为:"+min+",平均分为:"+total*1.0/num);bf.close();}catch(FileNotFoundExceptione){System.out.println(e.toString());}catch(IOExceptione1){System.out.println(e1.toString());}}}习题十一6、importjava.awt.*;importjava.awt.event.*;publicclassChangeColorextendsFrame{privateButtonred=newButton("红");privateButtongreen=newButton("绿");privateButtonblue=newButton("蓝");privateTextFieldtext=newTextField();publicChangeColor(){super("改变颜色");this.setLayout(null);text.setBackground(Color.WHITE);red.setBounds(25,50,50,20);this.add(red);green.setBounds(125,50,50,20);this.add(green);blue.setBounds(225,50,50,20);this.add(blue);text.setBounds(25,100,250,30);this.add(text);red.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.RED);}});green.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.GREEN);}});blue.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.BLUE);}});addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setSize(300,200);setVisible(true);}publicstaticvoidmain(String[]args){ChangeColorcolor=newChangeColor();}}习题十二5、importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassGoodsextendsJFrame{privateJComboBoxlist;privateJTextAreainfo;privateStringnames[]={"请选择你要查询的商品","A商品","B商品","C商品","D商品","E商品","F商品"};privateStringgoods[][]={{"","",""},{"A商品","北京","¥300"},{"B商品","上海","¥400"},{"C商品","广州","¥500"},{"D商品","长沙","¥600"},{"E商品","武汉","¥700"},{"F商品","天津","¥800"}};publicGoods(){super("商品信息");Containerpane=this.getContentPane();pane.setLayout(newBorderLayout());list=newJComboBox(names);info=newJTextArea(5,20);pane.add(list,BorderLayout.NORTH);pane.add(info,BorderLayout.CENTER);list.addItemListener(newItemListener(){publicvoiditemStateChanged(ItemEvente){intindex=list.getSelectedIndex();info.setText("商品名:"+goods[index][0]+"\n");info.append("产地:"+goods[index][1]+"\n");info.append("价格:"+goods[index][2]+"\n");}});this.setSize(250,300);this.setVisible(true);}publicstaticvoidmain(String[]args){Goodsccc=newGoods();ccc.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}} 习题十三4、importjava.awt.*;importjavax.swing.JApplet;publicclassSimpleAppletextendsJAppletimplementsRunnable{intj=0;Threadthread;publicvoidinit(){thread=newThread(this);thread.start();}publicvoidstop(){thread.destroy();thread=null;}publicvoidrun(){while(true){repaint();j++;j=j%3;try{thread.sleep(4000);}catch(InterruptedExceptione){}}}publicvoidpaint(Graphicsg){while(true){g.setFont(newFont("黑体",Font.BOLD,24));switch(j){case0:g.setColor(Color.RED);break;case1:g.setColor(Color.BLUE);break;case2:g.setColor(Color.GREEN);}g.drawString("各位同学大家好,你们辛苦啦!",50,60);}}}习题十四4、publicclassUseThreadextendsThread{intst,en,count=0;UseThread(intm,intn){this.st=
a[i][j]=a[i-1][j-1]+a[i-1][j];
for(i=0;i{//for(j=0;j<=max-i;j++)System.out.print("");for(j=0;j<=a[i].length-1;j++)System.out.print(a[i][j]+"");System.out.println();}}} 5.//不能用t=a[i][j];a[i][j]=a[j][i];a[j][i]=t来实现,此法只适合方阵习题六9、publicclassVehicle{Stringcolor,kind;intspeed;Vehicle(){color=”Red”;kind=”卡车”;speed=0;}publicvoidsetColor(Stringcolor1){color=color1;}publicvoidsetSpeed(Stringspeed1){speed=speed1;}publicvoidsetKind(Stringkind1){kind=kind1;}publicStringgetColor(){returncolor;}publicStringgetKind(){returnkind;}publicintgetSpeed(){returnspeed;}publicstaticvoidmain(String[]args){Vehicleche=newVehicle();Che.setColor(“Blue”);Che.setSpeed(150);Che.setKind(“跑车”);System.out.println(“有一辆”+che.getColor()+”的”+che.getKind()+”行驶在高速公路上”);System.out.println(“时速”+che.getSpeed()+”km/h”);}}习题七7、publicclassVehicle{Stringcolor,kind;intspeed;Vehicle(){color=””;kind=””;speed=0;}publicvoidsetColor(Stringcolor1){color=color1;}publicvoidsetSpeed(Stringspeed1){speed=speed1;}publicvoidsetKind(Stringkind1){kind=kind1;}publicStringgetColor(){returncolor;}publicStringgetKind(){returnkind;}publicintgetSpeed(){returnspeed;}}publicclassCarextendsVehicle{intpassenger;publicCar(){super();passenger=0;}publicvoidsetPassenger(intpassenger){this.passenger=passenger;}publicintgetPassenger(){returnpassenger;}publicstaticvoidmain(String[]args){Carbenz=newCar();benz.setColor(“Yellow”);benz.setKind(“roadster”);benz.setSpeed(120);benz.setPassenger(4);System.out.println(“benz:“);System.out.println(“Color“+benz.getColor());System.out.print(“Speed(km/h)“);System.out.println(benz.getSpeed());System.out.println(“Kind:“+benz.getKind());System.out.print(“Passenger:“);System.out.println(benz.getPassenger());}}习题九4、importjava.io.*;publicclassUseException{publicstaticvoidmain(String[]args){System.out.println("请输入一个整数字符串");try{BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));inta=Integer.parseInt(in.readLine());System.out.println("您输入的整数是:"+a);}catch(IOExceptione){System.out.println("IO错误");}catch(NumberFormatExceptione1){System.out.println("您输入的不是一个整数字符串");}}}习题十7、importjava.io.*;publicclassSaveName{publicstaticvoidmain(String[]args){try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbw=newBufferedWriter(newFileWriter("name.txt"));Strings;while(true){System.out.println("请输入姓名:");s=br.readLine();if(s.length()==0)break;bw.write(s);bw.newLine();}br.close();bw.close();}catch(FileNotFoundExceptione){System.out.println(e.toString());}catch(IOExceptione1){System.out.println(e1.toString());}}}8、importjava.io.*;publicclassSaveGrade{publicstaticvoidmain(String[]args){try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbw=newBufferedWriter(newFileWriter("grade.txt"));Strings,ss;while(true){System.out.println("请输入姓名:");s=br.readLine();if(s.length()==0)break;bw.write(s);bw.newLine();System.out.println("请输入学号:");s=br.readLine();bw.write(s);bw.newLine();System.out.println("请输入成绩:");s=br.readLine();bw.write(s);bw.newLine();}br.close();bw.close();intmax=0,min=100,total=0,num=0;BufferedReaderbf=newBufferedReader(newFileReader("grade.txt"));while(true){ss=bf.readLine();if(ss==null)break;ss=bf.readLine();ss=bf.readLine();intgrade=Integer.parseInt(ss);total+=grade;num+=1;if(grade>max)max=grade;if(grade}System.out.println("学生成绩中最高为:"+max+",最低为:"+min+",平均分为:"+total*1.0/num);bf.close();}catch(FileNotFoundExceptione){System.out.println(e.toString());}catch(IOExceptione1){System.out.println(e1.toString());}}}习题十一6、importjava.awt.*;importjava.awt.event.*;publicclassChangeColorextendsFrame{privateButtonred=newButton("红");privateButtongreen=newButton("绿");privateButtonblue=newButton("蓝");privateTextFieldtext=newTextField();publicChangeColor(){super("改变颜色");this.setLayout(null);text.setBackground(Color.WHITE);red.setBounds(25,50,50,20);this.add(red);green.setBounds(125,50,50,20);this.add(green);blue.setBounds(225,50,50,20);this.add(blue);text.setBounds(25,100,250,30);this.add(text);red.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.RED);}});green.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.GREEN);}});blue.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.BLUE);}});addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setSize(300,200);setVisible(true);}publicstaticvoidmain(String[]args){ChangeColorcolor=newChangeColor();}}习题十二5、importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassGoodsextendsJFrame{privateJComboBoxlist;privateJTextAreainfo;privateStringnames[]={"请选择你要查询的商品","A商品","B商品","C商品","D商品","E商品","F商品"};privateStringgoods[][]={{"","",""},{"A商品","北京","¥300"},{"B商品","上海","¥400"},{"C商品","广州","¥500"},{"D商品","长沙","¥600"},{"E商品","武汉","¥700"},{"F商品","天津","¥800"}};publicGoods(){super("商品信息");Containerpane=this.getContentPane();pane.setLayout(newBorderLayout());list=newJComboBox(names);info=newJTextArea(5,20);pane.add(list,BorderLayout.NORTH);pane.add(info,BorderLayout.CENTER);list.addItemListener(newItemListener(){publicvoiditemStateChanged(ItemEvente){intindex=list.getSelectedIndex();info.setText("商品名:"+goods[index][0]+"\n");info.append("产地:"+goods[index][1]+"\n");info.append("价格:"+goods[index][2]+"\n");}});this.setSize(250,300);this.setVisible(true);}publicstaticvoidmain(String[]args){Goodsccc=newGoods();ccc.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}} 习题十三4、importjava.awt.*;importjavax.swing.JApplet;publicclassSimpleAppletextendsJAppletimplementsRunnable{intj=0;Threadthread;publicvoidinit(){thread=newThread(this);thread.start();}publicvoidstop(){thread.destroy();thread=null;}publicvoidrun(){while(true){repaint();j++;j=j%3;try{thread.sleep(4000);}catch(InterruptedExceptione){}}}publicvoidpaint(Graphicsg){while(true){g.setFont(newFont("黑体",Font.BOLD,24));switch(j){case0:g.setColor(Color.RED);break;case1:g.setColor(Color.BLUE);break;case2:g.setColor(Color.GREEN);}g.drawString("各位同学大家好,你们辛苦啦!",50,60);}}}习题十四4、publicclassUseThreadextendsThread{intst,en,count=0;UseThread(intm,intn){this.st=
//for(j=0;j<=max-i;j++)System.out.print("");
for(j=0;j<=a[i].length-1;j++)System.out.print(a[i][j]+"");
System.out.println();
5.//不能用t=a[i][j];a[i][j]=a[j][i];a[j][i]=t来实现,此法只适合方阵
习题六
publicclassVehicle
{
Stringcolor,kind;
intspeed;
Vehicle()
color=”Red”;
kind=”卡车”;
speed=0;
publicvoidsetColor(Stringcolor1)
color=color1;
publicvoidsetSpeed(Stringspeed1)
speed=speed1;
publicvoidsetKind(Stringkind1)
kind=kind1;
publicStringgetColor()
returncolor;
publicStringgetKind()
returnkind;
publicintgetSpeed()
returnspeed;
Vehicleche=newVehicle();
Che.setColor(“Blue”);
Che.setSpeed(150);
Che.setKind(“跑车”);
System.out.println(“有一辆”+che.getColor()+”的”+che.getKind()+”行驶在高速公路上”);
System.out.println(“时速”+che.getSpeed()+”km/h”);
习题七
7、
color=””;
kind=””;
publicclassCarextendsVehicle
intpassenger;
publicCar()
super();
passenger=0;
publicvoidsetPassenger(intpassenger)
this.passenger=passenger;
publicintgetPassenger()
returnpassenger;
Carbenz=newCar();
benz.setColor(“Yellow”);
benz.setKind(“roadster”);
benz.setSpeed(120);
benz.setPassenger(4);
System.out.println(“benz:
“);
System.out.println(“Color“+benz.getColor());
System.out.print(“Speed(km/h)“);
System.out.println(benz.getSpeed());
System.out.println(“Kind:
“+benz.getKind());
System.out.print(“Passenger:
System.out.println(benz.getPassenger());
习题九
4、
publicclassUseException
System.out.println("请输入一个整数字符串");
try
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
inta=Integer.parseInt(in.readLine());
System.out.println("您输入的整数是:
"+a);
catch(IOExceptione)
System.out.println("IO错误");
catch(NumberFormatExceptione1)
System.out.println("您输入的不是一个整数字符串");
习题十
publicclassSaveName
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
BufferedWriterbw=newBufferedWriter(newFileWriter("name.txt"));
Strings;
while(true)
System.out.println("请输入姓名:
s=br.readLine();
if(s.length()==0)
break;
bw.write(s);
bw.newLine();
br.close();
bw.close();
catch(FileNotFoundExceptione)
System.out.println(e.toString());
catch(IOExceptione1)
System.out.println(e1.toString());
publicclassSaveGrade
BufferedWriterbw=newBufferedWriter(newFileWriter("grade.txt"));
Strings,ss;
System.out.println("请输入学号:
System.out.println("请输入成绩:
intmax=0,min=100,total=0,num=0;
BufferedReaderbf=newBufferedReader(newFileReader("grade.txt"));
ss=bf.readLine();
if(ss==null)
intgrade=Integer.parseInt(ss);
total+=grade;
num+=1;
if(grade>max)max=grade;
if(grade}System.out.println("学生成绩中最高为:"+max+",最低为:"+min+",平均分为:"+total*1.0/num);bf.close();}catch(FileNotFoundExceptione){System.out.println(e.toString());}catch(IOExceptione1){System.out.println(e1.toString());}}}习题十一6、importjava.awt.*;importjava.awt.event.*;publicclassChangeColorextendsFrame{privateButtonred=newButton("红");privateButtongreen=newButton("绿");privateButtonblue=newButton("蓝");privateTextFieldtext=newTextField();publicChangeColor(){super("改变颜色");this.setLayout(null);text.setBackground(Color.WHITE);red.setBounds(25,50,50,20);this.add(red);green.setBounds(125,50,50,20);this.add(green);blue.setBounds(225,50,50,20);this.add(blue);text.setBounds(25,100,250,30);this.add(text);red.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.RED);}});green.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.GREEN);}});blue.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.setBackground(Color.BLUE);}});addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setSize(300,200);setVisible(true);}publicstaticvoidmain(String[]args){ChangeColorcolor=newChangeColor();}}习题十二5、importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassGoodsextendsJFrame{privateJComboBoxlist;privateJTextAreainfo;privateStringnames[]={"请选择你要查询的商品","A商品","B商品","C商品","D商品","E商品","F商品"};privateStringgoods[][]={{"","",""},{"A商品","北京","¥300"},{"B商品","上海","¥400"},{"C商品","广州","¥500"},{"D商品","长沙","¥600"},{"E商品","武汉","¥700"},{"F商品","天津","¥800"}};publicGoods(){super("商品信息");Containerpane=this.getContentPane();pane.setLayout(newBorderLayout());list=newJComboBox(names);info=newJTextArea(5,20);pane.add(list,BorderLayout.NORTH);pane.add(info,BorderLayout.CENTER);list.addItemListener(newItemListener(){publicvoiditemStateChanged(ItemEvente){intindex=list.getSelectedIndex();info.setText("商品名:"+goods[index][0]+"\n");info.append("产地:"+goods[index][1]+"\n");info.append("价格:"+goods[index][2]+"\n");}});this.setSize(250,300);this.setVisible(true);}publicstaticvoidmain(String[]args){Goodsccc=newGoods();ccc.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}} 习题十三4、importjava.awt.*;importjavax.swing.JApplet;publicclassSimpleAppletextendsJAppletimplementsRunnable{intj=0;Threadthread;publicvoidinit(){thread=newThread(this);thread.start();}publicvoidstop(){thread.destroy();thread=null;}publicvoidrun(){while(true){repaint();j++;j=j%3;try{thread.sleep(4000);}catch(InterruptedExceptione){}}}publicvoidpaint(Graphicsg){while(true){g.setFont(newFont("黑体",Font.BOLD,24));switch(j){case0:g.setColor(Color.RED);break;case1:g.setColor(Color.BLUE);break;case2:g.setColor(Color.GREEN);}g.drawString("各位同学大家好,你们辛苦啦!",50,60);}}}习题十四4、publicclassUseThreadextendsThread{intst,en,count=0;UseThread(intm,intn){this.st=
System.out.println("学生成绩中最高为:
"+max+",最低为:
"+min+",平均分为:
"+total*1.0/num);
bf.close();
习题十一
6、
importjava.awt.*;
importjava.awt.event.*;
publicclassChangeColorextendsFrame
privateButtonred=newButton("红");
privateButtongreen=newButton("绿");
privateButtonblue=newButton("蓝");
privateTextFieldtext=newTextField();
publicChangeColor()
super("改变颜色");
this.setLayout(null);
text.setBackground(Color.WHITE);
red.setBounds(25,50,50,20);
this.add(red);
green.setBounds(125,50,50,20);
this.add(green);
blue.setBounds(225,50,50,20);
this.add(blue);
text.setBounds(25,100,250,30);
this.add(text);
red.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente)
text.setBackground(Color.RED);
});
green.addActionListener(newActionListener(){
text.setBackground(Color.GREEN);
blue.addActionListener(newActionListener(){
text.setBackground(Color.BLUE);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente)
System.exit(0);
setSize(300,200);
setVisible(true);
ChangeColorcolor=newChangeColor();
习题十二
importjavax.swing.*;
publicclassGoodsextendsJFrame
privateJComboBoxlist;
privateJTextAreainfo;
privateStringnames[]={
"请选择你要查询的商品",
"A商品",
"B商品",
"C商品",
"D商品",
"E商品",
"F商品"
};
privateStringgoods[][]={
{"","",""},
{"A商品","北京","¥300"},
{"B商品","上海","¥400"},
{"C商品","广州","¥500"},
{"D商品","长沙","¥600"},
{"E商品","武汉","¥700"},
{"F商品","天津","¥800"}
publicGoods()
super("商品信息");
Containerpane=this.getContentPane();
pane.setLayout(newBorderLayout());
list=newJComboBox(names);
info=newJTextArea(5,20);
pane.add(list,BorderLayout.NORTH);
pane.add(info,BorderLayout.CENTER);
list.addItemListener(newItemListener(){
publicvoiditemStateChanged(ItemEvente)
intindex=list.getSelectedIndex();
info.setText("商品名:
"+goods[index][0]+"\n");
info.append("产地:
"+goods[index][1]+"\n");
info.append("价格:
"+goods[index][2]+"\n");
this.setSize(250,300);
this.setVisible(true);
Goodsccc=newGoods();
ccc.addWindowListener(newWindowAdapter(){
习题十三
importjavax.swing.JApplet;
publicclassSimpleAppletextendsJAppletimplementsRunnable{
intj=0;
Threadthread;
publicvoidinit()
thread=newThread(this);
thread.start();
publicvoidstop()
thread.destroy();
thread=null;
publicvoidrun()
repaint();
j=j%3;
thread.sleep(4000);
catch(InterruptedExceptione){}
publicvoidpaint(Graphicsg)
g.setFont(newFont("黑体",Font.BOLD,24));
switch(j){
case0:
g.setColor(Color.RED);
case1:
g.setColor(Color.BLUE);
case2:
g.setColor(Color.GREEN);
g.drawString("各位同学大家好,你们辛苦啦!
",50,60);
习题十四
publicclassUseThreadextendsThread
intst,en,count=0;
UseThread(intm,intn)
this.st=
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1