Question: Add a user-defined exception that can be thrown by one of the methods as part of the validation or error checking. The main method should

Add a user-defined exception that can be thrown by one of the methods as part of the validation or error checking. The main method should then create a/n instance of the class and call the method in such a way that the exception is thrown (e.g. invalid input or state of the system).

Current Code:

class Car { int numWheels; Car(int numWheels) { this.numWheels = 4; } public void driving() { System.out.println("Cars use wheels to travel"); System.out.println("Cars have " + numWheels + " wheels"); } } class Bicycle extends Car { int numTires; Bicycle(int numWheels,int numTires) { super(numWheels); this.numTires = 2; } public void biking(){ System.out.println("Bicycles use tires to travel"); System.out.println("Bicycles use " + numTires + " tires"); } public void biking(int numberTires) { System.out.println("Tricycles use " + numberTires + " tires"); }

@Override public void driving() { System.out.println("Bicycle: Overriding driving method from parent Car class"); } } class People extends Car { int numLegs;

People(int numWheels,int numLegs) { super(numWheels); this.numLegs = 2; } public void walking(){ System.out.println("People walk to travel"); System.out.println("People have " + numLegs + " legs"); } public void walking(int numberLegs) { System.out.println("People crawl on " + numberLegs + " limbs"); }

@Override public void driving() { System.out.println("People: Overriding driving method from parent Car class"); } }

import java.util.*;

public class MainClass { public static void main(String[] args) { Scanner scan=new Scanner(System.in); Car Car = new Car(4); Car.driving(); Bicycle Bicycle = new Bicycle(4,2); Bicycle.biking(); Bicycle.biking(3); Bicycle.biking(); People walking = new People(4,2); walking.walking(); walking.walking(4); walking.walking();

scan.close(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Java class InvalidTireException extends Exception public InvalidTireExceptionString message supermessage class Car int numWheels Carint numWheels this... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!