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);
}
}
}
}
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

Need some help on the following problem in java. I have to modify the code I...
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 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...
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 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....
JAVA CODE This program causes an error and crashes. Compile and give it a test run, note the exception that is thrown. Then do the following to fix the problem 1. Write code to handle this exception 2. Use try, catch and finally blocks. 3. Display, the name of the exception 4. Display a message from the user about what happened. Here is the starting file BadArray.java: public class BadArray { public static void main(String[] args) { // Create an...
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){...
Please I need help with the following questions in JAVA programming language. Please comment the code to help me understand, thanks. Exercise 1: Inheritance 1. Firstly, create and compile a simple class called Parent. Give it the following behaviour: a. A default constructor that does nothing other than print out “Parent default constructor” using System.out b. A single method called getMessage which returns a String, e.g. “Parent message” 2. Next, create and compile a class called Child. Give it the...
******Java Programming Hi guys, I really need you help. I created a code for my java course, but it keep giving me error messages. Majority of my code is fine but some keep display error on my console. I was hoping someone could pin points the problem. .There are three classes with the testCenter class being the main class. In the following is the assignment, and the bottom is my code. Please help! Assignment: Concepts: GUI User Design Graphics Deployment...
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 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”);...