《JAVA程序设计实验指导书.doc》由会员分享,可在线阅读,更多相关《JAVA程序设计实验指导书.doc(24页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、JAVA程序设计(公选课)实验指导书实验1实验目的:掌握如何编写、编译、运行Java应用程序。实验内容: 编写一个提示“Java is interesting!”信息的应用程序。实验过程:1、 编写一个提示“Java is interesting!”信息的应用程序具体实验内容:编写一个应用程序,当运行时,会出现一个提示信息对话框,其中显示信息“Java is interesting!”。编写代码:(可使用记事本)import javax.swing.JOptionPane;public class Demo1 public static void main(String args) JOpti
2、onPane.showMessageDialog(null,Java is interesting! ); System.exit(0); 实验2:实验目的:掌握使用图形化界面的形式,正确使用表达式和流控制语句进行编程,掌握break语句和continue语句如何在多重循环语句中正确使用。熟练掌握各种运算符和表达式的使用法。实验内容: 编程显示星形符号组成的图形:矩形和下三角。 开发一个具有数字计算功能的应用程序。实验过程:1、 编程显示星形符号组成的图形:矩形和下三角编写代码:import javax.swing.JOptionPane;public class LabelTest publ
3、ic static void main(String args) String output=The first graph:n; stop: for(int row=1; row=15; row+) for(int column=1; column=5; column+) if(row=5) break stop; output+=* ; output+=n; output+=nnn; output+=The second graph:; nextRow: for(int row=1;row=5;row+) output+=n; for(int column=1; columnrow) co
4、ntinue nextRow; output+=* ; JOptionPane.showMessageDialog(null,output,Testing break with a label,JOptionPane.INFORMATION_MESSAGE); System.exit(0); 将以上代码保存为文件名为LabelTest.java的文件。2、 开发一个具有数字计算功能的应用程序实验内容: 首先产生两个随机数作为操作数,对它们分别进行加、减、乘、除和取余运算,将结果以图形化的形式显示出来。分两种情况进行:两个操作数都是整数类型和两个操作数都是实数类型。另外,使用扩展运算符进行以上运
5、算,验证运算结果是否正确。编写代码:import javax.swing.JOptionPane;import java.util.*;public class MathOps public static void main(String args) Random rand=new Random(); int i,j,k; i=rand.nextInt(100)+1; j=rand.nextInt(100)+1; String output=Tow Integer Operands:n+i=+i+ +j=+j; k=i+j; output+=nAfter adding operation,re
6、sult:n+i+j=+k; k=i-j; output+=nAfter subtracting operation,result:n+i-j=+k; k=i*j; output+=nAfter multiplying operation,result:n+i*j=+k; k=i/j; output+=nAfter dividing operation,result:n+i/j=+k; k=i%j; output+=nAfter caculating remainder operation,result:n+i%j=+k; float u,v,w; u=rand.nextFloat(); v=
7、rand.nextFloat(); output=nnTow Float Operands:n+u=+u+ +v=+v; w=u+v; output+=nAfter adding operation,result:n+u+v=+w; w=u-v; output+=nAfter subtracting operation,result:n+u-v=+w; w=u*v; output+=nAfter multiplying operation,result:n+u*v=+w; w=u/v; output+=nAfter dividing operation,result:n+u/v=+w; out
8、put+=nnExtended Operation result:; u+=v; output+=nAfter extended adding operation(u+=v), result:n+u=+u; u-=v; output+=nAfter extended subtracting operation(u-=v), result:n+u=+u; u*=v; output+=nAfter extended multiplying operation(u*=v), result:n+u=+u; u/=v; output+=nAfter extended dividing operation
9、(u/=v), result:n+u=+u; JOptionPane.showMessageDialog(null,output,Arithmetic OperationDemon,JOptionPane.INFORMATION_MESSAGE); System.exit(0); 实验3实验目的:掌握定义类的方法,特别是要掌握如何编写构造函数和main()函数,怎样创建和使用类的对象。实验内容: 创建一个日期类。 创建一个图书类。实验过程:1、 创建日期类具体实验内容:创建一个日期类,使得该类能够对日期的合法性进行验证,并将验证的结果以图形化的形式对日期信息进行正确显示。程序编码:import
10、 javax.swing.JOptionPane;public class DateTest private int month; private int day; private int year; private String validateDateString=new String(); public DateTest(int month,int day,int year) validateDateString+=nDate object constructor for date+month+/+day+/+year+ is calling!; if(month0&month0 & t
11、estDay=daysPerMonthmonth) validateDateString+=nThe dat is valid.; return testDay; if(month=2&testDay=29&(year%400=0|(year%4=0&year%100!=0) validateDateString+=nThe dat is valid.; return testDay; validateDateString+=nDay +testDay+ is invalid.Set its value to day 1.; return 1; public String toString()
12、 return month+/+day+/+year; public String getValidateDateString() return validateDateString; public static void main(String args) DateTest date1=new DateTest(3,21,2004); DateTest date2=new DateTest(2,29,2004); DateTest date3=new DateTest(21,2,2003); JOptionPane.showMessageDialog(null,date1.getValida
13、teDateString()+date2.getValidateDateString()+date3.getValidateDateString(), DateTest Demo,JOptionPane.INFORMATION_MESSAGE); System.exit(0); 2、 创建一个图书类具体实验内容:创建一个图书类,为该类定义属性,并定义两个构造函数,一个是默认的构造函数,一个是带参数的构造函数,分别用这两个构造函数创建两个对象,将对象信息以Applet的形式打印显示。程序编码:/import java.awt.*;import java.awt.Graphics;import j
14、ava.util.*;import javax.swing.JApplet;public class ClassApplet extends JApplet private Book book1,book2; public void init() book1=new Book(); book1.setName(Java 2); book1.setAuthor(houjunjie); book1.setType(Computer science); book1.setDate(new GregorianCalendar(2004,8,15); book2=new Book(Visual C+.n
15、et,Baoyijun,Computer science,new GregorianCalendar(2003,10,23); public void paint(Graphics g) super.paint(g); g.setColor(new Color(255,0,0); g.drawString(下面是两本书的信息:,120,40); g.drawString(第一本书的信息:,60,60); g.setColor(new Color(0,0,255); g.drawString(书名:+book1.getName(),60,80); g.drawString(作者:+book1.g
16、etAuthor(),60,100); g.drawString(类别:+book1.getType(),60,120); g.drawString(出版日期:+book1.getDate().get(Calendar.MONTH)+/+book1.getDate().get(Calendar.YEAR),60,140); g.setColor(new Color(255,0,0); g.drawString(第二本书的信息:,215,60); g.setColor(new Color(0,0,255); g.drawString(书名:+book2.getName(),215,80); g.
17、drawString(作者:+book2.getAuthor(),215,100); g.drawString(类别:+book2.getType(),215,120); g.drawString(出版日期:+book2.getDate().get(Calendar.MONTH)+/+book2.getDate().get(Calendar.YEAR),215,140); class Book private String bookName; private String author; private String type; private GregorianCalendar publis
18、hedDate; public Book() this(unknown,unknown,unknown,new GregorianCalendar(); public Book(String bookName,String author,String type, GregorianCalendar publishedDate) this.bookName=bookName; this.author=author; this.type=type; this.publishedDate=publishedDate; public void setName(String name) bookName
19、=name; public String getName() return bookName; public void setAuthor(String bookAuthor) author=bookAuthor; public String getAuthor() return author; public void setType(String bookType) type=bookType; public String getType() return type; public void setDate(GregorianCalendar date) publishedDate=date
20、; public GregorianCalendar getDate() return publishedDate; 实验4实验目的:了解类的继承和多态的含义,了解使用接口的编程思想,掌握使用接口的编程方法.实验内容:使用类的继承和接口综合举例.实验过程:具体实验内容:定义一个接口Shape,在其中声明计算面积和体积的方法,用Point、Circle、Cylinder实现该接口,在类中详细定义接口中的方法。程序编码:public class InterfaceTest public static void main(String args) Point point=new Point(3,15
21、); Circle circle=new Circle(5.5,23,10); Cylinder cylinder=new Cylinder(10,6.5,20,20); Shape shapes=new Shape3; shapes0=point; shapes1=circle; shapes2=cylinder; String output=point.getName()+: +point.toString()+n+circle.getName()+: +circle.toString()+n+cylinder.getName()+: +cylinder.toString(); for(i
22、nt i=0; i=0 ? circleRadius:0); public double getRadius() return radius; public double area() return Math.PI*radius*radius; public String toString() return Center= +super.toString()+;tRadius= +radius; public String getName() return Circle; class Cylinder extends Circle protected double height; public
23、 Cylinder() super(); setHeight(0); public Cylinder(double cylinderHeight,double cylinderRadius,int xPos,int yPos) super(cylinderRadius,xPos,yPos); setHeight(cylinderHeight); public void setHeight(double cylinderHeight) height=(cylinderHeight=0 ? cylinderHeight:0); public double getHeight() return height; public double area() return 2*super.area()+2*Math.PI*radius*height; public double volume() return super.area()*height; public String toString() return super.toString()+; Height= +height; public String getName() return CyPositionlinder;
限制150内