From 97c634f97967477e297fa6d7f090608b7bf3678b Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Wed, 28 Feb 2024 09:43:13 -0500 Subject: [PATCH 01/20] finished Exercise27 --- .../jonathanbirkey/chapter05/Exercise27.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise27.java diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise27.java b/src/com/github/jonathanbirkey/chapter05/Exercise27.java new file mode 100644 index 0000000..c410e29 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise27.java @@ -0,0 +1,33 @@ +/** + * @author : Jonathan Birkey + * @mailto : jonathan.birkey@gmail.com + * @created : 27Dec2021 + *

(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); + } +} From c4004f691e285ec99593dae2d57e31ab579f146a Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Wed, 28 Feb 2024 10:21:53 -0500 Subject: [PATCH 02/20] refactoring --- README.md | 4 ++-- src/com/github/jonathanbirkey/chapter05/Exercise19.java | 1 + src/com/github/jonathanbirkey/chapter05/Exercise27.java | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) 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/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 index c410e29..7e29702 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise27.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise27.java @@ -1,7 +1,7 @@ /** * @author : Jonathan Birkey * @mailto : jonathan.birkey@gmail.com - * @created : 27Dec2021 + * @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. From 7ec8ff4537c1b94dc8525a577ff11a9eb32846b0 Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Thu, 29 Feb 2024 06:39:13 -0500 Subject: [PATCH 03/20] finished exercise 28 --- .../jonathanbirkey/chapter05/Exercise28.java | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise28.java 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; + } + } +} From beaf0e8a0f1a89f14bb70faa1cb39ecc5c29a094 Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Thu, 29 Feb 2024 06:39:55 -0500 Subject: [PATCH 04/20] refactoring --- src/com/github/jonathanbirkey/chapter03/Exercise21.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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: From 3ce63be7617598e68bc8b9916c94f004a4f45e8e Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Mon, 4 Mar 2024 18:28:50 -0500 Subject: [PATCH 05/20] finished exercise 29 --- .../jonathanbirkey/chapter05/Exercise29.java | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise29.java 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; + } + } +} From e32c84a28e79852faf0baf7b205505a072680939 Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Thu, 7 Mar 2024 08:21:05 -0500 Subject: [PATCH 06/20] finished exercise 30 --- .../jonathanbirkey/chapter05/Exercise30.java | 41 +++++++++++++++++++ .../jonathanbirkey/chapter05/Exercise31.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise32.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise33.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise34.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise35.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise36.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise37.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise38.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise39.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise40.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise41.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise42.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise43.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise44.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise45.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise46.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise47.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise48.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise49.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise50.java | 15 +++++++ .../jonathanbirkey/chapter05/Exercise51.java | 15 +++++++ 22 files changed, 356 insertions(+) create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise30.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise31.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise32.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise33.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise34.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise35.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise36.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise37.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise38.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise39.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise40.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise41.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise42.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise43.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise44.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise45.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise46.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise47.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise48.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise49.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise50.java create mode 100644 src/com/github/jonathanbirkey/chapter05/Exercise51.java 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..b2ecc18 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise31.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 Exercise31 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise32.java b/src/com/github/jonathanbirkey/chapter05/Exercise32.java new file mode 100644 index 0000000..fa388ac --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise32.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 Exercise32 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise33.java b/src/com/github/jonathanbirkey/chapter05/Exercise33.java new file mode 100644 index 0000000..9310c76 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise33.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 Exercise33 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise34.java b/src/com/github/jonathanbirkey/chapter05/Exercise34.java new file mode 100644 index 0000000..5a28250 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise34.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 Exercise34 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise35.java b/src/com/github/jonathanbirkey/chapter05/Exercise35.java new file mode 100644 index 0000000..c2c90a8 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise35.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 Exercise35 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise36.java b/src/com/github/jonathanbirkey/chapter05/Exercise36.java new file mode 100644 index 0000000..6857864 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise36.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 Exercise36 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise37.java b/src/com/github/jonathanbirkey/chapter05/Exercise37.java new file mode 100644 index 0000000..dab6746 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise37.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 Exercise37 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise38.java b/src/com/github/jonathanbirkey/chapter05/Exercise38.java new file mode 100644 index 0000000..93ae612 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise38.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 Exercise38 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise39.java b/src/com/github/jonathanbirkey/chapter05/Exercise39.java new file mode 100644 index 0000000..82fb0fe --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise39.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 Exercise39 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise40.java b/src/com/github/jonathanbirkey/chapter05/Exercise40.java new file mode 100644 index 0000000..38cb720 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise40.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 Exercise40 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise41.java b/src/com/github/jonathanbirkey/chapter05/Exercise41.java new file mode 100644 index 0000000..c1af0d2 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise41.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 Exercise41 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise42.java b/src/com/github/jonathanbirkey/chapter05/Exercise42.java new file mode 100644 index 0000000..8a204cc --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise42.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 Exercise42 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise43.java b/src/com/github/jonathanbirkey/chapter05/Exercise43.java new file mode 100644 index 0000000..832cee7 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise43.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 Exercise43 { + public static void main(String[] args) { + // TODO: solve + } +} diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise44.java b/src/com/github/jonathanbirkey/chapter05/Exercise44.java new file mode 100644 index 0000000..cd934d9 --- /dev/null +++ b/src/com/github/jonathanbirkey/chapter05/Exercise44.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 Exercise44 { + public static void main(String[] args) { + // TODO: solve + } +} 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 + } +} From 1ea55ac364cb580d874a61d9e5b366538d467ae7 Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Thu, 7 Mar 2024 08:37:25 -0500 Subject: [PATCH 07/20] finished exercise 31 --- .../jonathanbirkey/chapter05/Exercise31.java | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise31.java b/src/com/github/jonathanbirkey/chapter05/Exercise31.java index b2ecc18..03a6265 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise31.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise31.java @@ -2,14 +2,47 @@ * @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. + *

(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) { - // TODO: solve + 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); + } } } From c214fc315e8e95c75c9683fff19392c74e8eb9bf Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Thu, 7 Mar 2024 09:39:07 -0500 Subject: [PATCH 08/20] finished exercise 32 --- .../jonathanbirkey/chapter05/Exercise32.java | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise32.java b/src/com/github/jonathanbirkey/chapter05/Exercise32.java index fa388ac..bab3606 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise32.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise32.java @@ -2,14 +2,44 @@ * @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. + *

(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) { - // TODO: solve + 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"); + } } } From c3cd77df1ec86d69d2c6c2d44590c4edd1e524b0 Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Thu, 7 Mar 2024 11:17:56 -0500 Subject: [PATCH 09/20] finished exercise 33 --- .../jonathanbirkey/chapter05/Exercise33.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise33.java b/src/com/github/jonathanbirkey/chapter05/Exercise33.java index 9310c76..000bb23 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise33.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise33.java @@ -2,14 +2,26 @@ * @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. + *

(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) { - // TODO: solve + + 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); + } + } } } From e67ea10c81aece8edcd4678ccdbda5f9900f67c7 Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Thu, 7 Mar 2024 11:27:14 -0500 Subject: [PATCH 10/20] finised exercise 34 --- .../jonathanbirkey/chapter03/Exercise17.java | 2 + .../jonathanbirkey/chapter05/Exercise34.java | 67 +++++++++++++++++-- 2 files changed, 65 insertions(+), 4 deletions(-) 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/chapter05/Exercise34.java b/src/com/github/jonathanbirkey/chapter05/Exercise34.java index 5a28250..c8aa969 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise34.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise34.java @@ -2,14 +2,73 @@ * @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. + *

(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) { - // TODO: solve + 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); } } From 5f2cabe20cca677219cb0de1be7ae6cb079e39ad Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Thu, 7 Mar 2024 11:35:52 -0500 Subject: [PATCH 11/20] finished exercise 35 --- .../github/jonathanbirkey/chapter05/Exercise35.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise35.java b/src/com/github/jonathanbirkey/chapter05/Exercise35.java index c2c90a8..5e3a255 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise35.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise35.java @@ -2,14 +2,17 @@ * @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. + *

(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) { - // TODO: solve + 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); } } From 14340d21d71ba5f0f3af4f5b4d1b47607db405b8 Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Thu, 7 Mar 2024 14:57:50 -0500 Subject: [PATCH 12/20] finished exercise 36 --- .../jonathanbirkey/chapter03/Exercise09.java | 10 +++-- .../jonathanbirkey/chapter05/Exercise36.java | 41 +++++++++++++++++-- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter03/Exercise09.java b/src/com/github/jonathanbirkey/chapter03/Exercise09.java index c9b0f76..20a6a6d 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,9 @@ 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/chapter05/Exercise36.java b/src/com/github/jonathanbirkey/chapter05/Exercise36.java index 6857864..825a947 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise36.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise36.java @@ -2,14 +2,47 @@ * @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. + *

(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) { - // TODO: solve + 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); + } } } From 06ab66c0b20059faff3316d418e8626ea3cf9c3c Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Sun, 10 Mar 2024 19:48:49 -0400 Subject: [PATCH 13/20] finished exercise 37, this one was interesting to solve using only loops --- .../jonathanbirkey/chapter05/Exercise37.java | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise37.java b/src/com/github/jonathanbirkey/chapter05/Exercise37.java index dab6746..ab2dab9 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise37.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise37.java @@ -2,14 +2,41 @@ * @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. + *

(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) { - // TODO: solve + Scanner input = new Scanner(System.in); + System.out.print("Enter a decimal integer: "); + int decimal = input.nextInt(); + input.close(); + + int count = 0; + int binary = 0; + + while (decimal > 0) { + if (decimal % 2 == 0) { + binary += 0; + } else { + binary += 1; + } + decimal = decimal / 2; + if (decimal > 0) { + binary = binary * 10; + } + + count++; + } + + for (int i = 0; i < count; i++) { + System.out.printf("%d", binary % 10); + binary = binary / 10; + } } } From 3e1db122c032686425949f7a2a6ccdea888c066c Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Sun, 10 Mar 2024 19:59:52 -0400 Subject: [PATCH 14/20] finished exercise 38 --- .../jonathanbirkey/chapter05/Exercise37.java | 12 +++---- .../jonathanbirkey/chapter05/Exercise38.java | 33 ++++++++++++++++--- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise37.java b/src/com/github/jonathanbirkey/chapter05/Exercise37.java index ab2dab9..6f1950f 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise37.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise37.java @@ -18,25 +18,25 @@ public static void main(String[] args) { input.close(); int count = 0; - int binary = 0; + int binaryNum = 0; while (decimal > 0) { if (decimal % 2 == 0) { - binary += 0; + binaryNum += 0; } else { - binary += 1; + binaryNum += 1; } decimal = decimal / 2; if (decimal > 0) { - binary = binary * 10; + binaryNum = binaryNum * 10; } count++; } for (int i = 0; i < count; i++) { - System.out.printf("%d", binary % 10); - binary = binary / 10; + 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 index 93ae612..030ed40 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise38.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise38.java @@ -2,14 +2,39 @@ * @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. + *

(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) { - // TODO: solve + 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; + } } } From 01d3d3e433ac58aa8e6518d86c76e43548a6ceff Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Sun, 10 Mar 2024 20:21:56 -0400 Subject: [PATCH 15/20] finished exercise 39 --- .../jonathanbirkey/chapter05/Exercise39.java | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise39.java b/src/com/github/jonathanbirkey/chapter05/Exercise39.java index 82fb0fe..406f58e 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise39.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise39.java @@ -2,14 +2,38 @@ * @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. + *

(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) { - // TODO: solve + 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 From c5756fd02a158bb484f7b243ca8f27d297c05081 Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Sun, 10 Mar 2024 20:33:43 -0400 Subject: [PATCH 16/20] finished exercise 40 --- .../jonathanbirkey/chapter05/Exercise40.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise40.java b/src/com/github/jonathanbirkey/chapter05/Exercise40.java index 38cb720..0c10261 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise40.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise40.java @@ -2,14 +2,25 @@ * @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. + *

(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) { - // TODO: solve + 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); } } From 12b996b282c9b7d685c6fae019ea855c1bdabd29 Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Mon, 11 Mar 2024 10:27:48 -0400 Subject: [PATCH 17/20] finished exercise 41 --- .../jonathanbirkey/chapter05/Exercise41.java | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise41.java b/src/com/github/jonathanbirkey/chapter05/Exercise41.java index c1af0d2..f32a71e 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise41.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise41.java @@ -2,14 +2,41 @@ * @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. + *

(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) { - // TODO: solve + 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); } } From 54643ac0147c12082c6d13aa1a8ff17c2307c1d4 Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Mon, 11 Mar 2024 10:39:37 -0400 Subject: [PATCH 18/20] finished exercise 42 --- .../jonathanbirkey/chapter05/Exercise42.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise42.java b/src/com/github/jonathanbirkey/chapter05/Exercise42.java index 8a204cc..8d933ee 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise42.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise42.java @@ -2,14 +2,34 @@ * @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. + *

(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) { - // TODO: solve + 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); } } From 1e24be700ba4e066ceb2378917bb4f554bb5c68b Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Wed, 13 Mar 2024 11:34:30 -0400 Subject: [PATCH 19/20] finished exercise 43 --- .../jonathanbirkey/chapter05/Exercise43.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise43.java b/src/com/github/jonathanbirkey/chapter05/Exercise43.java index 832cee7..6576f9b 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise43.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise43.java @@ -2,14 +2,29 @@ * @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. + *

(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) { - // TODO: solve + 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); } } From be8453639b4d6be9e4c0060aa25b4949f0cd96dd Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Wed, 13 Mar 2024 11:46:14 -0400 Subject: [PATCH 20/20] fixing format errors --- .../jonathanbirkey/chapter03/Exercise09.java | 7 +++- .../jonathanbirkey/chapter05/Exercise32.java | 7 ++-- .../jonathanbirkey/chapter05/Exercise36.java | 38 +++++++++---------- .../jonathanbirkey/chapter05/Exercise40.java | 16 ++++---- .../jonathanbirkey/chapter05/Exercise41.java | 23 ++++++----- .../jonathanbirkey/chapter05/Exercise44.java | 19 ++++++++-- 6 files changed, 61 insertions(+), 49 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter03/Exercise09.java b/src/com/github/jonathanbirkey/chapter03/Exercise09.java index 20a6a6d..c0600aa 100644 --- a/src/com/github/jonathanbirkey/chapter03/Exercise09.java +++ b/src/com/github/jonathanbirkey/chapter03/Exercise09.java @@ -47,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("The ISBN-10 number is %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("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); + 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/chapter05/Exercise32.java b/src/com/github/jonathanbirkey/chapter05/Exercise32.java index bab3606..f91e745 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise32.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise32.java @@ -15,10 +15,9 @@ public static void main(String[] args) { int lotteryDigit1 = (int) (Math.random() * 10); int lotteryDigit2 = 0; do { - lotteryDigit2 = (int) (Math.random() * 10); - }while(lotteryDigit1 == lotteryDigit2); - - + 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(); diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise36.java b/src/com/github/jonathanbirkey/chapter05/Exercise36.java index 825a947..dfe5aa9 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise36.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise36.java @@ -14,35 +14,35 @@ public static void main(String[] args) { 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; + 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); + 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); + 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/Exercise40.java b/src/com/github/jonathanbirkey/chapter05/Exercise40.java index 0c10261..d9fc920 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise40.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise40.java @@ -12,15 +12,15 @@ 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++; - } + coin = (int) (Math.random() * 2); + if (coin == 1) { + heads++; + } else { + tails++; + } } - System.out.printf("Heads: %d\nTails: %d",heads, 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 index f32a71e..85818dd 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise41.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise41.java @@ -18,22 +18,21 @@ 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++; - } - + + 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); diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise44.java b/src/com/github/jonathanbirkey/chapter05/Exercise44.java index cd934d9..644fae5 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise44.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise44.java @@ -2,14 +2,25 @@ * @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. + *

(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) { - // TODO: solve + 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); } }