《Java语言程序设计(基础篇)》(第10版梁勇著)
第十三章练习题答案
13.1
public class Exercise13_01 {
public static void main(String[] args) {
TriangleNew triangle = new TriangleNew(1, 1.5, 1);
triangle.setColor("yellow");
triangle.setFilled(true);
System.out.println(triangle);
System.out.println("The area is " + Area());
System.out.println("The perimeter is "
+ Perimeter());
System.out.println(triangle);
}
}
class TriangleNew extends GeometricObject {
private double side1 = 1.0, side2 = 1.0, side3 = 1.0;
/** Constructor */
public TriangleNew() {
}
/** Constructor */
public TriangleNew(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
/** Implement the abstract method findArea in GeometricObject */ public double getArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
/** Implement the abstract method findCircumference in
* GeometricObject
**/
public double getPerimeter() {
return side1 + side2 + side3;
}
@Override
public String toString() {
// Implement it to return the three sides
return"TriangleNew: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;
}
}
13.1附加
import java.util.Scanner;
public class Exercise13_01Extra {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter time1 (hour minute second): ");
int hour, minute, second;
hour = Int();
minute = Int();
second = Int();
Time time1 = new Time(hour, minute, second);
System.out.println(time1);
System.out.println("Elapsed seconds in time1: " + Seconds());
System.out.print("\nEnter time2 (elapsed time): ");
long seconds = Long();
Time time2 = new Time(seconds);
System.out.println(time2);
System.out.println("Elapsed seconds in time2: " + Seconds());
System.out.println("\ntime1pareTo(time2)? " +
time1pareTo(time2));
Time time3 = (Time)time1.clone();
System.out.println("\ntime1pareTo(time3)? " +
time1pareTo(time3));
}
}
class Time implements Comparable<Time>, Cloneable {
private long seconds;
public Time() {
seconds = System.currentTimeMillis() / 1000;
}
public Time(int hour, int minute, int second) {
seconds = hour * 3600 + minute * 60 + second;
}
public Time(long seconds) {
this.seconds = seconds;
}
public long getSeconds() {
return seconds;
}
public int getHour() {
return (int)(seconds / 3600 % 24);
}
public int getMinute() {
return (int)(seconds / 60 % 60);
}
public int getSecond() {
return (int)(seconds % 60);
}
public String toString() {
return getHour() + (getHour() > 1 ? " hours " : " hour ") +
getMinute() + (getMinute() > 1 ? " minutes " : " minute ") +        getSecond() + (getSecond() > 1 ? " seconds " : " second ");
}
public int compareTo(Time t) {
return (int)(this.seconds - t.seconds);
}
public Object clone() {
try {
return super.clone();
}
catch (CloneNotSupportedException ex) {
ex.printStackTrace();
}
return null;
}
}
13.2
import java.util.ArrayList;
public class Exercise13_02 {
public static void main(String[] args) {
ArrayList<Number> list = new ArrayList<Number>();    list.add(14);
list.add(24);
list.add(4);
list.add(42);
list.add(5);
shuffle(list);
for (int i = 0; i < list.size(); i++)
System.out.(i) + " ");
}
public static void shuffle(ArrayList<Number> list) { for (int i = 0; i < list.size() - 1; i++) {
int index = (int)(Math.random() * list.size());      Number temp = (i);
list.set(i, (index));
list.set(index, temp);
}
}
}
13.3
import java.util.ArrayList;
public class Exercise13_03 {
public static void main(String[] args) {
ArrayList<Number> list = new ArrayList<Number>();    list.add(14);
list.add(24);
list.add(4);
list.add(42);
list.add(5);
sort(list);
for (int i = 0; i < list.size(); i++)
System.out.(i) + " ");
}
java程序设计基础视频public static void sort(ArrayList<Number> list) {
for (int i = 0; i < list.size() - 1; i++) {
/
/ Find the minimum in the list[i..list.length-1]
Number currentMin = (i);
int currentMinIndex = i;
for (int j = i + 1; j < list.size(); j++) {
if (currentMin.doubleValue() > (j).doubleValue()) {          currentMin = (j);
currentMinIndex = j;
}
}
// (i) (currentMinIndex) if necessary;
if (currentMinIndex != i) {
list.set(currentMinIndex, (i));
list.set(i, currentMin);
}
}
}
}
13.4
import java.util.*;
public class Exercise13_04 {
static MyCalendar calendar = new MyCalendar();
public static void main(String[] args) {
int month = (MyCalendar.MONTH) + 1;
int year = (MyCalendar.YEAR);
if (args.length > 2)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。