java语言程序设计基础篇第十版第十三章练习答案Word下载.docx
《java语言程序设计基础篇第十版第十三章练习答案Word下载.docx》由会员分享,可在线阅读,更多相关《java语言程序设计基础篇第十版第十三章练习答案Word下载.docx(43页珍藏版)》请在冰豆网上搜索。
}
}
classTriangleNewextendsGeometricObject{
privatedoubleside1=1.0,side2=1.0,side3=1.0;
/**Constructor*/
publicTriangleNew(){
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;
@Override
publicStringtoString(){
//Implementittoreturnthethreesides
return"
TriangleNew:
side1="
+side1+"
side2="
+side2+
"
side3="
+side3;
02
importjava.util.ArrayList;
publicclassExercise13_02{
ArrayList<
Number>
list=newArrayList<
();
list.add(14);
list.add(24);
list.add(4);
list.add(42);
list.add(5);
shuffle(list);
for(inti=0;
i<
list.size();
i++)
System.out.print(list.get(i)+"
publicstaticvoidshuffle(ArrayList<
list){
list.size()-1;
i++){
intindex=(int)(Math.random()*list.size());
Numbertemp=list.get(i);
list.set(i,list.get(index));
list.set(index,temp);
03
publicclassExercise13_03{
sort(list);
publicstaticvoidsort(ArrayList<
//Findtheminimuminthelist[i..list.length-1]
NumbercurrentMin=list.get(i);
intcurrentMinIndex=i;
for(intj=i+1;
j<
j++){
if(currentMin.doubleValue()>
list.get(j).doubleValue()){
currentMin=list.get(j);
currentMinIndex=j;
//S(i)withlist.get(currentMinIndex)ifnecessary;
if(currentMinIndex!
=i){
list.set(currentMinIndex,list.get(i));
list.set(i,currentMin);
04
importjava.util.*;
publicclassExercise13_04{
staticMyCalendarcalendar=newMyCalendar();
intmonth=calendar.get(MyCalendar.MONTH)+1;
intyear=calendar.get(MyCalendar.YEAR);
if(args.length>
2)
UsagejavaExercise13_04monthyear"
elseif(args.length==2){
//useuser-definedmonthandyear
year=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-definedmonthforthecurrentyear
calendar.set(Calendar.MONTH,month-1);
//setdatetothefirstdayinamonth
calendar.set(Calendar.DATE,1);
//printcalendarforthemonth
printMonth(year,month);
staticvoidprintMonth(intyear,intmonth){
//getstartdayoftheweekforthefirstdateinthemonth
intstartDay=getStartDay();
//getnumberofdaysinthemonth
intnumOfDaysInMonth=calendar.daysInMonth();
//printheadings
printMonthTitle(year,month);
//printbody
printMonthBody(startDay,numOfDaysInMonth);
staticintgetStartDay(){
returncalendar.get(Calendar.DAY_OF_WEEK);
staticvoidprintMonthBody(intstartDay,intnumOfDaysInMonth){
//printpaddingspacebeforethefirstdayofthemonth
inti=0;
for(i=0;
startDay-1;
i++)
System.out.print("
for(i=1;
=numOfDaysInMonth;
if(i<
10)
+i);
else
if((i+startDay-1)%7==0)
System.out.println();
"
staticvoidprintMonthTitle(intyear,intmonth){
+calendar.getMonthName()+"
"
+year);
-----------------------------"
SunMonTueWedThuFriSat"
05
publicclassExercise13_05{
//Mainmethod
//Createtwocomparablecircles
Circle1circle1=newCircle1(5);
Circle1circle2=newCircle1(4);
//Displaythemaxcircle
Circle1circle=(Circle1)GeometricObject1.max(circle1,circle2);
Themaxcircle'
sradiusis"
+circle