Using Arrays


Using Arrays

UsingArrays.java
ManyNames.java -- class with array instance var
ManyNamesHarness.java

Classwork


Part A

Create a project, and give it a class with a main.  Other methods in the class with main also need to be static.

Add a method printBack, which takes an array of ints as parameter, and prints them out in reverse order. Think about where the loop for this should start and where it should end.

Add a method makeRandomList, which takes an int len as parameter, and returns an array of ints.  It should create an array of len ints (this means "an array of ints that is of length len"), and then fill it with random numbers between 0 and 100 (inclusive).  Note that when I say "random numbers" I always mean that your program should generate random numbers, not that you should hardcode literals you choose.

Add a method doubleIt, which takes an array of ints as a parameter, and returns an array of ints.  It should create an array of ints that is the same size as the array passed in, and set all the values to double the values in the original array, and then return it. (It should not change the original array!)

So if it were passed the array

{1, 3, 5, 7, 9}

it would return the new array

{2, 6, 10, 14, 18}

In main, create an array with special initialization and test you can print it backwards with printBack.  Also test makeRandomList and doubleIt, using printBack to print the results.


Part B

☑ Add the class Worker to your project.  Add to Worker an instance variable daysWorking which is an array of booleans.   We generally don't provide accessors and mutators for array instance variables.

In the default constructor, set up the array to be 7 long.  The elements of this array represent what days of the week the worker works on.  We'll assume the week starts on Monday, so the last two elements in the array will represent the weekend.  By default, the Worker is working no days.

To Worker, add a method, workWeekday(), which sets all the elements in the array for weekdays to true, and the ones for weekend to false. For full credit, use a loop for the weekdays at least.  This method should work no matter what days were or weren't true previously (i.e. don't assume you start with all days false). 

[EC] Also add workWeekend() which does the opposite. (Test this later in main if you do it)

 Add a method daysWorked which returns an int.  It should go through the array and count how many days the worker is working, then return that number.

 In the toString, use daysWorked to generate something like

worker Constance (working 3 days this week).

☑ In your main, create a worker, print them, then have them work week days and print again.

 

[EC]  Add to Worker a method schedule which takes as parameters an int day and a boolean isWorking.  day should be a number from 1 to 7 indicating the day of the week worked, with 1 being Monday, etc.  Check that day has a valid value, and if so set the appropriate day in daysWorking to the value of working.  (hint: day runs from 1 to 7 but the array runs from . . . )

So myWorker.schedule(3,true) means that myWorker is working on Wednesday, and myWorker.schedule(2,false) means myWorker is not working on Tuesday.   

In your main, make a worker and randomly choose three days out of the week and have them work those days (you don't have to worry about the case where the same random number happens to come up twice).  Print out the worker.

[EC] In Worker's toString, add a local array dayLetters which is an array of Strings for the letters representing each day of the week (MTWRFSU).  Think of this as a parallel array with daysWorking array of booleans.  In the result of the toString also include which days are worked by putting either the letter  if they work that day or a dash if they don't, so for someone who is working Monday, Thursday, and Sunday it would look like

worker Constance (working 3 days this week: M--R--U).

(hint: you should never actually have to change dayLetters after you have set it up; it is just holding the values in the right order)