Using Dr Java Objective: Write a class that represents a person in a simple way
ID: 3865164 • Letter: U
Question
Using Dr Java
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!
//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();
}
}
}
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 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 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
Explanation / Answer
This is main Vehicle class:
package com.vehicle;
public class Vehicle {
String manuName;
int cylinders;
String ownersName;
//default constructor
public Vehicle() {
super();
// TODO Auto-generated constructor stub
}
//param constructor
public String getManuName() {
return manuName;
}
public Vehicle(String manuName, int cylinders, String ownersName) {
super();
this.manuName = manuName;
this.cylinders = cylinders;
this.ownersName = ownersName;
}
//Accessors and Mutators
public void setManuName(String manuName) {
this.manuName = manuName;
}
public int getCylinders() {
return cylinders;
}
public void setCylinders(int cylinders) {
if(cylinders>=0){
this.cylinders = cylinders;
}else{
this.cylinders =0;
}
}
public String getOwnersName() {
return ownersName;
}
public void setOwnersName(String ownersName) {
this.ownersName = ownersName;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
This is sub class car:
package com.vehicle;
public class Car extends Vehicle {
double mileage;
int passengers;
//default constructor
public Car() {
super();
// TODO Auto-generated constructor stub
}
//param constructor
public Car(String manuName, int cylinders, String ownersName,double mileage, int passengers) {
super(manuName, cylinders, ownersName);
this.mileage = mileage;
this.passengers = passengers;
}
//Accessors and Mutators
public double getMileage() {
return mileage;
}
public void setMileage(double mileage) {
if(mileage>=0.0){
this.mileage = mileage;
}else{
this.mileage =0.0;
}
}
public int getPassengers() {
return passengers;
}
public void setPassengers(int passengers) {
if(passengers>=0){
this.passengers = passengers;
}else{
this.passengers = 0;
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
This is sub class Truck:
package com.vehicle;
public class Truck extends Vehicle {
double loadCap;
double towCap;
//default constructor
public Truck() {
super();
// TODO Auto-generated constructor stub
}
//param constructor
public Truck(String manuName, int cylinders, String ownersName,double loadCap, double towCap) {
super(manuName, cylinders, ownersName);
this.loadCap = loadCap;
this.towCap = towCap;
}
//Accessors and Mutators
public double getLoadCap() {
return loadCap;
}
public void setLoadCap(double loadCap) {
if(loadCap>=0.0){
this.loadCap = loadCap;
}else{
this.loadCap =0.0;
}
}
public double getTowCap() {
return towCap;
}
public void setTowCap(double towCap) {
if(towCap>=0.0){
this.towCap = towCap;
}else{
this.towCap =0.0;
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
This is Driver class:
package com.vehicle;
import java.util.Scanner;
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;
if(v instanceof Car){
System.out.println("Manufacturer's Name: "+v.manuName);
System.out.println("Number Of Cylinders: "+v.cylinders);
System.out.println("Owner's Name: "+v.ownersName);
System.out.println("Gas Mileage: "+((Car) v).mileage);
System.out.println("Number of Passengers: "+((Car) v).passengers);
}
if(v instanceof Truck){
System.out.println("Manufacturer's Name: "+v.manuName);
System.out.println("Number Of Cylinders: "+v.cylinders);
System.out.println("Owner's Name: "+v.ownersName);
System.out.println("Towing Capacity: "+((Truck) v).towCap);
System.out.println("Load Capacity: "+((Truck) v).loadCap);
}
}
}
}
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
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.