Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: cant solve this one out

  1. #1
    Junior Member
    Join Date
    Apr 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default cant solve this one out

    im working on some project, and cant solve this Airport class I've been making, this class is working with two other classes written, Time1 and Flight, for some reason i can't figure this out, and it's not working at all...
    kinda urgent ill be glad for help ASAP
    ill be adding the other 2 classes as attachments.
    this is the Airport class:
    /**
     * This program Represents an airport of Flights objects
     
     * version 16422
     */
    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 wich 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 wich selected from the currect 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 ther 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 numebr 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){
                    fullFli++;
                }
            }
            return fullFli;
        }
     
        /**
         * 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 wich ticket is most expensive, if ther is 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 ther is 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 dosent 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 dosent 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 diffrence in minute between 2 time objects
         * return the diffrence 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);
        }
    }


    --- Update ---

    Flight:
     
     
    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
     
         */
        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;
            dur = (dur<MIN_VAL)?MIN_VAL:_flightDuration;
            _price = (price<MIN_VAL)?MIN_VAL:price;
            //_departure = new Time1(depH, depM);
     
        }
     
     
        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;
     
        }
     
     
        public String getOrigin()
        {
            return _origin;
        }
     
     
        public String getDestination()
        {
            return _destination;
        }
     
     
        public Time1 getDeparture()
        {
            return new Time1(_departure);
     
        }
     
     
        public int getFlightDuration()
        {
            return _flightDuration;
        }
     
     
        public int getNoOfPassengers()
        {
            return _noOfPassengers;
        }
     
     
        public boolean getIsFull()
        {
            return _isFull;
        }
     
     
        public int getPrice()
        {
            return _price;
        }
     
     
        public void setOrigin(String origin)
        {
            if (origin.equals(""))
                _origin = _origin;
            else    
                _origin = origin;
        }
     
     
        public void setDestination(String destination)
        {
            if (destination.equals(""))
                _destination = _destination;
            else    
                _destination = destination;
        }
     
     
        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;            
        }
     
     
        public void setNoOfPassengers(int noOfPass)
        {
            if (noOfPass>=0 && noOfPass<=MAX_CAPACITY)
                _noOfPassengers = noOfPass;
        }
     
     
        public void setPrice(int price)
        {
            if(price >=MIN_VAL)
                _price = price;
        }
     
     
        public boolean equals(Flight other)
        {
            return _origin.equals(other._origin)&&
            _destination.equals(other._destination)&&
            _departure==other._departure;
        }
     
     
        public Time1 getArrivalTime()
        {
            return  _departure.addMinutes(_flightDuration);
        }
     
     
        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;
        }
     
     
        public boolean isCheaper(Flight other)
        {
            return this._price<other._price;
        }
     
     
        public int totalPrice()
        {
            return this._price * _noOfPassengers;
        }
     
     
        public boolean landsEarlier(Flight other)
        {
     
            return this.getArrivalTime().before(other.getArrivalTime());
     
        }
     
        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;
        }
    }

  2. #2
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: cant solve this one out

    There are 3 errors:
    1)The Flight constructor is a comment.
    2)The variable FullFli doesn't exist line 131 Class Airport
    3)You don't close the if brackets in the first Flight constructor.
    Last edited by andreaita44; April 19th, 2022 at 02:52 PM.

  3. #3
    Junior Member
    Join Date
    Apr 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: cant solve this one out

    thanks for the reply,
    1) its a comment because i got an error on posting the thread so tried a few things https://www.javaprogrammingforums.co...milies/eek.png
    2)same for FullFli, it marked the word FULL for a forbidden word for some reason
    3) whats incorrect with the flight constructor?

  4. #4
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: cant solve this one out

    line 35 Flight class if doesn't have close brackets

  5. #5
    Junior Member
    Join Date
    Apr 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: cant solve this one out

    Quote Originally Posted by andreaita44 View Post
    line 35 Flight class if doesn't have close brackets
    fixed it, methods on AIRPORT still not working properly

  6. #6
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: cant solve this one out

    what errors does it give to you?

Similar Threads

  1. How to solve this?
    By jellescheer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 12th, 2012, 09:14 AM
  2. anyone can help me to solve this?
    By d3sm0nd in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 27th, 2012, 06:47 AM
  3. Solve Them Please
    By omath in forum Java Theory & Questions
    Replies: 1
    Last Post: December 25th, 2010, 04:26 PM
  4. solve it plz
    By tillu in forum Java Theory & Questions
    Replies: 4
    Last Post: December 17th, 2010, 01:45 PM

Tags for this Thread