In: Computer Science
Concepts: Classes, Objects, Inheritance, Method overriding (toString), polymorphic methods, Array of objects, loops
1. (Classes and Objects) Hand-write a complete Java class that can be used to create a Car object as described below.
a. A Vehicle has-a: i. Registration number ii. Owner name iii. Price iv. Year manufactured
b. Add all instance variables
c. The class must have getters and setters for all instance variables
d. The class must have two constructors, a no-args and a constructor that receives input parameters for each instance variable.
2. (Inheritance) Hand-write two Java classes, Car and Truck. They are both subclasses of Vehicle.
a. A car has an additional instance variable, number of doors.
b. A truck has an additional instance variable, number of axles
c. Write a constructor that requires input for the instance variables of each class (including registration number and owner name).
d. Write getters and setters for all instance variables for both classes.
3. (Array of objects) The array of Vehicles below, write the static method, findMin which returns the lowest price of any Vehicle in the Array Vehicle [] myVehicles = new Vehicle[10]; public static double findMin(Vehicle [] theVehicles) { }
4.Write a class, TestVehicle, which does the following: 1. Creates one Car object from information entered by the user. 2. Creates one Truck object from information entered by the user. 3. Creates an Array of Vehicles that can hold 10 objects. 4. It creates up to 10 Vehicles objects from information entered by the user 5. Prints the information about each object in the format shown below using the toString methods of the classes
*****PLEASE USE THE CODE BELOW I HAVE SO FAR TO COMPLETE THE PROGRAM****
****************************************************************************************
public class Vehicle {
//Instance Variables
private int
registrationNumber;
private String ownerName;
private double price;
private int yearManufactured;
//Constructors
public Vehicle (){
this.registrationNumber=0;
this.ownerName=
" ";
this.price=0;
this.yearManufactured=0;
}
public Vehicle (int
newRegisteration, String newName, double newPrice, int newMnf)
{
this.registrationNumber = newRegisteration;
this.ownerName =
newName;
this.price =
newPrice;
this.yearManufactured = newMnf;
}
//Get methods
public int getRegistration ()
{
return
this.registrationNumber;
}
public String getName () {
return
this.ownerName;
}
public double getPrice () {
return
this.price;
}
public int getYearManufactured () {
return
this.yearManufactured;
}
//Mutators or
Setters
public void
setRegistration (int newRegistration) {
this.registrationNumber=newRegistration;
}
public void setName (String newName) {
this.ownerName=newName;
}
public void
setPrice (double newPrice) {
this.price=newPrice;
}
public void setYearManufactured (int newYearManufactured) {
this.yearManufactured=newYearManufactured;
}
public String toString () {
return "
Vehicle: \nRegistration Number: " +registrationNumber
+ " Owner Name: "+ownerName+
" Price: "+price+ " Year Manufactured: " +yearManufactured;
}
}//end of class
**********************************************************************************************
public class Car extends Vehicle {
//Instance Variables
private int numOfDoors;
//Constructors
public Car() {
super();
}
}//end of class
CODE IN JAVA:
Vechicle.java file:
public class Vehicle {
// Instance Variables
private int registrationNumber;
private String ownerName;
private double price;
private int yearManufactured;
// Constructors
public Vehicle() {
this.registrationNumber = 0;
this.ownerName = " ";
this.price = 0;
this.yearManufactured = 0;
}
public Vehicle(int newRegisteration, String
newName, double newPrice, int newMnf) {
this.registrationNumber =
newRegisteration;
this.ownerName = newName;
this.price = newPrice;
this.yearManufactured =
newMnf;
}
// Get methods
public int getRegistration() {
return
this.registrationNumber;
}
public String getName() {
return this.ownerName;
}
public double getPrice() {
return this.price;
}
public int getYearManufactured() {
return this.yearManufactured;
}
// Mutators or Setters
public void setRegistration(int newRegistration)
{
this.registrationNumber =
newRegistration;
}
public void setName(String newName) {
this.ownerName = newName;
}
public void setPrice(double newPrice) {
this.price = newPrice;
}
public void setYearManufactured(int
newYearManufactured) {
this.yearManufactured =
newYearManufactured;
}
public String toString() {
return " Vehicle: \nRegistration
Number: " + registrationNumber + " Owner Name: " + ownerName + "
Price: " + price + " Year Manufactured: " + yearManufactured;
}
public static double findMin(Vehicle[] theVehicles)
{
int n = theVehicles.length;
double low =
theVehicles[0].getPrice();
for(int i=1;i<n;i++) {
if(theVehicles[i].getPrice()<low)
low = theVehicles[i].getPrice();
}
return low;
}
}
Car.java file:
public class Car extends Vehicle {
//Instance Variables
private int numOfDoors;
// Constructors
public Car(int newRegisteration, String newName,
double newPrice, int newMnf,int numOfDoors) {
super(newRegisteration,newName,newPrice,newMnf);
this.numOfDoors = numOfDoors;
}
public String toString() {
return " Car: \nRegistration
Number: " + this.getRegistration() + " Owner Name: " +
this.getName() + " Price: " + getPrice() + " Year Manufactured: " +
getYearManufactured()+" numOfDoors=" + numOfDoors ;
}
public int getNumOfDoors() {
return numOfDoors;
}
public void setNumOfDoors(int numOfDoors) {
this.numOfDoors = numOfDoors;
}
}
Truck.java file:
public class Truck extends Vehicle{
private int numOfAxles;
Truck(int newRegisteration, String newName, double
newPrice, int newMnf,int numOfAxles){
super(newRegisteration,newName,newPrice,newMnf);
this.numOfAxles = numOfAxles;
}
public int getNumOfAxles() {
return numOfAxles;
}
public void setNumOfAxles(int numOfAxles) {
this.numOfAxles = numOfAxles;
}
public String toString() {
return " Truck: \nRegistration
Number: " + this.getRegistration() + " Owner Name: " +
this.getName() + " Price: " + getPrice() + " Year Manufactured: " +
getYearManufactured()+" numberOfAxles=" + numOfAxles ;
}
}
TestVehicle.java file:
import java.util.Scanner;
public class TestVehicle {
public static void main(String[] args) {
int registrationNumber;
String ownerName;
double price;
int yearManufactured;
int numOfDoors;
int numOfAxles;
Scanner sc = new
Scanner(System.in);
System.out.print("Enter Registratin
number:");
registrationNumber =
sc.nextInt();
System.out.print("Enter Owner name
:");
ownerName = sc.nextLine();
ownerName = sc.nextLine();
System.out.print("Enter
price:");
price = sc.nextDouble();
System.out.print("Enter year
manfactured:");
yearManufactured =
sc.nextInt();
System.out.print("Enter number of
doors:");
numOfDoors = sc.nextInt();
Car c = new Car(registrationNumber,
ownerName, price, yearManufactured, numOfDoors);
System.out.print("Enter Registratin
number:");
registrationNumber =
sc.nextInt();
System.out.print("Enter Owner name
:");
ownerName = sc.nextLine();
ownerName = sc.nextLine();
System.out.print("Enter
price:");
price = sc.nextDouble();
System.out.print("Enter year
manfactured:");
yearManufactured =
sc.nextInt();
System.out.print("Enter number of
axles:");
numOfAxles = sc.nextInt();
Truck t = new
Truck(registrationNumber, ownerName, price, yearManufactured,
numOfAxles);
System.out.println(c);
System.out.println();
System.out.println(t);
System.out.println();
Vehicle v[] = new
Vehicle[10];
for (int i = 0; i < 10; i++)
{
System.out.println("Enter vehicle information:");
System.out.print("Enter Registratin number:");
registrationNumber = sc.nextInt();
System.out.print("Enter Owner name :");
ownerName =
sc.nextLine();
ownerName =
sc.nextLine();
System.out.print("Enter price:");
price =
sc.nextDouble();
System.out.print("Enter year manfactured:");
yearManufactured
= sc.nextInt();
Vehicle vh = new
Vehicle(registrationNumber, ownerName, price,
yearManufactured);
v[i] = vh;
}
System.out.println("Below is all 10
vehicles information:");
for (int i = 0; i < 10;
i++)
System.out.println(v[i]);
}
}
OUTPUT:



