In: Computer Science
There is a lab assignment. Basically includes array and some other basic stuff. Comment if you have any questions. Thank you.
I have got all classes here so that it might be more make sense.
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.
}
}
Here is the code,
I have completed the full code, only 3 parts are remaining(in Driver class)that can be done on your own. If you have any doubts about any of the parts feel free to ask in the comment section, I would try to help you with my best.
Bus.java
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
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)
private double MSRP;
private int numSeats;
private boolean isICE;
private String licensePlate;
private 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(){
MSRP = 0.0;
numSeats = 0;
isICE = false;
licensePlate = "";
regState = "";
}
public Car(double MSRP, int numSeats, boolean isICE, String licensePlate, String regState){
this.MSRP = MSRP;
this.numSeats = numSeats;
this.isICE = isICE;
this.licensePlate = licensePlate;
this.regState = regState;
}
public Car(String licensePlate, String regState){
this.licensePlate = licensePlate;
this.regState = regState;
}
public Car(double MSRP, String licensePlate, String regState){
this.MSRP = MSRP;
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 Vehicle with VIN "+getVIN()+" 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) {
this.MSRP = MSRP;
}
public int getNumSeats() {
return numSeats;
}
public void setNumSeats(int numSeats) {
this.numSeats = numSeats;
}
public boolean isICE() {
return isICE;
}
public void setICE(boolean ICE) {
isICE = ICE;
}
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;
}
}
Vehicle.java
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;
}
}
Commercial.java
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
public class Motorbike extends Vehicle {
// TODO Add the following data members to the class Motorbike: MSRP (double), numWheels (int), licensePlate (String), regState (String)
private double MSRP;
private int numWheels;
private String licensePlate;
private String regState;
// 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)
public Motorbike(){
MSRP = 0.0;
numWheels = 0;
licensePlate = "";
regState = "";
}
public Motorbike(double MSRP, int numWheels, String licensePlate, String regState){
this.MSRP = MSRP;
this.numWheels = numWheels;
this.licensePlate = licensePlate;
this.regState = regState;
}
public Motorbike(double MSRP, String licensePlate, String regState){
this.MSRP = MSRP;
this.licensePlate = licensePlate;
this.regState = regState;
}
public Motorbike(int numWheels, String licensePlate, String regState){
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 Vehicle with VIN "+getVIN()+" 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) {
this.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;
}
}
Demo.java
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
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
// TODO Add necessary imports
public class Driver {
private static Car cheapestCar(Vehicle[] inventory){
Car car = null;
for (Vehicle vehicle : inventory){
if (vehicle instanceof Car){
Car mCar = (Car) vehicle;
if(car == null) car = mCar;
else if(car.getMSRP() > mCar.getMSRP()) car = mCar;
}
}
return car;
}
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
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!
inventory[0] = new Motorbike(435, "ABC134", "STATE_A");
inventory[1] = new Motorbike(785, "HFS621", "STATE_B");
inventory[2] = new Motorbike(6575, "EJF787", "STATE_B");
inventory[3] = new Motorbike(876, "LSH853", "STATE_X");
inventory[4] = new Car(43657, "LFONFH-144", "STATE_Y");
inventory[5] = new Car(87732, "QOPSLF-345", "STATE_V");
inventory[6] = new Car(32784, "FHFKSL-634", "STATE_V");
inventory[7] = new Car(18393, "WEJRKF-253", "STATE_U");
inventory[8] = new Car(20983, "ADJGGG-678", "STATE_W");
inventory[9] = new Car(54672, "RTSKFN-324", "STATE_A");
// TODO Write a loop to print out the toString information for each vehicle in the array
for(Vehicle vehicle : inventory) System.out.println(vehicle.toString());
// 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(Vehicle vehicle : inventory){
if(vehicle instanceof Motorbike){
Motorbike motorbike = (Motorbike) vehicle;
System.out.println("Motorbike{" +
"MSRP=" + motorbike.getMSRP() +
", numWheels=" + motorbike.getNumWheels() +
", licensePlate='" + motorbike.getLicensePlate() + '\'' +
", regState='" + motorbike.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(Vehicle vehicle : inventory){
if(vehicle instanceof Car){
Car car = (Car) vehicle;
System.out.println("Car{" +
"MSRP=" + car.getMSRP() +
", numSeats=" + car.getNumSeats() +
", isICE=" + car.isICE() +
", licensePlate='" + car.getLicensePlate() + '\'' +
", regState='" + car.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!
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.
List<Vehicle> inventoryList = Arrays.asList(inventory);
Collections.shuffle(inventoryList);
inventoryList.toArray(inventory);
Car cheapestCar = cheapestCar(inventory);
if(cheapestCar == null) System.out.println("No car found in inventory");
else System.out.println("Cheapest car : "+cheapestCar.toString());
// 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.
}
}
Semi.java
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;
}
}
Also please don't forget to upvote the solution.