




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第二章作业4、角谷猜想:任何一个正整数n,如果它是偶数,则对它除以2,如果是奇数,则对它乘3再加1,这样得到一个新的正整数,如此继续进行上述处理,最终都能够得到1 。编写应用程序和小程序分别证明:在310000之间的所有正整数都符合上述规则。public class JiaoGu public static void main(String args) int i,j; for(j=3;j=10000;j+)i=j; do if(i%2=0)i=i/2; else i=i*3+1; while(i!=1); System.out.println(证明完毕!); import java.awt.Graphics; import java.applet.Applet; public class JiaoGu extends Applet public void paint(Graphics g) int i,j; for(j=3;j=10000;j+)i=j; do if(i%2=0)i=i/2; else i=i*3+1; while(i!=1); g.drawString(证明完毕!,100,100); public class JiaoGu public static void main(String args) int i,j; for(j=3;j=10000;j+) i=j; do if(i%2=0)i=i/2; else if(i(2147483647-1)/3)i=i*3+1;else System.out.println(变量溢出!); System.exit(0); while(i!=1); System.out.println(证明完毕!); 6、编写一个模拟掷色子的程序: public class Shaizi public static void main(String args) int a,b,i,k=0; for(i=0;i3600;i+) a=(int)(Math.random()*6)+1; b=(int)(Math.random()*6)+1; if(a+b=7)k+; System.out.println(+k); 第三章作业1、编程:编写一个applet,要求输入一个任意长度的整数(long型变量所允许的范围内),将这个数分成独立的数字并分开显示。 import java.awt.*; import java.applet.Applet; public class ShuZi extends AppletLabel prompt;TextField input; long n=0;public void init( ) prompt=new Label(输入一个任意整数);input=new TextField(10);add(prompt);add(input);public void paint(Graphics g) long a; int x=0; do a=n%10; n=n/10; g.drawString(+a, 200-x*10, 100); x+; while(n!=0); public boolean action(Event e , Object o) n=Long.parseLong(o.toString( ) ; repaint( );return true;2、编程:计算010之间各个整数的平方值和立方值。 public class ChengFang public static void main(String args) int i; System.out.printf(整数t平方t立方n);for(i=0;i=10;i+) System.out.printf(%dt%dt%dn,i,i*i,i*i*i); 4、编程:编写一个applet,读取一个矩形的边长,然后输出一个空心矩形。 import java.awt.*; import java.applet.Applet; public class SayHello extends Applet Label prompt;TextField input; int n=0; public void init( ) prompt=new Label(输入边长);input=new TextField(5);add(prompt);add(input);public void paint(Graphics g) int i;for(i=0;in;i+)g.drawString(*, 20+i*10, 80);for(i=0;in;i+)g.drawString(*, 20, 80+i*10);for(i=0;in;i+)g.drawString(*, 20+i*10, 80+(n-1)*10);for(i=0;in;i+)g.drawString(*, 20+(n-1)*10, 80+i*10); public boolean action(Event e , Object o) n=Integer.parseInt(input.getText() ; repaint( );return true; 第四章作业1.class A int x; A(int x) this.x=x; int show() int m,n,s=0; m=x; while(m!=0) n=m%10; m=m/10; s=s*10+n; return s; import java.applet.Applet;import java.awt.*;public class TestA extends Applet Label prompt; TextField input; public void init() prompt = new Label(“请输入一个整数: ); input = new TextField(6); add(prompt); add(input); public void paint(Graphics g) int x; x= Integer.valueOf(input.getText().intValue(); A a=new A(x); g.drawString(+a.show(),60,60); public boolean action (Event e,Object o) repaint();return true;2.class Circle float radius; Circle(float r) radius=r; double area()return Math.PI*radius*radius; double length()return 2*Math.PI*radius;import java.applet.Applet;import java.awt.*;public class CircleArea extends Applet Label prompt; TextField input; public void init() prompt = new Label(请输入圆半径: ); input = new TextField(6); add(prompt); add(input); public void paint(Graphics g) float r; r= Float.valueOf(input.getText().floatValue(); if(r0)g.drawString(半径不能为负, 60, 60); else Circle c=new Circle(r); g.drawString(圆的面积是:+c.area(),60,60); g.drawString(圆的周长是:+c.length(),60,90); public boolean action (Event e,Object o) repaint();return true;第五章引入接口的原因在程序设计中经常遇到这样一个问题:有些类互不相关,但却具有相似的方法。并且这些方法在各个类中的实现互不相同。我们不能为这些类定义一个共同的父类,但又希望在程序中体现出它们共同的接口。Java语言使用接口(interface)来解决这一类问题。接口是用来实现类似多重继承功能的一种结构,它在语法上和类很相似: 它也有属性和方法 接口间也可以形成继承关系但接口和类有很大的区别: 接口的属性全都是常量 接口的方法全都是抽象方法接口的声明public interface 接口名 extends 父接口名列表public static final 类型 域名=常量;public abstract 返回类型 方法名(参数表);例如:public interface raises final amplitude=1000;abstract void increase(String s);接口的实现由于接口中的方法都是抽象方法,所以接口定义完之后,必须在某个类中对他的方法具体化,即重新定义接口的所有方法,并编写具体的方法体。此过程称为接口的实现。步骤: 声明类时用关键字implements声名该类要实现的接口。在类体中实现接口的方法,为其编写方法体。若为非抽象类,必须实现接口的所有抽象方法。例如:public class policeman implement raisespublic void Increase(String S)salary+=amplitude;例:定义图形接口Shapeinterface Shapeint MAX_NUMBER=10;void draw(Graphics g);double area();接口中定义的属性和方法默认都是public的定义类Circle实现Shape接口public class Circle implements Shapedouble centerX, centerY; double radius; public Circle(double x, double y, double r) centerX=x; centerY=y; radius=r; public double area() return Math.PI*radius*radius; public void draw(Graphics g) g.drawOval(x-radius,y-radius,2*radius,2*radius); 接口的定义和应用小结v 接口是一系列常量和抽象方法的集合,它提供了多个类共同的方法,但不限制每个类如何实现这些方法。v Java允许一个类同时实现多个接口,相当于实现多继承的功能。 v 接口中不能声明任何变量和构造函数。v 如果一个类实现多个接口,应该在接口名之间用逗号隔开。 v 当一个类实现接口时,必须实现接口中给出的所有抽象方法,若实现接口的类是一个抽象类,可以把实现接口的任务交给子类去实现。第六章作业3.public class Sentences public static void main(String args) String article=the,a,one,some,any; String noun=boy,girl,dog,town,car; String verb=drove,jumped,ran,walked,skipped; String preposition=to,from,over,under,on; String source=new String6; String purpose=new String20; int i,j; source0=article; source1=noun; source2=verb; source3=preposition; source4=article; source5=noun;for(i=0;i20;i+) purposei=new String(); for(j=0;j6;j+) purposei+=sourcej(int)(Math.random()*5)+ ; System.out.println(purposei); 5.import java.applet.Applet;import java.awt.*;public class DateToString extends Applet Label lab; TextField input; public void init() lab=new Label(请输入一个日期,格式如(2010-10-24):); input=new TextField (10); add(lab); add(input); public boolean action(Event e,Object o) repaint(); return true; public void paint(Graphics g) String d=input.getText(); if(d.length()=0) return; else if(d.length()!=10|d.charAt(4)!=-|d.charAt(7)!=-) g.drawString(日期格式错误!,100, 100); else String year=d.substring(0,4); String day=d.substring(8); String month; int m=Integer.parseInt(d.substring(5, 7);switch(m) case 1:month=January;break; case 2:month=February;break; case 3:month=March;break; case 4:month=April;break; case 5:month=May;break; case 6:month=June;break; case 7:month=July;break; case 8:month=August;break; case 9:month=September;break; case 10:month=October;break; case 11:month=November;break; case 12:month=December;break; default:month=January; g.drawString(month+ +day+,+year, 100, 100); 第七章作业2.定义一个Circle类,其中包含计算圆周长和面积的方法,若输入的半径小于零,就要抛出一个自定义异常。class IllegalRadius extends Exception public String toString() return 半径不能为负数!; class IllegalRadius extends Exception public IllegalRadius() super(半径不能为负数!); class Circle float radius; Circle(float r)throws IllegalRadius if(r0) throw(new IllegalRadius(); else radius=r; double area() return Math.PI*radius*radius; double length() return 2*Math.PI*radius;import java.applet.Applet;import java.awt.*;public class UseCircle extends Applet Label prompt; TextField input; public void init() prompt = new Label(请输入圆半径: ); input = new TextField(6); add(prompt); add(input); public boolean action (Event e,Object o) repaint();return true;public void paint(Graphics g) float r; r= Float.valueOf(input.getText().floatValue(); try Circle c=new Circle(r); g.drawString(圆的面积是:+c.area(),60,60); g.drawString(圆的周长是:+c.length(),60,90); catch(IllegalRadius e) g.drawString(+e,60,60); 第十章作业1.编程:在屏幕上画出5个圆,它们的位置和半径都是随机的。import java.applet.Applet;import java.awt.*;public class DrawCircles extends Applet public void paint(Graphics g) int x,y,d; for(int i=0;i5;i+) x=(int)(Math.random()*300); y=(int)(Math.random()*100); d=(int)(Math.random()*200); g.drawOval(x, y,d, d); 2.编程:模拟屏幕保护程序,在屏幕上随机画线,他们的位置和颜色都是随机的,一旦画了100根,就清除屏幕,然后重新开始划线。 import java.applet.Applet;import java.awt.*;public class DrawLines extends Applet implements RunnableThread animator;public void start() animator=new Thread(this);animator.start();public void stop()animator=null; public void run() while(Thread.currentThread()=animator)repaint(); public void paint(Graphics g) int x1,y1,x2,y2,R,G,B; Color c; for(int i=0;i100;i+) x1=(int)(Math.random()*300); y1=(int)(Math.random()*200); x2=(int)(Math.random()*300); y2=(int)(Math.random()*200); R=(int)(Math.random()*256); G=(int)(Math.random()*256); B=(int)(Math.random()*256); c=new Color(R,G,B); g.setColor(c); g.drawLine(x1, y1, x2, y2); try Thread.sleep(10); catch (InterruptedException ex) System.out.println(ex.toString(); 第十一章作业1.编写一个华氏温度到摄氏温度的转换程序。通过一个文本框输入华氏温度,通过一个标签输出相应得摄氏温度。使用Applet来实现程序:import java.applet.Applet;import java.awt.event.*;import javax.swing.*;public class testEventField extends Applet implements ActionListener JTextField input; JLabel label,output; public void init() input=new JTextField(10); label=new JLabel(请输入华氏温度); output=new JLabel(); add(label); add(input); add(output); input.addActionListener(this); public void actionPerformed(ActionEvent e) double c; c=Double.parseDouble(input.getText(); c=5.0/9*(c-32); output.setText(摄氏温度:+c); 2.编写一个GUI,界面上提供的按钮有“画圆”、“画矩形”、“画椭圆”和“画直线”,另外提供两个文本框,用户可以在其中输入坐标位置。当用户按不同按钮时,就在屏幕输出不同的图形。 import java.applet
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 公司环境安全培训记录课件
- 公司消防安全培训记录课件
- 护理人文病房成果汇报
- 《秋游归来话秋游》课件
- 系统稳定性汇报
- 《祖父的园子》课件
- 《短文两篇》课件
- 2025设备租赁合同的样书
- 公司每周安全培训内容课件
- 公司机关安全培训计划课件
- 收单商户管理办法
- 分类管理理念下国有企业股权投资后评价体系的构建与实践
- 新生儿支气管肺炎护理查房
- 2025年芳香保健师(初级)职业技能鉴定全真试题(含解析)
- 银行外包人员管理办法
- 外贸订单发货管理办法
- 学堂在线 逻辑学概论 章节测试答案
- 招生表彰活动方案
- 2025年安徽高考地理试卷真题(含答案解析)
- 学校党组织家访活动方案
- 2025至2030中国海上风电行业深度研究及发展前景投资评估分析
评论
0/150
提交评论