Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualCryZe

Posted 11 December 2012 - 08:38 AM

I think it could look like this, if I understood everything correctly (code untested):

[mod edit: removed code]

Edit: Oh, I'm sorry. Didn't read that it was homework :(

#7rip-off

Posted 11 December 2012 - 06:10 AM

I think it could look like this, if I understood everything correctly (code untested):

[mod edit: removed code]

#6CryZe

Posted 11 December 2012 - 03:21 AM

I think it could look like this, if I understood everything correctly (code untested):
public class Plane
{
    private int freeSeats;
    private int lastFreeSeat;
    private int[] seats;

    public Plane(int seatCount)
    {
        seats = new int[seatCount];
        freeSeats = seatCount;
    }

    public boolean assign(int groupId, int persons)
    {
        if (persons > freeSeats)
        {
            return false;
        }
		
        while (persons > 0)
        {
            lastFreeSeat++;
            seats[lastFreeSeat] = groupId;
            freeSeats--;
            persons--;
        }
		
        return true;
    }
	
    public boolean hasFreeSeats()
    {
        return freeSeats > 0;
    }
}

private static void assignFlightGuests(Plane plane)
{
    showMessage(MESSAGE_WELCOME);

    int groupId = FIRST_GROUP_ID;

    while (plane.hasFreeSeats())
    {
        int guestCount = requestFlightGuests();
        boolean groupCanTakeThePlane = plane.assign(groupId, guestCount);
		
        if (groupCanTakeThePlane)
        {
            showMessage(MESSAGE_HAVE_A_NICE_FLIGHT);
            groupId++;
        }
        else
        {
            showMessage(MESSAGE_NOT_ENOUGH_SEATS);	
        }	
    }
}

public static void main(String[] args)
{
    Plane smallPlane = new Plane(5);

    assignFlightGuests(smallPlane);

    showMessage(MESSAGE_PLANE_DEPARTING);
}

#5CryZe

Posted 11 December 2012 - 03:21 AM

I think it could look like this, if I understood everything correctly (code untested):
public class Plane
{
    private int freeSeats;
    private int lastFreeSeat;
    private int[] seats;

    public Plane(int seatCount)
    {
        seats = new int[seatCount];
        freeSeats = seatCount;
    }

    public boolean assign(int groupId, int persons)
    {
        if (persons > freeSeats)
        {
            return false;
        }
		
        while (persons > 0)
        {
            lastFreeSeat++;
            seats[lastFreeSeat] = groupId;
            freeSeats--;
            persons--;
        }
		
        return true;
    }
	
    public boolean hasFreeSeats()
    {
        return freeSeats > 0;
    }
}

private static void assignFlightGuests(Plane plane)
{
    showMessage(MESSAGE_WELCOME);

    int groupId = FIRST_GROUP_ID;

    while (plane.hasFreeSeats())
    {
        int guestCount = requestFlightGuests();
        boolean groupCanTakeThePlane = plane.assign(groupId, guestCount);
		
        if (groupCanTakeThePlane)
        {
            showMessage(MESSAGE_HAVE_A_NICE_FLIGHT);
            groupId++;
        }
        else
        {
            showMessage(MESSAGE_NOT_ENOUGH_SEATS);	
        }	
    }
}

public static void main(String[] args)
{
    Plane smallPlane = new Plane(5);

    assignFlightGuests(smallPlane);

    showMessage(MESSAGE_PLANE_DEPARTING);
}

#4CryZe

Posted 11 December 2012 - 03:17 AM

I think it could look like this, if I understood everything correctly (code untested):
public class Plane
{
    private int freeSeats;
    private int lastFreeSeat;
    private int[] seats;

    public Plane(int seatCount)
    {
        seats = new int[seatCount];
        freeSeats = seatCount;
    }

    public boolean assign(int groupId, int persons)
    {
        if (persons > freeSeats)
        {
            return false;
        }
		
        while (persons > 0)
        {
            lastFreeSeat++;
            seats[lastFreeSeat] = groupId;
            freeSeats--;
            persons--;
        }
		
        return true;
    }
	
    public int getFreeSeatCount()
    {
        return freeSeats;
    }
}

private static void assignFlightGuests(Plane plane)
{
    showMessage(MESSAGE_WELCOME);

    int groupId = FIRST_GROUP_ID;

    while (plane.getFreeSeatCount() > 0)
    {
        int guestCount = requestFlightGuests();
        boolean groupCanTakeThePlane = plane.assign(groupId, guestCount);
		
        if (groupCanTakeThePlane)
        {
            showMessage(MESSAGE_HAVE_A_NICE_FLIGHT);
            groupId++;
        }
        else
        {
            showMessage(MESSAGE_NOT_ENOUGH_SEATS);	
        }	
    }
}

public static void main(String[] args)
{
    Plane smallPlane = new Plane(5);

    assignFlightGuests(smallPlane);

    showMessage(MESSAGE_PLANE_DEPARTING);
}

#3CryZe

Posted 11 December 2012 - 03:15 AM

I think it could look like this, if I understood everything correctly (code untested):
public class Plane
{
    private int freeSeats;
    private int lastFreeSeat;
    private int[] seats;

    public Plane(int seatCount)
    {
        seats = new int[seatCount];
        freeSeats = seatCount;
    }

    public boolean assign(int groupId, int persons)
    {
        if (persons > freeSeats)
        {
            return false;
        }
		
        for (; persons > 0; persons--)
        {
            lastFreeSeat++;
            seats[lastFreeSeat] = groupId;
            freeSeats--;
        }
		
        return true;
    }
	
    public int getFreeSeatCount()
    {
        return freeSeats;
    }
}

private static void assignFlightGuests(Plane plane)
{
    showMessage(MESSAGE_WELCOME);

    int groupId = FIRST_GROUP_ID;

    for (plane.getFreeSeatCount() > 0)
    {
        int guestCount = requestFlightGuests();
        boolean groupCanTakeThePlane = plane.assign(groupId, guestCount);
		
        if (groupCanTakeThePlane)
        {
            showMessage(MESSAGE_HAVE_A_NICE_FLIGHT);
            groupId++;
        }
        else
        {
            showMessage(MESSAGE_NOT_ENOUGH_SEATS);	
        }	
    }
}

public static void main(String[] args)
{
    Plane smallPlane = new Plane(5);

    assignFlightGuests(smallPlane);

    showMessage(MESSAGE_PLANE_DEPARTING);
}

PARTNERS