Java 语言程序设计 基础篇 原书第10版 第四章 第1,6~12,16题答案

import java.util.Scanner;

/**
 * @author lenovo
 *
 */
public class Main {
    // 4.1
    public static void question1() {
        Scanner cin = new Scanner(System.in);
        System.out.print("Enter the length from the center to a vertex:");
        double r = cin.nextDouble();
        double s = 2 * r * Math.sin(Math.PI / 5);
        double num = (5 * s * s) / (4 * Math.tan(Math.PI / 5));
        System.out.printf("The area of the pentagon is %.2f", num);
    }

    // 4.6
    public static void question2() {
        double r = 40.0;
        double rs = Math.random() * Math.PI * 2;
        double x1 = r * Math.cos(rs);
        double y1 = r * Math.sin(rs);

        rs = Math.random() * Math.PI * 2;
        double x2 = r * Math.cos(rs);
        double y2 = r * Math.sin(rs);

        rs = Math.random() * Math.PI * 2;
        double x3 = r * Math.cos(rs);
        double y3 = r * Math.sin(rs);

        double sideA = Math.sqrt(Math.pow(x1 - x2, 2.0) + Math.pow(y1 - y2, 2.0));
        double sideB = Math.sqrt(Math.pow(x1 - x3, 2.0) + Math.pow(y1 - y3, 2.0));
        double sideC = Math.sqrt(Math.pow(x3 - x2, 2.0) + Math.pow(y3 - y2, 2.0));

        double cosA = (sideB * sideB + sideC * sideC - sideA * sideA) / (2 * sideB * sideC);
        double cosB = (sideA * sideA + sideC * sideC - sideB * sideB) / (2 * sideA * sideC);
        double cosC = (sideB * sideB + sideA * sideA - sideC * sideC) / (2 * sideB * sideA);

        System.out.println("cosA: " + Math.toDegrees(Math.acos(cosA)));
        System.out.println("cosB: " + Math.toDegrees(Math.acos(cosB)));
        System.out.println("cosC: " + Math.toDegrees(Math.acos(cosC)));

    }

    // 4.7
    public static void question3() {
        Scanner cin = new Scanner(System.in);
        System.out.print("Enter the radius of the bounding circle:");
        double r = cin.nextDouble();
        System.out.println("The coordinating of five points on the pentagon are");
        double p2x = 0;
        double p2y = 100;
        double p3x = Math.cos(Math.PI / 10) * (-1) * r;
        double p3y = Math.sin(Math.PI / 10) * r;
        double p1y = Math.sin(Math.PI / 10) * r;
        double p1x = Math.cos(Math.PI / 10) * r;
        double p4y = Math.sin((3 * Math.PI) / 10) * (-1) * r;
        double p4x = Math.cos((3 * Math.PI) / 10) * (-1) * r;
        double p5y = p4y;
        double p5x = Math.cos((3 * Math.PI) / 10) * r;
        System.out.printf("(%f,%f)", p1x, p1y);
        System.out.printf("\n" + "(%f,%f)", p2x, p2y);
        System.out.printf("\n" + "(%f,%f)", p3x, p3y);
        System.out.printf("\n" + "(%f,%f)", p4x, p4y);
        System.out.printf("\n" + "(%f,%f)", p5x, p5y);

    }

    // 4.8
    public static void question4() {
        Scanner cin = new Scanner(System.in);
        System.out.print("Enter an ACSII code:");
        byte num = cin.nextByte();
        char c = (char) num;
        System.out.println("The character for ACSII code " + num + " is " + c);

    }

    // 4.9
    public static void question5() {
        Scanner cin = new Scanner(System.in);
        System.out.print("Enter a charater:");
        String s = cin.nextLine();
        char c = s.charAt(0);
        int num = (int) c;
        System.out.println("The Unicode for the character " + c + " is " + num);

    }

    // 4.10
    public static void question6() {
        String set1 = " 1  3  5  7\n" + " 9 11 13 15\n" + "17 19 21 23\n" + "25 27 29 31";

        String set2 = " 2  3  6  7\n" + "10 11 14 15\n" + "18 19 22 23\n" + "26 27 30 31";

        String set3 = " 4  5  6  7\n" + "12 13 14 15\n" + "20 21 22 23\n" + "28 29 30 31";

        String set4 = " 8  9 10 11\n" + "12 13 14 15\n" + "24 25 26 27\n" + "28 29 30 31";

        String set5 = "16 17 18 19\n" + "20 21 22 23\n" + "24 25 26 27\n" + "28 29 30 31";
        int day = 0;
        Scanner cin = new Scanner(System.in);

        System.out.println("Is your birthday in set1?");
        System.out.print(set1);
        System.out.print("\nEnter N for No and Y for Yes:");
        String s = cin.nextLine();

        if (s.charAt(0) == 'Y')
            ++day;

        System.out.println("Is your birthday in set2?");
        System.out.print(set2);
        System.out.print("\nEnter N for No and Y for Yes:");
        s = cin.nextLine();

        if (s.charAt(0) == 'Y')
            day += 2;

        System.out.println("Is your birthday in set3?");
        System.out.print(set3);
        System.out.print("\nEnter N for No and Y for Yes:");
        s = cin.nextLine();

        if (s.charAt(0) == 'Y')
            day += 4;

        System.out.println("Is your birthday in set4?");
        System.out.print(set4);
        System.out.print("\nEnter N for No and Y for Yes:");
        s = cin.nextLine();

        if (s.charAt(0) == 'Y')
            day += 8;

        System.out.println("Is your birthday in set5?");
        System.out.print(set5);
        System.out.print("\nEnter N for No and Y for Yes:");
        s = cin.nextLine();

        if (s.charAt(0) == 'Y')
            day += 16;

        System.out.println("\nYour birthday is " + day + "!");
    }

    // 4.11
    public static void question7() {
        System.out.print("Enter a decimal value(0 to 15):");
        Scanner cin = new Scanner(System.in);
        int num = cin.nextInt();
        if (num >= 10 && num <= 15) {
            char ch = (char) num;
            char hex = (char) (ch + 'A' - 10);
            System.out.println("The hex value is " + hex);
        } else if (num < 10 && num >= 0) {
            System.out.println("The hex value is " + num);
        } else {
            System.out.println(num + " is an invalid input");
        }
    }

    // 4.12
    public static void question8() {
        String[] he = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011",
                "1100", "1101", "1110", "1111" };
        System.out.print("Enter a hex digit:");
        Scanner cin = new Scanner(System.in);
        String h = cin.nextLine();
        if (h.charAt(0) > 'F' || h.charAt(0) < '0')
            System.out.println(h.charAt(0) + " is an invalid input.");
        else
            System.out.println("The binary value is " + he[(int) (h.charAt(0) - 'A' + 10)]);
    }

    // 4.13
    public static void question9() {
        int num = (int) (Math.random() * 26);
        System.out.println((char) (num + 'A'));
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        question1();
        question2();
        question3();
        question4();
        question5();
        question6();
        question7();
        question8();
        question9();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值