Skip to content

Commit

Permalink
Merge pull request #62 from HarryDulaney/hg-new-solutions
Browse files Browse the repository at this point in the history
Add Ch 16_05 + Ch 16_10 + updated Ch 03_24 to use if else select stat…
  • Loading branch information
HarryDulaney authored Nov 10, 2023
2 parents 13add97 + 6eb8cd2 commit 61fefb8
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 64 deletions.
5 changes: 0 additions & 5 deletions ch_02/Exercise02_09.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
3 changes: 0 additions & 3 deletions ch_02/Exercise02_12.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
6 changes: 0 additions & 6 deletions ch_02/Exercise02_13.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 $");
Expand Down
9 changes: 1 addition & 8 deletions ch_02/Exercise02_14.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
9 changes: 0 additions & 9 deletions ch_02/Exercise02_15.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);


}

}
2 changes: 0 additions & 2 deletions ch_02/Exercise02_16.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions ch_02/Exercise02_17.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}


}
3 changes: 0 additions & 3 deletions ch_02/Exercise02_20.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();


}
}
9 changes: 0 additions & 9 deletions ch_02/Exercise02_21.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion ch_03/Exercise03_11.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*
*/
public class Exercise03_11 {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

Expand Down
2 changes: 0 additions & 2 deletions ch_03/Exercise03_13.java
Original file line number Diff line number Diff line change
Expand Up @@ -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: ");
Expand Down
46 changes: 38 additions & 8 deletions ch_03/Exercise03_24.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
90 changes: 90 additions & 0 deletions ch_16/Exercise16_05.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* (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();
}
}
1 change: 0 additions & 1 deletion ch_16/Exercise16_08.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 61fefb8

Please sign in to comment.