java实验答案13Word格式文档下载.docx
《java实验答案13Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《java实验答案13Word格式文档下载.docx(12页珍藏版)》请在冰豆网上搜索。
![java实验答案13Word格式文档下载.docx](https://file1.bdocx.com/fileroot1/2022-11/23/0a745acb-8a2f-4f20-b416-bfae3321724a/0a745acb-8a2f-4f20-b416-bfae3321724a1.gif)
publicstaticvoidmain(Stringargs[])
{MyJavaClassc1=newMyJavaClass();
System.out.println(c1.a);
2、答案:
这个程序的源代码应该保存为MyJavaClass.java。
3、答案
(1)
setclass=c:
\j2sdk\bin
setclasspath=d:
\java
编译:
javacd:
\java\MyJavaClass.java
运行:
javaMyJavaClass
答案
(2)
c:
\j2sdk\bin\javacd:
setclasspath=d:
4、答案:
如果在程序最前面加入语句:
packagepkg;
环境变量同3、设置
javac-d.d:
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.util.*;
classRandomMonth2
{publicstaticvoidmain(Stringargs[])
{Stringmonths[]={"
Randomr=newRandom();
i=Math.abs(r.nextInt())%12;
1-
(2)
classRandomPx
{intnum[]=newint[20];
inti,j,temp;
Randomr=newRandom();
for(i=0;
i<
20;
i++)
num[i]=r.nextInt();
19;
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;
{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="
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)
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;
s.length;
i++)
{try{messInfo=in.readLine();
st=newStringTokenizer(messInfo);
for(j=0;
info.length;
{info[j]=st.nextToken();
}catch(IOExceptionex){}
s[i]=newStudent(info[0],info[1],Float.parseFloat(info[2]),Float.parseFloat(info[3]));
for(i=0;
if(isEnroll(s[i]))
System.out.println(s[i].name+"
+s[i].id+"
+"
被录取!
else
没有录取!
2-(3)思考题:
classComplex
{doubleRealPart;
doubleImageinPart;
Complex(doublerp,doubleip)
{RealPart=rp;
ImageinPart=ip;
ComplexcomplexAdd(Complexa)
{Complextemp;
temp=newComplex(RealPart+a.RealPart,ImageinPart+a.ImageinPart);
returntemp;
ComplexcomplexMinus(Complexa)
{Complextemp;
temp=newComplex(RealPart-a.RealPart,ImageinPart-a.ImageinPart);
publicStringtoString()
{if(ImageinPart<
0)
return(RealPart+"
-"
+Math.abs(ImageinPart)+"
i"
else
+ImageinPart*+"
}
classComplexApp
{charoperator;
try{
Complexc1=newComplex(1,2);
Complexc2=newComplex(3,4);
ComplexcomplexResult;
operator=(char)System.in.read();
switch(operator)
{case'
+'
:
complexResult=plexAdd(c2);
break;
case'
-'
complexResult=plexMinus(c2);
}
System.out.println(complexResult);
catch(IOExceptione)
{System.out.println(e);
III实验3答案
3-
(1)
1、classPerson
{
publicStringname;
publicStringsex;
intage;
Person(Stringname,Stringsex,intage)
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;
{System.out.println("
+name+"
sex="
+sex+"
age="
+age+"
sno="
+sno+"
classno"
+classno+"
return"
sno="
publicclassTestStudent
publicstaticvoidmain(String[]args)
{Students1=newStudent("
susan"
女"
20,"
20031015123"
2);
s1.updateAge(21);
s1.toString();
3-(3)
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:
no:
pay:
8000"
classSaleextendsEmployee
{longsaleNum;
doublegetPay(){returnsaleNum*0.4;
Sale(Stringname,longsaleNum)
{super(name);
this.saleNum=saleNum;
{return"
+getPay();
classTechextendsEmployee
{longsalaryHours;
doublegetPay()
{returnsalaryHours*100;
Tech(Stringname,longsalaryHours)
this.salaryHours=salaryHours;
{return"
classEmployApp
{
Vectorv=newVector();
Employeeem;
doublenumPay=0;
v.addElement(newSale("
liping"
10000));
12000));
v.addElement(newTech("
linda"
40));
write"
20));
v.addElement(newManager("
gucci"
));
jessica"
Enumeratione=v.elements();
while(e.hasMoreElements())
{
em=(Employee)e.nextElement();
numPay=numPay+em.getPay();
System.out.println(em);
System.out.println("
总工资:
+numPay);