Skip to content

Instantly share code, notes, and snippets.

# rjlutz/ComputeAngles.java Last active Jun 19, 2017

Math, Characters, Strings (Chapter 4) - examples from Liang Intro to Java Comprehensive 10e
 import java.util.Scanner; public class ComputeAngles { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter three points System.out.print("Enter three points: "); double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); double x3 = input.nextDouble(); double y3 = input.nextDouble(); // Compute three sides double a = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)); double b = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); double c = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); // Compute three angles double A = Math.toDegrees(Math.acos((a * a - b * b - c * c) / (-2 * b * c))); double B = Math.toDegrees(Math.acos((b * b - a * a - c * c) / (-2 * a * c))); double C = Math.toDegrees(Math.acos((c * c - b * b - a * a) / (-2 * a * b))); // Display results System.out.println("The three angles are " + Math.round(A * 100) / 100.0 + " " + Math.round(B * 100) / 100.0 + " " + Math.round(C * 100) / 100.0); } }
 public class FormatDemo { public static void main(String[] args) { // Display the header of the table System.out.printf("%-10s%-10s%-10s%-10s%-10s\n", "Degrees", "Radians", "Sine", "Cosine", "Tangent"); // Display values for 30 degrees int degrees = 30; double radians = Math.toRadians(degrees); System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n", degrees, radians, Math.sin(radians), Math.cos(radians), Math.tan(radians)); // Display values for 60 degrees degrees = 60; radians = Math.toRadians(degrees); System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n", degrees, radians, Math.sin(radians), Math.cos(radians), Math.tan(radians)); } }
 import java.util.Scanner; public class GuessBirthday { public static void main(String[] args) { 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; // Create a Scanner Scanner input = new Scanner(System.in); // Prompt the user to answer questions System.out.print("Is your birthday in Set1?\n"); System.out.print(set1); System.out.print("\nEnter 0 for No and 1 for Yes: "); int answer = input.nextInt(); if (answer == 1) day += 1; // Prompt the user to answer questions System.out.print("\nIs your birthday in Set2?\n"); System.out.print(set2); System.out.print("\nEnter 0 for No and 1 for Yes: "); answer = input.nextInt(); if (answer == 1) day += 2; // Prompt the user to answer questions System.out.print("Is your birthday in Set3?\n"); System.out.print(set3); System.out.print("\nEnter 0 for No and 1 for Yes: "); answer = input.nextInt(); if (answer == 1) day += 4; // Prompt the user to answer questions System.out.print("\nIs your birthday in Set4?\n"); System.out.print(set4); System.out.print("\nEnter 0 for No and 1 for Yes: "); answer = input.nextInt(); if (answer == 1) day += 8; // Prompt the user to answer questions System.out.print("\nIs your birthday in Set5?\n"); System.out.print(set5); System.out.print("\nEnter 0 for No and 1 for Yes: "); answer = input.nextInt(); if (answer == 1) day += 16; System.out.println("\nYour birthday is " + day + "!"); } }
 import java.util.Scanner; public class HexDigit2Dec { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a hex digit: "); String hexString = input.nextLine(); // Check if the hex string has exactly one character if (hexString.length() != 1) { System.out.println("You must enter exactly one character"); System.exit(1); } // Display decimal value for the hex digit char ch = Character.toUpperCase(hexString.charAt(0)); if (ch <= 'F' && ch >= 'A') { int value = ch - 'A' + 10; System.out.println("The decimal value for hex digit " + ch + " is " + value); } else if (Character.isDigit(ch)) { System.out.println("The decimal value for hex digit " + ch + " is " + ch); } else { System.out.println(ch + " is an invalid input"); } } }
 import java.util.Scanner; public class LotteryUsingStrings { public static void main(String[] args) { // Generate a lottery as a two-digit string String lottery = "" + (int)(Math.random() * 10) + (int)(Math.random() * 10); // Prompt the user to enter a guess Scanner input = new Scanner(System.in); System.out.print("Enter your lottery pick (two digits): "); String guess = input.nextLine(); // Get digits from lottery int lotteryDigit1 = lottery.charAt(0); int lotteryDigit2 = lottery.charAt(1); // Get digits from guess int guessDigit1 = guess.charAt(0); int guessDigit2 = guess.charAt(1); System.out.println("The lottery number is " + lottery); // Check the guess if (guess.equals(lottery)) System.out.println("Exact match: you win \$10,000"); else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) System.out.println("Match all digits: you win \$3,000"); else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2) System.out.println("Match one digit: you win \$1,000"); else System.out.println("Sorry, no match"); } }
 import java.util.Scanner; public class OrderTwoCities { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter two cities System.out.print("Enter the first city: "); String city1 = input.nextLine(); System.out.print("Enter the second city: "); String city2 = input.nextLine(); if (city1.compareTo(city2) < 0) System.out.println("The cities in alphabetical order are " + city1 + " " + city2); else System.out.println("The cities in alphabetical order are " + city2 + " " + city1); } }
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.