Monday, October 8, 2012

Hints on Array Problems

Many of you are struggling on the array problems. In this post, I have provided some hints on some of the solutions. In some cases, I provide you with an algorithm to help you find a solution. For some others, I give you pieces of code.

 First, here is how you parse an array of unknown length. Lets say you have an array called a. Here is a loop that prints every element, one per line:

          for (int i=0; i < a.length; ++i)
         { 
               System.out.println(a[i]);
         }


Notice we use the length attribute of array a to figure out how many times we need to run through the loop. That means this loop will work for any length array.

Ok, now for some help on individual problems:

7.4: create an integer called previousValue and set it initially to be equal to the first element of the array. Then set up a loop to parse the array from the second element to the last element. If the current element being checked is smaller than the previousValue immediately return false. Otherwise, update previousValue to the current element and go to the next element in the loop. If you make it through the entire loop without getting to the return false statement, just return true.

7.11: Its important to understand that the printBackwards() method does not return a value. It justs prints some stuff. The trick here is to parse the array from the largest index to the smallest. So use a loop like this:

          for (int i=a.length-1; i >=0; --i)    // i is the index of the next element from biggest to zero
         { 
               // add print statements here
         }

7.1:  Create an integer called returnValue and set it equal to -1. Parse the array from start to finish. If the target is located, update the returnValue to the current index. If the target is in the array multiple times, the returnValue will simply get updated multiple times. After parsing the entire array, just return returnValue;

7.2:  Parse the array and locate the largest element. Parse the array and locate the smallest element. subtract the two answers and add one. Return that as the final answer.

7.3: Create an integer called returnValue and initialize it to zero. Parse the array. For each element, check to see if it is greater than equal to the minimum value AND less than or equal to the max value. If so, increment the returnValue. After parsing the entire array, return the returnValue.

Array Deadlines Extended Again

The deadlines for the practice-it array problems have been extended (again) to 10/10 (the day of our test). They will be due on midnight that day. My suggestion is to submit on the practice-it site whatever you have at that time (even if it doesn't work) because I will not be extending the deadline for the class any further. Some have asked for after-school help on Tuesday - you are welcome to come to 429 - but I have to give preference to the Algebra kids who show up on that day. On Sunday (yesterday) around midnight, I logged into practice-it to see how far along the class was on these problems and was disappointed to see so many of you had barely started. Mr. Sarkar

Wednesday, October 3, 2012

Array Homework Deadlines

I've pushed out the deadlines for the array homework given how much Practice-It has been down. The new deadlines are listed right next to the titles on Juno for each assignment. Also, I've eliminated Problem Set D and replaced it with a group of problems on Coding Bat. The Coding Bat problems (which we started in class today) are due midnight on Friday. All the other array homework is due Tuesday, when you get back from the three-day weekend. Mr. Sarkar

Tuesday, October 2, 2012

Practice It is Down Again

The Practice-It site is down again. So I have extended the deadlines for all the array problems. The new deadlines are listed in the names of the corresponding Juno assignments. You are still strongly encouraged to check back frequently to see when the site comes back up and try to get ahead of the deadlines. Sorry for this inconvenience. Mr. Sarkar

Monday, October 1, 2012

instanceof Operator

Some of you were absent or need a refresher on the instanceof operator in Java. Unlike most other java topics, instanceof did not have any good videos on youtube. So I have made the following tutorial to help you:

Mr. Sarkar