java实验答案.docx
《java实验答案.docx》由会员分享,可在线阅读,更多相关《java实验答案.docx(25页珍藏版)》请在冰豆网上搜索。
java实验答案
1、读app3_1b.java程序,写出执行结果:
classapp3_1b
{publicstaticvoidmain(String[]args)
{inti=0;
Stringgreetings[]={"Helloworld","NoImeanit","HELLOWORLD"};
while(i<4)
{try
{System.out.println(greetings[i]);
}
catch(ArrayIndexOutOfBoundsExceptione)
{System.out.println("Re_settingindexvalue");}
finally
{System.out.println("thisisalwaysprinted");}
i++;
}
}
}
2、读app3_2b.java程序,写出执行结果:
classCcircle
{privatedoublepi=3.14;
privatedoubleradius;
publicCcircle()
{System.out.println("Ccircle()constructorcalled");}
publicCcircle(doubler)
{System.out.println("Ccircle(doubler)constructorcalled");
radius=r;
}
publicvoidshow()
{System.out.println("area="+pi*radius*radius);}
}
classCcoinextendsCcircle
{privateintvalue;
publicCcoin()
{System.out.println("coin()constructorcalled");}
publicCcoin(doubler,intv)
{super(r);
System.out.println("Ccoin(doubler,intv)constructorcalled");
}
};
publicclassapp3_2b
{publicstaticvoidmain(String[]args)
{Ccoincoin1=newCcoin();
Ccoincoin2=newCcoin(2.5,10);
coin1.show();
coin2.show();
}
}
一、阅读程序、写出执行结果
1.Helloworld
thisisalwaysprinted
NoImeanit
thisisalwaysprinted
HELLOWORLD
thisisalwaysprinted
Re_settingindexvalue
thisisalwaysprinted
2Ccircle()constructorcalled
coin()constructorcalled
Ccircle(doubler)constructorcalled
Ccoin(doubler,intv)constructorcalled
area=0.0
area=19.625
ClassTestException
{publicstaticvoidmain(String[]args)
{try{arraySize();}
catch(IndexOutOfBoundsExceptione)
{System.out.println("IndexOutOfBoundsException");}
catch(NegativeArraySizeExceptione)
{System.out.println("NegativeArraySizeException");}
catch(Exceptione)
{System.out.println("Exception");}
finally{System.out.println("finally");}
System.out.println("outoftrycatch");
}
staticvoidarraySize()throwsNegativeArraySizeException
{
try{inta[]=newint[-3];}
catch(NegativeArraySizeExceptione)
{System.out.println("arraySize()throwNegativeArraySizeException");
throwe;}
}
}
补充第一节的内容
publicclassMyJavaClass
{inta;
myjavaclass(inta){this.a=a;}
publicvoidmain()
{myjavaclassc1=newMyJavaClass();
System.out.printIn(c1.a);
}
System.out.printIn("程序结束。
");
}
1.上面的程序有几处错误?
请依次改正。
2.这个程序的源代码应该保存为什么名字的文件?
3.请写出编译和运行这个文件的完整命令,注意给出环境变量的设置(假设源文件保存在d:
\java目录)
4.如果在程序最前面加入下面语句:
packagePkg;
该程序又该如何编译和运行?
答案:
答案
(1):
修改以后的程序为(粗体为修改过的地方)
publicclassMyJavaClass
{inta;
MyJavaClass(inta){this.a=a;}
publicStaticvoidmain(Stringargs[])
{MyJavaClassc1=newMyJavaClass
(2)//此处的实际参数可为任意整数;
System.out.println(c1.a);
System.out.println("程序结束。
");
}
}
答案
(2)
publicclassMyJavaClass
{inta;
intmyjavaClass(inta){this.a=a;returna;}//注此处函数返回值可为任意类型
publicstaticvoidmain(Stringargs[])
{MyJavaClassc1=newMyJavaClass();
System.out.println(c1.a);System.out.println("程序结束。
");
}
}
2、答案:
这个程序的源代码应该保存为MyJavaClass.java。
3、答案
(1)
setclass=c:
\j2sdk\bin
setclasspath=d:
\java
编译:
javacd:
\java\MyJavaClass.java
运行:
javaMyJavaClass
答案
(2)
编译:
c:
\j2sdk\bin\javacd:
\java\MyJavaClass.java
setclasspath=d:
\java
运行:
javaMyJavaClass
4、答案:
如果在程序最前面加入语句:
packagepkg;
环境变量同3、设置
编译:
javac-d.d:
\java\MyJavaClass.java
运行:
javapkg.MyJavaClass
I实验1答案
1-(1-1):
importjava.io.*;
classRandomMonth
{publicstaticvoidmain(Stringargs[])
{Stringmonths[]={"January","February","March",
"April","May","June","July",
"Aguest","September","October",
"November","December"};
inti;
i=(int)(Math.random()*12);
System.out.println(months[i]);
}
}
1-(1-2):
importjava.io.*;
importjava.util.*;
classRandomMonth2
{publicstaticvoidmain(Stringargs[])
{Stringmonths[]={"January","February","March",
"April","May","June","July",
"Aguest","September","October",
"November","December"};
Randomr=newRandom();
inti;
i=Math.abs(r.nextInt())%12;
System.out.println(months[i]);
}
}
1-
(2)
importjava.util.*;
classRandomPx
{publicstaticvoidmain(Stringargs[])
{intnum[]=newint[20];
inti,j,temp;
Randomr=newRandom();
for(i=0;i<20;i++)
num[i]=r.nextInt();
for(i=0;i<19;i++)
for(j=0;j<19-i;j++)
{if(num[j]>num[j+1])
{temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
for(i=0;i<20;i++)
{if(i%5==0)System.out.print("\n\r");
System.out.print(num[i]+"");
}
}
}
II实验2答案
2-
(1)
publicclassstudentx{
privateStringno;
privateStringclassno
privateStringname;
privateStringsex;
privateintage;
studentx(){
no="1";
classno="1";
name="lucy";
sex="male";
age=0;
}
Stringgetno(){
returnno;
}
Stringgetclassno(){
returnclassno;
}
Stringgetname(){
returnname;
}
intgetage(){
returnage;
}
voidupdateage(intnewage){
this.age=newage;
}
publicStringtoString(){
return(name+"学生:
"+"性别"+sex+",年龄"+age+",学号为"+no+",班级为"+classno);
}
publicstaticvoidmain(String[]args){
studentxp=newstudentx();
p.updateage(25);
System.out.println(p.getage());
System.out.println(p);
}
};
2-
(2)
importjava.io.*;
importjava.util.*;
classSchool
{staticfloatscoreLine;
staticvoidsetscoreLine(floatscore)
{scoreLine=score;}
staticfloatgetscoreLine()
{returnscoreLine;}
}
classStudent
{Stringname,id;
floatcomScore,score;
floatgetcomScore()
{returncomScore;}
floatgetscore()
{returnscore;}
Student(Stringname,Stringid,floatcomScore,floatscore)
{this.name=name;
this.id=id;
Score=comScore;
this.score=score;
}
}
classEnroll
{staticbooleanisEnroll(Students)
{if(s.getcomScore()>=School.getscoreLine()||
(s.getscore()>96&&s.getcomScore()>=300))
returntrue;
elsereturnfalse;
}
publicstaticvoidmain(Stringargs[])
{Students[]=newStudent[3];
Stringinfo[]=newString[4];
inti,j;
StringmessInfo;
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
StringTokenizerst;
School.setscoreLine(550);
for(i=0;i{try{messInfo=in.readLine();
st=newStringTokenizer(messInfo);
for(j=0;j{info[j]=st.nextToken();}
}catch(IOExceptionex){}
s[i]=newStudent(info[0],info[1],Float.parseFloat(info[2]),Float.parseFloat(info[3]));
}
for(i=0;iif(isEnroll(s[i]))
System.out.println(s[i].name+""+s[i].id+""+"被录取!
");
else
System.out.println(s[i].name+""+s[i].id+""+"没有录取!
");
}
}
2-(3)思考题:
classTestTriangle
{
publicstaticvoidmain(String[]args)
{
Trianglet=newTriangle(3,5,7);
System.out.println("area="+t.area());
}
}
classTriangle
{
privatedoublea,b,c;
Triangle(doublea,doubleb,doublec)
{doublemin;
if((a+b)<=c||(a+c)<=b||(b+c)<=a)
{min=a
a:
b;
min=minmin:
c;
a=min;
b=min;
c=min;
}
this.a=a;
this.b=b;
this.c=c;
}
doublearea()
{doubles;
s=(a+b+c)/2.0;
System.out.println(s);
return(Math.sqrt(s*(s-a)*(s-b)*(s-c)));
}
};
III实验3答案
3-
(1)
1、classPerson
{
publicStringname;
publicStringsex;
intage;
Person(Stringname,Stringsex,intage)
{this.name=name;
this.sex=sex;
this.age=age;
}
}
classStudentextendsPerson
{
publicStringsno;
publicintclassno;
publicvoidupdateAge(intage)
{this.age=age;
}
Student(Stringname,Stringsex,intage,Stringsno,intclassno)
{super(name,sex,age);
this.sno=sno;
this.classno=classno;
}
publicStringtoString()
{System.out.println("name="+name+"sex="+sex+"age="+age+"
sno="+sno+"classno"+classno+"");
return"name="+name+"sex="+sex+"age="+age+"sno="+sno+"classno"+classno+"";
}
};
publicclassTestStudent
{
publicstaticvoidmain(String[]args)
{Students1=newStudent("susan","女",20,"20031015123",2);
s1.updateAge(21);
s1.toString();
}
}
3-(3)
importjava.util.*;
abstractclassEmployee
{Stringname;
longno;
staticlongid=1000;
abstractdoublegetPay();
Employee(Stringname){this.name=name;id=id+1;}
};
classManagerextendsEmployee
{
doublepay=8000;
doublegetPay(){return8000;}
Manager(Stringname)
{super(name);
no=id;
}
publicStringtoString(){return"name:
"+name+"no:
"+no+"pay:
8000";}
};
classSaleextendsEmployee
{longsaleNum;
doublegetPay(){returnsaleNum*0.4;}
Sale(Stringname,longsaleNum)
{super(name);
no=id;
this.saleNum=saleNum;
}
publicStringtoString()
{return"name:
"+name+"no:
"+no+"pay:
"+getPay();}
}
classTechextendsEmployee
{longsalaryHours;
doublegetPay()
{returnsalaryHours*100;}
Tech(Stringname,longsalaryHours)
{super(name);
no=id;
this.salaryHours=salaryHours;}
publicStringtoString()
{return"name:
"+name+"no:
"+no+"pay:
"+getPay();}
};
classEmployApp
{
publicstaticvoidmain(Stringargs[])
{
Vectorv=newVector();
Employeeem;
doublenumPay=0;
v.addElement(newSale("liping",10000));
v.addElement(newSale("susan",12000));
v.addElement(newTech("linda",40));
v.addElement(newTech("write",20));
v.addElement(newManager("gucci"));
v.addElement(newManager("jessica"));
Enumeratione=v.elements();
while(e.hasMoreElements())
{
em=(Employee)e.nextElement();
numPay=numPay+em.getPay();
System.out.println(em);
}
System.out.println("总工资:
"+numPay);
}
};
实验4答案
4-
(1)
importjava.lang.*;
importjava.io.*;
classTestException
{
publicstaticvoidmain(String[]args)
{
try{
outofBound();
arraySize();
nullPointer();
}
catch(NegativeArraySizeExceptione)
{System.out.println(e.toString());}
}
staticvoidoutofBound()
{
try{
inti;inta[];
a=newint[10];
for(i=0;i<11;i++)
{a[i]=i;
System.out.println("a["+i+"]="+a[i]);
}
}
catch(IndexOutOfBoundsExceptione)
{System.out.pri