HW1 Solutions Page

Return to homework page.


Problem 1.1 

(a) An Airline Reservations System manages Passengers, 
Airlines, Aircrafts, and Airports. Passengers make Bookings on Flights between Airports. 
Airlines provide Flights between Airports at various times and on various
kinds of Aircraft. Airports are located in various cities and countries.
Aircraft have Seats which may be next to a window or an aisle.
Seats may also be first class, business class, or economy class.
Bookings can be refundable or not refundable. Passengers may belong to
frequent flyer programs of Airlines. Flights occur at a particular
time of the day, but the same flight may fly on many different dates.

(b) A Passenger may try to make a booking, delete a booking, or modify
an existing booking. Airlines might change their Flight schedules. They
may buy a new type of Aircraft. A Passenger may want to join a frequent
flyer program. The reservation system need to be able to add and delete
Passengers, Airports, and Airlines.


(c) Here are some classes with their methods and attributes I came up
with:
( <#> - means compositional aggregation <> - means nonexclusive )


____________________________
|Airline Reservation System|
|__________________________|
| addAirline               |
| delAirline               |
| addPassenger             |
| delPassenger             |
| addAirplane              |
| delAirplane              |
| addBooking               |
| delBooking               |
|__________________________|
_______________ 
|Passenger    |
|_____________|         
|name         |       
|frequentFlyer|
|_____________|
| joinProgram |
| makeBooking |                
|_____________|

_______________ 
|Airport      |
|_____________|         
|callCode     |       
|city         |
|country      |                
|_____________|

______________ 
|Booking     |
|____________|          
|passenger   |
|flight      |                
|date        |
|____________|
| modify     |
| delete     |
|____________|

______________ 1      *____________________
|Airline     |<#>------|Flight            |
|____________|         |__________________| 
|name        |         |time              |
|headquarters|         |departureAirport  |
|____________|         |arrivalAirport    |
| addFlight  |         |__________________|
| delFlight  |
| addPlane   |
| delPlane   |
|____________|          These are all 1 since I am ruling out
<#> 1                   things like Airlines without aircraft.
 |
 |  *
_|____________ 1      *__________
|Aircraft    |<#>------|Seat    |
|____________|         |________| 
|name        |         |class   |
|manufacturer|         |location|
|date built  |         |________|
|____________|

(d) Here is another UML diagram showing how all of the classes relate.

____________________________ 0                          * ___________
|Airline Reservation System|<#>---------------------------|Passenger|
|__________________________|<#>----|                      |_________|
  <#> 0               <#> 0  0     |                             |1
   |                   |           |----------------------       |
   |  *                |  *                              |       |1
___|_____        ______|__   1  * __________   * 1______ | * ____<>____
|Airport|        |Airline|<#>-----|Aircraft|<#>---|Seat| |---|Bookings|
|_______|        |_______|        |________|      |____|     |________|
   |2              <#> 1                                       <#>
   |                |                                           |1
   |                |  *                                        |
   |           1 ___|____                                       |
   |-----------<>|Flight|---------------------------------------|
                 |______|1

/**
   cs151sec3hw1 -- driver class for hw1 

   Run from the command line with a command like:

   java cs151sec3hw1 3 45 3 6 7

   In this example the application returns the 3rd largest number out 
   of the remaining numbers 45 3 6 7. In general, returns the args[0]th
   largest number from the remaining args.

   @author Chris Pollett
   @version 1.0
*/

public class cs151sec3hw1
{

   public static void main( String args[])
   {
      int k = Integer.parseInt(args[0]);

      OrderedNums myNums = new OrderedNums(1,args);

      System.out.println(myNums.kLargest(k));		
   }



/**
   The OrderedNums class maintains an array of ordered integers
   and allows one to project out the kth largest number.	

   Javadoc comments will only work on public classes. Since I wanted 
   this homework submitted as only one Java file I made this class 
   an inner class. We will talk about jar files later in the semester.
			
   @author Chris Pollett
   @version 1.0 
*/
public class OrderedNums
{
	private int numbers[];

   /**
      Main constructor used to create order numbers objects.

      @param startIndex - beginning position in arr to start
                          storing number at
      @param arr - string array that will be converted into an
                   
   */
   public OrderedNums(int startIndex, String arr[])
   {
      numbers = new int[arr.length - startIndex];

      for(int j =0; j < numbers.length; j++)
         numbers[j] = Integer.parseInt(arr[i+j]);		
		
      sort(); /* after converting the string array to
                 int's we immediately sort the int's.
              */
   }

   /**
      Computes the kth largest number in OrderedNums object

      @param k - used to determine the k in kth largest
      @return the kth largest <code>integer</code> in
              OrderedNums 
   */
   public int kLargest(int k)
   {
	return numbers[k-1];
   }

   /*
      Performs insertion sort on the array numbers. 

   */
   private void sort()
   {
      for( int i = 1; i< numbers.length; i++)
         insert(i);
   }

   /*
      Inserts numbers[sortIndex] into the <code>integers</code>.
      numbers[0],...,numbers[sortIndex-1] so that the result is sorted.

   */
   private void insert(int sortIndex)
   {
      int i=sortIndex;

      while(numbers[i-1] < numbers[i])
      {
          swap(i-1,i);
          i--;
      }
			
   }

   /*
      Swap the values stored in numbers[index1] and numbers[index2].
		
      @param index1 - first array location
      @param index2 - second array location		
   */
   private void swap(int index1, int index2)
   {
      int tmp = numbers[index1];

      numbers[index1] = numbers[index2];
      numbers[index2] =tmp;
   }

} //end class OrderedNums

} //end class cs151sec3hw1