您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > java语言程序设计基础篇第十版第十三章练习答案
01publicclassExercise13_01{publicstaticvoidmain(String[]args){TriangleNewtriangle=newTriangleNew(1,1.5,1);triangle.setColor(yellow);triangle.setFilled(true);System.out.println(triangle);System.out.println(Theareais+triangle.getArea());System.out.println(Theperimeteris+triangle.getPerimeter());System.out.println(triangle);}}classTriangleNewextendsGeometricObject{privatedoubleside1=1.0,side2=1.0,side3=1.0;/**Constructor*/publicTriangleNew(){}/**Constructor*/publicTriangleNew(doubleside1,doubleside2,doubleside3){this.side1=side1;this.side2=side2;this.side3=side3;}/**ImplementtheabstractmethodfindAreainGeometricObject*/publicdoublegetArea(){doubles=(side1+side2+side3)/2;returnMath.sqrt(s*(s-side1)*(s-side2)*(s-side3));}/**ImplementtheabstractmethodfindCircumferencein*GeometricObject**/publicdoublegetPerimeter(){returnside1+side2+side3;}@OverridepublicStringtoString(){//ImplementittoreturnthethreesidesreturnTriangleNew:side1=+side1+side2=+side2+side3=+side3;}}02importjava.util.ArrayList;publicclassExercise13_02{publicstaticvoidmain(String[]args){ArrayListNumberlist=newArrayListNumber();list.add(14);list.add(24);list.add(4);list.add(42);list.add(5);shuffle(list);for(inti=0;ilist.size();i++)System.out.print(list.get(i)+);}publicstaticvoidshuffle(ArrayListNumberlist){for(inti=0;ilist.size()-1;i++){intindex=(int)(Math.random()*list.size());Numbertemp=list.get(i);list.set(i,list.get(index));list.set(index,temp);}}}03importjava.util.ArrayList;publicclassExercise13_03{publicstaticvoidmain(String[]args){ArrayListNumberlist=newArrayListNumber();list.add(14);list.add(24);list.add(4);list.add(42);list.add(5);sort(list);for(inti=0;ilist.size();i++)System.out.print(list.get(i)+);}publicstaticvoidsort(ArrayListNumberlist){for(inti=0;ilist.size()-1;i++){//Findtheminimuminthelist[i..list.length-1]NumbercurrentMin=list.get(i);intcurrentMinIndex=i;for(intj=i+1;jlist.size();j++){if(currentMin.doubleValue()list.get(j).doubleValue()){currentMin=list.get(j);currentMinIndex=j;}}//Swaplist.get(i)withlist.get(currentMinIndex)ifnecessary;if(currentMinIndex!=i){list.set(currentMinIndex,list.get(i));list.set(i,currentMin);}}}}04importjava.util.*;publicclassExercise13_04{staticMyCalendarcalendar=newMyCalendar();publicstaticvoidmain(String[]args){intmonth=calendar.get(MyCalendar.MONTH)+1;intyear=calendar.get(MyCalendar.YEAR);if(args.length2)System.out.println(UsagejavaExercise13_04monthyear);elseif(args.length==2){//useuser-definedmonthandyearyear=Integer.parseInt(args[1]);month=Integer.parseInt(args[0]);calendar.set(Calendar.YEAR,year);calendar.set(Calendar.MONTH,month-1);}elseif(args.length==1){//useuser-definedmonthforthecurrentyearmonth=Integer.parseInt(args[0]);calendar.set(Calendar.MONTH,month-1);}//setdatetothefirstdayinamonthcalendar.set(Calendar.DATE,1);//printcalendarforthemonthprintMonth(year,month);}staticvoidprintMonth(intyear,intmonth){//getstartdayoftheweekforthefirstdateinthemonthintstartDay=getStartDay();//getnumberofdaysinthemonthintnumOfDaysInMonth=calendar.daysInMonth();//printheadingsprintMonthTitle(year,month);//printbodyprintMonthBody(startDay,numOfDaysInMonth);}staticintgetStartDay(){returncalendar.get(Calendar.DAY_OF_WEEK);}staticvoidprintMonthBody(intstartDay,intnumOfDaysInMonth){//printpaddingspacebeforethefirstdayofthemonthinti=0;for(i=0;istartDay-1;i++)System.out.print();for(i=1;i=numOfDaysInMonth;i++){if(i10)System.out.print(+i);elseSystem.out.print(+i);if((i+startDay-1)%7==0)System.out.println();}System.out.println();}staticvoidprintMonthTitle(intyear,intmonth){System.out.println(+calendar.getMonthName()+,+year);System.out.println(-----------------------------);System.out.println(SunMonTueWedThuFriSat);}}05publicclassExercise13_05{//Mainmethodpublicstaticvoidmain(String[]args){//CreatetwocomparablecirclesCircle1circle1=newCircle1(5);Circle1circle2=newCircle1(4);//DisplaythemaxcircleCircle1circle=(Circle1)GeometricObject1.max(circle1,circle2);System.out.println(Themaxcircle'sradiusis+circle.getRadius());System.out.println(circle);}}abstractclassGeometricObject1implementsComparableGeometricObject1{protectedStringcolor;protecteddoubleweight;//DefaultconstructprotectedGeometricObject1(){color=white;weight=1.0;}//ConstructageometricobjectprotectedGeometricObject1(Stringcolor,doubleweight){this.color=color;this.weight=weight;}//GettermethodforcolorpublicStringgetColor(){returncolor;}//SettermethodforcolorpublicvoidsetColor(Stringcolor){this.color=color;}//GettermethodforweightpublicdoublegetWeight(){returnweight;}//SettermethodforweightpublicvoidsetWeight(doubleweight){this.weight=weight;}//AbstractmethodpublicabstractdoublegetArea();//AbstractmethodpublicabstractdoublegetPerimeter();publicintcompareTo(GeometricObject1o){if(getArea()o.getArea())return-1;elseif(getArea()==o.getArea())return0;elsereturn1;}publicstaticGeometricObject1max(GeometricObject1o1,GeometricObject1o2){if(o1.compareTo(o2)0)returno1;elsereturno2;}}//Circle.java:ThecircleclassthatextendsGeometricObjectclassCircle1extendsGe
本文标题:java语言程序设计基础篇第十版第十三章练习答案
链接地址:https://www.777doc.com/doc-4830278 .html