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略(懒得写了)

  • 16
    点赞
  • 136
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值