java程序設(shè)計課后習題答案_第1頁
java程序設(shè)計課后習題答案_第2頁
java程序設(shè)計課后習題答案_第3頁
java程序設(shè)計課后習題答案_第4頁
java程序設(shè)計課后習題答案_第5頁
已閱讀5頁,還剩278頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

序言

先說在前面,如果本電子版侵犯了原書的版權(quán),請立即刪除。本電子版用于個人學習交流,嚴禁用于

商業(yè)出版。

本電子版答案的java源代碼源自于《Java程序設(shè)計(第二版)題解與上機指導(dǎo)》,本人對該書的全部源代

碼都進行了編輯,編譯,運行等工作,修改了其中的錯誤源代碼。不過有些題目的解答實在是莫米其妙本

人也無法理解原作者的意思,本人會在最后給出其他例子用來彌補此之不足。其中加入了大量的注釋說明

語句,這些是原書所沒有的。這些代碼在jbuilder9.0卜.調(diào)試通過,部分的是在jbuilder2006卜調(diào)試通過

的,還有在jcreator下調(diào)試通過的。其中大部分源代碼在jbuilder9.0下就能編譯運行。還有一些源代碼

是需要更高的jdk版本才能編譯運行,所以這些源代碼就在jbuilder2006卜.編譯運行。Jbuilder9.0自帶

的jdk是jdkl.4的,jbuilder2006自帶的jdk是jdkl.5的。特別說明的是jbuilder2006所支持的字體

特別少,要想加入宋體等其他字體的方法請參看839682048的空間。

ed2k:〃Ifilel%5BJBuiIder.2006.%E4%BC%81%E4%B8%9A%E5%AE%8C%E6%95%B4%E7%89%88%5D.JB2006CD1%5B

free_guru%5D.rar(68782371043a2e30e546edf87edcbe426fb9fe600|h=sbmjqn54tio7msl331vabc4jtuz41g

cnl/

以上是jbuilder2006Windo\vs平臺的下載地址,復(fù)制以上代碼,打開迅雷,新建,就可以下載了,里面有

破解補丁。安裝需要虛擬光驅(qū)。

眾所周知,程序設(shè)計題目的答案是不唯一的,讀者發(fā)揮的潛能很大。本電子版是為了方便讀者,使其免去

輸代碼這一關(guān)鍵步驟。因為原書的作者的水平有限,本人的水平也有限,難免有錯誤和不妥之處,實現(xiàn)的

代碼也不一定是最優(yōu)的,讀者可以參考本電子版答案和其他參考書的內(nèi)容,給出更全面的答案,懇請廣大

讀者特別是同行專家批評指正。

(通過閱讀原書,我發(fā)現(xiàn)作者編寫此書的態(tài)度不是很認真,不像此書同系列的譚浩強的《c程序設(shè)計》一

歸那樣認真嚴謹。不過還是要感謝原書作者。在此也懇請所有出書的人要抱著?種對讀者,特別是對中國

的讀者負責的認真態(tài)度。)

如果你有什么疑問可以和本人聯(lián)系839682048<>。

最后,在此祝愿所有讀者,特別是中國的讀者:學業(yè)有成,事業(yè)更上一層樓。讓我們一起為中國的計算機

發(fā)展而努力奮斗吧?。?!

〃習題2.2

importjava.util.*;

classMyDate{

privateintyear;

privateintmonth;

privateintday;

publicMyDate(inty,intm,intd){〃構(gòu)造函數(shù),構(gòu)造方法

year=y;

month=m;

day=d;

}//endpublicMyDate(inty,intin,intd)

publicintgetYearO{〃返回年

returnyear;

)//endgetYear()

publicintgetMonth。{〃返回月

returnmonth;

}//endgetMonthO

publicintgetDay(){〃返回日

returnday;

}//endgetDay0

}//endclassMyDate

classEmployee{

privateStringname;

privatedoublesalary;

privateMyDatehireDay;

publicEmployee(Stringn,doubles,MyDated){

name=n;

salary二s;

hireDay=d;

}//endpublicEmployee(Stringn,doubles,MyDated)

publicvoidprint(){

System.out.printin(〃名字:"+name+"\n工資:"+salary+"\n雇傭年份:"+hireYear()+"\n");

}//endprint()

publicvoidraiseSalary(doublebyPercent){

salary*=l+byPercent/100;

}//end

publicinthireYearO{

returnhireDay.getYearO;

)

}//endclassEmployee

publicclassMyTestClass{

publicstaticvoidmain(String[]args){

Employee[]staff=newEmployee[3]:

staff[O]=newEmployee(?,HarryHacker^,35000,newMyDate(1989,10,1));

staff[l]=newEmployeeC'CarlCarcker*,75000,newMyDate(1987,12,15));

staff[2]=newEmployee(z,TonyTester*,38000,newMyDate(1990,3,12));

intintegerValue;

System,out.printin(''Theinformationofemployeeare:");

for(integerValue=0;integerValue<=2;integerValue++){

staff[integerValue].raiseSalary(5);

}//endfor()

for(integerValue=0;integerValue<=2;integerValue++){

staff[integerValue].print();

}//endfor0

}//endmain()

}//endclassMyTestClass

〃習題2.4

importjava,util.*;

publicclassDataType{

publicstaticvoidmain(String[]args){

booleanflag;

charyesChar;

bytefinByte;

intintValue;

longlongValue;

shortshortValue;

floatfloatValue;

doubledoubleValue;

flag=true;

yesChar='y,;

finByte=30;

intValue=-7000;

longValue=2001;

shortValue=20000;

floatValue=9.997E-5f;

doubleValue=floatValue*floatValue;

System,out.printin(z,thevaluesare:");

System.out.printin("布爾類型變量flag=,,+flag);

System,oul.printin("字符型變量yesChar=〃+yesChar);

System.out.printin(〃字節(jié)型變量finByte="+finByte);

System,out.prinlln("整型變量intValue=/z+intValue);

System.out.printin(“長整型變量longValue=z,+longValue);

System,oul.prinlln(〃短整型變量shortValue="z+shortValue);

System.out.printin(“浮點型變量floatValue=z,+floatValue);

System.out.printin(“雙精度浮點型變量doubleValue=z/+doubleValue);

}//endmain()

}

〃習題2.9

importjava,util.*;

classPubTestl{

privateintivarl;

privatefloatfvarl,fvar2;

publicPubTestl(){

fvar2=0.Of;

}

publicfloatsum_f_I(){

fvar2=fvarl+ivarl;

returnfvar2;

}

publicvoidprint(){

System,out.printin(/,fvar2="z+fvar2);

}

publicvoidsetlvarl(intivalue){

ivarl=ivalue;

)

publicvoidsetFvarl(floativalue){

fvarl=ivalue;

}

}

publicclassPubMainTest{

publicstaticvoidmain(String[]args){

PubTestlpubtl=newPubTestl();

pubt1.setlvarl(10);

pubtl.setFvarl(100.02f);

pubtl.sum_f_I();

pubtl.print0;

}

}

〃習題2.10

importjava,util.*;

classDate{

privateintyear;

privateintmonth;

privateintday;

publicDate(intday,intmonth,intyear){〃構(gòu)造函數(shù),構(gòu)造方法

this.year=year;

this,month=month;

this,day=day;

}//endpublicMyDate(inty,intm,intd)

publicintgetYear(){〃返回年

returnyear;

}//endgetYear()

publicintgetMonthO{//返回月

returnmonth;

}//endgetMonthO

publicintgetDayO{〃返回日

returnday;

}//endgetDay()

)//endclassDate

publicclassTeacher{

Stringname;〃教師名字

booleansex;〃性別,true表示男性

Datebirth;〃出生日期

StringsalarylD;〃工資號

Stringdepart;〃教師所在系所

Stringposit;〃教師職稱

StringgetName(){

returnname;

)

voidsetName(Stringname){

this,name=name;

)

booleangetSex(){

returnsex;

)

voidsetSex(booleansex){

this.sex=sex;

)

DategetBirthO{

returnbirth;

)

voidsetBirth(Datebirth){

this,birth=birth;

)

StringgetSalaryID(){

returnsalaryID;

}

voidsetSalaryll)(StringsalaryID){

this.salarylD=salarylD;

StringgetDepart(){

returndepart;

)

voidsetDepart(Stringdepart)(

this.depart=depart;

}

StringgetPosit(){

returnposit;

}

voidsetPosit(Stringposit)(

this,posit=posit;

}

publicTeacher(){

System.out.printin("父類無參數(shù)的構(gòu)造方法!!!!!!!");

"/如果這里不加上這個無參數(shù)的構(gòu)造方法將會出錯?。。?!

publicTeacher(Stringname,booleansex,Datebirth,

Stringsalaryid,Stringdepart,Stringposit){

=name;

this,sex=sex;

this.birth=birth;

this.salaryID=salaryid;

this.depart=depart;

this,posil=posit;

}//endTeacher()

publicvoidprint(){

System,out.print("theteacher,name:");

System,out.println(this.getName());

System,out.print(z/theteacher*sex:〃);

if(this.getSex()==false){

System,out.printin(〃女”);

)

else{

System.out.printIn(,z5]^);

}

System.out.print("theteacher,birth:〃);

System,out.printIn(this.getBirth().getYear()+“-"+

this.getBirthO.getMonth()+〃-"+

this.getBirthO.getDay());

System,out.print("theteacher'salaryid:〃);

System,out.printin(this.getSalarylDO);

System.out.print("theteacher,posit:〃);

System,out.println(this.getPosit());

System,out.print("theteacher*depart:〃);

System,out.print1n(this.getDepart());

}//endprint()

publicstaticvoidmain(StringE]args){

Datedll=newDate(11,23,1989);

Datedt2=newDate(2,6,1975);

Datedt3=newDate(11,8,1964);

Datedt4=newDate(10,4,1975);

Datedt5=newDate(8,9,1969);

〃創(chuàng)建各系教師實例,用來測試

Teachertl二newTeacher(“王瑩",false,dll,“123","經(jīng)濟學","prefessor");

ResearchTeacherrt=newResearchTeacher("楊zi青",true,dt2,〃421〃,"軟件工程”,

“associateprefessor*,“software");

LabTeacherlat=newLabTeacher("王夏理〃,false,dt3,"163”,〃外語〃,

“pinslrucor","speechlab");

LibTeacherlit=newLibTeacher(〃馬二孩",true,dt4,"521〃,”大學物理〃,

“prefessor","physicalLib");

AdminTeacherat=newAdminTeachcr(“王xi〃,false,dt5,〃663",〃環(huán)境”,

preiessor,dean);

〃〃〃〃/分別調(diào)用各自的輸出方法,輸出相應(yīng)信息〃〃〃/〃〃〃〃〃〃〃〃〃〃/

System,out.println(z,");

tl.print();〃普通教師信息

System,out.printinC〃);

rt.print0;//研究系列教師信息

System,out.printin(/z〃);

lat.print();〃普通教師信息

System,out.printing〃);

lit.print。;〃實驗系列教師信息

System,out.printing");

at.print();〃行政系列教師信息

System,out.printing〃);

}//endmain()

}//endpublicclassTeacher

classResearchTeacherextendsTeacher{

privateStringresField;

publicResearchTeacher(Stringname,booleansex,Datebirth,Stringsalaryid,

Stringdepart,Stringposit,StringresField){

this,name=name;

this.sex=sex;

this,birth=birth;

this.salarylD=salaryid;

this.depart=depart;

this.posit=posit;

this.resField=resField;

}//endpublicResearchTeacher(){}

StringgetResFieldO{

returnresField;

)

voidsetResField(StringresField){

this.resField=resField;

)

publicvoidprint(){

System,out.print(''researchteacherinfois:〃);

System,out.print(/ztheteacher*name:/z);

System,out.printin(this.getName0);

System,out.print("theteacher,sex:〃);

if(this.getSexO==false){

System.out.prinlln(〃女〃);

)

else{

System,out.printing男”);

)

System,out.print("theteacher*birth:〃);

System,out.println(this.getBirthO.getYearO+"-"+

this.getBirthO.getMonthO+〃-"+

this.getBirthO.getDay());

System,out.print(,ztheteacher'salaryid:^);

System,out.println(this.getSalarylDO);

System,out.print("theteacher'posit:〃);

System.out.printin(this.getPosit());

System,out.print("theteacher*depart:");

System,out.println(this.getDepart());

System,out.print("theteacher*resField:");

System,out.println(this.getResField());

}//endprint()

}//endclassResearchTeacher

classLabTeacherextendsTeacher(

privateStringlabName;

publicLabTeacher(Stringname,booleansex,Datebirth,

Stringsalaryid,Stringdepart,

Stringposit,StringlabName){

=name;

this,sex=sex;

this.birth=birth;

this.salarylD=salaryid;

this.depart=depart;

this,posit=posit;

this.labName=labName;

)//endpublicResearchTeacher0{}

StringgetLabName(){

returnlabName;

}

voidsetLabName(StringlabName){

this.labName=labName;

)

publicvoidprint(){

System,out.print(/zlabteacherinfois:");

System,out.print("theteacher*name:;

System,out.printin(this.getName());

System,out.print("theteacher*sex:〃);

if(this.getSex()二二false){

System.out.printin(〃女”);

)

else{

System,out.printin("男〃);

)

System,out.print("theteacher,birth:〃);

System,out.println(this.getBirth().getYearO+"-"+

this.getBirth().getMonthO+〃+

this.getBirthO.getDay());

System,out.print("theteacher,salaryid:〃);

System.out.println(this.getSalarylDO);

System,out.print("theteacher*posit:〃);

System,out.println(this.getPosit());

System,out.print("theteacher,depart:〃);

System.out.println(this.getDepart());

System,out.print(/ztheteacher*labName:*);

System.out.println(this.labName);

}//endprint()

}//endclassLabTeacher

classLibTeacherextendsTeacher{

privateString1ibName;

publicLibTeacher(Stringname,booleansex,Datebirth,

Stringsalaryid,Stringdepart,

Stringposit,StringlibName){

=name;

this,sex=sex;

this,birth=birth;

this.salarylD=salaryid;

this.depart=depart;

this.posit=posit;

this.libName=libName;

}//endpublicResearchTeacher(){}

StringgetLibNameO{

returnlibName;

)

voidsetLibName(StringlibName){

this.libName=libName;

}

publicvoidprint(){

System,out.print(z,libteacherinfois:〃);

System,out.printC'theteacher'name:");

System,out.printin(this.getName0);

System,out.print("theteacher*sex:〃);

if(this.getSexO==false){

System,out.printin("女");

)

else{

System,out.printin(〃男”):

}

System.out.print("theteacher'birth:〃);

System,out.printin(this.getBirth().getYearO+"-"+

this.getBirthO.getMonthO+-

this.getBirthO.getDay());

System,out.print("theteacher,salaryid:*);

System,out.printin(this.getSalaryID());

System,out.print("theteacher,posit/);

System,out.printin(this.getPosit());

System.out.print("theteacher*depart:;

System,out.print1n(this.getDepart());

System.out.print("theteacher,libName:");

System,out.printIn(this.1ibName);

}//endprint()

}//endclassLibTeacher

classAdminTeacherextendsTeacher{

privateStringmanagePos;

publicAdminTeacher(Stringname,booleansex,Datebirth,

Stringsalaryid,Stringdepart,

Stringposit,StringmanagePos){

=name;

this,sex=sex;

this.birth=birth;

this.salarylD=salaryid;

this,depart=depart;

this,posit=posit;

this.managePos=managePos;

}//endpublicResearchTeacher(){}

StringgetManagePos(){

returnmanagePos;

)

voidsetManagePos(StringmanagePos){

this.managePos=managePos;

}

publicvoidprint(){

System,out.print(^adminteacherinfois:*);

System,out.printC'theteacher,name/');

System,out.printin(this.getName0);

System,out.print(/?theteacher,sex:");

if(this.getSexO==false){

System.out.prinlln(〃女〃);

)

else{

System,out.printin("男〃);

)

System,out.print(z/theteacher*birth:");

System,out.printIn(this.getBirthO.getYearO+"-〃+

this.getBirthO.getMonthO+"-〃+

this.getBirthO.getDay());

System,out.print(z/theteacher,salaryid:;

System.out.println(this.getSalarylDO);

System,out.print(z,theteacher'posit:");

System.out.println(this.getPosit());

System,out.print(,ztheteacher*depart:");

System,out.printin(this.getDepart());

System,out.print("theteacher'managePos:");

System,out.printin(this.managePos);

}//endprint()

}//endclassAdminTeacher

習題2.11

publicclassCourse{

privateStringcourselD;

privateStringcourseName;

privateStringcourseType;

privateintclassHour;

privatefloatcredit;

publicCourse(StringcourselD,StringcourseName,StringcourseType,

intclassHour,floatcredit){

this.courseID=courseID;

this.courseName=courseName;

this.courseType=courseType;

this,classHour=classHour;

this.credit=credit;

}//endpublicCourseO{}

StringgetID(){

returncourselD;

}

voidsetID(Stringid){

this.courselD=id;

StringgetName(){

returncourseName;

voidsetName(Stringname){

this.courseName二name;

StringgetType(){

returncourseType;

voidsetType(Stringtype){

this.courseType=type;

)

intgetClassHour(){

returnclassHour;

}

voidsetClassHour(inthour){

this.classHour=hour;

floatgetCredit(){

returnclassHour;

}

voidsetCredit(floatcredit){

this.credit=credit;

)

publicvoidprint(){

System,out.printin(z,thebasicinfoofthiscourseasfollowed:z,);

System.out.printin(/zcourseID="z+this.getIDO);

System,out.printin(,,courseName=*+this.getName0);

System,out.printinC/courseType^^+this.getType());

System.out.printIn("classHour=〃+this.getClassIIourO);

System,out.prinlln("credit="+lhis.getCredit());

)

publicstaticvoidmain(Stringf]args){

Coursecs=newCourse("dl2","java程序設(shè)計(第二版)","cs〃,64,3.Of);

System,out.printin(*");

cs.print();

System.out.printIn("修改課程學分為4.Of");

cs.setCredit(4);

cs.print();

〃習題2.12

publicclassMyGraphic{

StringlineColor;

StringfillColor;

MyGraphic(String1c,Stringfc){

this.lineColor=lc;

this.fillColor=fc;

)

voidprint0(

System,out.printin(z,linecoloris/z+this.lineColor+

“\tfillcolorisz,+this.fillColor);

}

publicstaticvoidmain(String[]args){

floatrd二(float)4.5;

MyCirclemc=newMyCircle(rd,"black","white");

MyRectanglemr=newMyRectangle(4,6,"red","blue");

System,out.printin(''Circleinfo〃);

me.print0;

System,out.printin(^circumferenceis〃+me.calCircumO);

System,out.printin(''squareis"+me.calSquareO);

System,out.printin(^rectangleinfo:");

mr.print0;

System,out.printin(/zcircumferenceis〃+mr.calCircumO);

System,out.printIn(''squareis"+mr.calSquareO);

}//endmain(){}

}//endpublicclassMyGraphic

classMyRectangleextendsMyGraphic(

floatrLong;

floatrWidth;

MyRectangle(floatrl,floatrw,String1c,Stringfc){

super(1c,fc);

this.rLong=rl;

this.rWidth=rw;

}//endMyRectangle(){)

floatcalCircumO{

return((float)((this.rLong+this.rWidth)*2));

}

floatcalSquareO{

return((float)(this.rLong*this.rWidth));

}

}//endclassMyRectangle

classMyCircleextendsMyGraphic(

floatradius;

MyCircle(floatrd,String1c,Stringfc){

super(1c,fc);

this,radius二rd;

}//endMyRectangle(){}

floatcalCircumO{

return(float)((this.radius*3.12*2));

}

floatcalSquareO{

return((float)(this.radius*this.radius*3.14));

)

}//endclassMyCircle

〃習題2.13

publicclassVehicle{

Stringbrand;

Stringcolor;

intprice;

intnumber;

publicVehicle(Stringb,Stringc){

this.brand=b;

this.color=c;

}

publicVehicle(Stringb,Stringc,intp,intn){

this(b,c);

this,price=p;

this.number=n;

}

voidprint(){

System,out.printin(^Xn〃);

System,out.println(,zthevehicleinfoasfollowed

System,out.printIn("brand=〃+this,brand+"\t");

System,out.printlnC,color=/Z+this,color+〃\t");

System,out.printIn("price=〃+this,price+"\t");

System,out.printin("number=〃+this.number+〃\t〃);

}//endvoidprint()

publicstaticvoidmain(String[]args){

Vehiclecl=newVehicle("vehicle1","white");

Vehiclec2=newVehicle(〃vehicle2〃,“white”,300,1);

Carcr=newCar("carl","red”,300,4,400);

Trucklk2=newTruck("iruckl","black”,300,400);

cl.print0;

c2.print();

cr.print0;

tk2.print();

}//endmainO

}//endpublicclassVehicle

classCarextendsVehicle{

intspeed;

Car(Stringb,Stringc,intp,intn,ints){

super(b,c,p,n);

this.speed=s;

)

voidprint0(

super,print();

System,out.print("speed—+this.speed);

}

}//endclassCar

classTruckextendsVehicle(

intspeed;

intweight;

Truck(Stringb,Stringc,ints,intw){

super(b,c);

this,speed's;

this.weight=w;

}

voidprint0(

super,print();

System,out.print("speed="+this.speed);

System,out.print("weight=〃+lhis.weight);

)

}//endclassTruck

〃習題3.3

publicclassTest{

publicstaticvoidmain(String[]args){

intbl=l;

intb2=l;

System.out.printin(“bl="+bl);

System,out.printIn(*62=*+b2);

bl?=31;

b2?=31;

System.out.prinlln(〃bl=〃+bl);

System.out.printin(“b2=〃+b2);

bl?=31;

System,out.printin(z,bl=/z+bl);

bl?=1;

System,out.printin("bl=〃+bl);

b2?>=31;

System,out.printIn(*62=*+b2);

b2?>=1;

System,out.printin(,,b2=/z+b2);

)

〃習題3.4

publicclassFactorial{

privateintresult,initVal;

publicstaticintFactorial(intn){

if(n=0){

return1;

)

returnn*Factorial(n-1);

}

publicvoidprint(){

System,out.println(initVal+/,!=/z+result);

}

publicvoidsetlnitVal(intn){

initVal=n;

}

publicstaticvoidmain(String[]args){

Factorialff=newFactorial();

for(inti=0;i<=4;i++){

ff.setlnitVal(2*(i+l));

ff.result=Factorial(ff.initVal);

ff.print();

}//endfor()

}//endmain()

)//endpublicclassFactorial

publicclassFactorials{

privateintresult,initVal;

publicvoidprint(){

System,out.println(initVal+,z!=,z+result);

)

publicvoidsetlnitVal(intn){

initVal=n;

)

publicstaticvoidmain(StringE]args){

Factorialff=newFactorial();

for(inti=0;i<=4;i++){

ff.setlnitVal(2*(i+l));

ff.result=l;

for(intj=2;j<=ff.initVal;j++){

ff.result*=j;

)

ff.print();

}//endfor0

}//endmain()

}

〃習題3.5

publicclassMathRandomTest{

publicstaticvoidmain(String[]args){

intcount=0,MAXof100,MINofl00;

intnum,i;

MAXof100=(int)(100*Math.random());

MINof100=(int)(100*Math.random());

System.out.print(MAXof100+^〃);

System.out.print(MINof100+,z〃);

if(MAXof100>50)count++;

if(MINof100>50)count++;

if(MAXoflOO<MINoflOO){

num=MINofl00;

MINoflOO=MAXoflOO;

MAXof100=num;

}//endif()

for(i=0;i<98;i++){

num=(int)(100*Math.random());

System,out.print(mim+((i+2)%10=9?〃\n〃:”"));

if(num>MAXofl00){

MAXoflOO=num;

)

elseif(num<MINofl00){

MINof100=num;

)

if(num>50){

count++;

)

}//endfor()

System,out.printin(z,themaxof100randomintegersis'+MAXoflOO);

System,out.printlnC^theminof100randomintegersis//+MINoflOO);

System,out.printin(z,thenumberofrandommorethan50is"+count);

}//endmain()

}//endpublicclassMathRandomTest

〃習題3.7

publicclassPrintAst{

publicvoidprintAstar(){

System,out.print('*");

publicvoidprintSpace(){

System,out.print("");

publicstaticvoidmain(String[]args){

PrintAstpa=newPrintAst();

intinitNum=13;

for(inti=1;i<=initNum/2+1;i++){

for(intn=1;n<=i;n++){

pa.printSpace();

pa.printSpace();

for(intm=1;m<=initNum-2*i+2;m++)

pa.printSpace();

pa.printAstar();

)

System.out.printlnO;

}//endfor

if(initNum%2==0){

for(inti=1;i<=initNum/2;i++){

pa.printSpace();

pa.printSpace();

)

pa.printSpace();

pa.printAstar();

pa.printSpace();

pa.printAstar();

System.out.printlnO;

for(inti=initNum/2+2;i<=initNum;i++)

for(intn=1;n<=initNum-i+1;n++){

pa.printSpace();

pa.printSpace();

for(intm=l;m<=2*i-initNum;m++){

pa.printSpace0;

pa.printAstar();

)

System.out.printin();

}//endfor

System,out.printin();

1//endmainO

}//endpublicclassPrintAst

〃習題3.8

publicclassPrintTriag{

publicvoidprintAstar(){

System.out.print(〃*〃);

}

publicstaticvoidmain(String[]args){

intinitLine=10;

intinitNum=10;

PrintTriagpt=newPrintTriag();

for(inti=0;i<initLine;i++){

for(intj=0;j<initNum-i;j++){

pt.printAstar();

)

System.out.printIn();

)

)//endmainO

)//endpublicclassPrintTriag

習題3.9

importjava.util.*;

publicclassMuitipieTable(

publicvoidprintFormula(inti,intj,intres){

System,out.print(j+〃*〃+j+〃=〃+res+〃〃);

}

publicstaticvoidmain(String[]args){

MultipleTablemt=newMultipleTableO;

intinitNum=9;

intres=0;

for(inti=l;i<=initNum;i++){

for(intj=l;j<=i;j++){

res=i*j;

mt.printFormula(i,j,res);

)

System.out.printin();

}//endfor

}//endmain()

}//endpublicclassMultipleTable

習題3,10

importjava.io.*;

publicclassHuiWen{

booleanisHuiWen(charstr[],intn){

intnet=0;

inti,j;

for(i=0,j=n-1;i<n/2;i++,j-){

if(str[i]==str[j]){

net++;

}//endif

}//endfor

if(net==(int)(n/2)){

returntrue;

}//endif

else{

returnfalse;

)

}//endbooleanisHuiWen(charstr[],intn)

publicstaticvoidmain(String[]args){

HuiWenhwl=newHuiWen0;

Stringpm二

try{

InputStreamReaderreader=newInputStreamReader(System,in);

BufferedReaderinput=newBufferedReader(reader);

System.out.printC'giveyourteststring:Xn^);

pm=input.readLineO;

System.out.printin(pm);

}//endtry

catch(lOExceptione){

System.out.print(e);

}//endcatch

booleanbw=hwl.isIIuiWen(pm.toCharArray0,pm.length());

if(bw==true){

System,out.printin("是回文");

)

else{

System.out.printIn("不是回文");

}

}//endmain()

)//endpublicclassHuiWen

importjava.io.*;

publicclassHuiWen2{

Stringreverse(Stringwl){

Stringw2;

char[]strl=wl.toCharArray();

intlen=wl.length();

char[]str2=newchar[len];

for(inti=0;i<len;i++){

str2[i]=strl[len-l-i];

}

w2=newString(str2);

returnw2;

}

publicstaticvoidmain(String

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論