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());
}
}
}
Define subclasses of InvalidTimeException InvalidHourException and InvalidMinuteException Public static methods: checkHour(String hh), checkMinute(String mm) Test ≥...
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...
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 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 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 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 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.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 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...
Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...
What is the output of the following question? import java.io.IOException; import java.util. EmptyStackException: public class newclass public static void main(String[] args) try System.out.printf("%d", 1); throw (new Exception()); catch (IOException e) System.out.printf("%d", 2); catch(EmptyStackException e) System.out.printf("%d", 3); catch(Exception e) System.out.printf("%d", 4); } finally System.out.printf("%d", 5); Q3) (15 points) a.Write a JAVA program that reads an arra JAVA program that reads an array from input file and invo Sort and Max ,that sorts the elements of the array and s the elements...