CS102 Spring 2008

Lab # 1

Mr. Spock's Dilemma:

 

In this lab, you will create a NetBeans project in which you will implement Mr Spock's recursive algorithm for counting the number of combinations of k planets that may be visited in a solar system containing n planets.

 

How to get started

  1. Login to one of the Linux-based machines in the Asprey Laboratory.  
     
  2. Launch NetBeans. You may do this either from a terminal window by typing netbeans& at the command line prompt, or by selecting NetBeans from the toolbar by pressing the "mouse" button, then selecting "Development" and then "Netbeans".
         
  3. From NetBeans, select File and then New Project from the drop-down menu that appears.
       
  4. From the dialog window, under the General category, select Java Application under the Projects column and click on the Next button at the bottom of the window.
         
  5. The next window that appears has text fields labeled Project Name, Project Location, and Project Folder.  Each of these fields will contain a default value.  
    1. Change the Project Name to Lab1.  
    2. Change the Project Location to the cs102 directory you created last week in lab. 
    3. The Project Folder name will change to be consistent with the project name you select.  
    4. Below these three text fields are two choice boxes and a text field for naming the class holding the main function.  Since the first class you create in a project (and the only class in this laboratory exercise) is the class holding the main method, you will leave both of these choices set.  The default name used by NetBeans for the class holding the main method is Main.  The package and class name are shown in the text field.  You may change the class name to something else if you desire.  
    5. When you have selected a name for your project (and possibly changed the name of the application class), you will left-click on the Finish button at the bottom of the window.
         
  6. Complete the program described in the next section, Work to be Done.
       
  7. You are asked in the assignment to use command line arguments to supply the number of planets in the solar system and the number to be visited.  Right-click on the name of your project (in bold in the Projects window).  In the pop-up menu under Categories left-click on Run. In the second text field labeled Arguments type in the two values separated by a space, and then left-click on OK.
       
  8. When you have finished entering your code, save your project (it will be saved in the directory specified in the Location field in the first menu), select Build à Compile, and then Run.  When your project successfully runs, alert your lab instructor and follow the submit procedure detailed below.

 

Work to be Done

 

You are to implement Mr. Spock's algorithm for counting the number of ways in which k planets can be selected to visit in a solar system containing n planets (where n >= k).  You are to complete the following two methods:

 

      private static int countCombinations(int planets, int toVisit) {…}
   

      public static void main(String[ ] args) {..}

 

Method countCombinations() must be recursive. You've seen this code in chapter 3, and it's also provided under the code for chapter 3. Feel free to copy/paste the code to get you started (but remember to change the method's name from c() to countCombinations()...). Before returning a value for either base case or for the sum of the recursive calls, you should print a statement that displays the values in the activation record for each recursive call, as shown here:

 

      System.out.println("n = " + planets + ", k = " + toVisit

                      ", return value = " + ?); 

 

Note 1: The ? at the end of the println statement indicates where you will replace the ? with either a 1 for either of the base cases or the value returned by the sum of the two recursive calls.

Note 2: The println statement will need to occur after the recursive calls, but before you return from the current call. 

You will need to save the value returned from the sum of the two recursive calls in a temporary memory location, then use its contents to first complete the println statement and then to return.

 

The header for the main method will be automatically generated for you.  You will complete the main() method by reading the two command line arguments (remember they are read in as strings, stored in args[0] and args[1]), and using these values as parameters in a call to countCombinations(). You may find the Integer.parseInt(String) method helpful to convert the command line args from strings to ints.  Finally you will skip one or two lines to separate from the activation record data and write to the monitor the value for planets, toVisit, and the number of combinations obtained.  Run your program with the values 8 and 5 initially. 

Sample Output

 

Here is some sample output: (try to make your output look just like this)

init:
deps-jar:
compile:
run:
Computing number of ways to choose 5 out of 8 total planets...
n = 3, c = 0, return = 1
n = 2, c = 0, return = 1
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 3, c = 1, return = 3
n = 4, c = 1, return = 4
n = 2, c = 0, return = 1
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 3, c = 1, return = 3
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 2, c = 2, return = 1
n = 3, c = 2, return = 3
n = 4, c = 2, return = 6
n = 5, c = 2, return = 10
n = 2, c = 0, return = 1
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 3, c = 1, return = 3
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 2, c = 2, return = 1
n = 3, c = 2, return = 3
n = 4, c = 2, return = 6
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 2, c = 2, return = 1
n = 3, c = 2, return = 3
n = 3, c = 3, return = 1
n = 4, c = 3, return = 4
n = 5, c = 3, return = 10
n = 6, c = 3, return = 20
n = 2, c = 0, return = 1
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 3, c = 1, return = 3
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 2, c = 2, return = 1
n = 3, c = 2, return = 3
n = 4, c = 2, return = 6
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 2, c = 2, return = 1
n = 3, c = 2, return = 3
n = 3, c = 3, return = 1
n = 4, c = 3, return = 4
n = 5, c = 3, return = 10
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 2, c = 2, return = 1
n = 3, c = 2, return = 3
n = 3, c = 3, return = 1
n = 4, c = 3, return = 4
n = 4, c = 4, return = 1
n = 5, c = 4, return = 5
n = 6, c = 4, return = 15
n = 7, c = 4, return = 35
n = 2, c = 0, return = 1
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 3, c = 1, return = 3
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 2, c = 2, return = 1
n = 3, c = 2, return = 3
n = 4, c = 2, return = 6
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 2, c = 2, return = 1
n = 3, c = 2, return = 3
n = 3, c = 3, return = 1
n = 4, c = 3, return = 4
n = 5, c = 3, return = 10
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 2, c = 2, return = 1
n = 3, c = 2, return = 3
n = 3, c = 3, return = 1
n = 4, c = 3, return = 4
n = 4, c = 4, return = 1
n = 5, c = 4, return = 5
n = 6, c = 4, return = 15
n = 1, c = 0, return = 1
n = 1, c = 1, return = 1
n = 2, c = 1, return = 2
n = 2, c = 2, return = 1
n = 3, c = 2, return = 3
n = 3, c = 3, return = 1
n = 4, c = 3, return = 4
n = 4, c = 4, return = 1
n = 5, c = 4, return = 5
n = 5, c = 5, return = 1
n = 6, c = 5, return = 6
n = 7, c = 5, return = 21
n = 8, c = 5, return = 56

It's logical the number of combinations = 56
BUILD SUCCESSFUL (total time: 0 seconds)

 

Submitting your work

      From a terminal window, type the following commands:

 
        cd
        cd cs102
        submit102 Lab1

Log out

When you are done, close NetBeans, and then locate the logout button on the menu bar. Click on the logout button (red arrow pointing through an open door).  Choose "Logout..." and then click "Yes" when prompted.  Always remember to log out when you are done using the system, to ensure that no one else uses your account.