java课程设计及答案.doc_第1页
java课程设计及答案.doc_第2页
java课程设计及答案.doc_第3页
java课程设计及答案.doc_第4页
java课程设计及答案.doc_第5页
免费预览已结束,剩余44页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

金陵科技学院 信息技术学院学 生 实 验 报 告(理工类)课程名称: 编译原理 专业班级:08计算机科学与技术(单) 学生学号:0805110223 学生姓名: 徐佳佳 所属院部:信息技术学院 指导教师: 陈圣国 20 10 20 11 学年 第 二 学期 金陵科技学院教务处制实验报告书写要求实验报告原则上要求学生手写,要求书写工整。若因课程特点需打印的,要遵照以下字体、字号、间距等的具体要求。纸张一律采用A4的纸张。实验报告书写说明实验报告中一至四项内容为必填项,包括实验目的和要求;实验仪器和设备;实验内容与过程;实验结果与分析。各院部可根据学科特点和实验具体要求增加项目。填写注意事项(1)细致观察,及时、准确、如实记录。(2)准确说明,层次清晰。(3)尽量采用专用术语来说明事物。(4)外文、符号、公式要准确,应使用统一规定的名词和符号。(5)应独立完成实验报告的书写,严禁抄袭、复印,一经发现,以零分论处。实验报告批改说明实验报告的批改要及时、认真、仔细,一律用红色笔批改。实验报告的批改成绩采用百分制,具体评分标准由各院部自行制定。实验报告装订要求实验批改完毕后,任课老师将每门课程的每个实验项目的实验报告以自然班为单位、按学号升序排列,装订成册,并附上一份该门课程的实验大纲。实验项目名称: Java编程基础 实验学时: 6 同组学生姓名: / 实验地点: A201 实验日期:2011-4-22;2011-4-29 实验成绩: 批改教师: 批改时间: 实验1 Java编程基础一、实验目的(1) 熟练掌握JDK编写调试Java应用程序及Java小程序的方法。(2) 熟练掌握Java应用程序及小程序的结构。(3) 了解Java语言的特点,基本语句、运算符及表达式的使用方法。(4) 熟练掌握常见数据类型的使用。(5) 熟练掌握if-else、switch、while、do-while、for、continue、break、return语句的使用方法。二、实验要求(1)调试程序要记录调试过程中出现的问题及解决办法;(2)编写程序要规范、正确,上机调试过程和结果要有记录,不断积累编程及调试经验;(3)做完实验后给出本实验的实验报告。三、实验设备、环境奔腾以上计算机,Windows 2000 、J2SDK、IE浏览器四、实验步骤及内容(1)输入并调试下面的程序class HelloWorldpublic static void main(String args)System.out.println(“Hello,world”);调试结果如下:(2)编写程序,输出Fibonacci数列的前20个数程序如下:public class Fibonacci public static void main(String args) int i,a=1,b=1,c; for(i=0;i20;i+) if(i2) System.out.print(1+t); else c=a+b; System.out.print(c+t); a=b; b=c; 调试结果如下:(3)编写Java小程序,在IE浏览器中输出字符串”hello,world”程序如下:import java.awt.*; import java.applet.Applet; public class HWApplet extends Applet public void paint(Graphics g) g.drawString( Hello,World ,10,50); 在另建一个html文档并在其中输入以下代码:调试结果如下:(4)编写程序,输出如下形式的数字方阵:n=4 0 0 0 0 0 1 1 1 0 1 2 2 0 1 2 3程序如下:import java.util.Scanner;class fangzheng public static void main(String avgs) int n; Scanner cin = new Scanner(System.in); System.out.print(请输入方阵的阶数:); n = cin.nextInt(); int a=new intnn; for(int i=0;in;i+) for(int j=i;jn;j+) aij = aji = i; for (int i = 0; i n; i+) for (int j = 0; j n; j+) System.out.print(aij+ ); System.out.println(); 调试结果如下:(6) 编写一程序,求二维数组的鞍点。程序如下:public class andian public static final int array = 1,2,3, 4,5,6, 7,8,9 ; public static void main(String args) for(int i=0;iarray.length;i+) int minCol = getrowMin(i); if(iscolMin(minCol,i) System.out.println(array+i+minCol+); public static int getrowMin(int rowNo) int min=arrayrowNo0; int col=0; for(int i=1;iarrayrowNo.length;i+) if(arrayrowNoimin) min=arrayrowNoi; col=i; return col; public static boolean iscolMin(int colNo,int rowNo) for(int i=0;iarray.length;i+) if(arrayicolNoarrayrowNocolNo)return false; return true; 调试结果如下:(6)编写程序,采用二维数组输出数字方阵。输出下列方阵(当n=4时): 1 2 6 7 3 5 8 13 4 9 12 14 10 11 15 16程序如下:public class T public static void main(String args) int n=4;/可以修改 int n2=n*n; int arr=new intnn; for(int i=0;in;i+) if(i%2=0)arr0i=(i+1)*(i+2)/2; else arr0i=arr0i-1+1; for(int i=1;in;i+) int num=arr0i; if(i%2!=0) for(int j=1;j=i;j+) arrji-j=+num; else for(int j=1;j=i;j+) arrji-j=-num; for(int i=0;in-1;i+) int num=n2+1-arr0i; arrn-1n-i-1=num; if(i%2=0) for(int j=1;ji+1;j+) arrn-j-1n-i-1+j=+num; else for(int j=1;ji+1;j+) arrn-j-1n-i-1+j=-num; for(int i=0;in;i+) for(int j=0;j=1)&(m=1)&(dthis.daysOfMonth() day=1; month+; if(month12) month=1; year+; public MyDate yestoday() MyDate yes=new MyDate(this); yes.day-; if(yes.day=0) yes.month-; if(yes.month=0) yes.month=12; yes.year-; yes.day=daysOfMonth(yes.year,yes.month); return yes; class MyDate_ex public static void main(String args) System.out.println(今年是+MyDate.getThisYear()+,闰年?+MyDate.isLeapYear(MyDate.getThisYear(); MyDate d1=new MyDate(2008,8,8); MyDate d2=new MyDate(d1); System.out.println(d2+,+闰年?+d2.isLeapYear(); System.out.print(d2+的昨天是+d2.yestoday()+n+d2+的明天是); d2.tomorrow(); System.out.println(d2); 调试结果如下:(2) 设计一个复数类,成员包括实部和虚部,成员方法包括复数加法、减法、比较、转换成字符串等运算或操作。程序如下:class complex double x,y; complex(double a,double b)x=a;y=b; public class shiyan42 public static void main(String args) complex num1=new complex(6,-23); complex num2=new complex(1,-7); System.out.println(num1的realpart=+6+ 其imagepart=+-23i); System.out.println(num2的realpart=+1+ 其imagepart=+-7i); double realpart=num1.x+num2.x; double imagepart=num1.y+num2.y; System.out.println(相加后 realpart=+realpart+ imagepart=+imagepart+i); realpart=num1.x-num2.x; imagepart=num1.y-num2.y; System.out.println(相减后 realpart=+realpart+ imagepart=+imagepart+i); 调试结果如下:(3) 为例3.5的Student2类增加成绩的实例成员变量,并在例3.6的Group类中按照专业分别统计不同班级的学生成绩。Student类程序如下:class MyDateprivate int year, month, day;private static int thisYear;staticthisYear = 2009;public MyDate(int year, int month, int day)this.set(year, month, day);public MyDate()this(1970, 1, 1);public MyDate(MyDate d)this.set(d);public void set(int y, int m, int d)this.year = y;this.month = (m = 1) & (m = 1) & (d this.daysOfMonth()day = 1;month+;if (month 12)month = 1;year+;public MyDate yestoday()MyDate yes = new MyDate(this);yes.day-;if (yes.day = 0)yes.month-;if (yes.month = 0)yes.month = 12;yes.year-;yes.day = daysOfMonth(yes.year, yes.month);return yes;class Personprotected String name;protected MyDate birthday;private static int count = 0;public Person(String name, MyDate birthday) = name;this.birthday = birthday;count+;public Person(String name)this(name, new MyDate();public Person()this(, new MyDate();public Person(Person p)this(, new MyDate(p.birthday);public void set(String name) = name;public void set(MyDate birthday)this.birthday = new MyDate(birthday);public void set(String name, MyDate birthday)this.set(name);this.set(birthday);public String getName()return ;public MyDate getBirthday()return this.birthday;public String toString()return +,+this.birthday.toString()+,+this.getAge()+岁;public int getAge(int year)return year - this.birthday.getYear();public int getAge()return getAge(MyDate.getThisYear();public int olderThen(Person p)return p.birthday.getYear() - this.birthday.getYear();public static void howMany()System.out.println(count + 个Person对象);public void finalize()System.out.println(释放对象( + this.toString() + );this.count-;public class Student extends Personprivate String number;private String speciality;private static int count = 0;private static int max = 0;public Student(String name, MyDate birthday, String spec)super(name, birthday);Student.max+;this.number = 0101 + MyDate.getThisYear() + String.format(000%d,max);this.speciality = spec;this.count+;public Student(String name, MyDate birthday)this(name, birthday,);public Student(String name)this(name, new MyDate(), );public Student()this(, new MyDate(), );public Student(Person p, String spec)this(, p.birthday, spec);public Student(Student s)this(, s.birthday, s.speciality);public void finalize()super.finalize();this.count-;public void set(String name, MyDate birthday, String spec)super.set(name, birthday);this.setSpeciality(spec);public void setSpeciality(String spec)this.speciality = spec;public String getSpeciality()return this.speciality;public String toString()return number + , + name + , + birthday.toString() + , + speciality + ,专业;public static void howMany()Person.howMany();System.out.println(count + 个Student对象);public static void main(String args)MyDate m1 = new MyDate(1979, 3, 15);Person p1=new Person(李小明,m1);Student s1=new Student(p1,计算机);Student s2=new Student(s1);s2.set(胡东东,new MyDate(1982,4,3),电子信息工程);s2.howMany();System.out.println(p1:+p1.toString()+ns1:+s1.toString()+ns2:+s2.toString();System.out.println(s2.getName()+比+s1.getName()+大+s2.olderThen(s1)+岁);s1.finalize();Student.howMany();调试结果如下: Group类程序如下:public class Grouppublic static void print(Object table)if (table != null)for (int i = 0; i table.length; i+)System.out.println(tablei.toString(); public static int indexOf(Object table, Object obj)if(table!=null&obj!=null)for(int i=0;itable.length;i+)if (obj.equals(tablei)return i;return -1;public static Person oldest(Person table)if(table=null)return null;int max=0;for(int i=0;itablemax.getAge()max=i;return tablemax;public static void main(String args)Person table=new Person(李小明,new MyDate(1989,3,15),new Student(胡东东,new MyDate(1990,4,3),计算机),new Student(张小莉,new MyDate(1991,5,1),电子信息),;print(table);String name=张小莉;int index=indexOf(table,new Person(name);System.out.println(查找+name+结果:+(index!=-1)?tableindex.toString():未找到);Student.howMany();调试结果如下:(4) 声明三角形类,继承图形抽象类,计算三角形的周长和面积。程序如下:import java.lang.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;class Trangle_GUI extends Frame Label fist,second,third,area,result; TextField _fist,_second,_third; Button judge,calc; Box box1,box2,box3,basebox; Trangle_GUI() super(三角形面积计算); Toolkit tk=getToolkit(); Dimension dim=tk.getScreenSize(); setBounds(dim.width/3,dim.height/3,300,220); setLayout(new FlowLayout(); setResizable(false); setBackground(Color.LIGHT_GRAY); fist=new Label(请输入第一个边:); second=new Label(请输入第二个边:); third=new Label(请输入第三个边:); box1=Box.createVerticalBox();/创建列排列0 box1.add(fist); box1.add(second); box1.add(third); _fist=new TextField(20); _second=new TextField(20); _third=new TextField(20); box2=Box.createVerticalBox();/创建列排列1 box2.add(_fist); box2.add(_second); box2.add(_third); basebox=Box.createVerticalBox();/创建列排列2 Panel mypanel=new Panel();/创建面板 mypanel.add(box1); mypanel.add(box2); basebox.add(mypanel); area=new La

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论