已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1.编写Java Application程序,输出1900年到2000年之间的所有润年。(闰年的判断条件:能被4整除且不能被100整除,或能被400整除);public class Test2 public static void main(String args) int i;for(i=1900;i=2000;i+)if(i%4=0&i%100!=0)|(i%400=0)System.out.println(i+ is run nian); 2.编写Java Appet 程序打印 “水仙花” 数 (它的个、十、百位数字的立方的和等于该数本身,如:153=13+53+33) ;public class Test2_2_0public static void main(String args) int j,k,l,n;System.out.println(水仙花的数如下:);for(int i=100;i=999;i+)j=i%10;n=i/10;k=n%10;l=n/10; if(i=(j*j*j+k*k*k+l*l*l)System.out.print(i+ ); 3. 编写Java Application程序,分别用do-while和for循环计算1+1/2!+1/3!+1/4!.的前20项和;public class Test2_3public static void main(String args) double sum=0.0;for(int i=1;i=20;i+)sum+=jiechen(i);System.out.println(sum=+sum);public static double jiechen(int n)double flag=1.0;for(int j=1;j=n;j+)flag=flag*j;return 1/flag; 1定义一个学生类(Student),属性有private的名字(name), public的年龄(age),专业(specialty)和封装name属性的方法:setName(),getName(),getspecialty()编写Application,创建一个学生对象,设置name和age属性值,然后调用一个方法(此方法将对象作为参数传递,在方法中打印出学生的名字name和年龄age)。(学生类仅包含静态成员变量和方法。)public class Student private static String name;public static int age;public static String specialty;static void setname(String str) name=str;static void setage(int nl)age=nl;static void output()System.out.print(name=+name+; age=+age);public static void main(String args) Student s=new Student();s.setname(zqy);s.setage(99);s.output(); 2定义一个类MyPoint,代表一个点,public属性有x和y,方法有显示点坐标show(),构造函数有两个参数分别给x,y赋值,在main方法中构造两个对象,再创建一方法(getMiddle)为取两个点构成线段的中点的坐标,参数为2个点对象,调用此方法后得到一个新的点,编写Application,显示该对象的坐标值。public class Point_public int x,y;Point_(int x1,int y1)x=x1;y=y1;public void show()System.out.println(点坐标为:(+x+,+y+);public static void main(String args) Point_ p1,p2; p1=new Point_(2,2); p2=new Point_(4,4); getmiddle(p1,p2); public static void getmiddle(Point_ pp1,Point_ pp2) pp1.show(); pp2.show(); System.out.println(中点坐标为:(+(pp2.x+pp1.x)/2+,+(pp2.y+pp1.y)/2+); 3定义一个复数(z=x+iy)类Complex,包含: 两个属性:实部x和虚部y 默认构造函数 Complex(),设置x=0,y=0; 构造函数:Complex(int i,int j)1)求显示复数的方法:showComp()将其显示为如: 5+8i或5-8i 的形式。2)求复数的和的方法:(参数是两个复数类对象,返回值是复数类对象)3) 求复数的差的方法:(参数是两个复数类对象,返回值是复数类对象)4)求复数的乘积的方法:(参数是两个复数类对象,返回值是复数类对象)5)比较两个复数是否相等的方法(参数是两个复数类对象,返回值是boolean类型)public class Complex double x,y;Complex()x=0;y=0;Complex(int i,int j)x=i;y=j;void showcomp()System.out.println(x+y+i);public static Complex addcomp(Complex f1,Complex f2) Complex f3=new Complex(); f3.x=f1.x+f2.x; f3.y=f1.y+f2.y; return (f3); public static Complex subcomp(Complex f1,Complex f2) Complex f3=new Complex(); f3.x=f2.x-f1.x; f3.y=f2.y-f1.y; return (f3); public static Complex multicomp(Complex f1,Complex f2) Complex f3=new Complex(); f3.x=f1.x*f2.x-f1.y*f2.y; f3.y=f1.x*f2.y+f1.y*f2.x; return (f3); public static boolean equalcomp(Complex f1,Complex f2) if(f1.x=f2.x)&(f2.y=f1.y) return (true); else return (false); public static void main(String args) Complex F1,F2;F1=new Complex(1,2);F2=new Complex(3,4);System.out.print(两复数为:);F1.showcomp();F2.showcomp();System.out.print(相加结果为:);addcomp(F1,F2).showcomp();System.out.print(相减结果为:);subcomp(F1,F2).showcomp();System.out.print(相乘结果为:);multicomp(F1,F2).showcomp();if(equalcomp(F1,F2)=true)System.out.println(两复数相同!);elseSystem.out.println(两复数不相同!); 1分别编写两个类Point2D,Point3D来表示二维空间和三维空间的点,使之满足下列要求:1)Point2D有两个整型成员变量x, y (分别为二维空间的X,Y方向坐标),Point2D的构造方法要实现对其成员变量x, y的初始化。2)Point2D有一个void型成员方法offset(int a, int b),它可以实现Point2D的平移。3)Point3D是Point2D的直接子类,它有有三个整型成员变量x,y,z (分别为三维空间的X,Y,Z方向坐标),Point3D有两个构造方法:Point3D(int x,int y,int z)和Point3D(Point2D p,int z),两者均可实现对Point3D的成员变量x, y,z的初始化。4)Point3D有一个void型成员方法offset(int a, int b,int c),该方法可以实现Point3D的平移。5)在Point3D中的主函数main()中实例化两个Point2D的对象p2d1,p2d2,打印出它们之间的距离,再实例化两个Point2D的对象p3d1,p3d2,打印出他们之间的距离。class point2Ddouble x,y;point2D(double a,double b)x=a;y=b;System.out.println(坐标为:(+a+,+b+);void setoff(double a,double b)x+=a;y+=b;System.out.println(x轴方向上移动:+a);System.out.println(y轴方向上移动:+b);public class point3D extends point2Ddouble x,y,z;point3D(double a,double b,double c)super(a,b); x=a;y=b;z=c;point3D(point2D p,double c)super(p.x,p.y);x=p.x;y=p.y;z=c;void setoff(double a,double b,double c)x+=a;y+=b;z+=c;public static void main(String args) double d2,d3;point2D p2d1=new point2D(1,1);point2D p2d2=new point2D(2,2); d2=Math.sqrt(Math.pow(p2d2.x-p2d1.x),2)+Math.pow(p2d2.y-p2d1.y),2); System.out.println(原来2D两点距离为:+d2);p2d2.setoff(1,2);d2=Math.sqrt(Math.pow(p2d2.x-p2d1.x),2)+Math.pow(p2d2.y-p2d1.y),2); System.out.println(现在2D两点距离为:+d2); 21)定义抽象类Shape,抽象方法为showArea(),求出面积并显示,定义矩形类Rectangle,正方形类Square,圆类 Circle,根据各自的属性,用showArea方法求出各自的面积,在main方法中构造3个对象,调用showArea方法。2)定义接口DiagArea,其中包含方法double getDiagonal()求对角线长, double getArea()求面积,定义一个矩形类,实现此接口,并自行扩充成员变量和方法;3)定义一个正方形类继承矩形类(如矩形有长w和宽h,正方形有边x,并有相应的构造函数,有一个方法中一次直接显示边长、面积和对角线长),在另一类中的主方法里使用测试该类。interface DiagAreadouble getDiagonal();/求对角线double getArea();/求面积class Rectangle implements DiagArea double w,h,s; Rectangle()w=3;h=4;Rectangle(double a,double b)w=a;h=b; public double getDiagonal()return (Math.sqrt(Math.pow(w,2)+Math.pow(h,2);public double getArea()return (w*h); void display()System.out.println(边长为:w=+w+,h=+h);System.out.println(面积为:+getArea();System.out.println(对角线为:+getDiagonal();public class Squre extends Rectanglestatic double r=4;Squre(double a)super(r,r);r=a;public static void main(String args) Rectangle R=new Rectangle(6,8);System.out.println(矩形);R.display();Squre S=new Squre(+r);System.out.println(正方形);S.display(); 1编写Application程序, 从命令行参数输入10个整数并保存在数组中,求数组中最大、最小、数据的和、平均值。public class ShuZhu public static void main(String args) int a=new int10;for(int i=0;i10;i+)ai=Integer.parseInt(argsi);System.out.println(数组的最大值为:+Max(a,a.length);System.out.println(数组的最小值为:+Min(a,a.length);System.out.println(数组的和为:+Sum(a,a.length);System.out.println(数组的平均数为:+Pingjun(a,a.length);public static int Max(int a,int n) int max; max=a0; for(int j=1;jmax) max=aj; return (max);public static int Min(int a,int n) int min; min=a0; for(int j=1;jn;j+) if(ajmin) min=aj; return (min);public static int Sum(int a,int n) int sum=0; for(int j=0;jn;j+) sum+=aj; return (sum);public static float Pingjun(int a,int n) float Pj; return (Sum(a,a.length)/a.length); 2.从命令行参数一日期,如“2005-5-6”,计算与今日之间的天数间隔。public class Textdate public static void main(String args) int now=new int3;int ago=new int3;for(int i=0;i3;i+)agoi=Integer.parseInt(argsi);nowi=Integer.parseInt(argsi+3);System.out.println(两日期相差+Datesum(ago,now,ago.length)+天);public static float Datesum(int a,int b,int n)float sum,year,month,day,sumyear,summonth,sumday;year=bn-3-an-3;month=bn-2-an-2;day=bn-1-an-1;if(day0)month-;sumday=30-Math.abs(day);elsesumday=day;if(month0)year-;summonth=30*(12-Math.abs(month);elsesummonth=30*month; sumyear=365*year;return (sumyear+summonth+sumday); 3.将命令行第一个参数每个字符放入一字符数组,然后逆序输出public class Textchar public static void main(String args) int n=args0.length();char ch=new charn;Displaychar(args0,ch,ch.length);public static void Displaychar(String s,char a,int l)for(int i=0;i=0;j-) System.out.print(aj); 4编写Application程序,接受用户输入一段英文文字后,输出其中的字符数、单词数和句子数(设句子以“?”、“!”、“.”结束)。import java.util.Scanner;public class Textjz public static final int MAX=100;public static void main(String args) int i=0; String sim = new StringMAX;Scanner sc = new Scanner(System.in);simi = sc.next();while(!simi.equals(#)i+;simi = sc.next();/for ( i=0;isim.length;i+)/System.out.println(simi); Test(sim,i);public static void Test(String a,int n)int i,charcount=0,wordcount=0,int statementcount=0;for(i=0;in;i+) if(!(ai.equals(,)|ai.equals(?)|ai.equals(.)|ai.equals(!)wordcount+;charcount=charcount+ai.length();if(ai.equals(?)|ai.equals(!)|ai.equals(.)statementcount+;System.out.println(该字段的字符数为:+charcount);System.out.println(该字段的单词数为:+wordcount);System.out.println(该字段的句子数为:+statementcount); 5打印杨辉三角的前10层。public class yanghuisjpublic static void main(String args) int i,j,k,last=new int11,now=new int12;last1=1;for(i=1;i=10;i+)/控制行数for(k=1;k=10-i;k+)/输出行前的空格 System.out.print( ); for(k=1;k=1;j-)/算出下行的数 if(j=10) nowj=lastj-1+lastj; for(j=1;j=10;j+)/复制 lastj=nowj; System.out.println(); 6.编写Applet程序,使用 *画一条正弦曲线(提示:y=a* sin(x),坐标要取整)import java.applet.Applet;import java.awt.*;public class Sin extends Applet public void paint(Graphics g) int a=30,x=100;double y;g.drawString(y=a*sin(x):,95,150);for(int i=1;i800;i+)y=a*Math.sin(Math.PI*x/180); g.drawString(*,x,(int)y+200);x=x+5; 1. 编写Applet程序,改变applet窗口大小,使其最大化、最小化,查看init()、start()、paint()、和destroy()方法的执行情况(执行顺序和执行次数)。import java.applet.Applet;import java.awt.*; public class Textapplet extends Applet StringBuffer buffer;int i=0,j=0,k=0,l=0,h=50;/i为init();j为start();k为stop();l为destroy()的执行次数; public void init() buffer = new StringBuffer();i+; addItem(initializing. ); public void start() j+; addItem(starting. ); public void stop() k+; addItem(stopping. ); public void destroy() l+;addItem(preparing for unloading(destroy).); void addItem(String newWord) /System.out.println(newWord); buffer.append(newWord); repaint(); public void paint(Graphics g) for(int m=1;m5;m+)switch(m)case 1:g.setColor(Color.red);g.drawString(init,168,380 );g.fillRect(150,400, 50,i*10 );break;case 2:g.setColor(Color.blue);g.drawString(start,218,380 );g.fillRect(200,400, 50,j*10 );break;case 3:g.setColor(Color.green);g.drawString(init,268,380 );g.fillRect(250,400, 50,k*10 );break;case 4:g.setColor(Color.black);g.drawString(init,318,380 );g.fillRect(300,400, 50,l*10 );break; g.drawString(buffer.toString(), 150, h); h=h+50; 2 编写Applet显示字符串,显示字符串及其显示位置,字体、字体大小和颜色通过HTML文件中的参数来传递。import java.applet.Applet;import java.awt.*;public class StrApplet extends Applet String str;int x,y,h;Font fot;public void init()str=getParameter(string);x=Integer.parseInt(getParameter(x1);y=Integer.parseInt(getParameter(y1);h=Integer.parseInt(getParameter(size);fot=new Font(隶书,Font.ITALIC,h);public void paint(Graphics g) g.setColor(Color.red);g.setFont(fot);g.drawString(str,x,y); 3编写Applet(大小140*60),其背景色为蓝色,画一个长方形(其填充色为pink,各边离边小于10像素)和一个在填充的长方形中左右移动的小球(半径15)。import java.applet.Applet;import java.awt.*;public class TextApplet3 extends Applet int i=110,flag=0; Thread tr; public void init() tr=new Thread(); Graphics g1=getGraphics();setBackground(Color.blue);g1.setColor(Color.pink);g1.fillRect(50,50,150,60); public void paint(Graphics g)g.setColor(Color.pink);g.fillRect(50,50,150,60);g.setColor(Color.black);if(flag=0)i=i+10;if(i=190)flag=1;if(flag=1)i=i-10;if(i=50)flag=0;g.fillOval(i,80,15,15);trytr.sleep(500);catch (InterruptedException e)System.out.println(e);repaint();4. 实现一个Applet,显示一幅图片。不停地运行一个动画。import java.applet.Applet;import java.awt.*;public class DongHua extends Applet Image Im;final int N=5;int m=0; public void init()Im=new ImageN;for(int i=0;iN;i+)Imi=getImage(getDocumentBase(),i+.jpg);public void paint(Graphics g)g. drawImage(Imm,350,300,300,300,this);m=+m%N;tryThread.sleep(1000);catch (InterruptedException e) repaint(); 1.编写Application程序,在Frame中加入2个按钮(Button)和1个标签(Label),单击两个按钮,显示按钮的标签于Label。import java.awt.*;import java.awt.event.*;import javax.swing.JFrame;public class TextFrame public static void main(String args) myframe mf=new myframe();/mf.setVisible(true);class myframe extends Frame implements ActionListener,WindowListenerButton but1,but2;Label lb; StringBuffer str;double x,y;TextField tf;myframe()super(my window);setBounds(100,100,200,200);this.addWindowListener(this);this.setLayout(new FlowLayout();but1=new Button(按钮1);but1.addActionListener(this);add(but1);but2=new Button(按钮2);but2.addActionListener(this);add(but2);lb=new Label(标签);add(lb);tf=new TextField(显示那个按钮,10);add(tf);/pack();setVisible(true);/show();/*public void paint(Graphics g) g.drawString(str.toString(),(int)x+10,(int)y+20)*/public void actionPerformed(ActionEvent e)if(e.getSource()=but1)/str=new StringBuffer(按钮1);x=lb.getLocationOnScreen().getX();y=lb.getLocationOnScreen().getY();repaint();tf.setText(按钮1);lb.setText(按钮1);elseif(e.getSource()=but2)tf.setText(按钮2);lb.setText(按钮2);/str=new StringBuffer(按钮2);x=lb.getLocationOnScreen().getX();y=lb.getLocationOnScreen().getY();repaint(); public void windowClosing(WindowEvent e)/System.out.println(hello);this.dispose();System.exit(0); public void windowClosed(WindowEvent e)this.dispose();public void windowDeiconified(WindowEvent e)public void windowActivated(WindowEvent e)public void windowDeactivated(WindowEvent e)public void windowOpened(WindowEvent e)public void windowIconified(WindowEvent e)2在Applet中加入1个文本框,1个文本区,每次在文本框中输入文本,回车后将文本添加到文本区的最后一行。import java.applet.*;import java.awt.*;import java.awt.event.*;public class Exam5 extends Applet implements ActionListener,TextListenerTextArea ta1;TextField text1;public void init()setBackground(Color.white);text1=new TextField(,20);text1.addActionListener(this);text1.addTextListener(this);add(text1);ta1=new TextArea(文本区,10,15,TextArea.SCROLLBARS_BOTH);/ta1.addTextListener(this);add(ta1);public void actionPerformed(ActionEvent e)/ta1.setText();ta1.append(n+text1.getText(); repaint();public void textValueChanged(TextEvent e)3在Applet中加入2个复选框,显示标题为“学习”和“玩耍”,根据选择的情况,分别显示“玩耍”、“学习”、“劳逸结合”。import java.applet.Applet;import java.awt.*;import java.awt.event.*;public class checkbox extends Applet implements ItemListenerint i=0,j=0;Checkbox ck1,ck2;public void init()ck1=new Checkbox(学习,false);ck2=new Checkbox(玩耍,false);add(ck1);add(ck2);ck1.addItemListener(this);ck2.addItemListener(this);public void paint(Graphics g) if(i=1&j=1) g.drawString(劳逸结合,200,100);else if(i=1) g.drawString(学习状态,200,100); else if(j=1) g.drawString(玩耍状态,200,100); else g.drawString(暂停状态,200,100);public void itemStateChanged(ItemEvent e)i=j=0;if(ck1.getState()i=1;if(ck2.getState()j=1;repaint();4在Applet中加入1个滚 动列表框List、1个下拉列表框Choice和1个按钮,点击按钮将List中的项目移到Choice组件中,import java.applet.Applet;import java.awt.*;import java.awt.event.*;public class Listc extends Applet implements ActionListener,ItemListenerList ls;Choice co;Button but;String s;public void init()ls=new List(10,true);co=new Choice();but=new Button(按钮); s=new String10; add(ls); add(co); add(but); but.addActionListener(this); ls.addActionListener(this); co.addItemListener(this); for(int i=0;i10;i+) ls.add(list+i);public void actionPerformed(ActionEvent e)if(e.getSource()=but)/Graphics g=new Graphics();for(int i=0;ils.getItemCount();i+)si=ls.getItem(i);co.add(si);ls.removeAll();repaint();public void itemStateChanged(ItemEvent e)5. 编写Application程序,在Frame中加入80个按钮,分20行4列,用GridLayout布局方式,按钮背景为黄色(Color.yellow),按钮文字颜色为红色(Color.red)。import java.awt.*;import java.awt.event.*;public class button extends Frame implements ActionListenerButton but=new Button80;public button()super(我的80按钮窗口);setBounds(100,100,200,200
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年锅炉压力容器管理制度
- 人工授精并发症的健康宣教
- Rhein-1-O-glucoside-生命科学试剂-MCE
- 山地光伏微孔灌注桩方案
- 子宫输卵管造影术造影剂过敏反应的观察护理
- 棕色插画风世界勤俭日介绍模板
- 妇科肿瘤放疗定位操作规范
- 卵巢早衰中医如何调理
- 医学女性卵巢结构解剖教学课件
- 医学子宫输卵管造影专题课件
- 智鼎在线测评校招试题汇编2026
- 2026秋国家开放大学《中国近现代史纲要》(课程ID:04681)形考任务复习题库参考答案
- 江西金太阳2027届高三11月区域名校素养联考数学+答案
- 2026年党员领导干部党校培训结业全真模拟考试卷及答案(共七套)
- 2026年建行风险管理岗位题库及答案
- 考研数学一公式手册大全
- 2026年张家口市文旅集团笔试真题(附答案)
- 2021-2025年河南中考地理试题含答案
- 五节一环保培训
- 药房实习生考试及答案
- 设备振动分析报告范本
评论
0/150
提交评论