I have the finish the following lab. 2.1 The Car Class Create a class named Car.
ID: 3576191 • Letter: I
Question
I have the finish the following lab.
2.1 The Car Class Create a class named Car.
This class models a car with a driver and several passengers; therefore it should have fields for the driver (of type Person) and passengers (an array of Person objects). The length of the array indicates the cars capacity (that is, its maximum number of passengers after the car is created). The cars cannot be created with a size less than the minimum of 4 passengers or a size greater than the maximum of 8 passengers. The driver is also a passenger whose age is 16 or older and should also be in the “passengers” array.
The class has two constructors and ten methods, which should be defined exactly as shown below.
public Car()
A constructor that creates a small car seating up to 4 people. It initializes the passenger array to that size, and the driver to null.
public Car(int aCapacity)
A constructor that creates a car whose capacity is set by the given parameter. If the parameter is less that 4, the capacity is set to the allowable minimum. If the parameter is greater than 8, the capacity is set to the allowable maximum. It initialiazes the passenger array and the driver as above.
• public int getCapacity() Returns the capacity of the Car.
2
public int getOccupancy()
Returns the number of Person objects currently in the Car.
public boolean hasRoom()
Returns whether or not the occupancy of the car is equal to the capacity of the Car.
public Person getDriver()
Returns the Person driving the Car or null if the Car has no driver.
public boolean hasPassenger(Person person)
It receives a person and indicates whether this person is a passenger. Any two Person objects are the same if they bear the same name and age.
public boolean setDriver(Person person)
If allowed, it assigns the person as the driver and returns true. The person can become the driver if the person is a passenger or can become a passenger, and if the person is of driving age. Remember, two persons are the same if they have the same name and age. The method fails and returns false if the person is not a passenger and cannot become a passenger (due to full occupancy), or the person is not of driving age. If there is a current driver, that driver is not removed from the array of passengers.
public Person[] getPassengers()
Returns the list of passengers in the car. The array returned should be a shallow copy of the one kept in the car (thus changes in the copy are not reflected in the original). To make a shallow copy, create a new array of persons, copy all passengers to the new array, and return this copy at the end of the method. Be aware that returning a reference to the list of passengers does not result in a copy. This array should be an exact copy of the passengers array, that is if an entry in passengers is null, the corresponding entry in the returned array should also be null.
public boolean addPassenger(Person person)
It adds a person as a passenger. The method fails if the person to add is null, if the person is a passenger already, or if there is no room left in the car to add it. The person can be added to the first available seat, i.e., the first seat that is null.
public boolean removePassenger(Person person)
It receives a person and removes it as a passenger. If the person is also the driver,
3
then the driver seat becomes available (that is, it is set to null). The method fails if the person is null, or if he/she is not a passenger. It may be helpful to move all the occupied seats (non-null Person objects) to the front of the array.
• public boolean canDrive(Person person)
It indicates whether a person is of driving age (that is, 16 years or older). The method returns false if the person is null or if the person’s age is less than 16.
I have set up the person file but am having touble with setting up the car class. This is what I have so far. Does what I have currently (for the methods I have completed) look correct and how do I do the other methods?
import java.util.Arrays;
public class Car
{
private Person[] Car;
private Person Driver;
public Car() {
Person[] Car = new Person[4];
Driver = null;
Car[0] = null;
}
public Car(int aCapacity){
if (aCapacity > 8) {
aCapacity = 8;
}
if (aCapacity < 4) {
aCapacity = 4;
}
Person[] Car = new Person[aCapacity];
Driver = null;
Car[0] = Driver;
}
public int getCapacity(){
int capacity = Car.length;
return capacity;
}
public int getOccupancy(){
int counter = 0;
for (int i = 0; i < Car.length; i ++){
if (Car[i] != null){
counter ++;
}
}
return counter;
}
public boolean hasRoom(){
boolean a = true;
int counter = 0;
for (int i = 0; i < Car.length; i ++){
if (Car[i] != null){
counter ++;
}
}
if (counter == Car.length)
a = false;
return a;
}
public Person getDriver(){
return Car[0];
}
public Person[] getPassangers(){
Person[] copy = new Person[Car.length];
for (int i = 0; i < Car.length; i++) {
copy[i] = Car[i];
}
String a = Arrays.toString(Car);
return a;
//how do I return an object?
}
public boolean hasPassenger(Person person){
}
public boolean setDriver(Person person){
}
public boolean addPassenger(Person person){
}
public boolean removePassanger(Person person){
}
}
Explanation / Answer
Hello,
I am providing below the code for the car class with comments on each method.
Please go through and let me know in case of any issues
public class Car{
private int capacity;
private Person driver;
private Person passengers[];
private int occupancy;
private int seat;
public Car(){
capacity = 4;
passengers = new Person[capacity];
occupancy = 0;
}
public Car(int aCapacity){
if(aCapacity <=4){
capacity = 4;
}
else if (aCapacity >= 8){
capacity = 8;
}
else {
capacity=aCapacity;
}
passengers = new Person[capacity];
occupancy=0;
}
public int getCapacity(){
return capacity;
}
/* this method returns the number of passengers*/
public int getOccupancy(){
occupancy = 0;
for(int i=0;i<passengers.length;i++){
if(passengers[i]!=null){
occupancy++;
}
}
return occupancy;
}
/*This method is for identifying if the the car is full with passengers based on the car capacity*/
public boolean hasRoom(){
boolean results;
if (getCapacity()>getOccupancy()){
results = true;
}
else {
results = false;
}
return results;
}
/*this method is to check if there is a driver in the car or not*/
public Person getDriver(){
return driver;
}
/*This method returns true if the person ca drive the car or not*/
public boolean setDriver(Person person){
boolean result;
if(canDrive(person)==true && hasPassenger(person)==true && hasRoom()==true){
driver=person;
result=true;
}
else{
driver=null;
result=false;
}
return result;
}
/* This method returns the number of passengers in the car*/
public Person[] getPassengers(){
Person [] passengersList = new Person[getCapacity()];
for(int i=0;i < passengers.length;i++){
if (passengers[i]!=null){
passengersList[i]=passengers[i];
}
}
return passengersList;
}
/* This method checks if the input person is a passenger or not*/
public boolean hasPassenger(Person person){
boolean results=false;
if (person!=null){
for(int i = 0;i<passengers.length;i++){
if(passengers[i]==person){
results=true;
seat=i;
}
}
}
return results;
}
/* This method adds a person as a passenger*/
public boolean addPassenger(Person person){
boolean results = false;
if(person!=null && hasPassenger(person)==false && hasRoom()==true){
int i=0;
for(i=0;passengers[i]!=null;i++){}
passengers[i]=person;
results=true;
}
return results;
}
/*this method removes the person as passenger of the car*/
public boolean removePassenger(Person person){
boolean results;
if(hasPassenger(person)==true){
passengers[seat]=null;
if(person==driver){
driver=null;
}
results = true;
}
else{
results = false;
}
return results;
}
/*this method checks if the input person can drive the car or not based on the age*/
public boolean canDrive(Person person){
boolean results;
if(person!=null){
results=person.canDrive();
}
else{
results = false;
}
return results;
}
}