Question

Define subclasses of InvalidTimeException InvalidHourException and InvalidMinuteException Public static methods: checkHour(String hh), checkMinute(String mm) Test ≥...

Define subclasses of InvalidTimeException
InvalidHourException and InvalidMinuteException
Public static methods:
checkHour(String hh), checkMinute(String mm)
Test ≥ 2 strings; if invalid, throw an appropriate exception

java
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for the two required exception classes. Assuming InvalidTimeException is already defined, and has a constructor taking String value. Testing of the checkHour method and checkMinute method is done inside main method of each exception class respectively. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Make sure you copy below two classes into separate files as mentioned. Do not copy everything to a single file.

//InvalidHourException.java

public class InvalidHourException extends InvalidTimeException {

      // constructor taking an error message

      public InvalidHourException(String msg) {

            // passing error message to super class. assuming InvalidTimeException

            // has a constructor taking String argument, if not either add one, or

            // remove this constructor

            super(msg);

      }

      // method to check if a hour value is valid. does nothing if hour is

      // valid, throws InvalidHourException if hour is invalid

      public static void checkHour(String hh) throws InvalidHourException {

            // setting a flag error to true if either hh is null or if it has length

            // greater than 2

            boolean error = hh == null || hh.length() > 2;

            // parsing hours as integer

            try {

                  int h = Integer.parseInt(hh);

                  // if h is outside the range 1-12, setting error to true

                  if (h < 1 || h > 12) {

                        error = true;

                  }

            } catch (Exception e) {

                  // if any exception occurred while parsing, setting error to true

                  error = true;

            }

            // if error is true, throwing InvalidHourException

            if (error) {

                  throw new InvalidHourException("Invalid hour");

            }

      }

      // main method for testing

      public static void main(String[] args) {

            // testing checkHour method with three test cases

            String h = "12";

            System.out.println("Testing hour=" + h);

            try {

                  checkHour(h);

                  // if no exception is occurred, displaying that hour is valid

                  System.out.println(h + " is valid");

            } catch (InvalidHourException e) {

                  System.out.println(e.getMessage());

            }

            h = "01";

            System.out.println("Testing hour=" + h);

            try {

                  checkHour(h);

                  System.out.println(h + " is valid");

            } catch (InvalidHourException e) {

                  System.out.println(e.getMessage());

            }

            h = "-2";

            System.out.println("Testing hour=" + h);

            try {

                  checkHour(h);

                  System.out.println(h + " is valid");

            } catch (InvalidHourException e) {

                  System.out.println(e.getMessage());

            }

      }

}

//InvalidMinuteException.java

public class InvalidMinuteException extends InvalidTimeException {

      // constructor taking an error message

      public InvalidMinuteException(String msg) {

            // passing error message to super class. assuming InvalidTimeException

            // has a constructor taking String argument, if not either add one, or

            // remove this constructor

            super(msg);

      }

      // method to check if a minute value is valid. does nothing if minute is

      // valid, throws InvalidMinuteException if hour is invalid

      public static void checkMinute(String mm) throws InvalidMinuteException {

            // setting a flag error to true if either mm is null or if it has length

            // greater than 2

            boolean error = mm == null || mm.length() > 2;

            // parsing minute

            try {

                  int m = Integer.parseInt(mm);

                  // if m is outside the range 0-59, setting error to true

                  if (m < 0 || m > 59) {

                        error = true;

                  }

            } catch (Exception e) {

                  // if any exception occurred while parsing, setting error to true

                  error = true;

            }

            // if error is true, throwing InvalidMinuteException

            if (error) {

                  throw new InvalidMinuteException("Invalid minute");

            }

      }

      // main method for testing

      public static void main(String[] args) {

            // testing checkMinute method with three test cases

            String m = "26";

            System.out.println("Testing minute=" + m);

            try {

                  checkMinute(m);

                  System.out.println(m + " is valid");

            } catch (InvalidMinuteException e) {

                  System.out.println(e.getMessage());

            }

            m = "66";

            System.out.println("Testing minute=" + m);

            try {

                  checkMinute(m);

                  System.out.println(m + " is valid");

            } catch (InvalidMinuteException e) {

                  System.out.println(e.getMessage());

            }

            m = "-2";

            System.out.println("Testing minute=" + m);

            try {

                  checkMinute(m);

                  System.out.println(m + " is valid");

            } catch (InvalidMinuteException e) {

                  System.out.println(e.getMessage());

            }

      }

}

Add a comment
Know the answer?
Add Answer to:
Define subclasses of InvalidTimeException InvalidHourException and InvalidMinuteException Public static methods: checkHour(String hh), checkMinute(String mm) Test ≥...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java code about writing two methods: public static String randomStringone(int length) public static String randomStringtwo(int length)...

    Java code about writing two methods: public static String randomStringone(int length) public static String randomStringtwo(int length) This method should return a String of random lowercase letters with the given length by using for loops. To generate a random lowercase letter, use a local Random variable and the method nextInt() to generate a number between 97 and 122, then cast the result to a char. The method nextInt() can be found here: https://www.geeksforgeeks.org/java-util-random-nextint-java/    In randomStringone(), you should use String concatenation...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • (IN JAVA) Write a program named Review.java and implement the following methods: public static boolean isValidTicTacToeBoardString(String...

    (IN JAVA) Write a program named Review.java and implement the following methods: public static boolean isValidTicTacToeBoardString(String str) This method takes as its parameter a String that contains 9 characters representing the status of the Tic Tact Toe game. The String is mixed with X, O, x, o, and other letters. The first three letters represent the first row, letter 4 through letter 6 represent the second row, and the rest for the last row. To be considered as a valid...

  • You will write three static methods to manipulate an input String in different ways using various...

    You will write three static methods to manipulate an input String in different ways using various String methods. You need to provide the code for each of the three static methods in class StringPlay (requirements for each listed below). You will also change the control statement in the test harness to allow for variations in the sentinel value. You need to modify the loop control condition in Lab09.java so that user inputs of ‘finish’, “FINISH”, “FiniSH”, “fINISH”, etc. will end...

  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list...

    Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list using bubble sort Do not modify the test code in each function. You can look at that code for some ideas for implementing the methods. import java.lang.Comparable; import java.util.*; public class IteratorExercise {       public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {        // first line to start you off        ListIterator<E> iit = c.listIterator(), jit;...

  • 1-what do static, private and public modifiers do to methods and fields in java ? 2-difference...

    1-what do static, private and public modifiers do to methods and fields in java ? 2-difference between data streams and filtering streams? 3-when should an exception be thrown or catched?

  • Complete the program below in order to make run properly by competing validName methods and add...

    Complete the program below in order to make run properly by competing validName methods and add trycatch block in the main method public class ExceptionWithThrow { public static String validName(String name) throws InputMismatchException{ // check here if the name is a valid name // through InputMismatchException if invalid //throw // return the name if it is valid } public static void main(String[] args) { // Ask the user to enter a name and their validity through validName method // Add...

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT