I need help creating a program in Java. Here is the question: a. Create a \"stan
ID: 2247320 • Letter: I
Question
I need help creating a program in Java. Here is the question:
a. Create a "standard" exception class called ThirteenException.
b. Write a program that repeatedly prompts the user to enter a string. After each string is entered, the program outputs the length of the string, unless the length of the string is 13, in which case the ThirteenException is thrown with the message "Use thirteen letter words and stainless steel to protect yourself!" Your main method should simply throw the ThirteenException exception out to the run-time environment. A sample run of the program might be:
Input a string > Villanova University
That string has length of 20
Input a string > Triscadecaphobia
That string has length 16
Input a string < misprogrammed
At this point the program bombs and the system provides some information, including the "Use thirteen letter words and stainless steel to protect yourself!" message.
C. Create another program similar to the one you created for part b, except this time, within your code, include a try-catch clause so that you catch the exception when it is thrown. If it is thrown, then catch it, print its message, and end the program "normally".
Explanation / Answer
Below is your code: -
a)
ThirteenException.java
public class ThirteenException extends Exception {
public ThirteenException() {
super("The thirteen exception occurred");
}
public ThirteenException(String message) {
super(message);
}
}
b)
StringTest1.java
import java.util.Scanner;
public class StringTest1 {
public static void main(String[] args) throws ThirteenException {
boolean done = false;
Scanner sc = new Scanner(System.in);
String word;
while (!done) {
System.out.print("Input a string > ");
word = sc.nextLine();
if (word.length() != 13) {
System.out.println("That string has length of " + word.length());
} else {
throw new ThirteenException(
"Thirteen Exception Occured. Use thirteen letter words and stainless steel to protect yourself!");
}
}
}
}
Sample Run : -
Input a string > Villanova University
That string has length of 20
Input a string > Triscadecaphobia
That string has length of 16
Input a string > misprogrammed
Exception in thread "main" sorttree.ThirteenException: Thirteen Exception Occured.
Use thirteen letter words and stainless steel to protect yourself!
at sorttree.StringTest1.main(StringTest1.java:16)
c)
StringTest2.java
import java.util.Scanner;
public class StringTest2 {
public static void main(String[] args) {
boolean done = false;
Scanner sc = new Scanner(System.in);
String word;
try {
while (!done) {
System.out.print("Input a string > ");
word = sc.nextLine();
if (word.length() != 13) {
System.out.println("That string has length of " + word.length());
} else {
throw new ThirteenException(
"Thirteen Exception Occured. Use thirteen letter words and stainless steel to protect yourself!");
}
}
} catch (ThirteenException e) {
System.out.println(e.getMessage());
}
}
}
Output: -
Input a string > Villanova University
That string has length of 20
Input a string > Triscadecaphobia
That string has length of 16
Input a string > misprogrammed
Thirteen Exception Occured.
Use thirteen letter words and stainless steel to protect yourself!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.