《Java语言程序设计与数据结构》编程练习答案(第四章)(一)

《Java语言程序设计与数据结构》编程练习答案(第四章)(一)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

4.1

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        System.out.print("Enter the length from the center to a vertex: ");
        Scanner input = new Scanner(System.in);
        final double PI = 3.141592654;
        double r = input.nextDouble();
        double s = 2*r*Math.sin(PI/5);
        double a = 5*s*s/(4*Math.tan(PI/5));
        System.out.printf("The area of the pentagon is %.2f",a);
    }
}

4.2

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter point 1 (latitude and longitude) in degrees: ");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        System.out.print("Enter point 2 (latitude and longitude) in degrees: ");
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        double d = 6371.0*Math.acos(Math.sin(Math.toRadians(x1))*Math.sin(Math.toRadians(x2))+Math.cos(Math.toRadians(x1))*Math.cos(Math.toRadians(x2))*Math.cos(Math.toRadians(y1-y2)));
        System.out.println("The distance between the two points is "+d+" km");
    }
}

4.3

public class book {
    public static void main(String[] args)
    {
        double x1 = 35.2270869;
        double y1 = -80.8431267;
        double x2 = 32.0835407;
        double y2 = -81.0998342;
        double dcs = 6371.0*Math.acos(Math.sin(Math.toRadians(x1))*Math.sin(Math.toRadians(x2))+Math.cos(Math.toRadians(x1))*Math.cos(Math.toRadians(x2))*Math.cos(Math.toRadians(y1-y2)));
        x2 = 33.7489954;
        y2 = -84.3879824;
        double dca = 6371.0*Math.acos(Math.sin(Math.toRadians(x1))*Math.sin(Math.toRadians(x2))+Math.cos(Math.toRadians(x1))*Math.cos(Math.toRadians(x2))*Math.cos(Math.toRadians(y1-y2)));
        x1 = 28.5383355;
        y1 = -81.3792365;
        double doa = 6371.0*Math.acos(Math.sin(Math.toRadians(x1))*Math.sin(Math.toRadians(x2))+Math.cos(Math.toRadians(x1))*Math.cos(Math.toRadians(x2))*Math.cos(Math.toRadians(y1-y2)));
        x2 = 32.0835407;
        y2 = -81.0998342;
        double dos = 6371.0*Math.acos(Math.sin(Math.toRadians(x1))*Math.sin(Math.toRadians(x2))+Math.cos(Math.toRadians(x1))*Math.cos(Math.toRadians(x2))*Math.cos(Math.toRadians(y1-y2)));
        x1 = 33.7489954;
        y1 = -84.0998342;
        double das = 6371.0*Math.acos(Math.sin(Math.toRadians(x1))*Math.sin(Math.toRadians(x2))+Math.cos(Math.toRadians(x1))*Math.cos(Math.toRadians(x2))*Math.cos(Math.toRadians(y1-y2)));
        double s1 = (dca+dcs+das)/2;
        double s2 = (doa+dos+das)/2;
        double a = Math.sqrt(s1*(s1-dca)*(s1-dcs)*(s1-das))+Math.sqrt(s2*(s2-doa)*(s2-dos)*(s2-das));
        System.out.println("The area is "+a+" km^2");
    }
}

4.4

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        final double PI = 3.141592654;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the side: ");
        double s = input.nextDouble();
        double a = 6*s*s/(4*Math.tan(PI/6));
        System.out.println("The area of the hexagon is "+a);
    }
}

4.5

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        final double PI = 3.141592654;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of sides: ");
        int n = input.nextInt();
        System.out.print("Enter the side: ");
        double s = input.nextDouble();
        double a = n*s*s/(4*Math.tan(PI/n));
        System.out.println("The area of the polygon is "+a);
    }
}

4.6

public class book {
    public static void main(String[] args)
    {
        final double PI = 3.141592654;
        double alpha1 = Math.random()*2*PI;
        double alpha2 = Math.random()*2*PI;
        double alpha3 = Math.random()*2*PI;
        double x1 = 40*Math.cos(alpha1);
        double y1 = 40*Math.sin(alpha1);
        double x2 = 40*Math.cos(alpha2);
        double y2 = 40*Math.sin(alpha2);
        double x3 = 40*Math.cos(alpha3);
        double y3 = 40*Math.sin(alpha3);
        double a = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        double b = Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
        double c = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
        double aa = Math.toDegrees(Math.acos((a*a-b*b-c*c)/(-2*b*c)));
        double ab = Math.toDegrees(Math.acos((b*b-a*a-c*c)/(-2*a*c)));
        double ac = Math.toDegrees(Math.acos((c*c-b*b-a*a)/(-2*a*b)));
        System.out.println("The three angles are: "+aa+" "+ab+" "+ac);
    }
}

4.7

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        final double PI = 3.141592654;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the radius of the bounding circle: ");
        double r = input.nextDouble();
        double base = 0.1*PI;
        System.out.println("The coordinates of fibe points on the pentagon are");
        for(int i=0;i<5;i++)
        {
            System.out.printf("(%.2f, %.2f)%n",r*Math.cos(base),r*Math.sin(base));
            base+=0.4*PI;
        }
    }
}

4.8

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Ent6er an ASCII code: ");
        int ass = input.nextInt();
        char dick = (char)ass;
        System.out.printf("The character for ASCII code %d is %c",ass,dick);
    }
}

4.9

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a character: ");
        String jb = input.nextLine();
        char ass = jb.charAt(0);
        int dick = (int)ass;
        System.out.printf("The Unicode for the character %c is %d",ass,dick);
    }
}

4.10

public class book {
    public static void main(String[] args)
    {
        //原来代码中
        // int answer = input.nextInt()--->char answer = input.nextLine().charAt(0)
        // if(answer==1)--->if(answer=='Y')
    }
}

4.11

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        System.out.print("Enter a decimal value (0 to 15): ");
        Scanner input = new Scanner(System.in);
        int ass = input.nextInt();
        if(ass>=0&&ass<=15){
            if(ass<10)
                System.out.printf("The hex value is %d",ass);
            else
                System.out.printf("The hex value is %c",'A'+ass-10);
        }
        else
            System.out.printf("%d is an invalid input",ass);
    }
}

4.12

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        String[] bins = {"0","1","10","11","100","101","110","111","1000","1001","1010","1011","1100","1101","1110","1111"};
        System.out.print("Enter a hex digit: ");
        Scanner input = new Scanner(System.in);
        char ass = input.nextLine().charAt(0);
        if(ass>='0'&&ass<='9')
            System.out.printf("The binary value is %s\n",bins[ass-'0']);
        else if(ass>='A'&&ass<='F')
            System.out.printf("The binary value is %s\n",bins[ass-'A'+10]);
        else
            System.out.printf("%c is an invalid input\n",ass);
    }
}

4.13

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a letter: ");
        char ass = input.nextLine().charAt(0);
        if((ass<='z'&&ass>='a')||(ass<='Z'&&ass>='A'))
        {
            if(ass=='a'||ass=='e'||ass=='i'||ass=='o'||ass=='u'
            ||ass=='A'||ass=='E'||ass=='I'||ass=='O'||ass=='U')
                System.out.println(ass+" is a vowel");
            else
                System.out.println(ass+" is a consonant");
        }
        else
            System.out.println(ass+" is an invalid input");
    }
}
发布了75 篇原创文章 · 获赞 61 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/swy_swy_swy/article/details/104454845
今日推荐