'trying to figure these project errors
I'm stuck in my project, tried about anything, but can't solve this, getting errors, and testers are not working as well. airport class is the one making trouble, working on Time1 and Flight. I'll be glad for some help. I'm trying to figure this out for a few days. Also, some of the methods are not completed due to this airport:
/**
* This program Represents an airport of Flights objects
*/
public class Airport
{
final int MAX_FLIGHTS = 200;
final int MIN_VAL=0;
private String _city;
private Flight[] _flightsSchedule = new Flight[MAX_FLIGHTS];
private int _noOfFlights;
/**
* Constructor for an airport object
* Sets the selected city to be the airport destination
* Sets the number of flight to zero as default
* @param city The city which the airport will be
*
*/
public Airport(String city){
_city = city;
_noOfFlights = MIN_VAL;
_flightsSchedule =new Flight[MAX_FLIGHTS];
}
/**
* returns a boolean expression for adding a Flight object, the method get a FLIGHT as perimeter.
* @param f The flight to add to the airport.
* @return True if the flight added.
*/
public boolean addFlight(Flight f){
boolean flag = false;
if(_noOfFlights>=MAX_FLIGHTS){
return flag;
}else{
//String orig="",dest="";
//int depH=0,depM=0,dur=0,numP=0,price=0;
_flightsSchedule[_noOfFlights]=new Flight(f);
flag = true;
_noOfFlights++;
return flag;
}
}
/**
* Removes a Flight which selected from the current array.
* @param f The flight to remove.
* @return True if deleted.
*/
public boolean removeFlight(Flight f){
boolean flag = false;
for (int i = 0 ; i < _flightsSchedule.length; i++) {
if (f.equals(_flightsSchedule[i])) {//found the flight to remove
for(int j = i;j<_noOfFlights-1;j++)//move the rest of the array 1 step back
_flightsSchedule[j]=_flightsSchedule[j+1];
_flightsSchedule[_noOfFlights-1]=null;
_noOfFlights--;
flag = true;
}
}
return flag;
}
/**
* Returns the first flight at the same day from selected city
* If there are no flights from a selected city, Null returned.
* @param place The city to check the first flight from.
* @return The time the the first flight departures.
*/
public Time1 firstFlightFromOrigin (String place){
Time1 temp;
int y = 0;
while(!_flightsSchedule[y].getOrigin().equals(place)){
y++;
}
if(y==_noOfFlights){
return null;
}
temp = _flightsSchedule[y].getDeparture();
for(int i=y+1;i<_noOfFlights;i++){
if(_flightsSchedule[i].getOrigin().equals(place) &&
_flightsSchedule[i].getDeparture().before(temp))
temp = _flightsSchedule[i].getDeparture();
}
return temp;
}
/**
* Return a string representation of all flight in the airport (for example: "Flight from Tel-Aviv to London departs at 12:00. Flight is full.").
* @return String representation of all flight in the airport.
*/
public String toString(){
String orig="";
String dest="";
Time1 dep;
String print = ""; // flight not is Full
if (_noOfFlights == 0){
print = null;
}else if(_noOfFlights == MAX_FLIGHTS){
print = " flight is Full";
}else{
print = " flight is not Full";
}
System.out.print("The flights for airport "+ _city+" today are: "+"/n");
//" to "+_destination+ " departs at "+_departure+ isFull;
for(int i=0;i<_noOfFlights;i++){
if(_flightsSchedule[i].equals(_city)){
orig = _flightsSchedule[i].getOrigin();
dest = _flightsSchedule[i].getDestination();
dep = _flightsSchedule[i].getDeparture();
System.out.println("Flight from "+orig+" to "+dest+" departs at "+"/n");
}
}
return "Flight from "+orig+" to "+dest+" departs at "+"/n";
}
/**
* Returns the number of full flight in the airport at the same day
* @return The number of full flights
*/
public int howManyFullFlights(){
int fullF=0;//the number of full flight in the array
for(int i=0;i<_flightsSchedule.length;i++){
if (_flightsSchedule[i].getIsFull()==true){
fullF++;
}
}
return fullF;
}
/**
* Returns the number of flights departures from a selected city and lands at airport, and from the airport to the selected city.
* @param city The city to check flight from airport.
* @return the number of flights between a selected city and the airport.
*/
public int howManyFlightsBetween (String city){
city=_city;
int count = 0;
for(int i=0;i<_flightsSchedule.length;i++){
if(_flightsSchedule[i].getOrigin().equals(city)){
count++;
}
}
return count;
}
/**
* Returns a string representation of the most popular city. (the city that most flights lands at)
* @return the most popular destination.
*/
public String mostPopularDestination(){
String temp="";
int count =0,ind=0;
for(int i=0;i<_flightsSchedule.length;i++){
temp = _flightsSchedule[i].getDestination();
if(_flightsSchedule[i+1].getDestination().equals(temp)){
count++;
ind=i;
temp = _flightsSchedule[i].getDestination();
}
}
return new Flight(_flightsSchedule[ind]).getDestination();
}
/**
* Return the Flight that which ticket is most expensive, if there are no flights in the airport, returns Null.
* @return Most expensive Flight.
*/
public Flight mostExpensiveTicket(){
if(_noOfFlights==MIN_VAL){
return null;
}else{
int big=0;
int ind=0;
for(int i=0;i<_flightsSchedule.length;i++){
if(_flightsSchedule[i].getPrice()>big){
big=_flightsSchedule[i].getPrice();
ind = i;
}
}
return new Flight(_flightsSchedule[ind]);
}
}
/**
* Return the longest flight in the airport, if there are no flights in the airport, returns Null.
* @return Longest flight.
*/
public Flight longestFlight(){
int temp=0,count=0;
if(_noOfFlights==MIN_VAL){
return null;
}else{
for(int i=0;i<_flightsSchedule.length;i++){
if(_flightsSchedule[i].getFlightDuration()>temp)
count=i;
}
}
return new Flight(_flightsSchedule[count]);
}
}
Time1:
/**
* This class represent time
*/
public class Time1
{
private int _hour;
private int _minute;
public final int MIN_VAL = 0;
public final int MIN_IN_HOUR = 60;
public final int MAX_M_BOUND = 59;
public final int MAX_H_BOUND = 23;
/**
* Initialize an instance of Time and check for illegal input
* If one of the inputs is illegal, it sets it to 0 as default
* @param Hour the new hour
* @param Minute the new minutes
*/
public Time1 (int h, int m)
{
if (h >= MIN_VAL && h <= MAX_H_BOUND)
_hour=h;
else if (h < MIN_VAL && h > MAX_H_BOUND)
_hour=MIN_VAL;
if (m >= MIN_VAL && m <= MAX_M_BOUND)
_minute=m;
else if (m < MIN_VAL && m > MAX_M_BOUND)
_minute=MIN_VAL;
}
/**
* Copy Constructor
* Initialize an instance of Time identical to the given Time
* @param other The Time to copy
*/
public Time1 (Time1 other)
{
_hour = other._hour;
_minute = other._minute;
}
/**
* Return the hour from selected Time
* @param return Hour
*/
public int getHour()
{
return _hour;
}
/**
* Return the minutes from selected Time
* @param return Minute
*/
public int getMinute()
{
return _minute;
}
/**
* Sets a new Hour for selected Time object
* checks for legal inputs, if not, the hours doesn't update
* @param The new Hour
*/
public void setHour(int num)
{
if (num >= MIN_VAL && num <= MAX_H_BOUND)
_hour = num;
}
/**
* Sets a new Minute for selected Time object
* checks for legal inputs, if not, the minutes doesn't update
* @param The new Minute
*/
public void setMinute(int num)
{
if (num >= MIN_VAL && num <= MAX_M_BOUND)
_minute = num;
}
/**
* Returns a string representation of the Time object
* hh:mm format
*/
public String toString()
{
final int MAX_TIME_FOR_ZERO = 10;
String temp = "";
String temp1 = "";
if (_hour < MAX_TIME_FOR_ZERO)
temp = "0";
if (_minute < MAX_TIME_FOR_ZERO)
temp1 = "0";
return temp + _hour + ":" + temp1 + _minute;
}
/**
* Calculates the minutes from midnight (00:00) for selected time
* @return The number of minutes
*/
public int minFromMidnight()
{
int minute = (_hour*MIN_IN_HOUR) + _minute;
return minute;
}
/**
* Return true if the given Time is identical to the current Time(minutes & hour), otherwise false
* @return True if the given Time is identical to the current Time, otherwise false
*/
public boolean equals (Time1 other)
{
return this._hour == other._hour &&
this._minute == other._minute;
}
/**
* Return true if selected time object is before other, otherwise false.
* @return true if this time before another.
*/
public boolean before (Time1 other)
{
return this.getHour() < other.getHour() &&
this.getMinute() < other.getMinute();
}
/**
* Return true if selected time object is after other, otherwise false.
* @return true if this time after another.
*/
public boolean after (Time1 other)
{
return other.before(this);
}
/**
* Returns the difference in minute between 2 time objects
* @return the difference number of minute from a given object to another
*/
public int difference(Time1 other)
{
int diff = (this._hour*MIN_IN_HOUR) + _minute;
int diff2 = (other._hour*MIN_IN_HOUR) + other._minute;
return Math.abs(diff - diff2);
}
/**
* Add a number of minute to a selected time and return a new object of Time
* With an updated values
* @return New time object with given values
*/
public Time1 addMinutes(int num)
{
int minInDay = 1440; // Minutes in a full day (24 * 60 )
int newMinutes = minFromMidnight() + num;
newMinutes = newMinutes % minInDay;
if(newMinutes < MIN_VAL)
{
newMinutes = minInDay + newMinutes;
}
return new Time1(newMinutes/MIN_IN_HOUR, newMinutes%MIN_IN_HOUR);
}
}
Flight:
/**
* Represents a flight. A Flight object is represented by the flight's origin, destination, departure time, flight duration, no of passengers, if it is full and the price.
*/
public class Flight
{
private String _origin;
private String _destination;
private Time1 _departure;
private int _flightDuration;
private int _noOfPassengers;
private boolean _isFull;
private int _price;
public final int MAX_CAPACITY =250;
public final int MIN_VAL = 0;
/**
* Constructor for a Flight object
* Checks for illegal inputs, sets to zero if illegal
* @param origin - The city the flight leaves from.
* @param dest - The city the flight lands at.
* @param depH - the departure hour (should be between 0-23).
* @param depM - The departure minute (should be between 0-59).
* @param dur - The duration time in minutes(should not be negative).
* @param numP - The number of passengers (should be between 0-maximum capacity).
* @param price - The price (should not be negative).
*/
public Flight (String orig, String dest, int depH,int depM,int dur, int numP,int price)
{
_origin = orig;
_destination = dest;
_flightDuration = dur;
if(numP > MAX_CAPACITY){
numP = MAX_CAPACITY;
}
else if (numP < MIN_VAL){
numP = MIN_VAL;
}
else{
_noOfPassengers = numP;
}
if (_noOfPassengers >= MAX_CAPACITY){
_isFull = true;
}//else{
//_isFull = false;
//}
dur = (dur<MIN_VAL)?MIN_VAL:_flightDuration;
_price = (price<MIN_VAL)?MIN_VAL:price;
_departure = new Time1(depH, depM);
}
/**
* Copy constructor for a Flight object.
* @param other - The Flight object from which to construct the new Flight.
*/
public Flight(Flight other)
{
this._origin=other._origin;
this._destination=other._destination;
this._departure=other._departure;
this._flightDuration=other._flightDuration;
this._noOfPassengers=other._noOfPassengers;
this._isFull=other._isFull;
this._price=other._price;
}
/**
* Return the flight origin country
* @return The flight origin
*/
public String getOrigin()
{
return _origin;
}
/**
* Return the flight destination country
* @return The flight destination.
*/
public String getDestination()
{
return _destination;
}
/**
* Return the flight depratur time
* @return A copy of the flight departure time.
*/
public Time1 getDeparture()
{
return new Time1(_departure);
}
/**
* Return the flight duration in minutes
* @return The flight duration
*/
public int getFlightDuration()
{
return _flightDuration;
}
/**
* Return the number of passengers on the flight
* @return number of passengers
*/
public int getNoOfPassengers()
{
return _noOfPassengers;
}
/**
* Return whether the flight is full or not
* @return True if the flight is full
*/
public boolean getIsFull()
{
return _isFull;
}
/**
* Return the flight price
* @return The price
*/
public int getPrice()
{
return _price;
}
/**
* Changes the flight origin
* @param origin - The flight new origin
*/
public void setOrigin(String origin)
{
if (origin.equals(""))
_origin = _origin;
else
_origin = origin;
}
/**
* Changes the flight destination
* @param dest - The flight new destination
*/
public void setDestination(String destination)
{
if (destination.equals(""))
_destination = _destination;
else
_destination = destination;
}
/**
* Changes the flight departure time
* @param departureTime - The flight's new departure time.
*/
public void setDeparture(Time1 departureTime)
{
_departure = new Time1(departureTime);
}
/**
* Changes the flight duration in minutes
* If the parameter is negative the duration time will remain unchanged.
* @param durTimeMinutes - The flight's new duration time.
*/
public void setFlightDuration(int durTimeMinutes)
{
if(durTimeMinutes>MIN_VAL)
_flightDuration=durTimeMinutes;
}
/**
* Changes the number of passengers on the flight
* If the parameter is negative or larger than the maximum capacity the number of passengers will remain unchanged.
* @param noOfPass - The new number of passengers
*/
public void setNoOfPassengers(int noOfPass)
{
if (noOfPass>=0 && noOfPass<=MAX_CAPACITY)
_noOfPassengers = noOfPass;
}
/**
* Changes the flight price
* If the parameter is negative the price will remain unchanged.
* @param price - The new flight price
*/
public void setPrice(int price)
{
if(price >=MIN_VAL)
_price = price;
}
/**
* Checks for equality between flights
* equality is set by - origin, destination, departure
* @param other - The flight we compare to
* @return True if the received flight is equal to this flight.
*/
public boolean equals(Flight other)
{
return _origin.equals(other._origin)&&
_destination.equals(other._destination)&&
_departure==other._departure;
}
/**
* Return the arrival time of the flight
* @return The arrival time of this flight
*/
public Time1 getArrivalTime()
{
return _departure.addMinutes(_flightDuration);
}
/**
* Add passengers to this flight. If the number of passengers exceeds he maximum capacity, no passengers are added and alse is returned.
* If the flight becomes full, the boolean attribute describing whether the flight if full becomes true.
* @param num - The number of passengers added
* @return True if the passengers were added to the flight.
*/
public boolean addPassengers(int num)
{
boolean added = true;
if((_noOfPassengers+num) > MAX_CAPACITY){
_noOfPassengers = _noOfPassengers;
added = false;
}else if((_noOfPassengers+num) <= MAX_CAPACITY){
_noOfPassengers += num;
_isFull = _noOfPassengers == MAX_CAPACITY;
added = true;
}
return added;
}
/**
* Check if this flight is cheaper than another flight.
* @param other - The flight whose price is to be compared with this flight's price.
* @return True if this flight is cheaper than the received flight .
*/
public boolean isCheaper(Flight other)
{
return this._price<other._price;
}
/**
* Calculate the total price of the flight.
* @return The total price of the flight.
*/
public int totalPrice()
{
return this._price * _noOfPassengers;
}
/**
* Check if this flight lands before another flight.
* @param other - The flight whose arrival time to be compared with this flight's arrival time.
* @return True if this flight arrives before the received flight.
*/
public boolean landsEarlier(Flight other)
{
return this.getArrivalTime().before(other.getArrivalTime());
}
/**
* Return a string representation of this flight (for example: "Flight from London to Paris departs at 09:24.Flight is full.").
* @return String representation of this flight
*/
public String toString()
{
String isFull = ""; // flight not is Full
if (_isFull != true){
isFull = " Flight is not full";
}else{
isFull = " flight is Full";
}
return "Flight from "+ _origin+ " to "+_destination+ " departs at "+_departure+ isFull;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
