Banner
Lab 1


Programming in Java

Spring 2012

Psychology 120


The aim of lab 1 is to build a Java program from scratch. When you or your group is done, you will show it to either your Professor or your TA for approval and credit.

Problem

You will build from scratch, an agent that can either print out the months of a year in order, print out a random list of months, or a month given a number. When it works, given the list command, it should print out a list that looks like this:

1. January
2. February
3. March
4. April
5. May
6. June
7. July
8. August
9. September
10. October
11. November
12. December

When given the random order command, it might look like this:

1. June
2. September
3. June
4. April
5. December
6. January
7. March
8. October
9. February
10. April
11. August
12. May

When given, say the number 5, it returns:

May

Step 1

First, create a project and a package in the “src” folder of your project.

Step 2

Create your Classes within the package of your project. One will be your agent that can do the things listed above. The other object is the environment object for running your simulation and remember it must have a “main” function. You can the lecture notes for help.

Step 3

Define your agent class in detail. It will need to store the months of the year and have methods for printing them out. Here are two hints.

Hint #1: A good way to solve this problem is to break it down into subproblems and then write a method that decides, which method to use. The form of this overall method could use a switch statement and look like this:

     public void doMonths(int action, int number) {//number could be the number
         switch(action) {                          //of months to be printed out
            case 1: printListOfMonths(number);     // or the month to be printed out
                    break;
            case 2: printRandomListOfMonths(number);
                    break;
            case 3: printMonth(number);
                    break;
            default: System.out.println("I don't understand what you are asking!");
         }
     }

Hint #2: When printing out data, you can use the “+” operator to combine data for printing. For example, consider the following code.


        for(int i=0;i > months.length; i++) {
           System.out.println(i + "." + " " + months[i]);
        {

This will print out:

1. January
2. February
3. March
4. April
5. May
6. June
7. July
8. August
9. September
10. October
11. November
12. December

Step 4

Define your environment.

Step 5

Run your program and debug it.

Step 6

Show us your program and that it does each of the three things listed above and you are done!