Java语言程序设计基础篇第四章课后习题(保持更新)

来源:互联网 发布:淘宝店铺装修图片链接 编辑:程序博客网 时间:2024/06/15 21:30

1)五边形的面积

package com.Wyhon.test;import java.util.*;import java.text.*;public abstract class Rectangle {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.print("Enter the length from the center to a vertex: ");        double r = input.nextDouble();        DecimalFormat df = new DecimalFormat("#.00");        System.out.println("The area of the pentagon is: " + df.format(GetArea(r)));    }    public static double GetArea(double r) {        double frameLength = 2 * r * Math.sin(Math.PI / 5);        double area = 5 * Math.pow(frameLength, 2) / (4 * Math.tan(Math.PI / 5));        return area;    }}

2)最大圆距离

package com.Wyhon.test;import java.util.*;import java.text.*;public class Rectangle {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.print("Enter point 1 (latitude and longitute) in degrees: ");        String s1 = input.nextLine();        System.out.print("Enter point 2 (latitude and longitute) in degrees: ");        String s2 = input.nextLine();        GetDistance(s1, s2);    }    public static void GetDistance(String s1, String s2) {        final double r = 6371.01;        String[] _s1 = s1.split(",");        String[] _s2 = s2.split(",");        double x1 = Math.toRadians(Double.parseDouble(_s1[0]));         double y1 = Math.toRadians(Double.parseDouble(_s1[1]));        double x2 = Math.toRadians(Double.parseDouble(_s2[0]));        double y2 = Math.toRadians(Double.parseDouble(_s2[1]));        double distance = r * Math.acos(Math.sin(x1) * Math.sin(x2) + Math.cos(x1) * Math.cos(x2) * Math.cos(y1 - y2)) ;        System.out.println("The distance between the two points is " + distance + " Km");    }}

4)六边形面积

package com.Wyhon.test;import java.util.Scanner;import java.text.*;public class test1 {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.print("Enter the side: ");        double s = input.nextDouble();        DecimalFormat df = new DecimalFormat("#.00");        System.out.println("The area of the hexagon is " + df.format(GetArea(s)));    }    public static double GetArea(double s) {        double area = 6 * Math.pow(s, 2) / (4 * Math.tan(Math.PI / 6));        return area;    }}

8)给出字符的Unicode码

package com.Wyhon.test;import java.util.Scanner;import java.text.*;public class test1 {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.print("Enter a character: ");        String str = input.next();        if(str.length() != 1) {            System.out.println("You must enter an exact character!");            System.exit(1);        }        char ch = str.charAt(0);        System.out.println("The Unicode for the character " + ch + " is " + (int) ch);    }}
阅读全文
0 0
原创粉丝点击