Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Source material: Object-Oriented Data Structures Using Java, 3rd Edition by Nell

ID: 3697365 • Letter: S

Question

Source material: Object-Oriented Data Structures Using Java, 3rd Edition by Nell Dale.

What is the answer to exercise 13 in chapter 3 (page 232)??

"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 of the runtime environment. At this point in the program bombs and the system provides some information, including the message.

c. Create another program similar to the one you just created, except this time, within your code, include a try-catch clause so that you catch the exception where it is thrown. If it is thrown, catch it, print it's message, and end the program normally."

Explanation / Answer

A.

// Exception class
class ThirteenException extends Exception {
  
   public ThirteenException(String message) {
       super(message);
   }

}


B.
public class TestException{
  
   public static String getString()throws IOException
   {
       InputStreamReader isr = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(isr);
       String s = br.readLine();
       return s;
   }
  
   public static void main(String[] args) throws IOException, ThirteenException {
      
       while(true){
           System.out.println("'Enter string: ");
          
           String input = getString();
          
           if(input.length() == 13)
               throw new ThirteenException("Use thirteen letter words and stainless steel to protect yourself!");
       }
   }
  
}


C.
public class TestException{
  
   public static String getString()throws IOException
   {
       InputStreamReader isr = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(isr);
       String s = br.readLine();
       return s;
   }
  
   public static void main(String[] args) throws IOException {
      
       try{
           while(true){
               System.out.println("'Enter string: ");
              
               String input = getString();
              
               if(input.length() == 13)
                   throw new ThirteenException("Use thirteen letter words and stainless steel to protect yourself!");
           }
       }catch(ThirteenException e){
           System.out.println(e.getMessage());
       }
   }
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote