From 6eb8cd26019a2f3d38050104358a8ccd20ac39c4 Mon Sep 17 00:00:00 2001 From: Harry Dulaney Date: Fri, 10 Nov 2023 00:11:31 -0500 Subject: [PATCH] Add Ch 16_05 + Ch 16_10 + updated Ch 03_24 to use if else select statements per ch 3. Reformatted Old exercises... --- ch_02/Exercise02_09.java | 5 -- ch_02/Exercise02_12.java | 3 - ch_02/Exercise02_13.java | 6 -- ch_02/Exercise02_14.java | 9 +-- ch_02/Exercise02_15.java | 9 --- ch_02/Exercise02_16.java | 2 - ch_02/Exercise02_17.java | 7 -- ch_02/Exercise02_20.java | 3 - ch_02/Exercise02_21.java | 9 --- ch_03/Exercise03_11.java | 1 - ch_03/Exercise03_13.java | 2 - ch_03/Exercise03_24.java | 46 ++++++++++--- ch_16/Exercise16_05.java | 90 ++++++++++++++++++++++++++ ch_16/Exercise16_08.java | 1 - ch_16/exercise16_10/Exercise16_10.java | 75 +++++++++++++++++++++ ch_16/exercise16_10/test/Welcome.java | 9 +++ 16 files changed, 213 insertions(+), 64 deletions(-) create mode 100644 ch_16/Exercise16_05.java create mode 100644 ch_16/exercise16_10/Exercise16_10.java create mode 100644 ch_16/exercise16_10/test/Welcome.java diff --git a/ch_02/Exercise02_09.java b/ch_02/Exercise02_09.java index 203b7b2..33fddb9 100644 --- a/ch_02/Exercise02_09.java +++ b/ch_02/Exercise02_09.java @@ -17,18 +17,13 @@ */ public class Exercise02_09 { public static void main(String[] args) { - double v0, v1, t; - Scanner input = new Scanner(System.in); out.println("Enter v0, v1, and t: "); - v0 = input.nextDouble(); v1 = input.nextDouble(); t = input.nextDouble(); - double a = (v1 - v0) / t; - out.printf("The average acceleration is %2.4f", a); } } diff --git a/ch_02/Exercise02_12.java b/ch_02/Exercise02_12.java index b3a50b9..f6b2c06 100644 --- a/ch_02/Exercise02_12.java +++ b/ch_02/Exercise02_12.java @@ -14,12 +14,9 @@ public class Exercise02_12 { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); - System.out.print("Enter speed (meters/second) and acceleration (meters/second squared): "); double speed = input.nextDouble(); double accel = input.nextDouble(); - - System.out.printf("The length of runway need is: %2.3f", Math.pow(speed, 2) / (2 * accel)); } } diff --git a/ch_02/Exercise02_13.java b/ch_02/Exercise02_13.java index c37b4c4..3ba90dd 100644 --- a/ch_02/Exercise02_13.java +++ b/ch_02/Exercise02_13.java @@ -12,24 +12,18 @@ * loop to simplify the code and display the account value for any month.) */ public class Exercise02_13 { - public static void main(String[] args) { - Scanner input = new Scanner(System.in); System.out.print("Enter monthly contribution: "); double monthDeposit = input.nextDouble(); double interestPerMonth = 0.05 / 12; double interestFactor = 1 + interestPerMonth; - int count = 6; double total = 0; while (count != 0) { - total = (total + monthDeposit) * interestFactor; - --count; - } System.out.print("At a 5% interest rate, you will have $"); diff --git a/ch_02/Exercise02_14.java b/ch_02/Exercise02_14.java index 1a0428f..75c6325 100644 --- a/ch_02/Exercise02_14.java +++ b/ch_02/Exercise02_14.java @@ -12,22 +12,15 @@ */ public class Exercise02_14 { public static void main(String[] args) { - Scanner input = new Scanner(System.in); - System.out.print("Please enter your weight in pounds: "); double weightInpounds = input.nextDouble(); - - System.out.print("Please enter you height in inches: "); - double heightInInches = input.nextDouble(); + double heightInInches = input.nextDouble(); double weightInkilograms = weightInpounds * 0.45359237; - double heightInmeters = heightInInches * 0.0254; - double metersFactor = Math.pow(heightInmeters, 2); - System.out.println("Your BMI is: " + weightInkilograms / metersFactor); input.close(); } diff --git a/ch_02/Exercise02_15.java b/ch_02/Exercise02_15.java index a1fb020..23a7d8f 100644 --- a/ch_02/Exercise02_15.java +++ b/ch_02/Exercise02_15.java @@ -18,23 +18,14 @@ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter x1 and y1: "); double x1, x2, y1, y2; - x1 = input.nextDouble(); y1 = input.nextDouble(); - System.out.println("Enter x2 and y2: "); - x2 = input.nextDouble(); y2 = input.nextDouble(); - double exs = Math.pow((x2 - x1), 2); double whys = Math.pow((y2 - y1), 2); - double a = Math.pow((exs + whys), 0.5); - System.out.println("The distance between the two points is " + a); - - } - } diff --git a/ch_02/Exercise02_16.java b/ch_02/Exercise02_16.java index 8257436..e61a119 100644 --- a/ch_02/Exercise02_16.java +++ b/ch_02/Exercise02_16.java @@ -9,10 +9,8 @@ public class Exercise02_16 { public static void main(String[] args) { Scanner input = new Scanner(System.in); - System.out.println("Enter a decimal for the length of the side of a hexagon: "); double side = input.nextDouble(); - double operand = (3 * Math.pow(3, 0.5)) / 2; double res = operand * Math.pow(side, 2); System.out.println("The area of the hexagon is " + res); diff --git a/ch_02/Exercise02_17.java b/ch_02/Exercise02_17.java index 7bac6b4..5a4df8a 100644 --- a/ch_02/Exercise02_17.java +++ b/ch_02/Exercise02_17.java @@ -21,19 +21,12 @@ public class Exercise02_17 { public static void main(String[] args) { Scanner input = new Scanner(System.in); - System.out.println("Enter the temperature in Fahrenheit between -58°F and 41°F: "); double temp = input.nextDouble(); - System.out.println("Enter the wind speed (>=2) in miles per hour: "); int windSpeed = input.nextInt(); - double vToPow16 = Math.pow(windSpeed, 0.16); - double twc = 35.74 + 0.6215 * temp - 35.75 * vToPow16 + 0.4275 * temp * vToPow16; System.out.println("The wind chill index is " + twc); - } - - } diff --git a/ch_02/Exercise02_20.java b/ch_02/Exercise02_20.java index 6729511..ed125f0 100644 --- a/ch_02/Exercise02_20.java +++ b/ch_02/Exercise02_20.java @@ -17,14 +17,11 @@ public class Exercise02_20 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter balance and interest rate: (e.g., 3 for 3%): "); - double balance = input.nextDouble(); double rate = input.nextDouble(); double interest = balance * (rate / 1200); double roundedInterest = Math.round(interest * 100000) / 100000.0; System.out.println("The interest rate is: " + roundedInterest); input.close(); - - } } diff --git a/ch_02/Exercise02_21.java b/ch_02/Exercise02_21.java index bb5f38c..8642e5a 100644 --- a/ch_02/Exercise02_21.java +++ b/ch_02/Exercise02_21.java @@ -17,25 +17,16 @@ * 4.25 Enter number of years: 1 Accumulated value is $1043.92 */ public class Exercise02_21 { - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - System.out.print("Enter investment amount: "); double investmentAmount = in.nextDouble(); - System.out.print("Enter annual interest rate in percentage: "); double monthInterestRate = in.nextDouble(); - System.out.print("Enter number of years: "); double years = in.nextInt(); - -// futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate)^(numberOfYears*12) - years *= 12; monthInterestRate /= 1200; //Convert from yearly to monthly and from percent to decimal - System.out.printf("Accumulated Value is $%.2f", investmentAmount * Math.pow(monthInterestRate + 1, years)); in.close(); diff --git a/ch_03/Exercise03_11.java b/ch_03/Exercise03_11.java index ad4f8f1..587029e 100644 --- a/ch_03/Exercise03_11.java +++ b/ch_03/Exercise03_11.java @@ -11,7 +11,6 @@ * */ public class Exercise03_11 { - public static void main(String[] args) { Scanner in = new Scanner(System.in); diff --git a/ch_03/Exercise03_13.java b/ch_03/Exercise03_13.java index aaccc61..ddd0407 100644 --- a/ch_03/Exercise03_13.java +++ b/ch_03/Exercise03_13.java @@ -10,9 +10,7 @@ * */ public class Exercise03_13 { - public static void main(String[] args) { - Scanner input = new Scanner(System.in); System.out.print("(0-single filer, 1-married jointly or " + "qualifying widow(er), 2-married separately, 3-head of " + "household) Enter the filing status: "); diff --git a/ch_03/Exercise03_24.java b/ch_03/Exercise03_24.java index e2886cc..2517cb5 100644 --- a/ch_03/Exercise03_24.java +++ b/ch_03/Exercise03_24.java @@ -13,18 +13,48 @@ * The card you picked is Jack of Hearts */ public class Exercise03_24 { + + private static String card = ""; + private static String suit = ""; + public static void main(String[] args) { + int randomCardNumber = new Random().nextInt(53); + // 0 is not a valid card number + if(randomCardNumber == 0) { + randomCardNumber = 1; + } + System.out.println("Random card whole number is: " + randomCardNumber); + setCardFromRandomNumber(randomCardNumber); + System.out.println("The card you picked is " + card + " of " + suit); - String card = ""; - String suit = ""; - String[] cards = new String[]{"Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; - String[] suits = new String[]{"Clubs", "Diamonds", "Hearts", "Spades"}; + } - int randomCard, randomSuit; - randomCard = new Random().nextInt(13); - randomSuit = new Random().nextInt(5); + static void setCardFromRandomNumber(int randomCardNumber) { + if (randomCardNumber <= 13) { + suit = "Clubs"; + } else if (randomCardNumber <= 26) { + suit = "Diamonds"; + } else if (randomCardNumber <= 39) { + suit = "Hearts"; + } else { + suit = "Spades"; + } - System.out.println("The card you picked is " + cards[randomCard] + " of " + suits[randomSuit]); + card = getCardValue(randomCardNumber); + } + static String getCardValue(int randomCardNumber) { + int cardValue = randomCardNumber % 13; // Remove the suit from the card number + if (cardValue == 1) { + return "Ace"; + } else if (cardValue == 11) { + return "Jack"; + } else if (cardValue == 12) { + return "Queen"; + } else if (cardValue == 0) { // 13 % 13 = 0 + return "King"; + } else { + return String.valueOf(cardValue); + } } } \ No newline at end of file diff --git a/ch_16/Exercise16_05.java b/ch_16/Exercise16_05.java new file mode 100644 index 0000000..6815b2f --- /dev/null +++ b/ch_16/Exercise16_05.java @@ -0,0 +1,90 @@ +package ch_16; + + +import javafx.application.Application; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; +import javafx.scene.input.KeyCode; +import javafx.scene.layout.GridPane; +import javafx.stage.Stage; + +/** + * 16.5 (Convert numbers) Write a program that converts between decimal, hex, and binary + * numbers, as shown in Figure 16.37c. + * When you enter a decimal value in the decimal value + * text field and press the Enter key, its corresponding hex and binary numbers are + * displayed in the other two text fields. Likewise, you can enter values in the other fields + * and convert them accordingly. + *

+ * (Hint: Use the Integer.parseInt(s, radix) + * method to parse a string to a decimal and use Integer.toHexString(decimal) + * and Integer.toBinaryString(decimal) to obtain a hex number or a binary + * number from a decimal.) + */ +public class Exercise16_05 extends Application { + + @Override + public void start(Stage primaryStage) { + TextField decimalTextField = new TextField(); + decimalTextField.textProperty().addListener((observable, oldValue, newValue) -> { + /* If decimal input field contains non-numeric characters */ + if (!newValue.matches("\\d*")) { + /* Remove any non-numeric characters */ + decimalTextField.setText(newValue.replaceAll("[^\\d]", "")); + } + }); + + TextField hexTextField = new TextField(); + TextField binaryTextField = new TextField(); + decimalTextField.setAlignment(Pos.BOTTOM_RIGHT); + hexTextField.setAlignment(Pos.BOTTOM_RIGHT); + binaryTextField.setAlignment(Pos.BOTTOM_RIGHT); + GridPane pane = new GridPane(); + pane.setAlignment(Pos.CENTER); + pane.setHgap(10); + pane.setVgap(2); + pane.add(new Label("Decimal"), 0, 0); + pane.add(decimalTextField, 1, 0); + pane.add(new Label("Hex"), 0, 1); + pane.add(hexTextField, 1, 1); + pane.add(new Label("Binary"), 0, 2); + pane.add(binaryTextField, 1, 2); + + decimalTextField.setOnKeyPressed(e -> { + if (e.getCode() == KeyCode.ENTER) { + hexTextField.setText(Integer.toHexString( + Integer.parseInt(decimalTextField.getText()))); + + binaryTextField.setText(Integer.toBinaryString( + Integer.parseInt(decimalTextField.getText()))); + } + }); + + hexTextField.setOnKeyPressed(e -> { + if (e.getCode() == KeyCode.ENTER) { + decimalTextField.setText(String.valueOf( + Integer.parseInt(hexTextField.getText(), 16))); + + binaryTextField.setText(Integer.toBinaryString( + Integer.parseInt(hexTextField.getText(), 16))); + } + }); + + binaryTextField.setOnKeyPressed(e -> { + if (e.getCode() == KeyCode.ENTER) { + decimalTextField.setText(String.valueOf( + Integer.parseInt(binaryTextField.getText(), 2))); + + hexTextField.setText(Integer.toHexString( + Integer.parseInt(binaryTextField.getText(), 2))); + } + }); + + Scene scene = new Scene(pane, 280, 160); + primaryStage.setTitle(getClass().getName()); + primaryStage.setScene(scene); + primaryStage.show(); + } +} \ No newline at end of file diff --git a/ch_16/Exercise16_08.java b/ch_16/Exercise16_08.java index 5f491ce..9bb8046 100644 --- a/ch_16/Exercise16_08.java +++ b/ch_16/Exercise16_08.java @@ -30,7 +30,6 @@ public class Exercise16_08 extends Application { private Circle circle2; private Rectangle rectangle; private Pane circlePane; - private TextField c1XTextfield; private TextField c1YTextfield; private TextField c1RTextfield; diff --git a/ch_16/exercise16_10/Exercise16_10.java b/ch_16/exercise16_10/Exercise16_10.java new file mode 100644 index 0000000..49b8ecc --- /dev/null +++ b/ch_16/exercise16_10/Exercise16_10.java @@ -0,0 +1,75 @@ +package ch_16.exercise16_10; + +import javafx.application.Application; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.*; +import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; +import javafx.stage.Stage; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +/** + * **16.10 (Text viewer) Write a program that displays a text file in a text area, as shown + * in Figure 16.40a. The user enters a file name in a text field and clicks the View + * button; the file is then displayed in a text area. + *

+ * To Test with the provided test file: + * enter the following in the filename text field: + * ...relative-path-to-project\intro-to-java-programming\ch_16\exercise16_10\test\Welcome.java + */ +public class Exercise16_10 extends Application { + protected TextField fileNameTextField = new TextField(); + protected TextArea textArea = new TextArea(); + + @Override + public void start(Stage primaryStage) throws Exception { + fileNameTextField.setPrefColumnCount(23); + Button viewButton = new Button("View"); + HBox paneForTextField = new HBox(); + paneForTextField.getChildren().addAll(new Label("Filename: "), + fileNameTextField, viewButton); + textArea.setEditable(false); + textArea.setWrapText(true); + ScrollPane scrollPane = new ScrollPane(textArea); + VBox pane = new VBox(); + pane.setAlignment(Pos.CENTER); + pane.getChildren().addAll(scrollPane, paneForTextField); + viewButton.setOnAction(e -> { + try { + Exercise16_10.getTextFile(fileNameTextField.getText(), textArea); + } catch (FileNotFoundException ex) { + textArea.setText("Error! File Not Found."); + } + }); + + Scene scene = new Scene(pane, 600, 200); + primaryStage.setTitle("Exercise_16_10"); + primaryStage.setScene(scene); + primaryStage.show(); + } + + /** + * @param fileName Name of file to read + * @param textArea TextArea to display text + * @throws FileNotFoundException Read file text from file and displays it in a text area + */ + private static void getTextFile(String fileName, TextArea textArea) throws FileNotFoundException { + File file = new File(fileName); + if (!file.exists()) { + textArea.setText(fileName + " does not exist"); + } else { + StringBuilder text = new StringBuilder(); + try (Scanner input = new Scanner(file)) { + while (input.hasNext()) { + text.append(input.nextLine()); + text.append("\n"); + } + textArea.setText(text.toString()); + } + } + } +} \ No newline at end of file diff --git a/ch_16/exercise16_10/test/Welcome.java b/ch_16/exercise16_10/test/Welcome.java new file mode 100644 index 0000000..0ba4ec2 --- /dev/null +++ b/ch_16/exercise16_10/test/Welcome.java @@ -0,0 +1,9 @@ +package ch_16.exercise16_10.test; + +// This application prints Welcome to Java! +public class Welcome { + public static void main(String[] args) { + System.out.println("Welcome to Java!"); + } + +}