网络编程实验报告Word文档格式.docx
《网络编程实验报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《网络编程实验报告Word文档格式.docx(62页珍藏版)》请在冰点文库上搜索。
i="
+i);
l="
+l);
c="
+c);
f="
+f);
d="
+d);
bool="
+bool);
}
}
///////////////////////////////////////////////////////////////////////////////////////
publicclassTestif
booleanleap;
intyear=2014;
if((year%4==0&
&
year%100!
=0)||(year%400==0))//
System.out.println(year+"
年是闰年"
);
else
年不是闰年"
//方法二///////////////////////////////////////
year=2008;
if(year%4!
=0)
leap=false;
elseif(year%100!
leap=true;
elseif(year%400!
else
if(leap==true)
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;
}
//使用switch语句/////////////////////////////////////
//1.编写程序用Switch语句实现从键盘上都1,2,3时,屏幕提示不同的信息
importjava.io.*;
classSwitchTest
publicstaticvoidmain(Stringargs[])throwIOException
chara;
Enteranumberfrom1--3:
"
a=(char)System.in.read();
switch(a)
{
case'
1'
:
System.out.println("
winaCar!
break;
case'
2'
pickedthegoat"
3'
gettokeepyour100!
default:
System.out.println("
entry"
//for循环语句
classForTest
publicstaticvoidmain(Stringargs[])throwsIOException
intfahr,cels;
System.out.println("
CelsiusFahrenheit"
for(cels=0;
cels<
=100;
cels+=5)
{
fahr=cels*9/5+32;
System.out.println(cels+"
"
+fahr);
chara;
outer:
//thisisthelablefortheouterloop
for(inti=0;
i<
10;
i++)
for(intj=0;
j<
j++){
a=(char)System.in.read();
if(a=='
b'
)
breakouter;
continueouter;
//while循环语句////////////////////////////////////////////
classWhileTest
publicstaticvoidmain(String[]args)throwsIOException
charch;
按1/2/3数字可获大奖!
按空格键后回车可退出循环操作"
while((ch=(char)System.in.read())!
='
'
System.in.skip
(2);
//跳过回车键
switch(ch){
case'
恭喜你获得大奖,一辆汽车"
不错呀,你得到一台笔记本电脑"
case'
没白来,你得到一台冰箱"
default:
System.out.println("
真不兴,你没有奖品!
下次再来"
//多重循环
publicclassMul99
publicstaticvoidmain(String[]args)
{inti,j,n=9;
System.out.print("
*|"
for(i=1;
\n-----|"
for(i=1;
---"
System.out.println();
=n;
i++){
System.out.print("
+i+"
|"
for(j=1;
=i;
j++)
+i*j);
实验感想:
实验二面向对象编程试验
通过编程和上机实验理解Java语言是如何体现面向对象编程基本思想,熟悉类的封装方法以及如何创建类和对象,熟悉成员变量和成员方法的特性,熟悉类的继承性和多态性的作用,熟悉包、接口的使用方法,掌握OOP方式进行程序设计的方法。
1、编写程序实现类的定义和使用。
2、编写不同成员和不同成员方法修饰方法的程序。
3、编写体现类的继承性(成员变量、成员方法、成员变量隐藏)的程序和多态性(成员方法重载、构造方法重载)的程序。
4、编写接口的定义和使用的程序。
5、编写包的定义和使用的程序。
实验内容-
1.日期类输出当前日期
publicclassDate
privateintyear,month,day;
//privatestaticthisYear;
publicDate(inty,intm,intd){
this.year=y;
this.month=m;
this.day=d;
publicvoidread(inty,intm,intd)
inty=System.in.read();
intm=System.in.read();
intd=System.in.read();
publicvoidset(inty,intm,intd)
if(m>
=1&
m<
=12)
returnm;
}else{
该日期错误"
if(d>
d<
=31)
returnd;
}else
publicvoidshow()
System.out.println(this.day+"
/"
+this.month+"
+this.year);
publicstaticvoidmain(Stringargs[])throwsIOException
Dates=newDate();
s.read();
s.set();
s.show();
//////////////////////////////////////////////////////////////////////////////////////
2.桌子类
publicclassTable
{
privateStringname;
privateintlongs;
privateintweight;
privateintheight;
privateintwidth;
publicTable(Stringn,intl,intwe,inth,intwi)
this.name=n;
this.longs=l;
this.weight=we;
this.height=h;
this.width=wi;
intArea()
returnthis.longs*this.width;
publicvoidDisplay()
桌子名称:
+this.name+"
\n"
+"
重量:
+this.weight+"
桌面宽度:
+this.width+"
桌面长度:
+this.longs+"
桌子高度:
+this.height+"
桌子面积"
+this.Area());
publicvoidChangeWeight(ints)
this.weight=s;
publicstaticvoidmain(Stringargs[])
TableT=newTable("
xiaozuo"
9,3,5,3);
T.Area();
T.Display();
T.ChangeWeight(90);
//////////////////////////////////////////////////////////////////////////////////////////////
classStaticDemo
staticintx;
inty;
publicstaticintgetX(){
returnx;
publicstaticvoidsetX(intnewX){
x=newX;
publicintgetY(){
returny;
publicvoidsetY(intnewY){
y=newY;
publicclassTestDemo
publicstaticvoidmain(Stringargs[]){
静态变量"
+StaticDemo.getX());
实例变量"
+StaticDemo.getY());
//非法编译时将出错
StaticDemoa=newStaticDemo();
StaticDemob=newStaticDemo();
a.setX
(1);
a.setY
(2);
b.setX(3);
b.setY(4);
静态变量a.x="
+a.getX());
静态变量a.y="
+a.getY());
静态变量b.x="
+b.getX());
+b.getY());
3.继承和多态的作用
/*
Date:
2014.11.239:
56:
00
@author:
Devon
function:
功能
•创建Rodent(啮齿动物):
Mouse(老鼠),Gerbil(沙鼠),Hamster(大频鼠)等
的一个继承分级结构。
在基础类中,提供适用于所有Rodent的方法,并在衍生类中覆盖它们,
从而根据不同类型的Rodent釆取不同的行动。
创建一个Rodent数组,在其中填充不同类型的Rodent,
然后调用自己的基础类方法,看看会有什么情况发生。
*/
classRodent
Rodentr[]=newRodent[4];
publicvoidTowTooth()
Rodentrodent=newRodent();
Mouthmouth=newMouth();
Gerbilgerbil=newGerbil();
Hamsterhamster=newHamster();
r[0]=rodent,r[1]=mouth,r[2]=gerbil,r[3]=hamster;
for(inti=0,i<
r.lenth,i++)
r[i].TowTooth();
classMouaeextendsRodent
classGerbilextendsMouse
classHamsterextendsGerbil
4、接口的定义和使用
interfaceTest.java
publicclassInterfaceTest
doublex;
circley=circle;
y.circle
(2);
x=y.calculate.area();
\n面积为:
+x+"
interfacecal_area
doublePI=3.14;
doubleclaculate_area();
classcircleimplementscla_area
doubler;
circle(doubler)
this.r=r;
//实现接口中的抽象方法,求圆面积
publicdoublecalculate_area()
returnPI*r*r;
5、包的定义和使用•创建自定义包Mypackage
///////////////////////////////////////////////////
packageMvpackage;
//声明存放类的包
importjava.util.*;
//引用java.util包
publicclassTest_YMD{
publicstaticvoidmain(String[]args){}
publicTest_YMD(inty,intm,intd){
year=y;
month=(((m>
=1)&
(m<
=12))?
m:
1);
day=(((d>
(d<
=31))?
d:
publicTest_YMD(){
this(0,0,0);
publicstaticintthisyear(){
returnCalendar.getInstance().get(Calendar.YEAR);
//返回当年的年份
publicintyear(){
returnyear;
//返回年份
publicStringtoString(){
returnyear+"
-n"
+month+"
n-"
+day;
//返回转化为字符串的年-月-日
//////////////////////////////////////////////////////
importMypackage.KY4_1_YMD;
//引用Mypackage包中的KY4_1_YMD类
publicclassYMD_2{
privateTest_YMDbirth;
YMD_2a=newYMD_2("
张驰"
1990,1,11);
a.output();
publicYMD_2(Stringnl,Test_YMDdl){
name=nl;
birth=dl;
publicYMD_2(Stringnl,inty,intm,intd){
this(nl,newTest_YMD(y,m,d));
//初始化变量与对象
publicintage()//计算年龄
returnTESt_YMD.thisyear()-birth.yearO;
//返回当前年与出生年的差即年龄
publicvoidoutput()
姓名:
+name);
出生日期:
+birth.toString());
今年年龄:
+age());
实验三异常处理程序设计
了解Java中异常处理(exception)的作用及常用的异常类,掌握异常处理的设计方法。
理解系统异常处理的机制和创建自定义异常的方法。
ClassInsufficientFoundsExceptionextendsException
privateBankAccountm-ba;
privatedoublegetAmount;
InsufficientFoundsException(BankAccountba,doubledAmount)
super("
Insufficientfoundsinaccount"
m-ba=ba;
getAmount=dAmount;
publicStringtoString()
StringBuffersb=newStringBuffer();
sb.append("