Java实验报告.docx
《Java实验报告.docx》由会员分享,可在线阅读,更多相关《Java实验报告.docx(100页珍藏版)》请在冰豆网上搜索。
Java实验报告
网络编程技术
实验报告
专业:
软件工程
班级:
软件131
姓名:
陈万全
学号:
132852
指导教师:
石陆魁
实验一Java基本语法
实验目的:
了解Java的数据类型,掌握各种变量的声明方式,理解运算符的优先级,掌握Java基本数据类型、运算符与表达式,掌握顺序结构、选择结构和循环结构语法的程序设计方法。
实验要求:
1、编写一个声明Java不同数据类型变量的程序。
2、编写使用不同选择结构的程序。
3、编写使用不同循环结构的程序。
实验内容:
1、声明不同数据类型变量
编写声明不同数据类型变量的程序
publicclassSimpleTypes
{
publicstaticvoidmain(Stringargs[])
{
byteb=0x55;
shorts=0x55ff;
inti=1000000;
longl=0xfffL;
charc='c';
floatf=0.23F;
doubled=0.7E-3;
booleanbool=true;
System.out.println("b="+b);
System.out.println("s="+s);
System.out.println("i="+i);
System.out.println("l="+l);
System.out.println("c="+c);
System.out.println("f="+f);
System.out.println("d="+d);
System.out.println("bool="+bool);
}
}
2、使用选择结构
使用if…else语句
(1)编写源程序文件,代码如下。
publicclassTestIf
{
publicstaticvoidmain(Stringargs[])
{
booleanleap;
intyear=2005;
if((year%4==0&&year%100!
=0)||(year%400==0))
System.out.println(year+"年是闰年");
else
System.out.println(year+"年不是闰年");
year=2008;
if(year%4!
=0)
leap=false;
elseif(year%100!
=0)
leap=true;
elseif(year%400!
=0)
leap=false;
else
leap=true;
if(leap==true)
System.out.println(year+"年是闰年");
else
System.out.println(year+"年不是闰年");
year=2050;
if(year%4==0)
{
if(year%100==0){
if(year%400==0)
leap=true;
else
leap=false;
}
else
leap=false;
}
else
leap=false;
if(leap==true)
System.out.println(year+"年是闰年");
else
System.out.println(year+"年不是闰年");
}
}
2.编写程序用switch语句实现从键盘读入1,2,3时,屏幕提示不同信息
classSwitchTest
{
publicstaticvoidmain(Stringargs[])throwsjava.io.IOException
{
chara;
System.out.println("Enteranumberfrom1--3:
");
a=(char)System.in.read();
switch(a){
case'1':
System.out.println("winaCar!
");break;
case'2':
System.out.println("pickedthegoat");break;
case'3':
System.out.println("gettokeepyour100");break;
default:
System.out.println("entry");
}
}
}
3、使用循环结构*使用for语句1)程序源代码如下:
importjava.io.IOException;
publicclassTestFor{
publicstaticvoidmain(String[]args){
intfathr,cels;
System.out.println("CelsiusFahrenheit");
for(cels=0;cels<=100;cels++){
fathr=cels*9/5+32;
System.out.println(cels+""+fathr);
}
chara;
outer:
//thisisthelabelfortheouterloop
for(inti=0;i<10;i++){
for(intj=0;j<10;j++){
try{
a=(char)System.in.read();
if(a=='b')
breakouter;
if(a=='c')
breakouter;
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}
}
2)编译运行程序。
使用while语句1)程序源代码如下:
importjava.io.IOException;
publicclassTestWhile{
publicstaticvoidmain(String[]args)throwsIOException{
charch;
System.out.println("按1/2/3可得大奖!
");
System.out.println("按空格键后回车可推出循环操作");
while((ch=(char)System.in.read())!
=''){
System.in.skip
(2);//跳过回车键
switch(ch){
case'1':
System.out.println("恭喜得大奖,一辆汽车!
");
break;
case'2':
System.out.println("不错呀,你得到一台笔记本电脑!
");
break;
case'3':
System.out.println("没有白来,你得到一台冰箱!
");
break;
default:
System.out.println("真不幸,你没有奖品!
下次再来吧。
");
}
}
}
}
2)求1+2+…+100之和的程序如下:
publicclassTestDoWhile{
publicstaticvoidmain(String[]args){
intn=1,sum=0;
do{
sum+=n++;
}while(n<=100);
System.out.println("1+2+...+100="+sum);
}
}
3)多重循环,输出九九乘法表的程序。
publicclassTestMul{
publicstaticvoidmain(Stringargs[]){
inti,j,n=9;
System.out.print("********|");
for(i=1;i<=n;i++)
System.out.print(""+i);
System.out.print("\n--------|");
for(i=1;i<=n;i++)
System.out.print("---------");
System.out.println();
for(i=1;i<=n;i++)
{
System.out.print(""+i+"|");
for(j=1;j<=i;j++)
System.out.print(""+String.format("%-4d",i*j));
System.out.println();
}
}
}
实验二:
面向对象编程
一、实验目的:
通过编程和上机实验理解Java语言是如何体现面向对象编程基本思想,熟悉类的封装方法以及如何创建类和对象,熟悉成员变量和成员方法的特性,熟悉类的继承性和多态性的作用,熟悉包、接口的使用方法,掌握OOP方式进行程序设计的方法。
二、实验要求:
1、编写程序实现类的定义和使用。
2、编写不同成员和不同成员方法修饰方法的程序。
3、编写体现类的继承性(成员变量、成员方法、成员变量隐藏)的程序和多态性(成员方法重载、构造方法重载)的程序。
4、编写接口的定义和使用的程序。
5、编写包的定义和使用的程序。
三、实验内容、源程序和实验结果
1、类的定义和使用
1)定义一个满足如下要求的的Date类:
A.用下面的格式输出日期:
日/月/年
B.可运行在日期上加一天的操作
C.设置日期
用该类编写一个显示当前日期的程序。
源程序:
importjava.io.IOException;
publicclassDate{
privateintyear=2015,month=10,day=25;
publicstaticvoidmain(Stringargs[])throwsIOException{
Datedate=newDate();
date.Out(date.getYear(),date.getMonth(),date.getDay());
date.DateAddOneDay();
date.Out(date.getYear(),date.getMonth(),date.getDay());
date.SetDate();
date.Out(date.getYear(),date.getMonth(),date.getDay());
}
privatevoidOut(intyear,intmonth,intday){
System.out.println("输出时间为:
"+day+"/"+month+"/"+year);
}
privatevoidDateAddOneDay(){
if(month==2&&day==28){
if((year%4==0&&year%100!
=0)||(year%400==0)){
month=3;
day=1;
}
else{
day++;
}
}
elseif(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
if(day==31){
month++;
day=1;
}
else{
day++;
}
}
elseif(month==4||month==6||month==9||month==11){
if(day==30){
month++;
day=1;
}
else{
day++;
}
}
}
privatevoidSetDate()throwsIOException{
this.year=1993;
this.month=06;
this.day=01;
}
publicintgetYear(){
returnyear;
}
publicvoidsetYear(intyear){
this.year=year;
}
publicintgetMonth(){
returnmonth;
}
publicvoidsetMonth(intmonth){
this.month=month;
}
publicintgetDay(){
returnday;
}
publicvoidsetDay(intday){
this.day=day;
}
}
实验结果:
输出时间为:
25/10/2015
输出时间为:
26/10/2015
输出时间为:
1/6/1993
2)创建一个桌子Table类,该类中有桌子名称、重量、桌面宽度、长度及桌子高度属性。
其中有:
A.构造函数初始化所有数据成员
B.Area():
计算桌面的面积
C.Display():
在屏幕上输出所有数据成员的值
D.ChangeWeight(int):
改变桌子重量的函数
E.在main()中实现创建一个桌子对象,计算桌面的面积,改变桌子重量,并在屏幕上输出所有桌子数据成员的值。
源程序:
publicclassTable{
privateStringname;
privatedoubleweigh,width,length,heigh;
Table(){
name="饭桌";
weigh=10;
width=1;
length=2;
heigh=1.5;
}
publicvoidArea(){
doublearea=width*length;
System.out.println("table'sarea:
"+area+"m^2");
System.out.println();
}
publicvoidDisplay(){
System.out.println("table'sname:
"+name);
System.out.println("table'sweigh:
"+weigh+"kg");
System.out.println("table'swidth:
"+width+"m");
System.out.println("table'slength:
"+length+"m");
System.out.println("table'sheigh:
"+heigh+"m");
}
publicvoidChangeWeigh(intweigh){
this.weigh=weigh;
}
publicstaticvoidmain(Stringargs[]){
Tablet1=newTable();
t1.Area();
t1.ChangeWeigh(12);
t1.Display();
}
}
实验结果:
table'sarea:
2.0m^2
table'sname:
饭桌
table'sweigh:
12.0kg
table'swidth:
1.0m
table'slength:
2.0m
table'sheigh:
1.5m
2、修饰符的使用
有时需要公开一些变量和方法,有时需要禁止其他对象使用变量和方法,这时可以使用修饰符来实现这个目的。
常用的修饰符如下。
Public,private,protected,package,static,final,transient,volatile。
程序功能:
通过两个类StaticDemo、TestDemo说明静态变量/方法与实例变量/方法的区别,程序源代码如下。
publicclassStaticDemo{
staticintx;
inty;
publicstaticintgetX(){
returnx;
}
publicstaticvoidsetX(intnewX){
x=newX;
}
publicintgetY(){
returny;
}
publicvoidsetY(intnewY){
y=newY;
}
}
publicclassTestDemoextendsStaticDemo{
publicstaticvoidmain(Stringargs[]){
System.out.println("静态变量x="+StaticDemo.getX());
StaticDemoa=newStaticDemo();
StaticDemob=newStaticDemo();
a.setX
(1);
a.setY
(2);
b.setX(3);
b.setY(4);
System.out.println("静态变量a。
x="+a.getX());
System.out.println("静态变量a。
y="+a.getY());
System.out.println("静态变量b。
x="+b.getX());
System.out.println("静态变量b。
y="+b.getY());
}
}
对源程序进行编译,查错并运行。
System.out.println("实例变量y="+StaticDemo.getY());这句话是错误的。
main函数是static型的,所以main函数中不能直接调用非static型的方法getY(),只能先创建一个对象然后再调用。
以下是将Y和getY()都变成static型之后的运行结果。
实验结果:
静态变量x=0
静态变量a.x=3
静态变量a.y=2
静态变量b.x=3
静态变量b.y=4
3、继承和多态的作用
创建Rodent(啮齿动物):
Mouse(老鼠),Gerbil(沙鼠),Hamster(大颊鼠)等的一个继承分级结构。
在基础类中,提供适用于所有Rodent的方法,并在衍生类中覆盖它们,从而根据不同类型的Rodent采取不同的行动。
创建一个Rodent数组,在其中填充不同类型的Rodent,然后调用自己的基础类方法,看看会有什么情况发生。
要求把Rodent定义成为抽象类。
Rodent类中要求的成员变量有name,成员方法有getName(),setName(Stringname)和方法run()。
其中方法run()为抽象方法。
Mouse(老鼠)继承Rodent(啮齿动物),Gerbil(沙鼠),Hamster(大颊鼠)继承Mouse(老鼠)类。
源程序:
publicclassRodent{
publicvoidEat(){
System.out.println("RodentEatsomething");
}
publicstaticvoidmain(Stringargs[])
{
Rodenta[]=newRodent[3];
a[0]=newMouse();
a[1]=newGerbil();
a[2]=newHamster();
a[0].Eat();
a[1].Eat();
a[2].Eat();
}
}
classMouseextendsRodent{
publicvoidEat(){
System.out.println("MouseEatsomething");
}
}
classGerbilextendsRodent{
publicvoidEat(){
System.out.println("GerbilEatsomething");
}
}
classHamsterextendsRodent{
publicvoidEat(){
System.out.println("HamsterEatsomething");
}
}
实验结果:
4、接口的定义和使用
Java中的接口在语法上有些类似于类,它定义了若干的抽象方法和常量,形成一个属性集合,该属性集合通常代表了某一功能的实现。
它的主要作用是可以帮助我们实现类似于类的多重继承的功能。
Java中各类获取某一接口定义的功能,并不是通过直接继承这个接口中的域和方法来实现的,因为接口中的域都是常量,接口中的方法都是没有方法体的抽象方法,也就是说,接口定义的仅仅是实现某一特定功能的功能接口和规范,而并没有真正的实现这个功能,这个功能的真正实现实在“继承”了这个接口的各个类中完成的,有这些类来具体定义接口中各抽象方法的方法体。
故在Java中,通常把对接口功能的“继承”称为实现。
Java中声明接口的语法如下:
[public]Interface接口名[extends父接口名列表]
{//接口体:
//常量域声明
[public][static][final]域类型域名=常量值;
//抽象方法声明
[public][abstract][native]返回值方法名(参数列表)[throw异常列表];
}
某个类为接口中的抽象方法书写语句并定义实在的方法体,称为该类实现可这个接口。
程序如下:
/**
*CreatedbyAdministratoron2015/11/12.
*/
publicclassInterfaceTest{
publicstaticvoidmain(Stringargs[]){
doublex;
circley=newcircle
(2);
//y.circle
(2);
x=y.calculate_atea();
System.out.println("\n面积为:
"+x+"\n");
}
}
interfacecal_area{
doublePI=3.14;
doublecalculate_atea();
}
classcircleimplement