1、3. 编写应用程序,求 1!+2!+10!。答:class Factpublic static void main(String args)int fact,sum=0;for(int i=1;i1000)MyException exception=new MyException(m);throw exception;else System.out.println(m);public class Test public static void main(String agrs)int m;Student stu1=new Student();m=987;trystu1.speak(m);m=1234;stu1.speak(m);catch(MyException e) e.showStr1(); 18.编写一个类,该类有一个方法 public int f(int a,int b),该方法返回 a和 b的最大公约数。然后再编写一个该类的子类,要求子类重写方法 f,而且重写的方法将返回 a和 b的最小公倍数。要求在重写的方法的方法体中首先调用被隐藏的方法返回 a和 b的最大公约数 m,然后
2、将乘积(a*b)/m 返回。要求在应用程序的主类中分别使用父类和子类创建对象,并分别调用方法 f计算两个正整数的最大公约数和最小公倍数。答: class Apublic int f(int a,int b)if(a0)String str1=text1.getText()+n+text2.getText()+n+text3.getText()+n;String str2=textarea.getText();textarea.setText(str2+str1);else text2.setText(输入了非法格式的 E-mail地址);public class Testpublic static void main(String args) new WindowBox();8. 写一个应用程序,要求编写一个 Panel的子类MyPanel,MyPanel 中有一个文本框和一个按钮,要求MyPanel的实例作为其按钮的 ActionEvent事件的监视器,当单击按钮时,程序获取文本框中的文本,并将该文本作为按钮的名称。然后在编写一个 Frame的子类,即窗口。窗口的布局为 Border
3、Layout布局。窗口中添加两个 MyPanel面板,分别添加到窗口的东部区域和西部区域。答: import java.awt.*;import java.awt.event.*;class MyPanel extends Panel implements ActionListenerString name;TextField text;Button button;MyPanel()text=new TextField(10);button=new Button(确定);add(text);add(button);button.addActionListener(this);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););public void actionPerformed(ActionEvent e)if(e.getSource()=button)name=text.getText();button.setLabel(name);class M
4、yFrame extends Frame MyPanel panel1,panel2;MyFrame()panel1=new MyPanel();panel2=new MyPanel();add(panel1,BorderLayout.EAST);add(panel2,BorderLayout.WEST);setBounds(100,100,400,100);setVisible(true);validate();public class Testpublic static void main(String args)MyFrame win=new MyFrame();9. 参照例子 7.18编写一个应用程序,要求有一个画布,在画布上绘制一个矩形,用户通过文本框输入矩形的宽和高以及矩形左上角的位置坐标。答: import java.awt.*;import java.awt.event.*;class Mycanvas extends Canvasint x,y,w,h;Mycanvas()setBackground(Color.cyan);public void setX(int x
5、)this.x=x;public void setY(int y)this.y=y;public void setW(int w) this.w=w;public void setH(int h) this.h=h;public void paint(Graphics g)g.drawRect(x,y,w,h);class WindowCanvas extends Frame implements ActionListenerMycanvas canvas;TextField text1,text2,text3,text4;Button button;WindowCanvas() canvas=new Mycanvas();text1=new TextField(4);text2=new TextField(4);text3=new TextField(5);text4=new TextField(5);Panel pNorth=new Panel(),pSouth=new Panel();button=new Button(确定);button.addActionListener(t
6、his);pNorth.add(new Label(矩形的宽: );pNorth.add(text3);pNorth.add(new Label(矩形的高: );pNorth.add(text4);pSouth.add(new Label(左上角位置坐标:);pSouth.add(text1);pSouth.add(text2);pSouth.add(button);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););add(canvas,BorderLayout.CENTER);add(pNorth,BorderLayout.NORTH);add(pSouth,BorderLayout.SOUTH);setBounds(100,100,500,500);setVisible(true);validate();public void actionPerformed(ActionEvent e)int x,y,w,h;tryx=Integer.parse
7、Int(text1.getText();y=Integer.parseInt(text2.getText();w=Integer.parseInt(text3.getText();h=Integer.parseInt(text4.getText();canvas.setX(x);canvas.setY(y);canvas.setW(w);canvas.setH(h);canvas.repaint();catch(NumberFormatException ee)x=0;y=0;w=0;h=0;public class Test public static void main(String args) new WindowCanvas();10.编写应用程序,有一个窗口对象,该窗口取它的默认布局: BorderLayout布局,北面添加一个 List组件,该组件有四个商品名称的选项。中心添加一个文本区,当选择 List组件中的某个选项后,文本区显示对该商品的价格和产地:当双击 List组件中的某个选项后,文本区显示该商品的详细广告。答: import java.awt.*;import
8、java.awt.event.*;class WindowGoods extends Frame implements ActionListener,ItemListenerString s=产地:北京,产地:上海,产地:沈阳,产地:广东;String p=价格:3200,价格:158,价格:13.2,价格:320/打;String a=本商品*,本商品*,本商品*,本商品*;List list;TextArea text;WindowGoods()list=new List(3,false);text=new TextArea(6,20);text.setEditable(false);list.add(商品 1);list.add(商品 2);list.add(商品 3);list.add(商品 4);add(list,BorderLayout.NORTH);add(text,BorderLayout.CENTER);list.addItemListener(this);list.addActionListener(this);addWindowListener(new Windo
9、wAdapter()public void windowClosing(WindowEvent e)System.exit(0););setBounds(100,100,300,300);setVisible(true);validate();public void itemStateChanged(ItemEvent e)if(e.getItemSelectable()=list)int m=list.getSelectedIndex();text.setText(pm+n+sm);public void actionPerformed(ActionEvent e)int n=list.getSelectedIndex();text.setText(an);public class Testpublic static void main(String args)new WindowGoods();11.编写程序,观察各种组件设置背景色和前景色的情况。答: import java.awt.*;import java.awt.event.*;class WindowColor extends Frame implements ActionListenerButton button; /按钮TextField textfield; /文本框TextArea textarea; /文本区Mypanel panel; /面板Checkbox box; /选择框Choice choice; /下拉列表List list; /滚动列表Label label; /标签Mycanvas can; /画布Button buttonBac
《java2实用教程课后习题答案(第三版编程题)》由会员第***分享,可在线阅读,更多相关《java2实用教程课后习题答案(第三版编程题)》请在金锄头文库上搜索。