Question: PLEASE PROVIDE CODE IN JAVA Program 1: Create a base class Car and it will have fields: name (data type String), cylinders (data type int),
PLEASE PROVIDE CODE IN JAVA
Program 1:
Create a base class Car and it will have fields: name (data type String), cylinders (data type int), engine (data type boolean), and wheels (data type int).
Write a constructor that receives name and cylinders as parameters and initialize the two fields. Set engine to true and wheels to 4.
Create appropriate getters.
Create methods startEngine, accelerate, and brake. These methods should return a message "Car -> startEngine()", "Car -> accelerate()" and "Car -> brake()"
Create 3 sub-classes: Honda, Toyota, Ford. In each of these classes, override the base class methods. The overridden methods should return a message "Honda -> startEngine()", "Honda -> accelerate()", "Honda -> brake()" for Honda. Override the base class methods for Toyota and Ford similar to Honda class.
In the demo class: Create objects of Car, Honda, Toyota and Ford classes passing the name and cylinder values to the respective constructor as follows:
Car: "Base Car", 8 Honda: "Honda Accord", 6 Toyota: "Toyota RAV4", 4 Ford: "Ford Truck F-250", 8
Create an array of Car class containing 4 elements and assign the Car, Honda, Toyota, and Ford objects to each of the element.
Use a loop to iterate through the array. The body of the loop should have a System.out.println statement that calls startEngine(), accelerate(), brake() methods for each element.
The output should be similar to the following:
Car -> startEngine() Car -> accelerate() Car -> brake() Honda -> startEngine() Honda -> accelerate() Honda -> brake() Toyota -> startEngine() Toyota -> accelerate() Toyota -> brake() Ford -> startEngine() Ford -> accellerate() Ford -> brake()
Program 2:
Prompt the user to enter age. If the age is less than 18, throw an exception with the message "You are underage, cannot vote yet". If the age is 18 or more, display the message "Congrats, you can vote!" and another message "Processing continues...".
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
