In: Computer Science
A lab assignment. Comment if you have any question.
BUS CLASS
package classes;
//DO NOT ERASE THE TODO STUBS! WRITE YOUR SOLUTIONS
BELOW THE STUBS!
public class Bus extends Commercial {
private int numPassengers;
public Bus()
{
super();
numPassengers = 0;
}
public Bus(String pVIN, String
comID)
{
super(pVIN, comID);
numPassengers = 0;
}
public Bus(String pVIN, String comID, int
passengerCount)
{
super(pVIN, comID);
numPassengers =
passengerCount;
}
@Override
public String toString()
{
return "Bus is " +
super.toString();
}
// Getters
public int getPassCount()
{
return numPassengers;
}
// Setters
public void setPassCount(int passCount)
{
numPassengers = passCount;
}
}
CAR CLASS
package classes;
public class Car extends Vehicle {
// TODO Add the following data
members to the class Car: MSRP (double), numSeats (int), isICE
(boolean), licensePlate (String), regState (String)
// Note that MSRP is in dollars and is the suggested
retail price of the car
// TODO Create at least 4 reasonable
constructors for the class Car (one will be default)
// TODO Create an Override for the
vehicle toString method.
// Example returned string: "Car is Vehicle with VIN
85545 and License Plate ABC123 from Wisconsin"
// TODO Create setters for all of the
data members in class Car
// TODO Create getters for all of the
data members in class Car
}
COMMERCIAL CLASS
package classes;
abstract public class Commercial extends Vehicle{
private String commercialID;
public Commercial()
{
super();
commercialID = "";
}
public Commercial(String pVIN, String
comID)
{
super(pVIN);
commercialID = comID;
}
@Override
public String toString()
{
return super.toString() + " is
commercial with CID " + commercialID;
}
// TODO Add setters and getters for
commercialID in the Commercial class
}
MOTORBIKE CLASS
package classes;
public class Motorbike extends Vehicle {
// TODO Add the following data
members to the class Motorbike: MSRP (double), numWheels (int),
licensePlate (String), regState (String)
// Note that MSRP is in dollars and is the suggested
retail price of the motorbike
// TODO Create at least 4 reasonable constructors for
the class Motorbike (one will be default)
// TODO Create an Override for the vehicle toString
method.
// Example returned string: "Motorbike is Vehicle with
VIN 85545 and License Plate ABC123 from Wisconsin"
// TODO Create setters for all of the data members in
class Motorbike
// TODO Create getters for all of the data members in
class Motorbike
}
SEMI CLASS
package classes;
public class Semi extends Commercial{
private int numAxles;
private double weight; // LBS
// Constructors
public Semi()
{
super();
numAxles = 0;
weight = 0.0;
}
public Semi(String pVIN, String comID)
{
super(pVIN, comID);
numAxles = 0;
weight = 0;
}
public Semi(String pVIN, String comID, int axleCount,
double weightLBS)
{
super(pVIN, comID);
numAxles = axleCount;
weight = weightLBS;
}
// toString Method
@Override
public String toString()
{
return "Semi is " +
super.toString();
}
// TODO Add setters and getters for
numAxles and weight in the Semi class
}
VEHICLE CLASS
package classes;
abstract public class Vehicle {
// Data Members
private String VIN;
// Constructors
public Vehicle()
{
VIN = "";
}
public Vehicle(String pVIN)
{
VIN = pVIN;
}
public String toString()
{
return "Vehicle with VIN " +
VIN;
}
// Getters
public String getVIN()
{
return VIN;
}
// Setters
public void setVIN(String pVIN)
{
VIN = pVIN;
}
}
DEMO CLASS
package programCode;
import classes.*;
public class Demo {
public static void main(String[] args) {
// Create Bus and Semi
objects
Bus myBus = new Bus("VIN12345",
"CID0001");
Semi mySemi = new Semi("VIN67890",
"CID2222");
// Call toString for both
System.out.println(myBus);
System.out.println(mySemi);
System.out.println("\n\n");
// Create an array of commercial
vehicles in the fleet
Commercial[] fleet = new
Commercial[3];
// Populate the array
fleet[0] = new Semi("VIN00000",
"CID0000");
fleet[1] = new Semi("VIN99999",
"CID3333");
fleet[2] = new Bus("VIN77777",
"CID1111");
// Print out the fleet's basic
information using toString
System.out.println("Our fleet
consists of: ");
for(Commercial x : fleet)
{
System.out.println(" " + x);
}
// Get the VIN number
of the vehicle in position 1 of fleet:
String extractedVIN =
fleet[1].getVIN();
System.out.println(extractedVIN);
}
}
DRIVER CLASS
package programCode;
// TODO Add necessary imports
public class driver {
public static void main(String[] args) {
// TODO Finish all of the TODO stubs
in the classes before proceeding! Use the sample code,
lectures,
// completed code in the Bus,
Commercial, Vehicle, and Demo files to assist you if you need help.
You
// need to reason about the
objectives in the lab in order to create an effective solution.
Don't just
// copy code from one class to
another! That won't work...
// TODO You should test all of the
methods (constructors, getters, and setters) for the classes.
Make
// sure they all do what they are
expected to do!
// TODO Create an array (called
inventory) of Vehicle objects to store 10 vehicles
// TODO Populate the array with 4
motorcycles and 6 cars. You need to set the members
appropriately.
// You may choose the values of
each data member, but make sure none of them are null!
// TODO Write a loop to print out
the toString information for each vehicle in the array
// TODO Write a loop that will
print out all data members of the motorcycles in the array. Note
that this
// loop MUST work regardless of
where the motorcycles appear in the array. HINT: Check the
reference types
// for each member of the array and
process only the motorcycles.
// TODO Write a loop that will
print out the data members of the cars in the array. Note that this
loop
// MUST work regardless of where
the cars appear in the array.
// TODO Write a method that accepts
as input a reference to an array of type Vehicle and returns
// the integer index where the
CHEAPEST car resides in that array. Place this method in the driver
file.
// Do not create a new file to hold
the method!
// TODO Use the method you just
wrote to return the cheapest car in your vehicle array. Make sure
it
// returns the correct information!
You should shuffle the entries in the array around some to make
sure
// the method reliably finds the
cheapest entry. If two cheapest entries are found, you can choose
any
// of them. HINT: Use a basic
linear search. You don't need to do anything complex.
// TODO Suppose the dealership just
obtained a new Semi. They'd like to add it to their inventory
list.
// QUESTION: Can they do
this?
// If so, modify your array code
and add a Semi object to index 10 of the array.
// If not, explain in detail why
the Semi cannot be added to the inventory list.
// QUESTION: Assume the Semi can
somehow be added to the inventory list. Given the code in the
lab
// right now, could the dealership
assign an MSRP to the semi? Why or why not? Note that
// you do NOT need to make this
possible in the code if it isn't possible. Just explain.
}
}
The code changes to implement above functionality is as below:
Bus.java
package classes;
//DO NOT ERASE THE TODO STUBS! WRITE YOUR SOLUTIONS BELOW THE STUBS!
public class Bus extends Commercial {
private int numPassengers;
public Bus()
{
super();
numPassengers = 0;
}
public Bus(String pVIN, String comID)
{
super(pVIN, comID);
numPassengers = 0;
}
public Bus(String pVIN, String comID, int passengerCount)
{
super(pVIN, comID);
numPassengers = passengerCount;
}
@Override
public String toString()
{
return "Bus is " + super.toString();
}
// Getters
public int getPassCount()
{
return numPassengers;
}
// Setters
public void setPassCount(int passCount)
{
numPassengers = passCount;
}
}
Car.java
package classes;
public class Car extends Vehicle {
// TODO Add the following data members to the class Car: MSRP (double), numSeats (int), isICE (boolean), licensePlate (String), regState (String)
double MSRP;
int numSeats;
boolean isICE;
String licensePlate;
String regState;
// Note that MSRP is in dollars and is the suggested retail price of the car
// TODO Create at least 4 reasonable constructors for the class Car (one will be default)
public Car() {
super();
}
public Car(String pVIN)
{
super(pVIN);
}
public Car(String pVIN, double mSRP, int numSeats, boolean isICE) {
super(pVIN);
MSRP = mSRP;
this.numSeats = numSeats;
this.isICE = isICE;
}
public Car(String pVIN, double mSRP, int numSeats, boolean isICE, String licensePlate, String regState) {
super(pVIN);
MSRP = mSRP;
this.numSeats = numSeats;
this.isICE = isICE;
this.licensePlate=licensePlate;
this.regState=regState;
}
// TODO Create an Override for the vehicle toString method.
// Example returned string: "Car is Vehicle with VIN 85545 and License Plate ABC123 from Wisconsin"
@Override
public String toString(){
return "Car is "+super.toString()+" and License Plate "+licensePlate+" from "+regState;
}
// TODO Create setters for all of the data members in class Car
// TODO Create getters for all of the data members in class Car
public double getMSRP() {
return MSRP;
}
public void setMSRP(double mSRP) {
MSRP = mSRP;
}
public int getNumSeats() {
return numSeats;
}
public void setNumSeats(int numSeats) {
this.numSeats = numSeats;
}
public boolean isICE() {
return isICE;
}
public void setICE(boolean isICE) {
this.isICE = isICE;
}
public String getLicensePlate() {
return licensePlate;
}
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
public String getRegState() {
return regState;
}
public void setRegState(String regState) {
this.regState = regState;
}
}
Commercial.java
package classes;
abstract public class Commercial extends Vehicle{
private String commercialID;
public Commercial()
{
super();
commercialID = "";
}
public Commercial(String pVIN, String comID)
{
super(pVIN);
commercialID = comID;
}
@Override
public String toString()
{
return super.toString() + " is commercial with CID " + commercialID;
}
// TODO Add setters and getters for commercialID in the Commercial class
public String getCommercialID() {
return commercialID;
}
public void setCommercialID(String commercialID) {
this.commercialID = commercialID;
}
}
Motorbike.java
package classes;
public class Motorbike extends Vehicle {
// TODO Add the following data members to the class Motorbike: MSRP (double), numWheels (int), licensePlate (String), regState (String)
// Note that MSRP is in dollars and is the suggested retail price of the motorbike
double MSRP;
int numWheels;
String licensePlate;
String regState;
// TODO Create at least 4 reasonable constructors for the class Motorbike (one will be default)
public Motorbike() {
super();
}
public Motorbike(String pVIN)
{
super(pVIN);
}
public Motorbike(String pVIN,double mSRP, int numWheels) {
super(pVIN);
MSRP = mSRP;
this.numWheels = numWheels;
}
public Motorbike(String pVIN,double mSRP, int numWheels,String licensePlate, String regState) {
super(pVIN);
MSRP = mSRP;
this.numWheels = numWheels;
this.licensePlate = licensePlate;
this.regState = regState;
}
// TODO Create an Override for the vehicle toString method.
// Example returned string: "Motorbike is Vehicle with VIN 85545 and License Plate ABC123 from Wisconsin"
@Override
public String toString(){
return "Motorbike is "+super.toString()+" and License Plate "+licensePlate+" from "+regState;
}
// TODO Create setters for all of the data members in class Motorbike
// TODO Create getters for all of the data members in class Motorbike
public double getMSRP() {
return MSRP;
}
public void setMSRP(double mSRP) {
MSRP = mSRP;
}
public int getNumWheels() {
return numWheels;
}
public void setNumWheels(int numWheels) {
this.numWheels = numWheels;
}
public String getLicensePlate() {
return licensePlate;
}
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
public String getRegState() {
return regState;
}
public void setRegState(String regState) {
this.regState = regState;
}
}
Semi.java
package classes;
public class Semi extends Commercial{
private int numAxles;
private double weight; // LBS
// Constructors
public Semi()
{
super();
numAxles = 0;
weight = 0.0;
}
public Semi(String pVIN, String comID)
{
super(pVIN, comID);
numAxles = 0;
weight = 0;
}
public Semi(String pVIN, String comID, int axleCount, double weightLBS)
{
super(pVIN, comID);
numAxles = axleCount;
weight = weightLBS;
}
// toString Method
@Override
public String toString()
{
return "Semi is " + super.toString();
}
// TODO Add setters and getters for numAxles and weight in the Semi class
public int getNumAxles() {
return numAxles;
}
public void setNumAxles(int numAxles) {
this.numAxles = numAxles;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
Vehicle.java
package classes;
abstract public class Vehicle {
// Data Members
private String VIN;
// Constructors
public Vehicle()
{
VIN = "";
}
public Vehicle(String pVIN)
{
VIN = pVIN;
}
public String toString()
{
return "Vehicle with VIN " + VIN;
}
// Getters
public String getVIN()
{
return VIN;
}
// Setters
public void setVIN(String pVIN)
{
VIN = pVIN;
}
}
Demo.java
package programCode;
import classes.*;
public class Demo {
public static void main(String[] args) {
// Create Bus and Semi objects
Bus myBus = new Bus("VIN12345", "CID0001");
Semi mySemi = new Semi("VIN67890", "CID2222");
// Call toString for both
System.out.println(myBus);
System.out.println(mySemi);
System.out.println("\n\n");
// Create an array of commercial vehicles in the fleet
Commercial[] fleet = new Commercial[3];
// Populate the array
fleet[0] = new Semi("VIN00000", "CID0000");
fleet[1] = new Semi("VIN99999", "CID3333");
fleet[2] = new Bus("VIN77777", "CID1111");
// Print out the fleet's basic information using toString
System.out.println("Our fleet consists of: ");
for(Commercial x : fleet)
{
System.out.println(" " + x);
}
// Get the VIN number of the vehicle in position 1 of fleet:
String extractedVIN = fleet[1].getVIN();
System.out.println(extractedVIN);
}
}
Driver.java
package programCode;
import classes.Car;
import classes.Motorbike;
import classes.Vehicle;
// TODO Add necessary imports
public class Driver {
public static void main(String[] args) {
// TODO Finish all of the TODO stubs in the classes before proceeding! Use the sample code, lectures,
// completed code in the Bus, Commercial, Vehicle, and Demo files to assist you if you need help. You
// need to reason about the objectives in the lab in order to create an effective solution. Don't just
// copy code from one class to another! That won't work...
//COMPLETED THE CODE IN ALL CLASSES
// TODO You should test all of the methods (constructors, getters, and setters) for the classes. Make
// sure they all do what they are expected to do!
//WILL USE ALL THE METHODS WHILE CREATING OBJECTS AND WHILE PERFORMING OPERATIONS ON IT
// TODO Create an array (called inventory) of Vehicle objects to store 10 vehicles
Vehicle[] inventory = new Vehicle[10];
// TODO Populate the array with 4 motorcycles and 6 cars. You need to set the members appropriately.
// You may choose the values of each data member, but make sure none of them are null!
Motorbike m1 = new Motorbike("VIN12345",10000,2,"WI1201","Wisconsin");
Motorbike m2 = new Motorbike("VIN12346",9000,2,"MO1209","Mosucuim");
Motorbike m3 = new Motorbike("VIN12347",12000,2,"JA1901","Jarban");
Motorbike m4 = new Motorbike("VIN12348",7000,2,"CH1001","Charaman");
Car c1 = new Car("VIN12012", 100000,4,true,"JA1975", "Jarban");
Car c2 = new Car("VIN13012", 102000,4,false,"CH1985", "Charaman");
Car c3 = new Car("VIN12016", 100600,4,true,"JA1915", "Wisconsin");
Car c4 = new Car("VIN12112", 108000,4,false,"MO1905", "Mosucuim");
Car c5 = new Car("VIN12002", 100900,4,true,"MO1905", "Mosucuim");
Car c6 = new Car("VIN12035", 101000,4,true,"JA1989", "Jarban");
inventory[0]=m1;inventory[1]=m2; inventory[2]=m3; inventory[3]=m4;
inventory[4]=c1;inventory[5]=c2; inventory[6]=c3;
inventory[7]=c4;inventory[8]=c5; inventory[9]=c6;
// TODO Write a loop to print out the toString information for each vehicle in the array
for(Vehicle v : inventory){
System.out.println(v);
}
// TODO Write a loop that will print out all data members of the motorcycles in the array. Note that this
// loop MUST work regardless of where the motorcycles appear in the array. HINT: Check the reference types
// for each member of the array and process only the motorcycles.
for(int i=0;i<inventory.length;i++){
if(inventory[i] instanceof Motorbike){
Motorbike m=((Motorbike)inventory[i]);
System.out.println("pVIN is "+m.getVIN()+" MSRP is "+m.getMSRP()+" Number of Wheels "+m.getNumWheels()+" license type "+m.getLicensePlate()+" reg State "+m.getRegState());
}
}
// TODO Write a loop that will print out the data members of the cars in the array. Note that this loop
// MUST work regardless of where the cars appear in the array.
for(int i=0;i<inventory.length;i++){
if(inventory[i] instanceof Car){
Car c=((Car)inventory[i]);
System.out.println("pVIN is "+c.getVIN()+" MSRP is "+c.getMSRP()+" Number of Wheels "+c.getNumSeats()+" isICE "+c.isICE()+" license type "+c.getLicensePlate()+" reg State "+c.getRegState());
}
}
// TODO Write a method that accepts as input a reference to an array of type Vehicle and returns
// the integer index where the CHEAPEST car resides in that array. Place this method in the driver file.
// Do not create a new file to hold the method!
Driver d = new Driver();
System.out.println(d.cheapestCar(inventory));
// TODO Use the method you just wrote to return the cheapest car in your vehicle array. Make sure it
// returns the correct information! You should shuffle the entries in the array around some to make sure
// the method reliably finds the cheapest entry. If two cheapest entries are found, you can choose any
// of them. HINT: Use a basic linear search. You don't need to do anything complex.
// TODO Suppose the dealership just obtained a new Semi. They'd like to add it to their inventory list.
// QUESTION: Can they do this?
// If so, modify your array code and add a Semi object to index 10 of the array.
// If not, explain in detail why the Semi cannot be added to the inventory list.
// QUESTION: Assume the Semi can somehow be added to the inventory list. Given the code in the lab
// right now, could the dealership assign an MSRP to the semi? Why or why not? Note that
// you do NOT need to make this possible in the code if it isn't possible. Just explain.
}
// TODO Write a method that accepts as input a reference to an array of type Vehicle and returns
// the integer index where the CHEAPEST car resides in that array. Place this method in the driver file.
// Do not create a new file to hold the method!
public int cheapestCar(Vehicle[] inventory){
double minMSRP=100000000;
int minIndex=0;
for(int i=1;i<inventory.length;i++){
if(inventory[i] instanceof Car){
if(((Car)inventory[i]).getMSRP()<minMSRP){
minMSRP =((Car)inventory[i]).getMSRP();
minIndex=i;
}
}
}
return minIndex;
}
}
The screenshot of the output after executing Demo.java is as below:
The ouput screenshot after executing the Driver,java is as below:
I have implemented only first four requirements form the Driver class as we can answer maximum 4 subparts of a question. if you need answer to remaining implementation, please submit the question as to implement remaining functionalities in Driver class.
If you have any queries regarding this answer, please reach out through the comment section.