Java語言程式設計(基礎篇)原書第十版第十一章答案
前面幾個題有的沒有匯入包,後面的加進去了
自己也是剛剛學Java不久可能有的寫的很囉嗦
如果有錯的話希望指正!!感謝
還有這個markdown編輯器第一次用,類名沒搞進去,大家湊活看。
//11.1
public class Triangle extends GeomertircObjact{
private double side1,side2,side3; Triangle(){ side1 = 1; side2 = 1; side3 = 1; } Triangle(double side1,double side2,double side3){ super.getDataCreated(); this.side1 = side1; this.side2 = side2; this.side3 = side3; } double getside1() { return side1; } void setside1(double side1) { this.side1 = side1; } double getside2() { return side2; } void setside2(double side2) { this.side1 = side2; } double getside3() { return side3; } void setside3(double side3) { this.side3 = side3; } //返回周長 double getPerimeter() { return side1 + side2 + side3; } //返回面積 String getArea() { double s = (side1+side2+side3)/2; s = Math.sqrt(s); String result = String .format("%.2f",s); return result; } @Override public String toString(){ return ("Triangle:side1 = " + side1 +"side2 = " + side2 +"side3 = " + side3); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
}
public class GeomertircObjact {
private String color = null; private boolean filled = false; private java.util.Date datacreated; GeomertircObjact(){ datacreated = new Date(); } GeomertircObjact(String color, boolean filled){ this.color = color; this.filled = filled; datacreated = new Date(); } public boolean isFilled() { if(this.color != null) filled = true; return filled; } public void setcolor(String color) { this.color = color; } public String getcolor() { return color; } public java.util.Date getDataCreated(){ return datacreated; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
}
//測試
public class TestTriangle {
public static void main(String[] args) {
Triangle triangle = new Triangle(8,1,2); triangle.setcolor("bull"); System.out.println("面積: " + triangle.getArea() + "周長: " + triangle.getPerimeter()); System.out.println("三邊: " + triangle.getside1()+" "+ triangle.getside2() + " "+ triangle.getside3()); System.out.println("是否填充顏色 " + triangle.isFilled()+" 顏色為: "+ triangle.getcolor()); System.out.println("建立日期: " + triangle.getDataCreated());
- 1
- 2
- 3
- 4
- 5
- 6
- 7
}
}
//11.2略(詳細畫出UML圖即可)
//11.3
public class Acount {
private int id;
private static double balance; //初始餘額
private double annualInterestRate; //當前利率
private Date dataCreated = new Date(); //開戶日期
Acount(){ //預設構造
id = 0;
annualInterestRate = 0;
}
Acount(int newid,double newbalance){ //設定(id,balance)
id = newid;
balance = newbalance;
}
//id、balance、annualInterRate的訪問器與修改器
String getdataCreated() {
return dataCreated.toString();
}
int getid() {
return id;
}
void setid(int newid) {
id = newid;
}
static double getbalance() {
return balance;
}
void setbalance(double newbalance) {
balance = newbalance;
}
double getannualInterestRate() {
return annualInterestRate;
}
void setannualInterestRate(double newannualInterestRate) {
annualInterestRate = newannualInterestRate;
}
//返回月利率
double getMonthlyIntestRate() {
return (annualInterestRate*100/12/100);
}
//提取金額
void withDraw(double money) {
balance = balance - money;
}
//存入金額
void deposit(double money) {
balance = balance + money;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
}
public class CheckingAcount extends Acount{
private double overdraft;
CheckingAcount(double overdraft){
this.overdraft = overdraft;
}
public void jiance() {
if(Acount.getbalance()<0-overdraft) {
System.out.println("Eorre 超過支票限定額");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
}
public class test {
public static void main(String[] args) {
SavingAcount a = new SavingAcount(2);
a.setbalance(100);
a.withDraw(99);
System.out.println(a.getbalance());
a.jiance();
- 1
- 2
- 3
- 4
- 5
- 6
}
}
//11.4
public class Java_eleven_four {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Scanner input = new Scanner(System.in);
while(true){
int num = input.nextInt();
if(num == 0) break;
else list.add(num);
}
int mmax = max(list);
System.out.println(mmax);
}
public static Integer max(ArrayList<Integer> list) {
int Max = 0;
if(list.isEmpty() && list.size() == 0) return 0;
else {
for(int i = 0;i<list.size();i++) {
if(list.get(i)>Max)
Max = list.get(i);
}
return Max;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
}
//11.6**
public class six {
public static void main(String[] args) {
ArrayList<Object> obj = new ArrayList<>();
obj.add(new Date());
obj.add("this is a string");
for(Object tmp : obj){
System.out.println(tmp.toString());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
}
//11.7
public class seven {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Scanner input = new Scanner(System.in);
while(true){
int num = input.nextInt();
if(num == 0) break;//輸入0結束輸入
else list.add(num);
}
shuffle(list);
for(Integer jkl :list) {
System.out.print(jkl + " ");
}
}
public static void shuffle(ArrayList<Integer> list) {
Integer xiabiao = 0; //用來存取隨機的下標
int temp = 0; //存取未換之前的值
Random rand = new Random();
for(int i = 0;i<list.size();i++) {
xiabiao = rand.nextInt(list.size());
temp = list.get(i);
//交換
list.set(i, list.get(xiabiao));
list.set(xiabiao, temp);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
}
//11.8略
//11.9
這道題感覺自己的方法一般,如果有更好方法希望大家指出
public class matrix {
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<>(); //存行
ArrayList<Integer> list2 = new ArrayList<>(); //存列
Scanner input = new Scanner(System.in);
System.out.println("請輸入您想建立的矩陣大小");
int num = input.nextInt();
Integer row = 0;
Integer column = 0;
Integer row_Max = 0; //行裡面最多有幾個1
Integer column_Max = 0; //列裡面最多有幾個1
int[][] array = new int[num][num];
Random rand = new Random();
//完成初始化並且用Max記錄行、列裡面各自最多有幾個1
for(int i = 0;i < array.length;i++) {
for(int j = 0;j < array[0].length;j++) {
int randInt = rand.nextInt(2); //0-1之間的隨機數
array[i][j] = randInt;
System.out.print(array[i][j] + " ");
if(randInt == 1) row++; //‘1’ 的行累加器
}
if(row > row_Max) row_Max = row; //儲存行裡面最多有幾個1
//歸零
row = 0;
System.out.println();
}
for(int i = 0;i < array.length;i++) {
for(int j = 0;j < array[0].length;j++) {
if(array[j][i] == 1) column++; //‘1’的列累加器
}
if(column > column_Max) column_Max = column; //儲存列裡面最多有幾個1
column = 0;
}
//遍歷陣列將最多1的行、列下標存到list1、list2裡面
for(int i = 0;i < array.length;i++) {
for(int j = 0;j < array[0].length;j++) {
if(array[i][j] == 1) row++;
if(array[j][i] == 1) column++;
}
if(row == row_Max) list1.add(i);
if(column == column_Max) list2.add(i);
row = 0;
column = 0;
}
//列印list1,list2的結果
System.out.println("具有最多1的行為");
for(Integer a:list1)
System.out.print(a + " ");
System.out.println('\n'+"具有最多1的列為");
for(Integer b:list2)
System.out.print(b + " ");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
}
//11.10略
//11.11
import java.util.ArrayList;
public class testsort {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(5);
list.add(6);
list.add(1);
list.add(2);
list.add(9);
for(Integer as:list)
System.out.print(as+" ");
System.out.println();
sort(list);
for(Integer as:list)
System.out.print(as+" ");
}
public static void sort(ArrayList<Integer> list) {
int temp = 0;
for(int i = 0;i<list.size() - 1;i++) {
for(int j = 1+i;j<list.size() ;j++) {
if(list.get(i) > list.get(j)){
temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
}
//11.12
import java.util.ArrayList;
public class test_sum {
public static void main(String[] args) {
ArrayList<Double> list = new ArrayList<>();
list.add(5.1);
list.add(6.0);
list.add(1.3);
list.add(2.2);
list.add(9.0);
for(Double as:list)
System.out.print(as+" ");
System.out.println();
double s = sum(list);
System.out.println("數字之和為 " + s);
}
public static double sum(ArrayList<Double> list) {
double Sum = 0;
for(int i = 0;i< list.size();i++) {
Sum += list.get(i);
}
return Sum;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
}
//11.13
import java.util.ArrayList;
public class test_removeDuplicatete {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(34);
list.add(5);
list.add(3);
list.add(5);
list.add(6);
list.add(4);
list.add(33);
list.add(2);
list.add(2);
list.add(4);
for(Integer as:list)
System.out.print(as+" ");
System.out.println();
removeDuplicate(list);
for(Integer as:list)
System.out.print(as+" ");
}
public static void removeDuplicate(ArrayList<Integer> list) {
int size = list.size();
int remove = 0;
for(int i = 0; i < size - remove;i++) {
for(int j = 1+i; j < size - remove;j++) {
if(list.get(i) == list.get(j)) {
list.remove(j);
remove++;
j=j-1;
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
}
//11.14
import java.util.ArrayList;
public class testUnion {
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
ArrayList<Integer> list3 = new ArrayList<>();
list1.add(3);
list1.add(5);
list1.add(45);
list1.add(4);
list1.add(3);
list2.add(33);
list2.add(51);
list2.add(5);
list2.add(4);
list2.add(13);
for(Integer as:list1)
System.out.print(as+" ");
System.out.println();
for(Integer as:list2)
System.out.print(as+" ");
list3 = union(list1,list2);
for(Integer as:list3)
System.out.print(as+" ");
}
public static ArrayList<Integer> union(ArrayList<Integer> list1,ArrayList<Integer> list2){
ArrayList<Integer> list3 = new ArrayList<>();
for(int i = 0; i<list1.size();i++) {
list3.set(i, list1.get(i));
}
for(int i = 0,j = list1.size();i < list2.size(); i++,j++) {
list3.set(j, list2.get(i));
}
return list3;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
//11.15
public class MyPoint{
private double x,y;
public MyPoint(){
x = 0;
y = 0;
}
public MyPoint(double x, double y){
this.x = x;
this.y = y;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public double distance(MyPoint p){
return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2));
}
public double diatance(double x, double y){
return Math.sqrt(Math.pow(this.x - x, 2) + Math.pow(this.y - y, 2));
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
}
import java.util.ArrayList;
public class polygon {
//ArrayList陣列存點物件
private ArrayList<MyPoint> array= new ArrayList<>();
polygon(){
}
public void setPoint(MyPoint p){
this.array.add(p);
}
//計算凸多邊形的思路:從一個定點開始和它不相鄰的頂點連線,這樣就可以把凸多邊形劃分成幾個三角形
//用第十章課後題中的計算三角形面積公式求出面積即可
//三角形面積之和等於凸多邊形的面積
public double Area(int pointNum) {
int i=0;
double temp=0;
for(; i <pointNum - 1;i++) {
temp += (this.array.get(i).getX() - this.array.get(i+1).getX())*(this.array.get(i).getY() + this.array.get(i+1).getY());
}
//防止ArrayList溢位
temp += (this.array.get(i).getX() - this.array.get(0).getX())*(this.array.get(i).getY() + this.array.get(0).getY());
if(temp < 0) temp = -temp;
return temp/2;
}
public void shuchu() {
for(int i =0 ;i < array.size();i++) {
System.out.print("(" + array.get(i).getX() +"," + array.get(i).getY() +")");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
}
import java.util.Scanner;
public class testPolygon {
public static void main(String[] args) {
polygon polygon = new polygon();
int pointNum = 0;
Scanner in = new Scanner(System.in);
System.out.println("請輸入多邊形頂點數,個數不能超過100個(順時針):");
pointNum=in.nextInt();
for(int i=0;i<pointNum;i++)
{
System.out.println("第"+i+"個頂點的橫座標:");
double x = in.nextDouble();
System.out.println("第"+i+"個頂點的縱座標:");
double y = in.nextDouble();
MyPoint p1 = new MyPoint(x,y);
polygon.setPoint(p1);
}
System.out.println("輸出polygon中儲存的所有點的座標:");
polygon.shuchu();
System.out.println("多邊形面積是:" + polygon.Area(pointNum));
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
}
//11.16
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class RepeatAdditionQuiz {
public static void main(String[] args) {
Random rand = new Random();
int number1 = rand.nextInt(10);
int number2 = rand.nextInt(10);
Scanner input = new Scanner(System.in);
System.out.println("what is " + number1 +"+" + number2 +"?");
ArrayList<Integer> answer = new ArrayList<>();
answer.add(input.nextInt());
while(!answer.contains(number1 + number2)) {
System.out.println("wrong answer.try again. what is " + number1 + "+" + number2 +"?");
answer.add(input.nextInt());
}
System.out.println("you got it !");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
}
//11.17略(懶得寫了)