diff --git a/README.md b/README.md index 534d68d..36c49b6 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ This is the textbook I used to first learn programming in 2016. After having gra ## Notes | Tool | Name | Version | Link | |---|---|---|---| -| IDE | Eclipse | 2023-12 R | | -| JVM | Oracle Java JRE | 21.0.2 | Installed from Eclipse IDE installer | +| IDE | Eclipse | 2023-12 R | | +| Java | Oracle Java JRE | 21.0.2 | Installed from Eclipse IDE installer | | Code Formatter | google-java-format-eclipse-plugin | v1.20.0 | | ## google-java-format diff --git a/src/com/github/jonathanbirkey/chapter03/Exercise09.java b/src/com/github/jonathanbirkey/chapter03/Exercise09.java index c9b0f76..c0600aa 100644 --- a/src/com/github/jonathanbirkey/chapter03/Exercise09.java +++ b/src/com/github/jonathanbirkey/chapter03/Exercise09.java @@ -10,8 +10,10 @@ * program that prompts the user to enter the first 9 digits and displays the 10-digit ISBN * (including leading zeros). Your program should read the input as an integer. Here are sample * runs: - *

Enter the first 9 digits of an ISBN as integer: 013601267 The ISBN-10 number is 0136012671 - * Enter the first 9 digits of an ISBN as integer: 013031997 The ISBN-10 number is 013031997X + *

Enter the first 9 digits of an ISBN as integer: 013601267 + *

The ISBN-10 number is 0136012671 + *

Enter the first 9 digits of an ISBN as integer: 013031997 + *

The ISBN-10 number is 013031997X */ package com.github.jonathanbirkey.chapter03; @@ -45,9 +47,12 @@ public static void main(String[] args) { int checksum = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11; if (checksum == 10) { - System.out.printf("%d%d%d%d%d%d%d%d%dX\n", d1, d2, d3, d4, d5, d6, d7, d8, d9); + System.out.printf( + "The ISBN-10 number is %d%d%d%d%d%d%d%d%dX\n", d1, d2, d3, d4, d5, d6, d7, d8, d9); } else { - System.out.printf("%d%d%d%d%d%d%d%d%d%d\n", d1, d2, d3, d4, d5, d6, d7, d8, d9, checksum); + System.out.printf( + "The ISBN-10 number is %d%d%d%d%d%d%d%d%d%d\n", + d1, d2, d3, d4, d5, d6, d7, d8, d9, checksum); } } } diff --git a/src/com/github/jonathanbirkey/chapter03/Exercise17.java b/src/com/github/jonathanbirkey/chapter03/Exercise17.java index dfc2af4..c63c1a3 100644 --- a/src/com/github/jonathanbirkey/chapter03/Exercise17.java +++ b/src/com/github/jonathanbirkey/chapter03/Exercise17.java @@ -31,6 +31,7 @@ public static void main(String[] args) { break; case 2: System.out.print("The computer is paper. "); + break; default: System.out.print("Invalide"); } @@ -43,6 +44,7 @@ public static void main(String[] args) { break; case 2: System.out.print("You are paper"); + break; default: System.out.print("Invalide"); } diff --git a/src/com/github/jonathanbirkey/chapter03/Exercise21.java b/src/com/github/jonathanbirkey/chapter03/Exercise21.java index 7f8ebae..75567ba 100644 --- a/src/com/github/jonathanbirkey/chapter03/Exercise21.java +++ b/src/com/github/jonathanbirkey/chapter03/Exercise21.java @@ -32,7 +32,6 @@ public static void main(String[] args) { int year = input.nextInt(); System.out.print("Enter month: 1-12: "); int m = input.nextInt(); - input.close(); if (m < 3) { m += 12; @@ -42,6 +41,7 @@ public static void main(String[] args) { int k = year % 100; System.out.print("Enter the day of the month: 1-31: "); int q = input.nextInt(); + input.close(); int h = (q + ((26 * (m + 1)) / 10) + k + (k / 4) + (j / 4) + 5 * j) % 7; switch (h) { case 0: diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise19.java b/src/com/github/jonathanbirkey/chapter05/Exercise19.java index f4518e4..3a2c542 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise19.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise19.java @@ -29,3 +29,4 @@ public static void main(String[] args) { } } } + diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise27.java b/src/com/github/jonathanbirkey/chapter05/Exercise27.java new file mode 100644 index 0000000..7e29702 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise27.java @@ -0,0 +1,33 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Display leap years) Write a program that displays all the leap years, 10 per line, from + * 101 to 2100, separated by exactly one space. Also display the number of leap years in this + * period. + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise27 { + public static void main(String[] args) { + int yearsPerLine = 10; + int printedYears = 0; + int startYear = 101; + int endYear = 2100; + int totalLeapYears = 0; + + for (int i = startYear; i <= endYear; i++) { + if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) { + printedYears++; + if (printedYears == yearsPerLine) { + System.out.printf("%d\n", i); + printedYears = 0; + } else { + System.out.printf("%d ", i); + } + totalLeapYears++; + } + } + System.out.printf("\nNumber of leap years in this period: %d", totalLeapYears); + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise28.java b/src/com/github/jonathanbirkey/chapter05/Exercise28.java new file mode 100644 index 0000000..e417170 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise28.java @@ -0,0 +1,116 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Display the first days of each month) Write a program that prompts the user to enter the + * year and first day of the year, then displays the first day of each month in the year. For + * example, if the user entered the year 2013, and 2 for Tuesday, January 1, 2013, your program + * should display the following output: + *

January 1, 2013 is Tuesday + *

... + *

December 1, 2013 is Sunday + */ +package com.github.jonathanbirkey.chapter05; + +import java.util.Scanner; + +public class Exercise28 { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter year: (e.g., 2012): "); + int year = input.nextInt(); + System.out.print("Enter first day of year (0 - Saturday, 1 - Sunday, 2 - Monday, etc): "); + int firstDay = input.nextInt(); + input.close(); + + int futureDay = firstDay; + int daysInMonth = 0; + String monthStr; + + for (int month = 1; month <= 12; month++) { + if (month == 1 + || month == 3 + || month == 5 + || month == 7 + || month == 8 + || month == 10 + || month == 12) { + daysInMonth = 31; + } else if (month == 4 || month == 6 || month == 9 || month == 11) { + daysInMonth = 30; + } else { + if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { + daysInMonth = 29; + } else { + daysInMonth = 28; + } + } + switch (month) { + case 1: + monthStr = "January"; + break; + case 2: + monthStr = "Febuary"; + break; + case 3: + monthStr = "March"; + break; + case 4: + monthStr = "April"; + break; + case 5: + monthStr = "May"; + break; + case 6: + monthStr = "June"; + break; + case 7: + monthStr = "July"; + break; + case 8: + monthStr = "August"; + break; + case 9: + monthStr = "September"; + break; + case 10: + monthStr = "October"; + break; + case 11: + monthStr = "November"; + break; + case 12: + monthStr = "December"; + break; + default: + monthStr = "Invalid"; + } + switch (futureDay) { + case 0: + System.out.printf("%s 1, %d is Saturday\n", monthStr, year); + break; + case 1: + System.out.printf("%s 1, %d is Sunday\n", monthStr, year); + break; + case 2: + System.out.printf("%s 1, %d is Monday\n", monthStr, year); + break; + case 3: + System.out.printf("%s 1, %d is Tuesday\n", monthStr, year); + break; + case 4: + System.out.printf("%s 1, %d is Wednesday\n", monthStr, year); + break; + case 5: + System.out.printf("%s 1, %d is Thursday\n", monthStr, year); + break; + case 6: + System.out.printf("%s 1, %d is Friday\n", monthStr, year); + break; + default: + System.out.println("Invalid"); + } + futureDay = (futureDay + daysInMonth) % 7; + } + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise29.java b/src/com/github/jonathanbirkey/chapter05/Exercise29.java new file mode 100644 index 0000000..9f3e8e3 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise29.java @@ -0,0 +1,140 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Display calendars) Write a program that prompts the user to enter the year and first day + * of the year and displays the calendar table for the year on the console. For example, if the + * user entered the year 2013, and 2 for Tuesday, January 1, 2013, your program should display + * the calendar for each month in the year, as follows: + *

January 2013 + *

--------------------------- + *

Sun Mon Tue Wed Thu Fri Sat + *

1 2 3 4 5 + *

6 7 8 9 10 11 12 + *

13 14 15 16 17 18 19 + *

20 21 22 23 24 25 26 + *

27 28 29 30 31 + *

+ *

... + *

+ *

December 2013 + *

--------------------------- + *

Sun Mon Tue Wed Thu Fri Sat + *

1 2 3 4 5 6 7 + *

8 9 10 11 12 13 14 + *

15 16 17 18 19 20 21 + *

22 23 24 25 26 27 28 + *

29 30 31 + */ +package com.github.jonathanbirkey.chapter05; + +import java.util.Scanner; + +public class Exercise29 { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter year: (e.g., 2012): "); + int year = input.nextInt(); + System.out.print("Enter first day of year (0 - Saturday, 1 - Sunday, 2 - Monday, etc): "); + int firstDay = input.nextInt(); + input.close(); + + int daysInMonth = 0; + String monthHeader; + + for (int month = 1; month <= 12; month++) { + if (month == 1 + || month == 3 + || month == 5 + || month == 7 + || month == 8 + || month == 10 + || month == 12) { + daysInMonth = 31; + } else if (month == 4 || month == 6 || month == 9 || month == 11) { + daysInMonth = 30; + } else { + if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { + daysInMonth = 29; + } else { + daysInMonth = 28; + } + } + switch (month) { + case 1: + monthHeader = " January " + year; + break; + case 2: + monthHeader = " Febuary " + year; + break; + case 3: + monthHeader = " March " + year; + break; + case 4: + monthHeader = " April " + year; + break; + case 5: + monthHeader = " May " + year; + break; + case 6: + monthHeader = " June " + year; + break; + case 7: + monthHeader = " July " + year; + break; + case 8: + monthHeader = " August " + year; + break; + case 9: + monthHeader = " September " + year; + break; + case 10: + monthHeader = " October " + year; + break; + case 11: + monthHeader = " November " + year; + break; + case 12: + monthHeader = " December " + year; + break; + default: + monthHeader = "Invalid"; + } + + System.out.printf( + "%s\n---------------------------\nSun Mon Tue Wed Thu Fri Sat\n", monthHeader); + + int dayShift = 0; + if (firstDay > 0) { + dayShift = firstDay - 1; + } else { + dayShift = firstDay + 6; + } + + for (int i = 0; i < dayShift; i++) { + System.out.print(" "); + } + + for (int date = 1; date <= daysInMonth; date++) { + if (dayShift == 6) { + if (date < 10) { + System.out.printf(" %d\n", date); + } else { + System.out.printf(" %d\n", date); + } + dayShift = 0; + } else { + if (date < 10) { + System.out.printf(" %d ", date); + } else { + System.out.printf(" %d ", date); + } + dayShift++; + } + } + System.out.print("\n\n"); + + firstDay = (firstDay + daysInMonth) % 7; + } + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise30.java b/src/com/github/jonathanbirkey/chapter05/Exercise30.java new file mode 100644 index 0000000..c4cf1fb --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise30.java @@ -0,0 +1,41 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Financial application: compound value) Suppose you save $100 each month into a savings + * account with the annual interest rate 5%. Thus, the monthly interest rate is 0.05 / 12 = + * 0.00417. After the first month, the value in the account becomes + *

100 * (1 + 0.00417) = 100.417 + *

After the second month, the value in the account becomes + *

(100 + 100.417) * (1 + 0.00417) = 201.252 + *

After the third month, the value in the account becomes + *

(100 + 201.252) * (1 + 0.00417) = 302.507 + *

and so on. + *

Write a program that prompts the user to enter an amount (e.g., 100), the annual interest + * rate (e.g., 5), and the number of months (e.g., 6) then displays the amount in the savings + * account after the given month. + */ +package com.github.jonathanbirkey.chapter05; + +import java.util.Scanner; + +public class Exercise30 { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter an amount (e.g., 100): "); + double monthlySavings = input.nextDouble(); + System.out.print("Enter annual interest rate (e.g., 5): "); + double annualInterestRate = input.nextDouble(); + double monthlyInterestRate = (annualInterestRate / 100) / 12; + System.out.print("Enter the number of months (e.g., 6): "); + int months = input.nextInt(); + input.close(); + + double total = 0; + + for (int i = 0; i < months; i++) { + total = (total + monthlySavings) * (1 + monthlyInterestRate); + } + System.out.printf("Amount in savings after %d months: %.3f", months, total); + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise31.java b/src/com/github/jonathanbirkey/chapter05/Exercise31.java new file mode 100644 index 0000000..03a6265 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise31.java @@ -0,0 +1,48 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Financial application: compute CD value) Suppose you put $10,000 into a CD with an annual + * percentage yield of 5.75%. After one month, the CD is worth + *

10000 + 10000 * 5.75 / 1200 = 10047.92 + *

After two months, the CD is worth + *

10047.91 + 10047.91 * 5.75 / 1200 = 10096.06 + *

After three months, the CD is worth + *

10096.06 + 10096.06 * 5.75 / 1200 = 10144.44 + *

and so on. + *

Write a program that prompts the user to enter an amount (e.g., 10000), the annual + * percentage yield (e.g., 5.75), and the number of months (e.g., 18) and displays a table as + * presented in the sample run. + *

Enter the initial deposit amount: 10000 + *

Enter annual percentage yield: 5.75 + *

Enter maturity period (number of months): 18 + *

Month CD Value + *

1 100047.92 + *

2 10096.06 + *

... + *

17 10846.57 + *

18 10898.54 + */ +package com.github.jonathanbirkey.chapter05; + +import java.util.Scanner; + +public class Exercise31 { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter the initial deposit amount: "); + double amount = input.nextDouble(); + System.out.print("Enter annual percentage yield: "); + double APY = input.nextDouble(); + System.out.print("Enter maturity period (number of months): "); + int months = input.nextInt(); + input.close(); + + System.out.printf("Month\tCD Value\n"); + + for (int i = 1; i <= months; i++) { + amount = amount + (amount * (APY / 1200)); + System.out.printf("%-7d %.2f\n", i, amount); + } + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise32.java b/src/com/github/jonathanbirkey/chapter05/Exercise32.java new file mode 100644 index 0000000..f91e745 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise32.java @@ -0,0 +1,44 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a two-digit + * number. The two digits in the number are distinct. (Hint: Generate the first digit. Use a + * loop to continuously generate the second digit until it is different from the first digit.) + */ +package com.github.jonathanbirkey.chapter05; + +import java.util.Scanner; + +public class Exercise32 { + public static void main(String[] args) { + int lotteryDigit1 = (int) (Math.random() * 10); + int lotteryDigit2 = 0; + do { + lotteryDigit2 = (int) (Math.random() * 10); + } while (lotteryDigit1 == lotteryDigit2); + + Scanner input = new Scanner(System.in); + System.out.print("Enter your lotter pick (two digits): "); + int guess = input.nextInt(); + input.close(); + + int guessDigit1 = guess / 10; + int guessDigit2 = guess % 10; + + System.out.printf("The lottery number is %d%d\n", lotteryDigit1, lotteryDigit2); + + if (guessDigit1 == lotteryDigit1 && guessDigit2 == lotteryDigit2) { + 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"); + } + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise33.java b/src/com/github/jonathanbirkey/chapter05/Exercise33.java new file mode 100644 index 0000000..000bb23 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise33.java @@ -0,0 +1,27 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Perfect number) A positive integer is called a perfect number if it is equal to the sum + * of all of its positive divisors, excluding itself. For example, 6 is the first perfect number + * because 6 = 3 + 2 + 1. The next is 28 = 14 + 7 + 4 + 2 + 1. There are four perfect numbers < + * 10,000. Write a program to find all these four numbers. + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise33 { + public static void main(String[] args) { + + for (int i = 1; i < 10000; i++) { + int sumPosDivisors = 0; + for (int j = 1; j <= (int) i / 2; j++) { + if (i % j == 0) { + sumPosDivisors += j; + } + } + if (i == sumPosDivisors) { + System.out.printf("Perfect number: %d\n", i); + } + } + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise34.java b/src/com/github/jonathanbirkey/chapter05/Exercise34.java new file mode 100644 index 0000000..c8aa969 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise34.java @@ -0,0 +1,74 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(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. + */ +package com.github.jonathanbirkey.chapter05; + +import java.util.Scanner; + +public class Exercise34 { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + int computerWins = 0; + int userWins = 0; + + while (computerWins < 2 && userWins < 2) { + int computerPick = (int) (Math.random() * 3); + System.out.print("scissor (0), rock (1), paper (2): "); + int userPick = input.nextInt(); + switch (computerPick) { + case 0: + System.out.print("The computer is scissor. "); + break; + case 1: + System.out.print("The computer is rock. "); + break; + case 2: + System.out.print("The computer is paper. "); + break; + default: + System.out.print("Invalide"); + } + switch (userPick) { + case 0: + System.out.print("You are scissor"); + break; + case 1: + System.out.print("You are rock"); + break; + case 2: + System.out.print("You are paper"); + break; + default: + System.out.print("Invalide"); + } + if (computerPick == userPick) { + System.out.println(" too. It is a draw"); + } else if (computerPick == 0 && userPick == 1) { + System.out.println(". You won\n"); + userWins++; + } else if (computerPick == 1 && userPick == 2) { + System.out.println(". You won\n"); + userWins++; + } else if (computerPick == 2 && userPick == 0) { + System.out.println(". You won\n"); + userWins++; + } else if (computerPick == 0 && userPick == 2) { + System.out.println(". You lost\n"); + computerWins++; + } else if (computerPick == 1 && userPick == 0) { + System.out.println(". You lost\n"); + computerWins++; + } else if (computerPick == 2 && userPick == 1) { + System.out.println(". You lost\n"); + computerWins++; + } + } + input.close(); + System.out.printf("\nFinal score\nComputer: %d\tUser: %d", computerWins, userWins); + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise35.java b/src/com/github/jonathanbirkey/chapter05/Exercise35.java new file mode 100644 index 0000000..5e3a255 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise35.java @@ -0,0 +1,18 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Summation) Write a program to compute the following summation: + *

(1/1+sqrt(2)) + (1/sqrt(2)+sqrt(3)) + (1/sqrt(3)+sqrt(4)) + ... + (1/sqrt(624)+sqrt(625)) + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise35 { + public static void main(String[] args) { + double summation = 0; + for (int i = 1; i <= 624; i++) { + summation += 1 / (Math.sqrt(i) + Math.sqrt(i + 1)); + } + System.out.printf("Summation = %f", summation); + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise36.java b/src/com/github/jonathanbirkey/chapter05/Exercise36.java new file mode 100644 index 0000000..dfe5aa9 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise36.java @@ -0,0 +1,48 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Business application: checking ISBN) Use loops to simplify Programming Exercise 3.9. + */ +package com.github.jonathanbirkey.chapter05; + +import java.util.Scanner; + +public class Exercise36 { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter the first 9 digits of an ISBN as integer: "); + int number = input.nextInt(); + input.close(); + + int tempNumber = number; + int leadingZeroCounter = 0; + int checksum = 0; + + for (int i = 9; i > 0; i--) { + int digit = tempNumber % 10; + tempNumber = tempNumber / 10; + if (digit == 0) { + leadingZeroCounter++; + } else { + leadingZeroCounter = 0; + } + checksum += digit * i; + } + + checksum = checksum % 11; + System.out.printf("The ISBN-10 number is "); + + if (checksum == 10) { + for (int j = 0; j < leadingZeroCounter; j++) { + System.out.print("0"); + } + System.out.printf("%dX", number); + } else { + for (int j = 0; j < leadingZeroCounter; j++) { + System.out.print("0"); + } + System.out.printf("%d%d", number, checksum); + } + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise37.java b/src/com/github/jonathanbirkey/chapter05/Exercise37.java new file mode 100644 index 0000000..6f1950f --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise37.java @@ -0,0 +1,42 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Decimal to binary) Write a program that prompts the user to enter a decimal integer then + * displays its corresponding binary value. Don’t use Java’s Integer.toBinaryString(int) in this + * program. + */ +package com.github.jonathanbirkey.chapter05; + +import java.util.Scanner; + +public class Exercise37 { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter a decimal integer: "); + int decimal = input.nextInt(); + input.close(); + + int count = 0; + int binaryNum = 0; + + while (decimal > 0) { + if (decimal % 2 == 0) { + binaryNum += 0; + } else { + binaryNum += 1; + } + decimal = decimal / 2; + if (decimal > 0) { + binaryNum = binaryNum * 10; + } + + count++; + } + + for (int i = 0; i < count; i++) { + System.out.printf("%d", binaryNum % 10); + binaryNum = binaryNum / 10; + } + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise38.java b/src/com/github/jonathanbirkey/chapter05/Exercise38.java new file mode 100644 index 0000000..030ed40 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise38.java @@ -0,0 +1,40 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Decimal to octal) Write a program that prompts the user to enter a decimal integer and + * displays its corresponding octal value. Don’t use Java’s Integer.toOctalString(int) in this + * program. + */ +package com.github.jonathanbirkey.chapter05; + +import java.util.Scanner; + +public class Exercise38 { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter a decimal integer: "); + int decimal = input.nextInt(); + input.close(); + + int count = 0; + int octalNum = 0; + + while (decimal > 0) { + octalNum += decimal % 8; + decimal = decimal / 8; + if (decimal > 0) { + octalNum = octalNum * 10; + } + + count++; + } + + // 127 = 177 + // 127 ÷ 8 = 15(Quotient) and (7)Remainder + for (int i = 0; i < count; i++) { + System.out.printf("%d", octalNum % 10); + octalNum = octalNum / 10; + } + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise39.java b/src/com/github/jonathanbirkey/chapter05/Exercise39.java new file mode 100644 index 0000000..406f58e --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise39.java @@ -0,0 +1,39 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Financial application: find the sales amount) You have just started a sales job in a + * department store. Your pay consists of a base salary and a commission. The base salary is + * $5,000. The scheme shown below is used to determine the commission rate. + *

Sales Amount - Commission Rate + *

$0.01–$5,000 - 8% + *

$5,000.01–$10,000 - 10% + *

$10,000.01 and above - 12% + *

Note this is a graduated rate. The rate for the first $5,000 is at 8%, the next $5,000 is + * at 10%, and the rest is at 12%. If the sales amount is 25,000, the commission is 5,000 * 8 + + * 5,000 * 10 + 15,000 * 12 = 2,700 + *

Your goal is to earn $30,000 a year. Write a program that finds out the minimum number of + * sales you have to generate in order to make $30,000. + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise39 { + public static void main(String[] args) { + double baseSalary = 5000; + double commission = 0; + double desiredEarnings = 30000; + double sales = 0; + + while (commission + baseSalary < desiredEarnings) { + if (sales <= 5000) { + commission = sales * 0.08; + } else if (sales <= 10000) { + commission = (5000 * 0.08) + ((sales - 5000) * 0.10); + } else { + commission = (5000 * 0.08) + (5000 * .10) + ((sales - 10000) * 0.12); + } + sales++; + } + System.out.printf("You would need to make $%.2f in sales to make at least $30,0000.", sales); + } +} \ No newline at end of file diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise40.java b/src/com/github/jonathanbirkey/chapter05/Exercise40.java new file mode 100644 index 0000000..d9fc920 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise40.java @@ -0,0 +1,26 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Simulation: heads or tails) Write a program that simulates flipping a coin one million + * times and displays the number of heads and tails. + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise40 { + public static void main(String[] args) { + int coin = 0; + int heads = 0; + int tails = 0; + + for (int i = 0; i < 1000000; i++) { + coin = (int) (Math.random() * 2); + if (coin == 1) { + heads++; + } else { + tails++; + } + } + System.out.printf("Heads: %d\nTails: %d", heads, tails); + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise41.java b/src/com/github/jonathanbirkey/chapter05/Exercise41.java new file mode 100644 index 0000000..85818dd --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise41.java @@ -0,0 +1,41 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Occurrence of max numbers) Write a program that reads integers, finds the largest of + * them, and counts its occurrences. Assume the input ends with number 0. Suppose you entered 3 + * 5 2 5 5 5 0; the program finds that the largest is 5 and the occurrence count for 5 is 4. If + * no input is entered, display "No numbers are entered except 0". + *

(Hint: Maintain two variables, max and count. max stores the current max number and count + * stores its occurrences. Initially, assign the first number to max and 1 to count. Compare + * each subsequent number with max. If the number is greater than max, assign it to max and + * reset count to 1. If the number is equal to max, increment count by 1.) + */ +package com.github.jonathanbirkey.chapter05; + +import java.util.Scanner; + +public class Exercise41 { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + + int max = 0; + int count = 0; + int number = -1; + + System.out.print("Enter Numbers: "); + + while (number != 0) { + number = input.nextInt(); + if (number > max) { + max = number; + count = 1; + } else if (number == max) { + count++; + } + } + input.close(); + System.out.printf("The largest number is %d\n", max); + System.out.printf("The occurrence count of the largest number is %d", count); + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise42.java b/src/com/github/jonathanbirkey/chapter05/Exercise42.java new file mode 100644 index 0000000..8d933ee --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise42.java @@ -0,0 +1,35 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Financial application: find the sales amount) Rewrite Programming Exercise 5.39 as + * follows: + *

Use a for loop instead of a do-while loop. + *

Let the user enter COMMISSION_SOUGHT instead of fixing it as a constant. + */ +package com.github.jonathanbirkey.chapter05; + +import java.util.Scanner; + +public class Exercise42 { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter commission sought: "); + double desiredCommission = input.nextDouble(); + double commission = 0; + double sales = 0; + + for (int i = 0; commission < desiredCommission; i++) { + if (sales <= 5000) { + commission = sales * 0.08; + } else if (sales <= 10000) { + commission = (5000 * 0.08) + ((sales - 5000) * 0.10); + } else { + commission = (5000 * 0.08) + (5000 * .10) + ((sales - 10000) * 0.12); + } + sales = i; + } + + System.out.printf("You would need to make $%.2f in sales to make at least $%.2f in commision.", sales, desiredCommission); + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise43.java b/src/com/github/jonathanbirkey/chapter05/Exercise43.java new file mode 100644 index 0000000..6576f9b --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise43.java @@ -0,0 +1,30 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Math: combinations) Write a program that displays all possible combinations for picking + * two numbers from integers 1 to 7. Also display the total number of all combinations. + *

1 2 + *

1 3 + *

... + *

... + *

The total number of all combinations is 21 + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise43 { + public static void main(String[] args) { + int totalCombinations = 0; + + for (int i = 1; i <= 7; i++) { + for (int j = 1; j <= 7; j++) { + if (j != i && j > i) { + System.out.printf("%d %d\n", i, j); + totalCombinations++; + } + } + } + + System.out.printf("The total number of all combinations is %d", totalCombinations); + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise44.java b/src/com/github/jonathanbirkey/chapter05/Exercise44.java new file mode 100644 index 0000000..644fae5 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise44.java @@ -0,0 +1,26 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Computer architecture: bit-level operations) A short value is stored in 16 bits. Write a + * program that prompts the user to enter a short integer and displays the 16 bits for the + * integer. Here are sample runs: + *

Enter an integer: 5 + *

The bits are 0000000000000101 + *

+ *

Enter an integer: -5 + *

The bits are 1111111111111011 + */ +package com.github.jonathanbirkey.chapter05; + +import java.util.Scanner; + +public class Exercise44 { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter an integer: "); + short decimal = input.nextShort(); + input.close(); + System.out.printf("The bits are %d", decimal); + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise45.java b/src/com/github/jonathanbirkey/chapter05/Exercise45.java new file mode 100644 index 0000000..c95083c --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise45.java @@ -0,0 +1,15 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Display leap years) Write a program that displays all the leap years, 10 per line, from + * 101 to 2100, separated by exactly one space. Also display the number of leap years in this + * period. + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise45 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise46.java b/src/com/github/jonathanbirkey/chapter05/Exercise46.java new file mode 100644 index 0000000..1c99188 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise46.java @@ -0,0 +1,15 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Display leap years) Write a program that displays all the leap years, 10 per line, from + * 101 to 2100, separated by exactly one space. Also display the number of leap years in this + * period. + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise46 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise47.java b/src/com/github/jonathanbirkey/chapter05/Exercise47.java new file mode 100644 index 0000000..63b5bb8 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise47.java @@ -0,0 +1,15 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Display leap years) Write a program that displays all the leap years, 10 per line, from + * 101 to 2100, separated by exactly one space. Also display the number of leap years in this + * period. + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise47 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise48.java b/src/com/github/jonathanbirkey/chapter05/Exercise48.java new file mode 100644 index 0000000..c9f9c0f --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise48.java @@ -0,0 +1,15 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Display leap years) Write a program that displays all the leap years, 10 per line, from + * 101 to 2100, separated by exactly one space. Also display the number of leap years in this + * period. + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise48 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise49.java b/src/com/github/jonathanbirkey/chapter05/Exercise49.java new file mode 100644 index 0000000..b1fc424 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise49.java @@ -0,0 +1,15 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Display leap years) Write a program that displays all the leap years, 10 per line, from + * 101 to 2100, separated by exactly one space. Also display the number of leap years in this + * period. + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise49 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise50.java b/src/com/github/jonathanbirkey/chapter05/Exercise50.java new file mode 100644 index 0000000..31be65c --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise50.java @@ -0,0 +1,15 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Display leap years) Write a program that displays all the leap years, 10 per line, from + * 101 to 2100, separated by exactly one space. Also display the number of leap years in this + * period. + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise50 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise51.java b/src/com/github/jonathanbirkey/chapter05/Exercise51.java new file mode 100644 index 0000000..1cb46da --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise51.java @@ -0,0 +1,15 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 28Feb2024 + *

(Display leap years) Write a program that displays all the leap years, 10 per line, from + * 101 to 2100, separated by exactly one space. Also display the number of leap years in this + * period. + */ +package com.github.jonathanbirkey.chapter05; + +public class Exercise51 { + public static void main(String[] args) { + // TODO: solve + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy