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
Step: 1
Java class InvalidTireException extends Exception public InvalidTireExceptionString message supermessage class Car int numWheels Carint numWheels this...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started