CSci2110 - Problem Set 1

Due: Tuesday January 23rd before 11:55pm

Revisions History:
- Created Jan. 3rd 2007

0) Sudoku

SUDOKU is the latest puzzle craze. You can find Sudoku puzzles in both the Globe and Mail and the National Post newspapers. You start with a 9 * 9 grid in which some cells are filled with numbers in the range 1-9.

To solve a SUDOKU puzzle, complete the grid such that every number from 1 to 9 appears in:

a) Write a Java program that uses recursion to solve SUDOKU puzzles. Your program should read the original grid from a text file using a method like readFile/1.

final int gridSideLength = 9;
final int gridSize = gridSideLength * gridSideLength;
public private static void readFile(String fileName) {
   try {
      Scanner scanner = new Scanner(new File(fileName));

      s = new int [gridSideLength] [gridSideLength];

      for (int row = 0;row <gridSideLength;row++)
         for (int col = 0; col < gridSideLength; col++) 
            s[row][col] = scanner.nextInt(); 

      scanner.close();
   } catch (FileNotFoundException e) {
        e.printStackTrace();}
}

To solve the puzzle use a brute force recursive method that tries all possibilities. The key recursive routine might look something like this:

     solve(int location)
       If location == gridSize // puzzle has been solved
          display_solution;
          return true;
       else
          Check each integer v between 1 and 9
              if v is OK in location //meaning that row, col and square conditions are met
                 set location to v
                 return solve(nextEmptyLocation(l))
              else return false

If you are unsure of how to go about designing a recursive solution you might want to look over the Maze_Search program.

b) Augment your program so that it reports timing statistics. If we call a Trial testing a single integer at a single location, how many trials per second does you code make. Show the output of your timing results in your testing.

c) Will your code always find a solution? Argue why or why not.

d) Using big-oh notation describe the complexity of your method.

1) Customers, AirLineTickets and Flights

Implement a class called Customer that represents a passenger on the airline DalhousieAir. Each Customer instance should keep track of its name, its address, its membership number in DalhousieAir's frequent flyer club, and the number of points accumulated in that club. Thus a Customer object has four  fields or instance variable:

name of type String
address of type String
membershipNumber of type int
membershipPoints of type int

Implement a class called Flight that represents a flight on DalhousieAir. Each Flight instance should keep track of its flight number, its origin, its destination, and its departure date and time. Thus a Flight instance has four fields (instance variables):

flight number of type int
origin of type String
destination of type String
departure of type String

Implement a class called AirlineTicket that represents a ticket for a flight on DalhousieAir. Each AirlineTicket instance should keep track of its passenger, its flight, and its price. Thus an AirlineTicket instance has three fields (instance variables):

passenger of type Customer
flight of type Flight
price of type double

For the Customer class, implement the following instance methods:


For the AirlineTicket class, implement the following instance methods:

For the Flight class, implement the following instance methods:

Testing:

Be sure to test your code thoroughly. Create a class called TestTicket with several static test methods for testing your Customer, AirlineTicket, and Flight objects.

Choose meaningful test cases. You are responsible for developing a convincing test plan, i.e., for convincing the TAs that your methods work properly. Here is an example of a test method:

public static void test1() {

       Customer anisa;
       AirlineTicket ticketAnisa;
       Flight flightCalgary;
       anisa = new Customer();
       anisa.setName("Anisa Shane");
       anisa.setAddress("101 Shangrala Street, Calgary");
       anisa.setMembershipNumber(123456);
       anisa.setMembershipPoints(0);
       flightCalgary = new Flight(101, "Ottawa", "Calgary", "03/02/99 7:50 pm");
       ticketAnisa = new AirlineTicket(anisa, flightCalgary, 600.00);
       System.out.println(anisa);
       System.out.println(flightCalgary);
       System.out.println(ticketAnisa);
       System.out.println(anisa.applyPoints(ticketAnisa));
       System.out.println(anisa);
}

Note: Submit a printout of your Customer, AirlineTicket, and Flight classes and their methods along with a copy of the output generated by the testing. Include comments in your code and ensure that all of the output is clearly explained. Be sure to focus on encapsulation. Make everything private that it is reasonable to do so. Testing will be marked!

Note: No late assignments will be accepted.