This assignment involves writing a program that uses an abstract class and concrete subclasses. An abstract Vehicle class declares an abstract method named display() that is used to display vehicles. Vehicle has two subclasses: Boat and Car. A Boat has a length attribute, and a draft attribute (a draft is how far below the surface the bottom of the boat is). A Car has a make attribute and a year attribute. Appropriate getter, setter, and constructor methods should be defined for the subclasses, as well as an implementation of the display() method. All the method needs to do is to display the type of object and its attributes. Develop a class that has a main method that creates an array of type Vehicle, and populates it with a few Boat and Car objects. Prompt the user to enter attributes for the objects, and then use a looping construct to iterate through the array and display object attributes and invoke the display() method.
import java.util.*;
abstract class Vehicle // base class
{
abstract void display();
}
class Boat extends Vehicle // derived class
{
private int length,draft;
public Boat() // default constructor
{
length = 0;
draft = 0;
}
public Boat(int length,int draft)// argument
constructor
{
this.length = length;
this.draft = draft;
}
public void display()
{
System.out.println("Boat length :
"+length+" draft : "+draft);
}
//set and get methods
public void setLength(int length)
{
this.length = length;
}
public void setDraft(int draft)
{
this.draft = draft;
}
public int getLength()
{
return length;
}
public int getDraft()
{
return draft;
}
}
class Car extends Vehicle
{
private String make;
private int year;
public Car()
{
make = "";
year = 0;
}
public Car(String make,int year)
{
this.make = make;
this.year = year;
}
public void display()
{
System.out.println("Car make :
"+make +" year : "+ year);
}
public void setMake(String make)
{
this.make = make;
}
public void setYear(int year)
{
this.year = year;
}
public String getMake()
{
return make;
}
public int getYear()
{
return year;
}
}
class TestVehicles
{
public static void main (String[] args)
{
Vehicle[] v = new Vehicle[5]; //
array of Vehicle objects
Scanner input = new
Scanner(System.in);
Boat b = new Boat();
System.out.println("Enter the
length and draft of the boat : ");
int length = input.nextInt();
int draft = input.nextInt();
b.setLength(length);
b.setDraft(draft);
v[0] = b;
Car c = new Car();
System.out.println("Enter the make
and year of the car : ");
String make = input.next();
int year = input.nextInt();
c.setMake(make);
c.setYear(year);
v[1] = c;
for(int i=0;i<=1;i++)
{
v[i].display();
}
}
}
Output:
Enter the length and draft of the boat : 34 5 Enter the make and year of the car : Ford 2017 Boat length : 34 draft : 5 Car make : Ford year : 2017
Do ask if any doubt. Please upvote.
This assignment involves writing a program that uses an abstract class and concrete subclasses. An abstract...
Write a program in C++ with comments! Create an abstract class Property, containing the data items owner, address and price. Add an abstract method showOwner(). Also provide getter/setters for all the data items. Make validations if necessary in the setter so that no invalid values are entered. Create a class House, to inherit the Property class. This class should contain additional data item – yardSize – an integer – the size of the yard of the house, getter and setter...
I want to write a superclass with one private attribute and one method in JAVA The super class will later derive into 2 subclasses each with its own private attributes and each with a method that OVERRIDES the superclass method with each subclasses with one method ( that will do something) unique to that subclass. ALL classes must have constructors initializing its attributes and getter/setter methods. Then, in the main method, keep initializing objects of type superclasses based on user's...
Write an abstract class “Student” and three concrete classes, “UnderGrad” and “Graduate” both inherit from Student and “PostGraduate” that inherits form “Graduate”. Write the class definition for the abstract class “Student”. The class definition should include private instance variables of type String to hold the student’s first name, a string for his/her major and an int to hold the number of units taken. Getter and setter methods for each of the variables should be included in the class definition. Also...
Write an abstract superclass encapsulating a vehicle: A vehicle has two attributes: its owner's name and its number of wheels. This class has two non-abstract subclasses: one encapsulating a bicycle, and the other encapsulating a motorized vehicle. A motorized vehicle has the following additional attributes: its engine volume displacement, in liters; and a method computing and returning a measure of horsepower which is the number of liters times the number of wheels. Write an application class utilizing these two classes.
Part I: Create a program class named TestArrays This class should have a main() method that behaves as follows: Create an integer array of size 10. Using a loop, fill the elements with increments of 5, starting with 5 in the first element. Using the array elements, sum the second, fifth and seventh elements. Display the sum with a label. Change the fourth element to 86. Subtract 9 from the ninth element. Using a loop, display the elements of the...
(The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...
Write an abstract superclass encapsulating a vehicle: A vehicle has two attributes: its owner's name and its number of wheels. This class has two non-abstract subclasses: one encapsulating a bicycle, ant the other encapsulating a motorized vehicle. A motorized vehicle has the following additional attributes: its engine volume displacement, in liters; and a method computing and returning a measure of horsepower which is the number of liters times the number of wheels. You also need to include a client class...
Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make Chicken and Cow as subclasses of Dairy. The Sheep and Dairy classes implement the Edible interface. howToEat) and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML...
What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...
JAVA The following is about creating a class Ruler and testing it. (i) Create a class Ruler with the attributes brand (which is a string) and length (an integer) which are used to store the brand and length of the ruler respectively. Write a constructor Ruler(String brand, int length) which assigns the brand and length appropriately. Write also the getter/setter methods of the two attributes. Save the content under an appropriate file name. Copy the content of the file as...