Banner
Java Lab

Programming in Java

Spring 2017

ANB/PSC 290


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 me.

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. You can name it Agent. The other object is the environment object for running your simulation and remember it must have a “main” function. Your can name it Environment. You can use 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. Create the following methods:

public String getMonth (int month); //returns the month corresponding to the integer entered

public void printRandomList(int length); //prints a list of months in random order

public void printMonths(); //prints all of the months in order

public void printRangeOfMonths(int start, int end); //make sure it checks start and end

 

Step 4 Define your environment.  That is, create your agent in the main method of your Environment and have it print out the months.  For example:

Agent a = new Agent();

a.getMonth(3);

print –>3. March

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!

 

Home Work

First, create a project and a package in the “src” folder of your project. Create a class MyMath with the following method.

public double arithmetic(String operator, double value1, double value2);

This method can add, subtract, multiply, and divide.  It recognizes the following String operators and performs the

appropriate mathematical operations:

“add”, “plus”, “+”,”subtract”,”minus”,”-“, “multiply”, “times”, “*”, “divide”, “/”

If one of these string operators is not used or misspelled, the method prints out, for example:

“Operator ‘add them’ is not defined!”

-1