Question

Need some help on the following problem in java. I have to modify the code I...

Need some help on the following problem in java. I have to modify the code I written so far(below) to do the following three things:

a). Write a method called diagnostics that occasionally throws any one of the exceptions you have created depending on the values generated by a random-number generator.

b). Write a method called display that calls diagnostics and provides exception handlers to display a message when an exception occurs. Otherwise, if an exception does not occur, method display should print out "Diagnostic test completed--no problem found."

c). Suppose that the display method contains three catch clauses with parameter types: FootBrakeException, BrakeException, and Exception. Explain which orders of the catch blocks would prevent the execution of an exception handler.

//code

public class AutoExceptions {
   class NoStartException extends Exception {
       public NoStartException() {
           this("No Start Exception");
       }
       public NoStartException(String message) {
           super(message);
       }
           class BadIgnitionException extends NoStartException{
               public BadIgnitionException() {
                   this("Bad Ignition Exception");
               }
               public BadIgnitionException(String message) {
                   super(message);
               }}
           class DeadBatteryException extends NoStartException{
               public DeadBatteryException() {
                   this("Dead Battery Exception");
               }
               public DeadBatteryException(String message) {
                   super(message);
               }
       }
   }
   class LightsException extends Exception {
       public LightsException() {
           this("Lights Exception");
       }
       public LightsException(String message) {
           super(message);
       }
           class HeadLightException extends LightsException{
               public HeadLightException() {
                   this("Head Light Exception");
               }
               public HeadLightException(String message) {
                   super(message);
               }}
           class BrakeLightException extends LightsException{
               public BrakeLightException() {
                   this("Brake Light Exception");
               }
               public BrakeLightException(String message) {
                   super(message);
               }
       }
           class TailLightException extends LightsException{
               public TailLightException() {
                   this("Tail Light Exception");
               }
               public TailLightException(String message) {
                   super(message);
               }
       }
   }
   class BrakeException extends Exception {
       public BrakeException() {
           this("Brake Exception");
       }
       public BrakeException(String message) {
           super(message);
       }
           class FootBrakeException extends BrakeException{
               public FootBrakeException() {
                   this("Foot Brake Exception");
               }
               public FootBrakeException(String message) {
                   super(message);
               }}
           class ParkingBrakeException extends BrakeException{
               public ParkingBrakeException() {
                   this("Parking Brake Exception");
               }
               public ParkingBrakeException(String message) {
                   super(message);
               }
       }
   }
}

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

Modified Code->

import java.util.Random;

public class AutoExceptions {

    class NoStartException extends Exception {

        public NoStartException() {

            this("No Start Exception");

        }

        public NoStartException(String message) {

            super(message);

        }

        class BadIgnitionException extends NoStartException {

            public BadIgnitionException() {

                this("Bad Ignition Exception");

            }

            public BadIgnitionException(String message) {

                super(message);

            }

        }

        class DeadBatteryException extends NoStartException {

            public DeadBatteryException() {

                this("Dead Battery Exception");

            }

            public DeadBatteryException(String message) {

                super(message);

            }

        }

    }

    class LightsException extends Exception {

        public LightsException() {

            this("Lights Exception");

        }

        public LightsException(String message) {

            super(message);

        }

        class HeadLightException extends LightsException {

            public HeadLightException() {

                this("Head Light Exception");

            }

            public HeadLightException(String message) {

                super(message);

            }

        }

        class BrakeLightException extends LightsException {

            public BrakeLightException() {

                this("Brake Light Exception");

            }

            public BrakeLightException(String message) {

                super(message);

            }

        }

        class TailLightException extends LightsException {

            public TailLightException() {

                this("Tail Light Exception");

            }

            public TailLightException(String message) {

                super(message);

            }

        }

    }

    class BrakeException extends Exception {

        public BrakeException() {

            this("Brake Exception");

        }

        public BrakeException(String message) {

            super(message);

        }

        class FootBrakeException extends BrakeException {

            public FootBrakeException() {

                this("Foot Brake Exception");

            }

            public FootBrakeException(String message) {

                super(message);

            }

        }

        class ParkingBrakeException extends BrakeException {

            public ParkingBrakeException() {

                this("Parking Brake Exception");

            }

            public ParkingBrakeException(String message) {

                super(message);

            }

        }

    }

    void diagnostics() throws Exception{

        Random random=new Random();

        int num = random.nextInt(10000)%5;

        switch(num){

            case 0:

                throw new NoStartException();

            case 1:

                throw new LightsException();

            case 2:

                throw new BrakeException();

            //add more here

        }

    }

    void display(){

        try {

            diagnostics();

            System.out.println("Diagnostic test completed--no problem found.");

        }

        catch (AutoExceptions.BrakeException.FootBrakeException e){

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

        }

        catch(AutoExceptions.BrakeException e){

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

        }

        catch (Exception e) {

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

        }

    }

    public static void main(String[] args) {

        

    }

}

part c)

If footbrakeexception is thrown, first catch blocks will pick it.

If any subclass or self - brakeexcption i thrown, second block will catch it.

else third catch block

ScreenShot

void diagnostics() throws Exception Random random=new Random(); int num = random.nextInt(10080)%5; switch(num) { case 0: thro

Add a comment
Know the answer?
Add Answer to:
Need some help on the following problem in java. I have to modify the code I...
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
  • What is wrong with this code? Please bold the changes that were made so this code...

    What is wrong with this code? Please bold the changes that were made so this code runs properly. The Problem is: 11.16 (Catching Exceptions with SuperClasses) Use inheritance to create an exception superclass (called Exception) and exception subclasses ExceptionB and ExceptionC, where ExceptionB inherites from ExceptionA and ExceptionC inherits from ExeptionB. Write a program to demonstrate that the catch block for type ExceptionA catches exceptions of types ExceptionB and ExceptionC. I got this far but I'm still getting a lot...

  • How would I alter this code to have the output to show the exceptions for not...

    How would I alter this code to have the output to show the exceptions for not just the negative starting balance and negative interest rate but a negative deposit as well? Here is the class code for BankAccount: /** * This class simulates a bank account. */ public class BankAccount { private double balance; // Account balance private double interestRate; // Interest rate private double interest; // Interest earned /** * The constructor initializes the balance * and interestRate fields...

  • Need help with writing a Client test in JAVA

    public class TutorialSpace {                  // slots — the number of available slots in a tutorial class         private int slots;         // activated — true if the tutorial class has been activated         private boolean activated;                  /**         * TutorialSpace(n) — a constructor for a tutorial class with n slots.         *          * @param n         */         public TutorialSpace(int n) {                 this.slots = n;                 this.activated = false;         }                  /**         * activate() — activates the tutorial class. Throws an exception if the         * tutorial class has already been activated.         *          * @throws NotActivatedException         */                  public void activate() throws NotActivatedException {                 if (activated) {                         throw new NotActivatedException("Already Activated");                 } else {                         activated = true;                 }         }                  /**         * reserveSlot()—enrol a student into the tutorial class by decreasing the         * number of slots left for enrolling in the class. Throws an exception if slot         * is either completely used or the tutorial class is not active.         *          * @throws EmptyException         */         public void reserveSlot() throws EmptyException {                                  if (!activated || slots == 0) {                         throw new EmptyException("Tutorial space is empty");                 } else {                         slots--;                 }         }                  /**         * slotsRemaining()—returns the number of slots remaining in the tutorial class.         */                  public int slotsRemaining() {                 return slots;         }          } public class NotActivatedException extends Exception {                  /**         *          */         private static final long serialVersionUID = 1L;                  public NotActivatedException(String msg) {                 super(msg);         }          } public class EmptyException extends Exception {                  /**         *          */         private static final long serialVersionUID = 1L;         public EmptyException(String msg) {                 super(msg);         }                  ...

  • Question 10 (3 points) Which of the following statement is not true? There is a recursive...

    Question 10 (3 points) Which of the following statement is not true? There is a recursive sum method as shown below. When sum (19) is called, summation of all odd numbers less than 19 will be calculated and returned public int sum(int x){ if (x == 0) return 0: else return sum(x-2) + x; The following code segment will throw a testException. This exception has been handled in the way that do nothing but to continue when this exception happens....

  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • need help with this JAVA lab, the starting code for the lab is below. directions: The...

    need help with this JAVA lab, the starting code for the lab is below. directions: The Clock class has fields to store the hours, minutes and meridian (a.m. or p.m.) of the clock. This class also has a method that compares two Clock instances and returns the one that is set to earlier in the day. The blahblahblah class has a method to get the hours, minutes and meridian from the user. Then a main method in that class creates...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {   super();   System.out.println(“B() called”); } public...

  • PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is to...

    PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is too difficult you do not have to do part 3 but it would be greatly appreciated if you do ! Obtain example code files Circle.java, Shape.java, CircleShape2.java, Sphere.java, and CircleShapeApp.java from the downloaded files in Ch8. Compile and execute the example and understand the polymorphism it performs. (I have copied and pasted all of these files below) Modify...

  • I need the following written in Java please, thank you ' We want you to implement...

    I need the following written in Java please, thank you ' We want you to implement a java class that will show details on users and throw exceptions where needed. The following requirements specify what fields you are expected to implement in your User class: - A private String firstName that specifies the first name of the user - A private String lastName that specifies the last name of the user - A private int age that specifies user's age...

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
ADVERTISEMENT