From 6e0e9aa70b74bdb1a564897bdc50f015666a519a Mon Sep 17 00:00:00 2001 From: Gavin Tran Date: Fri, 27 Jan 2023 12:46:36 -0500 Subject: [PATCH 1/2] Conformed to Java conventions and updated all directories of Chapter 7. --- .../chapter07/datetest/DateComparator.java | 47 ++++++++++++++++ .../java/chapter07/datetest/TheClass.java | 53 ------------------- .../chapter07/findbestfit/BestFitFinder.java | 36 +++++++++++++ .../java/chapter07/findbestfit/TheClass.java | 42 --------------- .../chapter07/leapyear/LeapYearChecker.java | 36 +++++++++++++ src/main/java/chapter07/leapyear/TheYear.java | 41 -------------- 6 files changed, 119 insertions(+), 136 deletions(-) create mode 100644 src/main/java/chapter07/datetest/DateComparator.java delete mode 100644 src/main/java/chapter07/datetest/TheClass.java create mode 100644 src/main/java/chapter07/findbestfit/BestFitFinder.java delete mode 100644 src/main/java/chapter07/findbestfit/TheClass.java create mode 100644 src/main/java/chapter07/leapyear/LeapYearChecker.java delete mode 100644 src/main/java/chapter07/leapyear/TheYear.java diff --git a/src/main/java/chapter07/datetest/DateComparator.java b/src/main/java/chapter07/datetest/DateComparator.java new file mode 100644 index 0000000..f0993f5 --- /dev/null +++ b/src/main/java/chapter07/datetest/DateComparator.java @@ -0,0 +1,47 @@ +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter07.datetest; + +import java.util.Scanner; + +public class DateComparator { + /** + * Compares two dates. No input verification. + * @return {@code true} if the first date is strictly later in the year and {@code false} otherwise. + */ + public static boolean isLater(int month1, int day1, int year1, int month2, int day2, int year2) { + if (year1 == year2) { + if (month1 == month2) { + return day1 > day2; + } else { + return month1 > month2; + } + } else { + return year1 > year2; + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter the first date (month day year): "); + int month1 = scanner.nextInt(); + int day1 = scanner.nextInt(); + int year1 = scanner.nextInt(); + + System.out.print("Enter the second date (month day year): "); + int month2 = scanner.nextInt(); + int day2 = scanner.nextInt(); + int year2 = scanner.nextInt(); + + System.out.println(); // newline + + String message = month1 + "/" + day1 + "/" + year1 + " "; + if (isLater(month1, day1, year1, month2, day2, year2)) { + message += "IS"; + } else { + message += "is NOT"; + } + message += " later than " + month2 + "/" + day2 + "/" + year2 + "."; + System.out.println(message); + } +} \ No newline at end of file diff --git a/src/main/java/chapter07/datetest/TheClass.java b/src/main/java/chapter07/datetest/TheClass.java deleted file mode 100644 index 3f9a051..0000000 --- a/src/main/java/chapter07/datetest/TheClass.java +++ /dev/null @@ -1,53 +0,0 @@ -package chapter07.datetest;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -import java.util.Scanner; - -public class TheClass { - - public static void main(String[] args) - { - Scanner kb = new Scanner(System.in); - - System.out.print("Enter the first date (month day year): "); - int month1 = kb.nextInt(); - int day1 = kb.nextInt(); - int year1 = kb.nextInt(); - - System.out.print("Enter the second date (month day year): "); - int month2 = kb.nextInt(); - int day2 = kb.nextInt(); - int year2 = kb.nextInt(); - - System.out.println(); // blank line - - String msg = month1 + "/" + day1 + "/" + year1; - if (isLater(month1, day1, year1, month2, day2, year2)) - msg += " IS "; - else - msg += " is NOT "; - msg += "later than " + month2 + "/" + day2 + "/" + year2; - System.out.println(msg); - } - - public static boolean isLater(int month1, int day1, int year1, int month2, int day2, int year2) - { - if (year1 > year2) - return true; - else if (year1 == year2) - { - if (month1 > month2) - return true; - else if (month1 == month2) - { - if (day1 > day2) - return true; - else - return false; - } - else - return false; - } - else - return false; - } - -} \ No newline at end of file diff --git a/src/main/java/chapter07/findbestfit/BestFitFinder.java b/src/main/java/chapter07/findbestfit/BestFitFinder.java new file mode 100644 index 0000000..872191b --- /dev/null +++ b/src/main/java/chapter07/findbestfit/BestFitFinder.java @@ -0,0 +1,36 @@ +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter07.findbestfit; + +import java.util.Scanner; + +public class BestFitFinder { + public static int findBestFit(int size1, int size2, int space) { + int sizeSum = size1 + size2; + int returnValue; + + if (sizeSum <= space) { + returnValue = 3; + } else if (size2 > size1 && size2 <= space) { + returnValue = 2; + } else if (size1 >= size2 && size1 <= space) { + returnValue = 1; + } else { + returnValue = 0; + } + + return returnValue; + } + + public static void main(String[] args) { + Scanner theScanner = new Scanner(System.in); + + System.out.print("Enter the first file size: "); + int size1 = theScanner.nextInt(); + System.out.print("Enter the second file size: "); + int size2 = theScanner.nextInt(); + System.out.print("Enter the total storage space: "); + int space = theScanner.nextInt(); + + System.out.println(findBestFit(size1, size2, space)); + } +} diff --git a/src/main/java/chapter07/findbestfit/TheClass.java b/src/main/java/chapter07/findbestfit/TheClass.java deleted file mode 100644 index 742ad0b..0000000 --- a/src/main/java/chapter07/findbestfit/TheClass.java +++ /dev/null @@ -1,42 +0,0 @@ -package chapter07.findbestfit;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -import java.util.Scanner; - -public class TheClass { - - public static void main(String[] args) - { - Scanner theScanner = new Scanner(System.in); - - System.out.print("Enter the first file size: "); - int size1 = theScanner.nextInt(); - System.out.print("Enter the second file size: "); - int size2 = theScanner.nextInt(); - System.out.print("Enter the total storage space: "); - int space = theScanner.nextInt(); - - System.out.println(findBestFit(size1, size2, space)); - } - - public static int findBestFit(int size1, int size2, int space) - { - int bothSize = size1 + size2; - int returnValue; - - if (bothSize <= space) - { - returnValue = 3; - } - else if (size2 > size1 && size2 <= space) - { - returnValue = 2; - } - else if (size1 >= size2 && size1 <= space) - { - returnValue = 1; - } - else - returnValue = 0; - - return returnValue; - } -} \ No newline at end of file diff --git a/src/main/java/chapter07/leapyear/LeapYearChecker.java b/src/main/java/chapter07/leapyear/LeapYearChecker.java new file mode 100644 index 0000000..ed540c9 --- /dev/null +++ b/src/main/java/chapter07/leapyear/LeapYearChecker.java @@ -0,0 +1,36 @@ +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter07.leapyear; + +import java.util.Scanner; + +/** + * A class that provides a method that determines if a year is a leap year. + */ +public class LeapYearChecker { + /** + * Determines if the given year is a leap year in the Gregorian calendar. + *

+ * Accurate only for positive integers as there is no 0 CE. Since the common era and before it starts at year + * 1, negative numbers are not accurately computed as years BCE. + * + * @param year a positive integer year. + * @return {@code true} if it is a leap year in the common era, undefined otherwise. + */ + public static Boolean isLeapYear(int year) { + return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); + } + + public static void main(String[] args) { + Scanner theScanner = new Scanner(System.in); + + System.out.print("Please input your year: "); + int years = theScanner.nextInt(); + theScanner.close(); + + if (isLeapYear(years)) { + System.out.println("This is a leap year"); + } else { + System.out.println("This is NOT a leap year"); + } + } +} \ No newline at end of file diff --git a/src/main/java/chapter07/leapyear/TheYear.java b/src/main/java/chapter07/leapyear/TheYear.java deleted file mode 100644 index b501708..0000000 --- a/src/main/java/chapter07/leapyear/TheYear.java +++ /dev/null @@ -1,41 +0,0 @@ -package chapter07.leapyear;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -import java.util.Scanner; - -public class TheYear -{ - public static void main(String[] args) - { - Scanner theScanner = new Scanner(System.in); - - System.out.print("Please input your year: "); - int years = theScanner.nextInt(); - - theScanner.close(); - - Boolean bool = isLeapYear(years); - if (bool == true) - System.out.println("This is a leap year"); - else - System.out.println("This is NOT a leap year"); - } - - public static Boolean isLeapYear(int year) - { - if (year % 4 == 0) - { - - if (year % 100 == 0) - { - - if (year % 400 == 0) - return true; - else - return false; - } - else - return true; - } - else - return false; - } -} \ No newline at end of file From d4156e5cfdd6acbc023113c5bd8c1c065f89c696 Mon Sep 17 00:00:00 2001 From: Gavin Tran Date: Fri, 27 Jan 2023 12:46:36 -0500 Subject: [PATCH 2/2] Conformed to Java conventions and updated all directories of Chapter 7. --- .../chapter07/datetest/DateComparator.java | 45 ++++++++++++++++ .../java/chapter07/datetest/TheClass.java | 53 ------------------- .../chapter07/findbestfit/BestFitFinder.java | 36 +++++++++++++ .../java/chapter07/findbestfit/TheClass.java | 42 --------------- .../chapter07/leapyear/LeapYearChecker.java | 36 +++++++++++++ src/main/java/chapter07/leapyear/TheYear.java | 41 -------------- 6 files changed, 117 insertions(+), 136 deletions(-) create mode 100644 src/main/java/chapter07/datetest/DateComparator.java delete mode 100644 src/main/java/chapter07/datetest/TheClass.java create mode 100644 src/main/java/chapter07/findbestfit/BestFitFinder.java delete mode 100644 src/main/java/chapter07/findbestfit/TheClass.java create mode 100644 src/main/java/chapter07/leapyear/LeapYearChecker.java delete mode 100644 src/main/java/chapter07/leapyear/TheYear.java diff --git a/src/main/java/chapter07/datetest/DateComparator.java b/src/main/java/chapter07/datetest/DateComparator.java new file mode 100644 index 0000000..41e8fda --- /dev/null +++ b/src/main/java/chapter07/datetest/DateComparator.java @@ -0,0 +1,45 @@ +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter07.datetest; + +import java.util.Scanner; + +public class DateComparator { + /** + * Compares two dates. No input verification. + * @return {@code true} if the first date is strictly later in the year and {@code false} otherwise. + */ + public static boolean isLater(int month1, int day1, int year1, int month2, int day2, int year2) { + if (year1 == year2) { + if (month1 == month2) { + return day1 > day2; + } + return month1 > month2; + } + return year1 > year2; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter the first date (month day year): "); + int month1 = scanner.nextInt(); + int day1 = scanner.nextInt(); + int year1 = scanner.nextInt(); + + System.out.print("Enter the second date (month day year): "); + int month2 = scanner.nextInt(); + int day2 = scanner.nextInt(); + int year2 = scanner.nextInt(); + + System.out.println(); // newline + + String message = month1 + "/" + day1 + "/" + year1 + " "; + if (isLater(month1, day1, year1, month2, day2, year2)) { + message += "IS"; + } else { + message += "is NOT"; + } + message += " later than " + month2 + "/" + day2 + "/" + year2 + "."; + System.out.println(message); + } +} \ No newline at end of file diff --git a/src/main/java/chapter07/datetest/TheClass.java b/src/main/java/chapter07/datetest/TheClass.java deleted file mode 100644 index 3f9a051..0000000 --- a/src/main/java/chapter07/datetest/TheClass.java +++ /dev/null @@ -1,53 +0,0 @@ -package chapter07.datetest;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -import java.util.Scanner; - -public class TheClass { - - public static void main(String[] args) - { - Scanner kb = new Scanner(System.in); - - System.out.print("Enter the first date (month day year): "); - int month1 = kb.nextInt(); - int day1 = kb.nextInt(); - int year1 = kb.nextInt(); - - System.out.print("Enter the second date (month day year): "); - int month2 = kb.nextInt(); - int day2 = kb.nextInt(); - int year2 = kb.nextInt(); - - System.out.println(); // blank line - - String msg = month1 + "/" + day1 + "/" + year1; - if (isLater(month1, day1, year1, month2, day2, year2)) - msg += " IS "; - else - msg += " is NOT "; - msg += "later than " + month2 + "/" + day2 + "/" + year2; - System.out.println(msg); - } - - public static boolean isLater(int month1, int day1, int year1, int month2, int day2, int year2) - { - if (year1 > year2) - return true; - else if (year1 == year2) - { - if (month1 > month2) - return true; - else if (month1 == month2) - { - if (day1 > day2) - return true; - else - return false; - } - else - return false; - } - else - return false; - } - -} \ No newline at end of file diff --git a/src/main/java/chapter07/findbestfit/BestFitFinder.java b/src/main/java/chapter07/findbestfit/BestFitFinder.java new file mode 100644 index 0000000..872191b --- /dev/null +++ b/src/main/java/chapter07/findbestfit/BestFitFinder.java @@ -0,0 +1,36 @@ +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter07.findbestfit; + +import java.util.Scanner; + +public class BestFitFinder { + public static int findBestFit(int size1, int size2, int space) { + int sizeSum = size1 + size2; + int returnValue; + + if (sizeSum <= space) { + returnValue = 3; + } else if (size2 > size1 && size2 <= space) { + returnValue = 2; + } else if (size1 >= size2 && size1 <= space) { + returnValue = 1; + } else { + returnValue = 0; + } + + return returnValue; + } + + public static void main(String[] args) { + Scanner theScanner = new Scanner(System.in); + + System.out.print("Enter the first file size: "); + int size1 = theScanner.nextInt(); + System.out.print("Enter the second file size: "); + int size2 = theScanner.nextInt(); + System.out.print("Enter the total storage space: "); + int space = theScanner.nextInt(); + + System.out.println(findBestFit(size1, size2, space)); + } +} diff --git a/src/main/java/chapter07/findbestfit/TheClass.java b/src/main/java/chapter07/findbestfit/TheClass.java deleted file mode 100644 index 742ad0b..0000000 --- a/src/main/java/chapter07/findbestfit/TheClass.java +++ /dev/null @@ -1,42 +0,0 @@ -package chapter07.findbestfit;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -import java.util.Scanner; - -public class TheClass { - - public static void main(String[] args) - { - Scanner theScanner = new Scanner(System.in); - - System.out.print("Enter the first file size: "); - int size1 = theScanner.nextInt(); - System.out.print("Enter the second file size: "); - int size2 = theScanner.nextInt(); - System.out.print("Enter the total storage space: "); - int space = theScanner.nextInt(); - - System.out.println(findBestFit(size1, size2, space)); - } - - public static int findBestFit(int size1, int size2, int space) - { - int bothSize = size1 + size2; - int returnValue; - - if (bothSize <= space) - { - returnValue = 3; - } - else if (size2 > size1 && size2 <= space) - { - returnValue = 2; - } - else if (size1 >= size2 && size1 <= space) - { - returnValue = 1; - } - else - returnValue = 0; - - return returnValue; - } -} \ No newline at end of file diff --git a/src/main/java/chapter07/leapyear/LeapYearChecker.java b/src/main/java/chapter07/leapyear/LeapYearChecker.java new file mode 100644 index 0000000..ed540c9 --- /dev/null +++ b/src/main/java/chapter07/leapyear/LeapYearChecker.java @@ -0,0 +1,36 @@ +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter07.leapyear; + +import java.util.Scanner; + +/** + * A class that provides a method that determines if a year is a leap year. + */ +public class LeapYearChecker { + /** + * Determines if the given year is a leap year in the Gregorian calendar. + *

+ * Accurate only for positive integers as there is no 0 CE. Since the common era and before it starts at year + * 1, negative numbers are not accurately computed as years BCE. + * + * @param year a positive integer year. + * @return {@code true} if it is a leap year in the common era, undefined otherwise. + */ + public static Boolean isLeapYear(int year) { + return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); + } + + public static void main(String[] args) { + Scanner theScanner = new Scanner(System.in); + + System.out.print("Please input your year: "); + int years = theScanner.nextInt(); + theScanner.close(); + + if (isLeapYear(years)) { + System.out.println("This is a leap year"); + } else { + System.out.println("This is NOT a leap year"); + } + } +} \ No newline at end of file diff --git a/src/main/java/chapter07/leapyear/TheYear.java b/src/main/java/chapter07/leapyear/TheYear.java deleted file mode 100644 index b501708..0000000 --- a/src/main/java/chapter07/leapyear/TheYear.java +++ /dev/null @@ -1,41 +0,0 @@ -package chapter07.leapyear;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -import java.util.Scanner; - -public class TheYear -{ - public static void main(String[] args) - { - Scanner theScanner = new Scanner(System.in); - - System.out.print("Please input your year: "); - int years = theScanner.nextInt(); - - theScanner.close(); - - Boolean bool = isLeapYear(years); - if (bool == true) - System.out.println("This is a leap year"); - else - System.out.println("This is NOT a leap year"); - } - - public static Boolean isLeapYear(int year) - { - if (year % 4 == 0) - { - - if (year % 100 == 0) - { - - if (year % 400 == 0) - return true; - else - return false; - } - else - return true; - } - else - return false; - } -} \ No newline at end of file