From c8ad5f9a8aa62440e28288dc9b0780a0da9226b6 Mon Sep 17 00:00:00 2001 From: antcam <41369980+antcam@users.noreply.github.com> Date: Wed, 9 Oct 2019 22:33:39 +0800 Subject: [PATCH 1/4] Update Exercise_04_03.java --- .../Exercise_04_03/Exercise_04_03.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/Exercise_04/Exercise_04_03/Exercise_04_03.java b/Exercise_04/Exercise_04_03/Exercise_04_03.java index 36e0b92a..c08ef455 100755 --- a/Exercise_04/Exercise_04_03/Exercise_04_03.java +++ b/Exercise_04/Exercise_04_03/Exercise_04_03.java @@ -6,3 +6,73 @@ distance between two cities. Divide the polygon into two triangles and use the formula in Programming Exercise 2.19 to compute the area of a triangle.) */ +public class Exercises_04_03 { + public static void main(String[] args){ + System.out.println("Atlanta, Georgia 33.7489954, -84.3879824"); + System.out.println("Orlando, Florida 28.538335,-81.379236"); + System.out.println("Savannah, Georgia 32.080899, -81.091203"); + System.out.println("Charlotte, North Carolina 35.227087, -80.843127"); + + final double RADIUS = 6371.01; // Constant value + + double xAtlantaGeorgia = 33.7489954; + double yAtlantaGeorgia = -84.3879824; + double xOrlandoFlorida = 28.538335; + double yOrlandoFlorida = -81.379236; + double xSavannahGeorgia = 32.080899; + double ySavannahGeorgia = -81.091203; + double xCharlotteNorthCarolina = 35.227087; + double yCharlotteNorthCarolina = -80.843127; + + // Convert degrees to radians + xAtlantaGeorgia = Math.toRadians(xAtlantaGeorgia); + yAtlantaGeorgia = Math.toRadians(yAtlantaGeorgia); + xOrlandoFlorida = Math.toRadians(xOrlandoFlorida); + yOrlandoFlorida = Math.toRadians(yOrlandoFlorida); + xSavannahGeorgia = Math.toRadians(xSavannahGeorgia); + ySavannahGeorgia = Math.toRadians(ySavannahGeorgia); + xCharlotteNorthCarolina = Math.toRadians(xCharlotteNorthCarolina); + yCharlotteNorthCarolina = Math.toRadians(yCharlotteNorthCarolina); + + // Calculate its great circle distance Atlanta Georgia to Orlando Florida + double distanceAtlantaGeorgiaToOrlandoFlorida = + RADIUS * Math.acos(Math.sin(xAtlantaGeorgia) * Math.sin(xOrlandoFlorida) + + Math.cos(xAtlantaGeorgia) * Math.cos(xOrlandoFlorida) * Math.cos(yAtlantaGeorgia - yOrlandoFlorida)); + // Calculate its great circle distance Orlando Florida to Savannah Georgia + double distanceOrlandoFloridaToSavannahGeorgia = + RADIUS * Math.acos(Math.sin(xOrlandoFlorida) * Math.sin(xSavannahGeorgia) + + Math.cos(xOrlandoFlorida) * Math.cos(xSavannahGeorgia) * Math.cos(yOrlandoFlorida - ySavannahGeorgia)); + + // Calculate its great circle distance Savannah Georgia to Charlotte North Carolina + double distanceSavannahGeorgiaToCharlotteNorthCarolina = + RADIUS * Math.acos(Math.sin(xSavannahGeorgia) * Math.sin(xCharlotteNorthCarolina) + + Math.cos(xSavannahGeorgia) * Math.cos(xCharlotteNorthCarolina) * Math.cos(ySavannahGeorgia - yCharlotteNorthCarolina)); + + // Calculate its great circle distance Charlotte North Carolina to Atlanta Georgia + double distanceCharlotteNorthCarolinaToAtlantaGeorgia = + RADIUS * Math.acos(Math.sin(xCharlotteNorthCarolina) * Math.sin(xAtlantaGeorgia) + + Math.cos(xCharlotteNorthCarolina) * Math.cos(xAtlantaGeorgia) * Math.cos(yCharlotteNorthCarolina - yAtlantaGeorgia)); + + // Calculate its great circle Atlanta Georgia to SavannahGeorgia + double distanceAtlantaGeorgiaToSavannahGeorgia = + RADIUS * Math.acos(Math.sin(xAtlantaGeorgia) * Math.sin(xSavannahGeorgia) + + Math.cos(xAtlantaGeorgia) * Math.cos(xSavannahGeorgia) * Math.cos(yAtlantaGeorgia - ySavannahGeorgia)); + + double side1 = (distanceAtlantaGeorgiaToOrlandoFlorida + distanceOrlandoFloridaToSavannahGeorgia + distanceAtlantaGeorgiaToSavannahGeorgia) / 2; + + double area1 = Math.sqrt (side1 * (side1 - distanceAtlantaGeorgiaToOrlandoFlorida) * (side1 - distanceOrlandoFloridaToSavannahGeorgia) * (side1 - distanceAtlantaGeorgiaToSavannahGeorgia)); + + double side2 = (distanceSavannahGeorgiaToCharlotteNorthCarolina + distanceCharlotteNorthCarolinaToAtlantaGeorgia + distanceAtlantaGeorgiaToSavannahGeorgia) / 2; + + double area2 = Math.sqrt (side2 * (side2 - distanceSavannahGeorgiaToCharlotteNorthCarolina) * (side2 - distanceCharlotteNorthCarolinaToAtlantaGeorgia) * (side2 - distanceAtlantaGeorgiaToSavannahGeorgia)); + + double totalArea = area1 + area2; + + System.out.printf("The area is %10.2f square kilometers",totalArea); + + + + + } + +} From d12db4f3613247dd1ed92f4ad33e1a5384f10ce3 Mon Sep 17 00:00:00 2001 From: antcam <41369980+antcam@users.noreply.github.com> Date: Thu, 10 Oct 2019 00:43:32 +0800 Subject: [PATCH 2/4] Create Exercise_04_07 --- Exercise_04/Exercise_04_07 | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Exercise_04/Exercise_04_07 diff --git a/Exercise_04/Exercise_04_07 b/Exercise_04/Exercise_04_07 new file mode 100644 index 00000000..1217e1ec --- /dev/null +++ b/Exercise_04/Exercise_04_07 @@ -0,0 +1,53 @@ +/*(Corner point coordinates) Suppose a pentagon is centered at (0, 0) with one point +at the 0 o’clock position, as shown in Figure 4.7c. Write a program that prompts +the user to enter the radius of the bounding circle of a pentagon and displays the +coordinates of the five corner points on the pentagon. Here is a sample run: + +Enter the radius of the bounding circle: 100 +The coordinates of five points on the pentagon are +(95.1057, 30.9017) +(0.000132679, 100) +(-95.1056, 30.9019) +(-58.7788, -80.9015) +(58.7782, -80.902) + + */ + +import java.util.Scanner; + +public class Exercise_04_07 { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + + System.out.print("Enter the radius of the bounding circle: "); + double radius = input.nextDouble(); + + // Starts with 18 degrees, 360 divided by 5 is 72 degrees according to sample + + + double angleP1 = Math.toRadians(18); + double angleP2 = Math.toRadians(18 + 72); + double angleP3 = Math.toRadians(18 + 72 * 2); + double angleP4 = Math.toRadians(18 + 72 * 3); + double angleP5 = Math.toRadians(18 + 72 * 4); + + double x1 = radius * Math.cos(angleP1); + double y1 = radius * Math.sin(angleP1); + double x2 = radius * Math.cos(angleP2); + double y2 = radius * Math.sin(angleP2); + double x3 = radius * Math.cos(angleP3); + double y3 = radius * Math.sin(angleP3); + double x4 = radius * Math.cos(angleP4); + double y4 = radius * Math.sin(angleP4); + double x5 = radius * Math.cos(angleP5); + double y5 = radius * Math.sin(angleP5); + + System.out.println("The coordinates of five points on the pentagon are "); + System.out.println("(" + x1 + "," + y1 + ")"); + System.out.println("(" + x2 + "," + y2 + ")"); + System.out.println("(" + x3 + "," + y3 + ")"); + System.out.println("(" + x4 + "," + y4 + ")"); + System.out.println("(" + x5 + "," + y5 + ")"); + } + +} From e28f53d62d884c3fdd6dc60ba23acf56b6154536 Mon Sep 17 00:00:00 2001 From: antcam <41369980+antcam@users.noreply.github.com> Date: Thu, 24 Oct 2019 10:20:10 +0800 Subject: [PATCH 3/4] Add files via upload --- ...xercises_5_34_Game_Scissor_Rock_Paper.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 Exercise_05/_Programming_Exercises_5_34_Game_Scissor_Rock_Paper.java diff --git a/Exercise_05/_Programming_Exercises_5_34_Game_Scissor_Rock_Paper.java b/Exercise_05/_Programming_Exercises_5_34_Game_Scissor_Rock_Paper.java new file mode 100644 index 00000000..32b18ac5 --- /dev/null +++ b/Exercise_05/_Programming_Exercises_5_34_Game_Scissor_Rock_Paper.java @@ -0,0 +1,112 @@ + +/*(Game: scissor, rock, paper) Programming Exercise 3.17 gives a program that +plays the scissor-rock-paper game. Revise the program to let the user continuously +play until either the user or the computer wins more than two times than its +opponent. + + */ + +import java.util.Scanner; + +public class _Programming_Exercises_5_34_Game_Scissor_Rock_Paper { + public static void main(String[] args) { + int computer = 0; + int player = 0; + + do { + + Scanner input = new Scanner(System.in); + + System.out.print("scissor (0), rock (1), paper (2): "); + int yourPick = input.nextInt(); + + int computerPick = (int) (Math.random() * 3); + + System.out.print("The computer is "); + + switch (computerPick) { + case 0: + System.out.print("scissor. "); + break; + case 1: + System.out.print("rock. "); + break; + case 2: + System.out.print("paper. "); + break; + } + + System.out.print("You are "); + + switch (yourPick) { + case 0: + System.out.print("scissor"); + break; + case 1: + System.out.print("rock"); + break; + case 2: + System.out.print("paper"); + break; + default: + System.out.print("selected wrong pick"); + break; + + } + + System.out.print((yourPick == computerPick) ? " too. " : ". "); + + if (yourPick == computerPick) { + System.out.println("It is as draw"); + } else if (yourPick == 0 && computerPick == 1) { + System.out.println("You Lose"); + computer++; + } else if (yourPick == 0 && computerPick == 2) { + System.out.println("You Won"); + player++; + } else if (yourPick == 1 && computerPick == 0) { + System.out.println("You Lose"); + computer++; + } else if (yourPick == 1 && computerPick == 2) { + System.out.println("You Won"); + player++; + } else if (yourPick == 2 && computerPick == 0) { + System.out.println("You Lose"); + computer++; + } else if (yourPick == 2 && computerPick == 1) { + System.out.println("You Won"); + player++; + } else { + System.out.println("Error"); + } + + System.out.println("Computer: " + computer); + System.out.println("Player " + player); + + } while ((computer != 2 || player != 0) && (computer != 0 || player != 2) + && !(computer > 2 * player && player !=0) && !(player > 2 * computer && computer !=0)); + + System.out.println("Game Results: "); + System.out.println("Computer: " + computer); + System.out.println("Player " + player); + + if (computer == 2 && player == 0) { + System.out.println("The computer scores 2 and you 0, you lose"); + } + + if (computer == 0 && player == 2) { + System.out.println("The computer scores 0 and you 2, you win"); + } + + if (computer > player && player !=0) { + System.out.println("The computer is more than two times the score than yours, you lose"); + } + + if (computer < player && computer !=0) { + System.out.println("The player is more than two times the score than computer, you won"); + } + + } + +} + From 657709fc516d0b76e18ee41027b221af1c8831d2 Mon Sep 17 00:00:00 2001 From: antcam <41369980+antcam@users.noreply.github.com> Date: Thu, 24 Oct 2019 10:24:17 +0800 Subject: [PATCH 4/4] Delete _Programming_Exercises_5_34_Game_Scissor_Rock_Paper.java --- ...xercises_5_34_Game_Scissor_Rock_Paper.java | 112 ------------------ 1 file changed, 112 deletions(-) delete mode 100644 Exercise_05/_Programming_Exercises_5_34_Game_Scissor_Rock_Paper.java diff --git a/Exercise_05/_Programming_Exercises_5_34_Game_Scissor_Rock_Paper.java b/Exercise_05/_Programming_Exercises_5_34_Game_Scissor_Rock_Paper.java deleted file mode 100644 index 32b18ac5..00000000 --- a/Exercise_05/_Programming_Exercises_5_34_Game_Scissor_Rock_Paper.java +++ /dev/null @@ -1,112 +0,0 @@ - -/*(Game: scissor, rock, paper) Programming Exercise 3.17 gives a program that -plays the scissor-rock-paper game. Revise the program to let the user continuously -play until either the user or the computer wins more than two times than its -opponent. - - */ - -import java.util.Scanner; - -public class _Programming_Exercises_5_34_Game_Scissor_Rock_Paper { - public static void main(String[] args) { - int computer = 0; - int player = 0; - - do { - - Scanner input = new Scanner(System.in); - - System.out.print("scissor (0), rock (1), paper (2): "); - int yourPick = input.nextInt(); - - int computerPick = (int) (Math.random() * 3); - - System.out.print("The computer is "); - - switch (computerPick) { - case 0: - System.out.print("scissor. "); - break; - case 1: - System.out.print("rock. "); - break; - case 2: - System.out.print("paper. "); - break; - } - - System.out.print("You are "); - - switch (yourPick) { - case 0: - System.out.print("scissor"); - break; - case 1: - System.out.print("rock"); - break; - case 2: - System.out.print("paper"); - break; - default: - System.out.print("selected wrong pick"); - break; - - } - - System.out.print((yourPick == computerPick) ? " too. " : ". "); - - if (yourPick == computerPick) { - System.out.println("It is as draw"); - } else if (yourPick == 0 && computerPick == 1) { - System.out.println("You Lose"); - computer++; - } else if (yourPick == 0 && computerPick == 2) { - System.out.println("You Won"); - player++; - } else if (yourPick == 1 && computerPick == 0) { - System.out.println("You Lose"); - computer++; - } else if (yourPick == 1 && computerPick == 2) { - System.out.println("You Won"); - player++; - } else if (yourPick == 2 && computerPick == 0) { - System.out.println("You Lose"); - computer++; - } else if (yourPick == 2 && computerPick == 1) { - System.out.println("You Won"); - player++; - } else { - System.out.println("Error"); - } - - System.out.println("Computer: " + computer); - System.out.println("Player " + player); - - } while ((computer != 2 || player != 0) && (computer != 0 || player != 2) - && !(computer > 2 * player && player !=0) && !(player > 2 * computer && computer !=0)); - - System.out.println("Game Results: "); - System.out.println("Computer: " + computer); - System.out.println("Player " + player); - - if (computer == 2 && player == 0) { - System.out.println("The computer scores 2 and you 0, you lose"); - } - - if (computer == 0 && player == 2) { - System.out.println("The computer scores 0 and you 2, you win"); - } - - if (computer > player && player !=0) { - System.out.println("The computer is more than two times the score than yours, you lose"); - } - - if (computer < player && computer !=0) { - System.out.println("The player is more than two times the score than computer, you won"); - } - - } - -} -