JAVA程序设计实验指导书.docx

上传人:b****4 文档编号:3859167 上传时间:2023-05-06 格式:DOCX 页数:18 大小:18.04KB
下载 相关 举报
JAVA程序设计实验指导书.docx_第1页
第1页 / 共18页
JAVA程序设计实验指导书.docx_第2页
第2页 / 共18页
JAVA程序设计实验指导书.docx_第3页
第3页 / 共18页
JAVA程序设计实验指导书.docx_第4页
第4页 / 共18页
JAVA程序设计实验指导书.docx_第5页
第5页 / 共18页
JAVA程序设计实验指导书.docx_第6页
第6页 / 共18页
JAVA程序设计实验指导书.docx_第7页
第7页 / 共18页
JAVA程序设计实验指导书.docx_第8页
第8页 / 共18页
JAVA程序设计实验指导书.docx_第9页
第9页 / 共18页
JAVA程序设计实验指导书.docx_第10页
第10页 / 共18页
JAVA程序设计实验指导书.docx_第11页
第11页 / 共18页
JAVA程序设计实验指导书.docx_第12页
第12页 / 共18页
JAVA程序设计实验指导书.docx_第13页
第13页 / 共18页
JAVA程序设计实验指导书.docx_第14页
第14页 / 共18页
JAVA程序设计实验指导书.docx_第15页
第15页 / 共18页
JAVA程序设计实验指导书.docx_第16页
第16页 / 共18页
JAVA程序设计实验指导书.docx_第17页
第17页 / 共18页
JAVA程序设计实验指导书.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

JAVA程序设计实验指导书.docx

《JAVA程序设计实验指导书.docx》由会员分享,可在线阅读,更多相关《JAVA程序设计实验指导书.docx(18页珍藏版)》请在冰点文库上搜索。

JAVA程序设计实验指导书.docx

JAVA程序设计实验指导书

 

JAVA程序设计(公选课)实验指导书

 

实验1

实验目的:

掌握如何编写、编译、运行Java应用程序。

实验内容:

●编写一个提示“Javaisinteresting!

 ”信息的应用程序。

实验过程:

1、编写一个提示“Javaisinteresting!

 ”信息的应用程序

具体实验内容:

编写一个应用程序,当运行时,会出现一个提示信息对话框,其中显示信息“Javaisinteresting!

 ”。

编写代码:

(可使用记事本)

importjavax.swing.JOptionPane;

publicclassDemo1

{

publicstaticvoidmain(Stringargs[])

{

JOptionPane.showMessageDialog(null,"Javaisinteresting!

");

System.exit(0);

}

}

实验2:

实验目的:

掌握使用图形化界面的形式,正确使用表达式和流控制语句进行编程,掌握break语句和continue语句如何在多重循环语句中正确使用。

熟练掌握各种运算符和表达式的使用法。

实验内容:

●编程显示星形符号组成的图形:

矩形和下三角。

●开发一个具有数字计算功能的应用程序。

实验过程:

1、编程显示星形符号组成的图形:

矩形和下三角

编写代码:

importjavax.swing.JOptionPane;

publicclassLabelTest

{

publicstaticvoidmain(Stringargs[])

{

Stringoutput="Thefirstgraph:

\n";

stop:

{

for(introw=1;row<=15;row++)

{

for(intcolumn=1;column<=5;column++)

{

if(row==5)

breakstop;

output+="*";

}

output+="\n";

}

output+="\n\n\n";

}

output+="Thesecondgraph:

";

nextRow:

for(introw=1;row<=5;row++)

{

output+="\n";

for(intcolumn=1;column<=10;column++)

{

if(column>row)

continuenextRow;

output+="*";

}

}

JOptionPane.showMessageDialog(null,output,"Testingbreakwithalabel",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

将以上代码保存为文件名为LabelTest.java的文件。

2、开发一个具有数字计算功能的应用程序

实验内容:

首先产生两个随机数作为操作数,对它们分别进行加、减、乘、除和取余运算,将结果以图形化的形式显示出来。

分两种情况进行:

两个操作数都是整数类型和两个操作数都是实数类型。

另外,使用扩展运算符进行以上运算,验证运算结果是否正确。

编写代码:

importjavax.swing.JOptionPane;

importjava.util.*;

publicclassMathOps

{

publicstaticvoidmain(Stringargs[])

{

Randomrand=newRandom();

inti,j,k;

i=rand.nextInt(100)+1;

j=rand.nextInt(100)+1;

Stringoutput="TowIntegerOperands:

\n"+"i="+i+""+"j="+j;

k=i+j;

output+="\nAfteraddingoperation,result:

\n"+"i+j="+k;

k=i-j;

output+="\nAftersubtractingoperation,result:

\n"+"i-j="+k;

k=i*j;

output+="\nAftermultiplyingoperation,result:

\n"+"i*j="+k;

k=i/j;

output+="\nAfterdividingoperation,result:

\n"+"i/j="+k;

k=i%j;

output+="\nAftercaculatingremainderoperation,result:

\n"+"i%j="+k;

floatu,v,w;

u=rand.nextFloat();

v=rand.nextFloat();

output="\n\nTowFloatOperands:

\n"+"u="+u+""+"v="+v;

w=u+v;

output+="\nAfteraddingoperation,result:

\n"+"u+v="+w;

w=u-v;

output+="\nAftersubtractingoperation,result:

\n"+"u-v="+w;

w=u*v;

output+="\nAftermultiplyingoperation,result:

\n"+"u*v="+w;

w=u/v;

output+="\nAfterdividingoperation,result:

\n"+"u/v="+w;

output+="\n\nExtendedOperationresult:

";

u+=v;

output+="\nAfterextendedaddingoperation(u+=v),result:

\n"+"u="+u;

u-=v;

output+="\nAfterextendedsubtractingoperation(u-=v),result:

\n"+"u="+u;

u*=v;

output+="\nAfterextendedmultiplyingoperation(u*=v),result:

\n"+"u="+u;

u/=v;

output+="\nAfterextendeddividingoperation(u/=v),result:

\n"+"u="+u;

JOptionPane.showMessageDialog(null,output,"ArithmeticOperationDemo\n",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

实验3

实验目的:

掌握定义类的方法,特别是要掌握如何编写构造函数和main ()函数,怎样创建和使用类的对象。

实验内容:

●创建一个日期类。

●创建一个图书类。

实验过程:

1、创建日期类

具体实验内容:

创建一个日期类,使得该类能够对日期的合法性进行验证,并将验证的结果以图形化的形式对日期信息进行正确显示。

程序编码:

importjavax.swing.JOptionPane;

publicclassDateTest

{

privateintmonth;

privateintday;

privateintyear;

privateStringvalidateDateString=newString("");

publicDateTest(intmonth,intday,intyear)

{

validateDateString+="\nDateobjectconstructorfordate"+month+"/"+day+"/"+year+"iscalling!

";

if(month>0&&month<=12)

{

this.month=month;

validateDateString+="\nThemonthisvalid.";

}

else

{

month=1;

validateDateString+="\nMonth"+month+"isinvalid.Setitsvaluetomonth1.";

}

this.year=year;

this.day=checkDay(day);

}

privateintcheckDay(inttestDay)

{

intdaysPerMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

if(testDay>0&&testDay<=daysPerMonth[month])

{

validateDateString+="\nThedatisvalid.";

returntestDay;

}

if(month==2&&testDay==29&&(year%400==0||(year%4==0&&year%100!

=0)))

{

validateDateString+="\nThedatisvalid.";

returntestDay;

}

validateDateString+="\nDay"+testDay+"isinvalid.Setitsvaluetoday1.";

return1;

}

publicStringtoString()

{

returnmonth+"/"+day+"/"+year;

}

publicStringgetValidateDateString()

{

returnvalidateDateString;

}

publicstaticvoidmain(String[]args)

{

DateTestdate1=newDateTest(3,21,2004);

DateTestdate2=newDateTest(2,29,2004);

DateTestdate3=newDateTest(21,2,2003);

JOptionPane.showMessageDialog(null,date1.getValidateDateString()+date2.getValidateDateString()+date3.getValidateDateString(),

"DateTestDemo",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

2、创建一个图书类

具体实验内容:

创建一个图书类,为该类定义属性,并定义两个构造函数,一个是默认的构造函数,一个是带参数的构造函数,分别用这两个构造函数创建两个对象,将对象信息以Applet的形式打印显示。

程序编码:

//

//

importjava.awt.*;

importjava.awt.Graphics;

importjava.util.*;

importjavax.swing.JApplet;

publicclassClassAppletextendsJApplet

{

privateBookbook1,book2;

publicvoidinit()

{

book1=newBook();

book1.setName("Java2");

book1.setAuthor("houjunjie");

book1.setType("Computerscience");

book1.setDate(newGregorianCalendar(2004,8,15));

book2=newBook("VisualC++.net","Baoyijun","Computerscience",newGregorianCalendar(2003,10,23));

}

publicvoidpaint(Graphicsg)

{

super.paint(g);

g.setColor(newColor(255,0,0));

g.drawString("下面是两本书的信息:

",120,40);

g.drawString("第一本书的信息:

",60,60);

g.setColor(newColor(0,0,255));

g.drawString("书名:

"+book1.getName(),60,80);

g.drawString("作者:

"+book1.getAuthor(),60,100);

g.drawString("类别:

"+book1.getType(),60,120);

g.drawString("出版日期:

"+book1.getDate().get(Calendar.MONTH)+"/"+book1.getDate().get(Calendar.YEAR),60,140);

g.setColor(newColor(255,0,0));

g.drawString("第二本书的信息:

",215,60);

g.setColor(newColor(0,0,255));

g.drawString("书名:

"+book2.getName(),215,80);

g.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);

}

}

 

classBook

{

privateStringbookName;

privateStringauthor;

privateStringtype;

privateGregorianCalendarpublishedDate;

publicBook()

{

this("unknown","unknown","unknown",newGregorianCalendar());

}

publicBook(StringbookName,Stringauthor,Stringtype,GregorianCalendarpublishedDate)

{

this.bookName=bookName;

this.author=author;

this.type=type;

this.publishedDate=publishedDate;

}

publicvoidsetName(Stringname)

{

bookName=name;

}

publicStringgetName()

{

returnbookName;

}

publicvoidsetAuthor(StringbookAuthor)

{

author=bookAuthor;

}

publicStringgetAuthor()

{

returnauthor;

}

publicvoidsetType(StringbookType)

{

type=bookType;

}

publicStringgetType()

{

returntype;

}

publicvoidsetDate(GregorianCalendardate)

{

publishedDate=date;

}

publicGregorianCalendargetDate()

{

returnpublishedDate;

}

}

实验4

实验目的:

了解类的继承和多态的含义,了解使用接口的编程思想,掌握使用接口的编程方法.

实验内容:

使用类的继承和接口综合举例.

实验过程:

具体实验内容:

定义一个接口Shape,在其中声明计算面积和体积的方法,用Point、Circle、Cylinder实现该接口,在类中详细定义接口中的方法。

程序编码:

publicclassInterfaceTest

{

publicstaticvoidmain(Stringargs[])

{

Pointpoint=newPoint(3,15);

Circlecircle=newCircle(5.5,23,10);

Cylindercylinder=newCylinder(10,6.5,20,20);

Shapeshapes[]=newShape[3];

shapes[0]=point;

shapes[1]=circle;

shapes[2]=cylinder;

Stringoutput=point.getName()+":

"+point.toString()+"\n"+circle.getName()+":

"+circle.toString()+"\n"+cylinder.getName()+":

"+cylinder.toString();

for(inti=0;i

{

output+="\n\n"+shapes[i].getName()+":

"+shapes[i].toString()+"\nArea="+shapes[i].area()+"\t\tVolume="+shapes[i].volume();

}

System.out.println();

System.out.println(output);

}

}

interfaceShape

{

publicabstractdoublearea();

publicabstractdoublevolume();

publicabstractStringgetName();

}

classPointimplementsShape

{

protectedintxPosition,yPosition;

publicPoint()

{

setPoint(0,0);

}

publicPoint(intxPos,intyPos)

{

setPoint(xPos,yPos);

}

publicvoidsetPoint(intxPos,intyPos)

{

xPosition=xPos;

yPosition=yPos;

}

publicintgetxPosition()

{

returnxPosition;

}

publicintgetyPosition()

{

returnyPosition;

}

publicStringtoString()

{

return"("+xPosition+","+yPosition+")";

}

publicdoublearea()

{

return0.0;

}

publicdoublevolume()

{

return0.0;

}

publicStringgetName()

{

return"Point";

}

}

classCircleextendsPoint

{

protecteddoubleradius;

publicCircle()

{

setRadius(0);

}

publicCircle(doublecircleRadius,intxPos,intyPos)

{

super(xPos,yPos);

setRadius(circleRadius);

}

publicvoidsetRadius(doublecircleRadius)

{

radius=(circleRadius>=0?

circleRadius:

0);

}

publicdoublegetRadius()

{

returnradius;

}

publicdoublearea()

{

returnMath.PI*radius*radius;

}

publicStringtoString()

{

return"Center="+super.toString()+";\tRadius="+radius;

}

publicStringgetName()

{

return"Circle";

}

}

classCylinderextendsCircle

{

protecteddoubleheight;

publicCylinder()

{

super();

setHeight(0);

}

publicCylinder(doublecylinderHeight,doublecylinderRadius,intxPos,intyPos)

{

super(cylinderRadius,xPos,yPos);

setHeight(cylinderHeight);

}

publicvoidsetHeight(doublecylinderHeight)

{

height=(cylinderHeight>=0?

cylinderHeight:

0);

}

publicdoublegetHeight()

{

returnheight;

}

publicdoublearea()

{

return2*super.area()+2*Math.PI*radius*height;

}

publicdoublevolume()

{

returnsuper.area()*height;

}

publicStringtoString()

{

returnsuper.toString()+";Height="+height;

}

publicStringgetName()

{

return"CyPositionlinder";

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2