Driver:
import java.util.Scanner;
//A class that keeps a fleet of different types of vehicles
public class FleetOfVehicles {
//An array of vehicles
private Vehicle[] fleet;
public static final int MAX_VEHICLES = 100;
public FleetOfVehicles()
{
fleet = new Vehicle[MAX_VEHICLES];
}
public Vehicle[] getFleet()
{
return this.fleet;
}
//Adds a new vehicle to the first empty spot in the fleet array
public void addVehicle(Vehicle aV)
{
for(int i=0;i<fleet.length;i++)
{
if(fleet[i] == null)
{
fleet[i] = aV;
return;
}
}
//If it reaches here the array is full
System.out.println(\"The fleet of vehicles is full!\");
}
//Removes a vehicle. It searches through the array for a vehicle that is equal to
//the parameter and once it is found that value is set to null
public void removeVehicle(Vehicle aV)
{
for(int i=0;i<fleet.length;i++)
{
if(fleet[i] != null && fleet[i].equals(aV))
{
fleet[i] = null;
return;
}
}
//If it reaches here then the vehicle was not found
System.out.println(\"The vehicle was not found\");
}
//A static keyboard to be used throughout the frontend
static Scanner keyboard;
//Entry point of the program
public static void main(String[] args)
{
keyboard = new Scanner(System.in);//Construct the keyboard
System.out.println(\"Welcome to the fleet manager\");
FleetOfVehicles fOfV = new FleetOfVehicles();//Creates a new instance of the FleetOfVehicles to be used
boolean quit = false;
while(!quit)//Runs until the user quits
{
printOptions();
int pick = keyboard.nextInt();
keyboard.nextLine();
switch(pick)
{
case 1: //Add vehicle
fOfV.addVehicle(makeAVehicleDialog());
break;
case 2: //Remove vehicle
fOfV.removeVehicle(makeAVehicleDialog());
break;
case 9:
quit = true;
break;
default:
System.out.println(\"Invalid input\");
}
System.out.println(\"The Fleet currently\");
printFleet(fOfV);
}
System.out.println(\"Goodbye\");
}
//Displays the options to the user
public static void printOptions()
{
System.out.println(\"Enter 1: to add a Vehicle\ Enter 2: to remove a Vehicle\ Enter 9 to quit\");
}
//Returns an instance of a vehicle based on user input
public static Vehicle makeAVehicleDialog()
{
Vehicle retV;
int pick = 0;
System.out.println(\"Enter 1: if it is a car\ Enter 2: if it is a truck\ Enter 3: if it is unclassified\");
pick = keyboard.nextInt();
keyboard.nextLine();
while(pick != 1 && pick != 2 && pick != 3)
{
System.out.println(\"Invalid choice pick again\");
pick = keyboard.nextInt();
keyboard.nextLine();
}
System.out.println(\"Enter the manufacturer\'s name\");
String manuName = keyboard.nextLine();
System.out.println(\"Enter the number of cylinders\");
int cylinders = keyboard.nextInt();
keyboard.nextLine();
System.out.println(\"Enter the owner\'s name\");
String ownersName = keyboard.nextLine();
switch(pick)
{
case 1://Constructing a car
System.out.println(\"Enter the car\'s gas mileage\");
double mileage = keyboard.nextDouble();
keyboard.nextLine();
System.out.println(\"Enter the number of passengers\");
int passengers = keyboard.nextInt();
keyboard.nextLine();
retV = new Car(manuName,cylinders,ownersName,mileage,passengers);
break;
case 2://Constructing a truck
System.out.println(\"Enter the truck\'s load capacity\");
double loadCap = keyboard.nextDouble();
keyboard.nextLine();
System.out.println(\"Enter the truck\'s towing capacity\");
double towCap = keyboard.nextDouble();
keyboard.nextLine();
retV = new Truck(manuName,cylinders,ownersName,loadCap,towCap);
break;
default:
retV = new Vehicle(manuName,cylinders,ownersName);
}
return retV;
}
public static void printFleet(FleetOfVehicles fV)
{
for(Vehicle v : fV.getFleet())
{
if(v == null)
continue;
System.out.println(v);
System.out.println();
}
}
}
Objective:
Write a class that represents a person in a simple way
First download the driver and put it in your project
DO NOT ALTER THE DRIVER!
Write a class file called Vehicle
Some of the attributes are
Manufacturer’s name
Number of Cylinders (must be greater than 0)
Owner’s name
Create the following Constructors
Default – sets everything to default values
Parameterized Constructor
Check for valid values
Accessors and Mutators for each variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following Methods
equals – takes in another instance of a Vehicle and returns true only if the names and the number of cylinders are equal
toString – returns a String that contains the Manufacturer’s name, number of cylinders, and the owners name
Write another class Truck which is a Vehicle
Some of the attributes of Truck are
Load capacity: a nonnegative number of tons represented by a decimal number
Towing capacity: a nonnegative number of tons represented by a decimal
Create the following constructors
Default – sets everything to default values
This includes calling the Vehicle’s default constructor
Parameterized Constructor
This must also take in via parameter the manufacturer’s name, number of cylinders, and the owner’s name in addition to the load and towing capacity.
Accessors and Mutators for each variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following methods
equals – This should override the vehicle’s equals method return true if all the properties of vehicle and truck are equal
toString – This should also override the vehicle’s toString method and also return the Vehicle’s toString along with the towing and load capacity
Write another class Car which is a Vehicle
Some of the attributes of Truck are
Gas Mileage: a nonnegative number of gallons represented by a decimal number
Number of passengers: a nonnegative number of passengers represented by a whole number
Create the following constructors
Default – sets everything to default values
This includes calling the Vehicle’s default constructor
Parameterized Constructor
This must also take in via parameter the manufacturer’s name, number of cylinders, and the owner’s name in addition to the gas mileage and number of passengers.
Accessors and Mutators for each variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following methods
equals – This should override the vehicle’s equals method return true if all the properties of vehicle and car are equal
toString – This should also override the vehicle’s toString method and also return the Vehicle’s toString along with the gas mileage and number of passengers
Example Dialog:
Welcome to the fleet manager
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
1
Enter the manufacturer\'s name
Nissan
Enter the number of cylinders
6
Enter the owner\'s name
JJ
Enter the car\'s gas mileage
29
Enter the number of passengers
5
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
2
Enter the manufacturer\'s name
Chevy
Enter the number of cylinders
8
Enter the owner\'s name
Eddie
Enter the truck\'s load capacity
1
Enter the truck\'s towing capacity
2
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Chevy
Number Of Cylinders: 8
Owner\'s Name: Eddie
Towing Capacity: 2.0
Load Capacity: 1.0
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
3
Enter the manufacturer\'s name
Ford
Enter the number of cylinders
6
Enter the owner\'s name
Bob
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Chevy
Number Of Cylinders: 8
Owner\'s Name: Eddie
Towing Capacity: 2.0
Load Capacity: 1.0
Manufacturer\'s Name: Ford
Number Of Cylinders: 6
Owner\'s Name: Bob
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
2
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
2
Enter the manufacturer\'s name
Chevy
Enter the number of cylinders
8
Enter the owner\'s name
Eddie
Enter the truck\'s load capacity
1
Enter the truck\'s towing capacity
2
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Ford
Number Of Cylinders: 6
Owner\'s Name: Bob
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
9
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Ford
Number Of Cylinders: 6
Owner\'s Name: Bob
Goodbye
Driver:
import java.util.Scanner;
//A class that keeps a fleet of different types of vehicles
public class FleetOfVehicles {
//An array of vehicles
private Vehicle[] fleet;
public static final int MAX_VEHICLES = 100;
public FleetOfVehicles()
{
fleet = new Vehicle[MAX_VEHICLES];
}
public Vehicle[] getFleet()
{
return this.fleet;
}
//Adds a new vehicle to the first empty spot in the fleet array
public void addVehicle(Vehicle aV)
{
for(int i=0;i<fleet.length;i++)
{
if(fleet[i] == null)
{
fleet[i] = aV;
return;
}
}
//If it reaches here the array is full
System.out.println(\"The fleet of vehicles is full!\");
}
//Removes a vehicle. It searches through the array for a vehicle that is equal to
//the parameter and once it is found that value is set to null
public void removeVehicle(Vehicle aV)
{
for(int i=0;i<fleet.length;i++)
{
if(fleet[i] != null && fleet[i].equals(aV))
{
fleet[i] = null;
return;
}
}
//If it reaches here then the vehicle was not found
System.out.println(\"The vehicle was not found\");
}
//A static keyboard to be used throughout the frontend
static Scanner keyboard;
//Entry point of the program
public static void main(String[] args)
{
keyboard = new Scanner(System.in);//Construct the keyboard
System.out.println(\"Welcome to the fleet manager\");
FleetOfVehicles fOfV = new FleetOfVehicles();//Creates a new instance of the FleetOfVehicles to be used
boolean quit = false;
while(!quit)//Runs until the user quits
{
printOptions();
int pick = keyboard.nextInt();
keyboard.nextLine();
switch(pick)
{
case 1: //Add vehicle
fOfV.addVehicle(makeAVehicleDialog());
break;
case 2: //Remove vehicle
fOfV.removeVehicle(makeAVehicleDialog());
break;
case 9:
quit = true;
break;
default:
System.out.println(\"Invalid input\");
}
System.out.println(\"The Fleet currently\");
printFleet(fOfV);
}
System.out.println(\"Goodbye\");
}
//Displays the options to the user
public static void printOptions()
{
System.out.println(\"Enter 1: to add a Vehicle\ Enter 2: to remove a Vehicle\ Enter 9 to quit\");
}
//Returns an instance of a vehicle based on user input
public static Vehicle makeAVehicleDialog()
{
Vehicle retV;
int pick = 0;
System.out.println(\"Enter 1: if it is a car\ Enter 2: if it is a truck\ Enter 3: if it is unclassified\");
pick = keyboard.nextInt();
keyboard.nextLine();
while(pick != 1 && pick != 2 && pick != 3)
{
System.out.println(\"Invalid choice pick again\");
pick = keyboard.nextInt();
keyboard.nextLine();
}
System.out.println(\"Enter the manufacturer\'s name\");
String manuName = keyboard.nextLine();
System.out.println(\"Enter the number of cylinders\");
int cylinders = keyboard.nextInt();
keyboard.nextLine();
System.out.println(\"Enter the owner\'s name\");
String ownersName = keyboard.nextLine();
switch(pick)
{
case 1://Constructing a car
System.out.println(\"Enter the car\'s gas mileage\");
double mileage = keyboard.nextDouble();
keyboard.nextLine();
System.out.println(\"Enter the number of passengers\");
int passengers = keyboard.nextInt();
keyboard.nextLine();
retV = new Car(manuName,cylinders,ownersName,mileage,passengers);
break;
case 2://Constructing a truck
System.out.println(\"Enter the truck\'s load capacity\");
double loadCap = keyboard.nextDouble();
keyboard.nextLine();
System.out.println(\"Enter the truck\'s towing capacity\");
double towCap = keyboard.nextDouble();
keyboard.nextLine();
retV = new Truck(manuName,cylinders,ownersName,loadCap,towCap);
break;
default:
retV = new Vehicle(manuName,cylinders,ownersName);
}
return retV;
}
public static void printFleet(FleetOfVehicles fV)
{
for(Vehicle v : fV.getFleet())
{
if(v == null)
continue;
System.out.println(v);
System.out.println();
}
}
}
Objective:
Write a class that represents a person in a simple way
First download the driver and put it in your project
DO NOT ALTER THE DRIVER!
Write a class file called Vehicle
Some of the attributes are
Manufacturer’s name
Number of Cylinders (must be greater than 0)
Owner’s name
Create the following Constructors
Default – sets everything to default values
Parameterized Constructor
Check for valid values
Accessors and Mutators for each variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following Methods
equals – takes in another instance of a Vehicle and returns true only if the names and the number of cylinders are equal
toString – returns a String that contains the Manufacturer’s name, number of cylinders, and the owners name
Write another class Truck which is a Vehicle
Some of the attributes of Truck are
Load capacity: a nonnegative number of tons represented by a decimal number
Towing capacity: a nonnegative number of tons represented by a decimal
Create the following constructors
Default – sets everything to default values
This includes calling the Vehicle’s default constructor
Parameterized Constructor
This must also take in via parameter the manufacturer’s name, number of cylinders, and the owner’s name in addition to the load and towing capacity.
Accessors and Mutators for each variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following methods
equals – This should override the vehicle’s equals method return true if all the properties of vehicle and truck are equal
toString – This should also override the vehicle’s toString method and also return the Vehicle’s toString along with the towing and load capacity
Write another class Car which is a Vehicle
Some of the attributes of Truck are
Gas Mileage: a nonnegative number of gallons represented by a decimal number
Number of passengers: a nonnegative number of passengers represented by a whole number
Create the following constructors
Default – sets everything to default values
This includes calling the Vehicle’s default constructor
Parameterized Constructor
This must also take in via parameter the manufacturer’s name, number of cylinders, and the owner’s name in addition to the gas mileage and number of passengers.
Accessors and Mutators for each variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following methods
equals – This should override the vehicle’s equals method return true if all the properties of vehicle and car are equal
toString – This should also override the vehicle’s toString method and also return the Vehicle’s toString along with the gas mileage and number of passengers
Example Dialog:
Welcome to the fleet manager
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
1
Enter the manufacturer\'s name
Nissan
Enter the number of cylinders
6
Enter the owner\'s name
JJ
Enter the car\'s gas mileage
29
Enter the number of passengers
5
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
2
Enter the manufacturer\'s name
Chevy
Enter the number of cylinders
8
Enter the owner\'s name
Eddie
Enter the truck\'s load capacity
1
Enter the truck\'s towing capacity
2
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Chevy
Number Of Cylinders: 8
Owner\'s Name: Eddie
Towing Capacity: 2.0
Load Capacity: 1.0
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
3
Enter the manufacturer\'s name
Ford
Enter the number of cylinders
6
Enter the owner\'s name
Bob
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Chevy
Number Of Cylinders: 8
Owner\'s Name: Eddie
Towing Capacity: 2.0
Load Capacity: 1.0
Manufacturer\'s Name: Ford
Number Of Cylinders: 6
Owner\'s Name: Bob
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
2
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
2
Enter the manufacturer\'s name
Chevy
Enter the number of cylinders
8
Enter the owner\'s name
Eddie
Enter the truck\'s load capacity
1
Enter the truck\'s towing capacity
2
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Ford
Number Of Cylinders: 6
Owner\'s Name: Bob
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
9
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Ford
Number Of Cylinders: 6
Owner\'s Name: Bob
Goodbye
Driver:
import java.util.Scanner;
//A class that keeps a fleet of different types of vehicles
public class FleetOfVehicles {
//An array of vehicles
private Vehicle[] fleet;
public static final int MAX_VEHICLES = 100;
public FleetOfVehicles()
{
fleet = new Vehicle[MAX_VEHICLES];
}
public Vehicle[] getFleet()
{
return this.fleet;
}
//Adds a new vehicle to the first empty spot in the fleet array
public void addVehicle(Vehicle aV)
{
for(int i=0;i<fleet.length;i++)
{
if(fleet[i] == null)
{
fleet[i] = aV;
return;
}
}
//If it reaches here the array is full
System.out.println(\"The fleet of vehicles is full!\");
}
//Removes a vehicle. It searches through the array for a vehicle that is equal to
//the parameter and once it is found that value is set to null
public void removeVehicle(Vehicle aV)
{
for(int i=0;i<fleet.length;i++)
{
if(fleet[i] != null && fleet[i].equals(aV))
{
fleet[i] = null;
return;
}
}
//If it reaches here then the vehicle was not found
System.out.println(\"The vehicle was not found\");
}
//A static keyboard to be used throughout the frontend
static Scanner keyboard;
//Entry point of the program
public static void main(String[] args)
{
keyboard = new Scanner(System.in);//Construct the keyboard
System.out.println(\"Welcome to the fleet manager\");
FleetOfVehicles fOfV = new FleetOfVehicles();//Creates a new instance of the FleetOfVehicles to be used
boolean quit = false;
while(!quit)//Runs until the user quits
{
printOptions();
int pick = keyboard.nextInt();
keyboard.nextLine();
switch(pick)
{
case 1: //Add vehicle
fOfV.addVehicle(makeAVehicleDialog());
break;
case 2: //Remove vehicle
fOfV.removeVehicle(makeAVehicleDialog());
break;
case 9:
quit = true;
break;
default:
System.out.println(\"Invalid input\");
}
System.out.println(\"The Fleet currently\");
printFleet(fOfV);
}
System.out.println(\"Goodbye\");
}
//Displays the options to the user
public static void printOptions()
{
System.out.println(\"Enter 1: to add a Vehicle\ Enter 2: to remove a Vehicle\ Enter 9 to quit\");
}
//Returns an instance of a vehicle based on user input
public static Vehicle makeAVehicleDialog()
{
Vehicle retV;
int pick = 0;
System.out.println(\"Enter 1: if it is a car\ Enter 2: if it is a truck\ Enter 3: if it is unclassified\");
pick = keyboard.nextInt();
keyboard.nextLine();
while(pick != 1 && pick != 2 && pick != 3)
{
System.out.println(\"Invalid choice pick again\");
pick = keyboard.nextInt();
keyboard.nextLine();
}
System.out.println(\"Enter the manufacturer\'s name\");
String manuName = keyboard.nextLine();
System.out.println(\"Enter the number of cylinders\");
int cylinders = keyboard.nextInt();
keyboard.nextLine();
System.out.println(\"Enter the owner\'s name\");
String ownersName = keyboard.nextLine();
switch(pick)
{
case 1://Constructing a car
System.out.println(\"Enter the car\'s gas mileage\");
double mileage = keyboard.nextDouble();
keyboard.nextLine();
System.out.println(\"Enter the number of passengers\");
int passengers = keyboard.nextInt();
keyboard.nextLine();
retV = new Car(manuName,cylinders,ownersName,mileage,passengers);
break;
case 2://Constructing a truck
System.out.println(\"Enter the truck\'s load capacity\");
double loadCap = keyboard.nextDouble();
keyboard.nextLine();
System.out.println(\"Enter the truck\'s towing capacity\");
double towCap = keyboard.nextDouble();
keyboard.nextLine();
retV = new Truck(manuName,cylinders,ownersName,loadCap,towCap);
break;
default:
retV = new Vehicle(manuName,cylinders,ownersName);
}
return retV;
}
public static void printFleet(FleetOfVehicles fV)
{
for(Vehicle v : fV.getFleet())
{
if(v == null)
continue;
System.out.println(v);
System.out.println();
}
}
}
Objective:
Write a class that represents a person in a simple way
First download the driver and put it in your project
DO NOT ALTER THE DRIVER!
Write a class file called Vehicle
Some of the attributes are
Manufacturer’s name
Number of Cylinders (must be greater than 0)
Owner’s name
Create the following Constructors
Default – sets everything to default values
Parameterized Constructor
Check for valid values
Accessors and Mutators for each variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following Methods
equals – takes in another instance of a Vehicle and returns true only if the names and the number of cylinders are equal
toString – returns a String that contains the Manufacturer’s name, number of cylinders, and the owners name
Write another class Truck which is a Vehicle
Some of the attributes of Truck are
Load capacity: a nonnegative number of tons represented by a decimal number
Towing capacity: a nonnegative number of tons represented by a decimal
Create the following constructors
Default – sets everything to default values
This includes calling the Vehicle’s default constructor
Parameterized Constructor
This must also take in via parameter the manufacturer’s name, number of cylinders, and the owner’s name in addition to the load and towing capacity.
Accessors and Mutators for each variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following methods
equals – This should override the vehicle’s equals method return true if all the properties of vehicle and truck are equal
toString – This should also override the vehicle’s toString method and also return the Vehicle’s toString along with the towing and load capacity
Write another class Car which is a Vehicle
Some of the attributes of Truck are
Gas Mileage: a nonnegative number of gallons represented by a decimal number
Number of passengers: a nonnegative number of passengers represented by a whole number
Create the following constructors
Default – sets everything to default values
This includes calling the Vehicle’s default constructor
Parameterized Constructor
This must also take in via parameter the manufacturer’s name, number of cylinders, and the owner’s name in addition to the gas mileage and number of passengers.
Accessors and Mutators for each variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following methods
equals – This should override the vehicle’s equals method return true if all the properties of vehicle and car are equal
toString – This should also override the vehicle’s toString method and also return the Vehicle’s toString along with the gas mileage and number of passengers
Example Dialog:
Welcome to the fleet manager
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
1
Enter the manufacturer\'s name
Nissan
Enter the number of cylinders
6
Enter the owner\'s name
JJ
Enter the car\'s gas mileage
29
Enter the number of passengers
5
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
2
Enter the manufacturer\'s name
Chevy
Enter the number of cylinders
8
Enter the owner\'s name
Eddie
Enter the truck\'s load capacity
1
Enter the truck\'s towing capacity
2
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Chevy
Number Of Cylinders: 8
Owner\'s Name: Eddie
Towing Capacity: 2.0
Load Capacity: 1.0
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
3
Enter the manufacturer\'s name
Ford
Enter the number of cylinders
6
Enter the owner\'s name
Bob
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Chevy
Number Of Cylinders: 8
Owner\'s Name: Eddie
Towing Capacity: 2.0
Load Capacity: 1.0
Manufacturer\'s Name: Ford
Number Of Cylinders: 6
Owner\'s Name: Bob
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
2
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
2
Enter the manufacturer\'s name
Chevy
Enter the number of cylinders
8
Enter the owner\'s name
Eddie
Enter the truck\'s load capacity
1
Enter the truck\'s towing capacity
2
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Ford
Number Of Cylinders: 6
Owner\'s Name: Bob
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
9
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Ford
Number Of Cylinders: 6
Owner\'s Name: Bob
Goodbye
Objective:
Write a class that represents a person in a simple way
First download the driver and put it in your project
DO NOT ALTER THE DRIVER!
Write a class file called Vehicle
Some of the attributes are
Manufacturer’s name
Number of Cylinders (must be greater than 0)
Owner’s name
Create the following Constructors
Default – sets everything to default values
Parameterized Constructor
Check for valid values
Accessors and Mutators for each variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following Methods
equals – takes in another instance of a Vehicle and returns true only if the names and the number of cylinders are equal
toString – returns a String that contains the Manufacturer’s name, number of cylinders, and the owners name
Write another class Truck which is a Vehicle
Some of the attributes of Truck are
Load capacity: a nonnegative number of tons represented by a decimal number
Towing capacity: a nonnegative number of tons represented by a decimal
Create the following constructors
Default – sets everything to default values
This includes calling the Vehicle’s default constructor
Parameterized Constructor
This must also take in via parameter the manufacturer’s name, number of cylinders, and the owner’s name in addition to the load and towing capacity.
Accessors and Mutators for each variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following methods
equals – This should override the vehicle’s equals method return true if all the properties of vehicle and truck are equal
toString – This should also override the vehicle’s toString method and also return the Vehicle’s toString along with the towing and load capacity
Write another class Car which is a Vehicle
Some of the attributes of Truck are
Gas Mileage: a nonnegative number of gallons represented by a decimal number
Number of passengers: a nonnegative number of passengers represented by a whole number
Create the following constructors
Default – sets everything to default values
This includes calling the Vehicle’s default constructor
Parameterized Constructor
This must also take in via parameter the manufacturer’s name, number of cylinders, and the owner’s name in addition to the gas mileage and number of passengers.
Accessors and Mutators for each variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following methods
equals – This should override the vehicle’s equals method return true if all the properties of vehicle and car are equal
toString – This should also override the vehicle’s toString method and also return the Vehicle’s toString along with the gas mileage and number of passengers
Example Dialog:
Welcome to the fleet manager
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
1
Enter the manufacturer\'s name
Nissan
Enter the number of cylinders
6
Enter the owner\'s name
JJ
Enter the car\'s gas mileage
29
Enter the number of passengers
5
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
2
Enter the manufacturer\'s name
Chevy
Enter the number of cylinders
8
Enter the owner\'s name
Eddie
Enter the truck\'s load capacity
1
Enter the truck\'s towing capacity
2
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Chevy
Number Of Cylinders: 8
Owner\'s Name: Eddie
Towing Capacity: 2.0
Load Capacity: 1.0
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
3
Enter the manufacturer\'s name
Ford
Enter the number of cylinders
6
Enter the owner\'s name
Bob
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Chevy
Number Of Cylinders: 8
Owner\'s Name: Eddie
Towing Capacity: 2.0
Load Capacity: 1.0
Manufacturer\'s Name: Ford
Number Of Cylinders: 6
Owner\'s Name: Bob
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
2
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
2
Enter the manufacturer\'s name
Chevy
Enter the number of cylinders
8
Enter the owner\'s name
Eddie
Enter the truck\'s load capacity
1
Enter the truck\'s towing capacity
2
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Ford
Number Of Cylinders: 6
Owner\'s Name: Bob
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
9
The Fleet currently
Manufacturer\'s Name: Nissan
Number Of Cylinders: 6
Owner\'s Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer\'s Name: Ford
Number Of Cylinders: 6
Owner\'s Name: Bob
Goodbye
import java.util.Scanner;
//A class that keeps a fleet of different types of vehicles
public class FleetOfVehicles {
//An array of vehicles
private Vehicle[] fleet;
public static final int MAX_VEHICLES = 100;
public FleetOfVehicles()
{
fleet = new Vehicle[MAX_VEHICLES];
}
public Vehicle[] getFleet()
{
return this.fleet;
}
//Adds a new vehicle to the first empty spot in the fleet array
public void addVehicle(Vehicle aV)
{
for(int i=0;i<fleet.length;i++)
{
if(fleet[i] == null)
{
fleet[i] = aV;
return;
}
}
//If it reaches here the array is full
System.out.println(\"The fleet of vehicles is full!\");
}
//Removes a vehicle. It searches through the array for a vehicle that is equal to
//the parameter and once it is found that value is set to null
public void removeVehicle(Vehicle aV)
{
for(int i=0;i<fleet.length;i++)
{
if(fleet[i] != null && fleet[i].equals(aV))
{
fleet[i] = null;
return;
}
}
//If it reaches here then the vehicle was not found
System.out.println(\"The vehicle was not found\");
}
//A static keyboard to be used throughout the frontend
static Scanner keyboard;
//Entry point of the program
public static void main(String[] args)
{
keyboard = new Scanner(System.in);//Construct the keyboard
System.out.println(\"Welcome to the fleet manager\");
FleetOfVehicles fOfV = new FleetOfVehicles();//Creates a new instance of the FleetOfVehicles to be used
boolean quit = false;
while(!quit)//Runs until the user quits
{
printOptions();
int pick = keyboard.nextInt();
keyboard.nextLine();
switch(pick)
{
case 1: //Add vehicle
fOfV.addVehicle(makeAVehicleDialog());
break;
case 2: //Remove vehicle
fOfV.removeVehicle(makeAVehicleDialog());
break;
case 9:
quit = true;
break;
default:
System.out.println(\"Invalid input\");
}
System.out.println(\"The Fleet currently\");
printFleet(fOfV);
}
System.out.println(\"Goodbye\");
}
//Displays the options to the user
public static void printOptions()
{
System.out.println(\"Enter 1: to add a Vehicle\ Enter 2: to remove a Vehicle\ Enter 9 to quit\");
}
//Returns an instance of a vehicle based on user input
public static Vehicle makeAVehicleDialog()
{
Vehicle retV;
int pick = 0;
System.out.println(\"Enter 1: if it is a car\ Enter 2: if it is a truck\ Enter 3: if it is unclassified\");
pick = keyboard.nextInt();
keyboard.nextLine();
while(pick != 1 && pick != 2 && pick != 3)
{
System.out.println(\"Invalid choice pick again\");
pick = keyboard.nextInt();
keyboard.nextLine();
}
System.out.println(\"Enter the manufacturer\'s name\");
String manuName = keyboard.nextLine();
System.out.println(\"Enter the number of cylinders\");
int cylinders = keyboard.nextInt();
keyboard.nextLine();
System.out.println(\"Enter the owner\'s name\");
String ownersName = keyboard.nextLine();
switch(pick)
{
case 1://Constructing a car
System.out.println(\"Enter the car\'s gas mileage\");
double mileage = keyboard.nextDouble();
keyboard.nextLine();
System.out.println(\"Enter the number of passengers\");
int passengers = keyboard.nextInt();
keyboard.nextLine();
retV = new Car(manuName,cylinders,ownersName,mileage,passengers);
break;
case 2://Constructing a truck
System.out.println(\"Enter the truck\'s load capacity\");
double loadCap = keyboard.nextDouble();
keyboard.nextLine();
System.out.println(\"Enter the truck\'s towing capacity\");
double towCap = keyboard.nextDouble();
keyboard.nextLine();
retV = new Truck(manuName,cylinders,ownersName,loadCap,towCap);
break;
default:
retV = new Vehicle(manuName,cylinders,ownersName);
}
return retV;
}
public static void printFleet(FleetOfVehicles fV)
{
for(Vehicle v : fV.getFleet())
{
if(v == null)
continue;
System.out.println(v);
System.out.println();
}
}
}
public class Vehicle {
String manuName;
int cylinders;
String ownersName;
public Vehicle() {
this.manuName = \"\";
setCylinders(0);
this.ownersName = \"\";
}
/**
* @param manuName
* @param cylinders
* @param ownersName
*/
public Vehicle(String manuName, int cylinders, String ownersName) {
this.manuName = manuName;
setCylinders(cylinders);
this.ownersName = ownersName;
}
/**
* @return the manuName
*/
public String getManuName() {
return manuName;
}
/**
* @param manuName
* the manuName to set
*/
public void setManuName(String manuName) {
this.manuName = manuName;
}
/**
* @return the cylinders
*/
public int getCylinders() {
return cylinders;
}
/**
* @param cylinders
* the cylinders to set
*/
public void setCylinders(int cylinders) {
if (cylinders > 0)
this.cylinders = cylinders;
else
this.cylinders = 0;
}
/**
* @return the ownersName
*/
public String getOwnersName() {
return ownersName;
}
/**
* @param ownersName
* the ownersName to set
*/
public void setOwnersName(String ownersName) {
this.ownersName = ownersName;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (obj instanceof Vehicle) {
Vehicle vehicle = (Vehicle) obj;
if ((this.getManuName().equals(vehicle.getManuName()))
&& (this.getOwnersName().equals(vehicle.getOwnersName()))
&& (this.getCylinders() == vehicle.getCylinders()))
return true;
else
return false;
} else
return false;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return getManuName() + \" \" + getCylinders() + \" \" + getOwnersName();
}
}
public class Truck extends Vehicle {
double loadCap, towCap;
public Truck() {
// TODO Auto-generated constructor stub
super();
setLoadCap(0);
setTowCap(0);
}
/**
* @param manuName
* @param cylinders
* @param ownersName
* @param loadCap
* @param towCap
*/
public Truck(String manuName, int cylinders, String ownersName,
double loadCap, double towCap) {
super(manuName, cylinders, ownersName);
setLoadCap(loadCap);
setTowCap(towCap);
}
/**
* @return the loadCap
*/
public double getLoadCap() {
return loadCap;
}
/**
* @param loadCap
* the loadCap to set
*/
public void setLoadCap(double loadCap) {
if (loadCap > 0)
this.loadCap = loadCap;
else
this.loadCap = 0;
}
/**
* @return the towCap
*/
public double getTowCap() {
return towCap;
}
/**
* @param towCap
* the towCap to set
*/
public void setTowCap(double towCap) {
if (towCap > 0)
this.towCap = towCap;
else
this.towCap = 0;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (obj instanceof Truck) {
if (super.equals(obj)) {
Truck vehicle = (Truck) obj;
if (this.getLoadCap() == vehicle.getLoadCap()
&& this.getTowCap() == vehicle.getTowCap())
return true;
else
return false;
} else
return false;
} else
return false;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString() + \" \" + getTowCap() + \" \" + getLoadCap();
}
}
public class Car extends Vehicle {
double mileage;
int passengers;
public Car() {
// TODO Auto-generated constructor stub
super();
setMileage(0);
setPassengers(0);
}
/**
* @param manuName
* @param cylinders
* @param ownersName
* @param mileage
* @param passengers
*
*/
public Car(String manuName, int cylinders, String ownersName,
double mileage, int passengers) {
super(manuName, cylinders, ownersName);
setMileage(mileage);
setPassengers(passengers);
}
/**
* @return the mileage
*/
public double getMileage() {
return mileage;
}
/**
* @param mileage
* the mileage to set
*/
public void setMileage(double mileage) {
if (mileage > 0)
this.mileage = mileage;
else
this.mileage = 0;
}
/**
* @return the passengers
*/
public int getPassengers() {
return passengers;
}
/**
* @param passengers
* the passengers to set
*/
public void setPassengers(int passengers) {
if (passengers > 0)
this.passengers = passengers;
else
this.passengers = 0;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (obj instanceof Car) {
if (super.equals(obj)) {
Car vehicle = (Car) obj;
if (this.getMileage() == vehicle.getMileage()
&& this.getPassengers() == vehicle.getPassengers())
return true;
else
return false;
} else
return false;
} else
return false;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString() + \" \" + getPassengers() + \" \" + getMileage();
}
}
OUTPUT:
Welcome to the fleet manager
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
1
Enter the manufacturer\'s name
Skoda
Enter the number of cylinders
2
Enter the owner\'s name
Srinivas
Enter the car\'s gas mileage
25
Enter the number of passengers
4
The Fleet currently
Skoda 2 Srinivas 4 25.0
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
2
Enter the manufacturer\'s name
Hyundai
Enter the number of cylinders
3
Enter the owner\'s name
Rajesh
Enter the truck\'s load capacity
522
Enter the truck\'s towing capacity
45
The Fleet currently
Skoda 2 Srinivas 4 25.0
Hyundai 3 Rajesh 45.0 522.0
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
2
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
1
Enter the manufacturer\'s name
Skoda
Enter the number of cylinders
2
Enter the owner\'s name
Srinivas
Enter the car\'s gas mileage
25
Enter the number of passengers
4
The Fleet currently
Hyundai 3 Rajesh 45.0 522.0
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
9
The Fleet currently
Hyundai 3 Rajesh 45.0 522.0
Goodbye