学生实验报告.doc
Java程序设计实验报告本专 业班 级姓 名学 号指导教师Java程序设计实验报告学院: 药学院 班级: 药剂121 学号: 实验名称Java程序设计得分实验题目1 用for语句输出以下数字金字塔层数由键盘输入: 1 1 3 1 1 3 5 3 11 3 5 7 5 3 1 1 3 5 7 9 7 5 3 12 编写程序:由键盘输入一正整数,求出小于且等于这个数的所有质数。3 编写一个复数类ComplexNumber,要求能进展复数间的根本数学运算:,三种运算,以与返回复数的数学表达式、实数局部、虚数局部等功能。说明:1复数的数学表达式为:abi其中a为实数、b为虚数2在具体的实现过程中,可以用add表示,sub表示,mul表示3返回该复数的数学表达式:以abi返回结果:a+bi也要考虑到多种情况4返回该复数的实数局部: 以abi返回结果:a 5返回该复数的虚数局部: 以abi返回结果:bi 4 编程实现桌面应用程序:1 2实验设备、环境Windows XP、Elipse/NetBeans/JDK实验地点实验一楼222程序清单与程序运行结果:1. eclipse)import java.io.*;publicclass Pyramid publicstaticvoid main(String args) throws IOException int a,b;int c,d;String str;BufferedReader buf;buf=new BufferedReader(new InputStreamReader(System.in); System.out.print("请输入金字塔层数:"); str=buf.readLine(); a=Integer.parseInt(str);for (b=0;b<=a-1;b+)for (c=0;c<=20-b;c+)System.out.print(" ");for (d=0;d<=2*b;d+)if (d<=b)System.out.print(" "+(2*d+1);elseSystem.out.print(" "+(2*(2*b-d)+1);System.out.println();运行结果:2.eclipse)import java.io.*;publicclassZhishu publicstaticvoid main(String args) throws IOExceptionint n;int m=0; String str; BufferedReader buf; buf=new BufferedReader(new InputStreamReader(System.in); System.out.print("请输入一正整数:"); str=buf.readLine(); n=Integer.parseInt(str);if(n>=2) System.out.print(" "+2); outer:for(int i=3;i<=n;i+=2)for(int j=2;j<i;j+)if(i%j=0)continue outer; System.out.print(" "+i); m+;if(m=10) System.out.println(); m=0; System.out.println();运行结果:3.eclipse)class ComplexNumberprotectedintshibu;protectedintxubu;public ComplexNumber(int s,int x)shibu=s;xubu=x;public String biaodashi()if(shibu=0) returnxubu+"i"if(xubu=0) returnshibu+""if(shibu=0&&xubu=0) return 0+""elsereturnshibu+"+"+xubu+"i"publicvoid add(ComplexNumber c)int shibuhe,xubuhe;shibuhe=this.shibu+c.shibu;xubuhe=this.xubu+c.xubu;System.out.println("("+this.biaodashi()+")+("+c.biaodashi()+")="+shibuhe+"+"+xubuhe+"i");publicvoid sub(ComplexNumber c)int shibucha,xubucha;shibucha=this.shibu-c.shibu;xubucha=this.xubu-c.xubu;System.out.println("("+this.biaodashi()+")+("+c.biaodashi()+")="+shibucha+"+"+xubucha+"i");publicvoid mul(ComplexNumber c)int shibuji,xubuji;shibuji=this.shibu*c.shibu-this.xubu*c.xubu;xubuji=this.shibu*c.xubu+this.xubu*c.shibu;System.out.println("("+this.biaodashi()+")+("+c.biaodashi()+")="+shibuji+"+"+xubuji+"i");publicint shishubufen()returnshibu;public String xushubufen()returnxubu+"i"publicstaticvoid main(String args) String c; String d;int p;String q;int m;String n;ComplexNumber a=new ComplexNumber(5,6);ComplexNumber b=new ComplexNumber(5,7);c=a.biaodashi();System.out.println("第一个复数的表达式为:"+c);p = a.shishubufen();q = a.xushubufen();System.out.println("该复数的实数局部为:" + p + ",虚数局部为:" + q);d = b.biaodashi();System.out.println("第二个复数的表达式为:" + d);m = b.shishubufen();n = b.xushubufen();System.out.println("该复数的实数局部为:" + m + ",虚数局部为:" + n);System.out.print("两复数的和为:");a.add(b);System.out.print("两复数的差为:");a.sub(b);System.out.print("两复数的积为:");a.mul(b);运行结果:4.(eclipse)(1)import java.awt.*;import java.applet.*;import java.awt.event.*;public class ShowPassword extends Applet implements ActionListenerTextField name=new TextField(30);TextField pw1=new TextField(30);TextField pw2=new TextField(25);Label la1=new Label();Label la2=new Label();Label la3=new Label();Label la4=new Label();Button b1=new Button("确定");Button b2=new Button("取消");public void actionPerformed(ActionEvent e)pw2.setText(pw1.getText();public void init()this.setLayout(new FlowLayout();la1.setText(" 用户名:");this.add(la1);this.add(name);la2.setText(" 密 码:");this.add(la2);pw1.setEchoChar('*');this.add(pw1);b1.addActionListener(this);la3.setText(" 你输入的密码是:");this.add(la3);this.add(pw2);this.add(b1);this.add(b2);la4.setText("界面制作人:药剂121,黄夏钦,学号10121350");this.add(la4);运行结果:2import java.awt.*;import java.applet.*;import java.awt.event.*;publicclass Applet2 extends AppletTextField color=new TextField(15);Label la1=new Label();Label la2=new Label();Label la3=new Label();CheckboxGroup cg=new CheckboxGroup();Checkbox ck1= new Checkbox("red",true,cg);Checkbox ck2= new Checkbox("blue",false,cg);Checkbox ck3= new Checkbox("green",false,cg);class CheckListener implements ItemListenerpublicvoid itemStateChanged(ItemEvent e)String source=(String)(e.getItem();if(source="red")if(e.getStateChange()=e.SELECTED)color.setText("red");if(source="blue")if(e.getStateChange()=e.SELECTED)color.setText("blue");if(source="green")if(e.getStateChange()=e.SELECTED)color.setText("green");publicvoid init()setLayout(new FlowLayout();la1.setText("请选择你喜欢的颜色:");add(la1);add(ck1);add(ck2);add(ck3);la2.setText("你喜欢的颜色是:");add(la2);add(color);la3.setText("界面制作人:药剂121,黄夏钦,学号10121350");add(la3);ck1.addItemListener(new CheckListener();ck2.addItemListener(new CheckListener();ck3.addItemListener(new CheckListener();运行结果:9 / 10