




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、一、编程求出自然数101205中的所有素数。 1、 算法设计 2、 流程图 3、 程序清单 public class LianXi1 public static void main(String args System.out.println(101205内的素数为:; int k=0; loop: for (int i = 101; i 205; i+ for (int j = 2; j i; j+ if (i % j = 0 continue loop; k+; if(k/5=k/5.0 System.out.println( + i; else System.out.print( + i
2、; 4、 调试结果 5、 结果的分析 二、百鸡百钱问题。公鸡每只3元,母鸡每只5元,小鸡3只1元,用100元钱买100只鸡,公鸡、母鸡、小鸡应各买多少? 1、算法设计 2、流程图 3、程序清单 public class SheJi2 public static void main(String a for (int i = 0; i = 33; i+ / 公鸡 for (int j = 0; j = 20; j+ / 母鸡 int k = 100 - i - j;/ 小鸡 if (3 * i + 5 * j + k / 3 = 100 & k % 3 = 0 System.out.printl
3、n(百鸡百钱问题: 公鸡 + i + 母鸡 + j + 小鸡 + k; 4、调试结果 5、结果的分析 三、编程输出如下图所示的图形。 * * * * * * * * * * * * * * * * * * * * * * * * * 1、算法设计 2、流程图 3、程序清单 import java.io.*; public class SheJi3 public static void main(String args char s1 = new char610; for(int i = 1;i 6;i+ for(int j = 1;j 10;j+ if(j - i 5 - i s1ij = *
4、; for(int i = 1;i 6;i+ for(int j = 1;j 10;j+ if(j = 9 System.out.println( +s1ij; else System.out.print( +s1ij; 4、调试结果 5、结果的分析 四、定义一个复数类complex,内有两个成员变量:实部和虚部,还有两个方法:求两个复数和以及求两个复数差。编程验证该程序的复数加减运算是正确的。 1、 算法设计 2、 流程图 3、 程序清单 import java.io.*; public class Complex private int shibu; private int xubu; C
5、omplex( Complex(int m,int n shibu =m; xubu = n; public Complex plus(Complex c Complex result = new Complex(; result.shibu = this.shibu + c.shibu; result.xubu = this.xubu + c.xubu; return result; public Complex minus(Complex c Complex result = new Complex(; result.shibu = this.shibu - c.shibu; result
6、.xubu = this.xubu - c.xubu; return result; public String output( if(this.xubu 0 return this.shibu+this.xubu+i; else return this.shibu+ ; public static void main(String args int m = Integer.valueOf(Value(; int n = Integer.valueOf(Value(; int a = Integ er.valueOf(Value(; int
7、 b = Integer.valueOf(Value(; Complex com1 = new Complex(m,n; Complex com2 = new Complex(a,b; System.out.print(两个复数之和:; System.out.println(com1.plus(com2.output(; System.out.print(两个复数之差:; System.out.println(com1.minus(com2.output(; 4、 调试结果 5、 结果的分析 五、编写Applet程序,在指定大小的屏幕界面上使用不同颜色绘制如下图形: (1)直
8、径为100像素点的圆; (2)两个半轴长分别为50像素点和100像素点的椭圆; (3)长和宽都为150像素点的矩形; (4)使用红色填充(3)中的矩形; (5)大小由读者指定的填充圆弧。 1、算法设计 2、流程图 3、程序清单 import java.applet.Applet; import java.awt.*; public class SheJi5 extends Applet Color color = Color.yellow,Color.blue,Color.pink,Color.red,Color.green; public void paint(Graphics g int
9、x = 100; g.setColor(color0; g.drawOval(40,20,100,100; g.setColor(color1; g.drawOval(150,30,100,50; g.setColor(color2; g.drawRect(10,120,150,150; g.setColor(color3; g.fillRect(10,120,150,150; g.setColor(color4; g.fillArc(150,120,100,100,0,180; 4、 调试结果 5、 结果的分析 六、编写Applet程序,选择3种不同字体写出字符串“I LOVE BEIJIN
10、G!”。 1、算法设计 2、流程图 3、程序清单 import java.applet.Applet; import java.awt.*; public class SheJi6 extends Applet Font fnt1=new Font(TimesRoman,Font.ITALIC,25; Font fnt2=new Font(Braggadcoio,Font.BOLD,40; Font fnt3=new Font(Monotype Corsiva,Font.BOLD,20; public void paint(Graphics g String str=new String(I
11、LOVE BEIJING! ; g.setFont(fnt1; g.drawString(str,30,40; g.setFont(fnt2; g.drawString(str,50,80; g.setFont(fnt3; g.drawString(str,0,80; 4、调试结果 5、结果的分析 七、使用Java Application编写满足下列要求的程序: (1) 使用GridLayout布局; (2)设计一个简单的计算器。包括数字按钮09,以及加、减、乘、除、清零、等于运算按钮和一个显示计算结果的文本区。 1、 算法设计 2、 流程图 3、 程序清单 import java.apple
12、t.*; import java.awt.*; import java.awt.event.*; public class JiSuanQi extends Applet implements ActionListener Button buts; String names = 0,1,2,3,4,5,6,7,8,9,+,-,*,/,=,Zhiling; TextArea text; public void init( text = new TextArea( ,20,2; add(text; setLayout(new GridLayout(9,4,5,5; buts = new Butto
13、nnames.length; for(int i = 0;i names.length;i+ butsi = new Button(namesi; butsi.addActionListener( this; add(butsi; static int y = 0; static float j = 0; static int z = 0; public void actionPerformed(ActionEvent e for(int i = 0;i 10;i+ if(e.getSource( = butsi char x; x = (char(i + 0; text.append(+x;
14、 y = y * 10 + i; if(e.getSource( = buts10 int i = y; y = 0; j = j + i; z = 1; text.append( + ; if(e.getSource( = buts11 int i = y; y = 0; if(j = 0 j = j + i; else j = j - i; z = 2; text.append( - ; if(e.getSource( = buts12 int i = y; y = 0; if(j = 0 j = j + i; else j = j * i; z = 3; text.append( * ;
15、 if(e.getSource( = buts13 int i = y; y = 0; if(j = 0 j = j + i; else j = j / i; z = 4; text.append( / ; if(e.getSource( = buts14 if(z = 1 j = j + y; else if(z = 2 j = j - y; else if(z = 3 j = j * y; else j = j / y; text.append( = +j; y = 0; if(e.getSource( = buts15 text.replaceRange( ,0,50; y = 0; j
16、 = 0; repaint(; 4、 调试结果 5、 结果的分析 八、编写一个图形用户界面的Application程序: (1)有一个菜单条; (2)菜单条中有3个菜单; (3)每个菜单有2个菜单项,其中一个菜单项又具有2个菜单子项。 1、算法设计 2、流程图 3、程序清单 import java.awt.*; import java.awt.event.*; public class SheJi8 extends Frame implements ActionListener,ItemListener public SheJi8( super(我的窗口; setSize(450,250; p
17、ublic void init( MenuBar myB = new MenuBar(; setMenuBar(myB; Menu m1 = new Menu(文件; m1.add(新建; Menu m12 = new Menu(打开; m12.add(Word; m12.add(Ppt2; m12.addActionListener(this; m1.addActionListener(this; m1.add(m12; myB.add(m1; Menu m2 = new Menu(编辑; m2.add(撤销; Menu m21 = new Menu(剪切; m21.add(Ppt3; m2
18、1.add(Ppt4; m21.addActionListener(this; m2.addActionListener(this; m2.add(m21; myB.add(m2; Menu m3 = new Menu(格式; m3.add(结束; Menu m31 = new Menu(字体; m31.add(大小; m31.add(颜色; m31.addActionListener(this; m3.addActionListener(this; m3.add(m31; myB.add(m3; public static void main(String args SheJi8 myMen
19、u = new SheJi8(; myMenu.init(; myMenu.setVisible(true; public void itemStateChanged(ItemEvent e public void actionPerformed(ActionEvent e if(e.getActionCommand( = 结束 System.exit(0; 4、调试结果 5、结果的分析 九、定义一个circle类,其中有求面积的方法,当圆的半径小于0时,抛出一个自定义的异常。编程实现之。 1、算法设计 2、流程图 3、程序清单 class MyException extends Except
20、io n private int num; MyException(int x num = x; public String toString( return MyException:+num; import java.io.*; public class Circle private int radius; static double area(int r throws MyException System.out.println(调用方法:area(+r+; double Area; final double Pi; Pi = 3.1415927; if(r 0 MyException e
21、 = new MyException(r; throw e; Area = Pi * r * r; return Area; public static void main(String args int i = Integer.valueOf(Value(; try double area1 = area(i; System.out.println(t圆面积=+area1+n; catch(MyException e System.out.println(t捕捉异常:+e.toString(+n; 4、调试结果 5、结果的分析 十、分析程序的输出结果题。 7-1程序实例1
22、public class Daemon extends Thread public static void main (String args Thread thread= new Daemon(; System.out.println (The Priority of Current Thread is + thread.getPriority(; thread.start(; for (int i=1;i=5;i+ System.out.println(Main Thread: +i; sleep(2000; System.out.println(Main Thread: done; pu
23、blic void run( String name=null; for(int i=1;i=10;i+ if(isDaemon( name=Daemon Thread:; else name=User Thread:; System.out.println(name +i; sleep(2000; System.out.println(name +done; public static void sleep (int msec try Thread.currentThread(.sleep(msec; catch (InterruptedException e System.out.println(e; 调试结果 7-2程序实例(注:该程序用到了java.util包中的两个类:Timer类和TimerTask类,TimerTask类实现了Runnable接口) import java.util.*; import java.io.*; public class TestTimers public static void doMain( throws Exception Timer t = newTimer(true; t.schedule(new Ping(Fixed delay,0,1000; Thread.currentTh
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 废气排放数据补充及环境治理合作合同
- 投资风险管理与财务决策试题及答案
- 2025知识产权许可合同范文 合同范本
- 商业用电合同协议书
- 工程法规考试复习过程中的团队合作与模型应用试题及答案
- 商品联合采购协议书
- 社会学视角下的年龄问题试题及答案
- 单晶供应合作协议书
- 分包合同安全协议书
- 合伙生意平分协议书
- 机关食堂整体服务方案范文
- 广东省深圳市2024年高一下学期期末调研考试英语试题含解析
- 中国茶文化与茶健康 知到智慧树网课答案
- 红色知识竞赛题库小学生
- 驾校安全生产应急演练方案
- 2024年宁波市奉化区农商发展集团有限公司招聘笔试参考题库附带答案详解
- 2024年小学语文教师招聘考试语文专业知识考试模拟试题及答案(共四套)
- 应急管理与突发事故处理
- 螺杆泵工作原理课件
- 中医护理方案实施难点与优化课件
- 新建铝厂可行性方案
评论
0/150
提交评论