diff --git a/OOP/Java/Lab/Week5/Book.java b/OOP/Java/Lab/Week5/Book.java index 462f32b..815f1fd 100644 --- a/OOP/Java/Lab/Week5/Book.java +++ b/OOP/Java/Lab/Week5/Book.java @@ -1,6 +1,7 @@ import java.util.Scanner; class Book { + String title, author; int edition; @@ -11,11 +12,14 @@ class Book { } public String toString() { - return String.format("Title: %s, Author: %s, Edition: %d", title, author, edition); + return String.format( + "Title: %s, Author: %s, Edition: %d", + title, + author, + edition + ); } -} -class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of books: "); @@ -41,9 +45,19 @@ class Main { String searchAuthor = sc.nextLine(); for (Book book : books) { - if (book.author.equals(searchAuthor)) { + if (book.author.equalsIgnoreCase(searchAuthor)) { System.out.println(book); + return; } } + System.out.println("No books found for the author: " + searchAuthor); + + if (!found) { + System.out.println( + "No books found for the author: " + searchAuthor + ); + } + + sc.close(); } } diff --git a/OOP/Java/Lab/Week5/Number.java b/OOP/Java/Lab/Week5/Number.java index a371df3..b18216b 100644 --- a/OOP/Java/Lab/Week5/Number.java +++ b/OOP/Java/Lab/Week5/Number.java @@ -25,6 +25,7 @@ class Number { } public boolean isEven() { + // This is an explicit narrowing typecast from double to int return (int) value % 2 == 0; } @@ -48,13 +49,22 @@ class Number { System.out.print("Enter a number: "); Number number = new Number(sc.nextDouble()); - System.out.println("Is Zero: " + number.isZero()); - System.out.println("Is Positive: " + number.isPositive()); - System.out.println("Is Negative: " + number.isNegative()); - System.out.println("Is Odd: " + number.isOdd()); - System.out.println("Is Even: " + number.isEven()); - System.out.println("Is Prime: " + number.isPrime()); - System.out.println("Is Armstrong: " + number.isArmstrong()); + System.out.println( + "Is Zero: " + + number.isZero() + + ", Is Positive: " + + number.isPositive() + + ", Is Negative: " + + number.isNegative() + + ", Is Odd: " + + number.isOdd() + + ", Is Even: " + + number.isEven() + + ", Is Prime: " + + number.isPrime() + + ", Is Armstrong: " + + number.isArmstrong() + ); sc.close(); } diff --git a/OOP/Java/Lab/Week5/Time.java b/OOP/Java/Lab/Week5/Time.java index 8888191..11e19c9 100644 --- a/OOP/Java/Lab/Week5/Time.java +++ b/OOP/Java/Lab/Week5/Time.java @@ -8,7 +8,7 @@ public class Time { // initializing to zero hr = 0; min = 0; - min = 0; + sec = 0; } Time(int hour, int minute, int second) {