Tuesday, April 28, 2015

Tuesday, April 28th

Today was a very split up class. It was separated into 4 different parts, in which I did different things.

In the first part of class, Tommy told me that he was starting Array 1, but he had no idea what to do. So, since I had completed it already and it was easy to me now, I explained to him all that I knew about Arrays, to get him going. And it seemed like I was a good teacher, since he flew through all the problems after that.

In the second part of class, I was working on the problem where I had to use indexOf. It was called getSandwich (I will show it) The thing is, that I was very confused, and when I called Mr. Daly over, we were both just as confused. We tried to solve it, but just couldn't figure it out. So we went to Mr. Daly's computer, and looked at his solution. It turned out that he had tried to use a method which was rather interesting : he used the IndexOf, but backwards ! This will all be explained in the problem. However, after looking at his problem and understanding how he did it, Mr. Daly introduced me to the Java library, which consists of all the possible methods in Java. It turns out that Mr. Daly from a few years ago could have saved himself a lot of time, because there is actually a method that does indexOf from backwards in just one line of code, and he could have used that instead of the 5 or so that he wrote.
----------------------------------------------------------------------------------------------------------------------------------
A sandwich is two pieces of bread with something in between. Return the string that is between the first and last appearance of "bread" in the given string, or return the empty string "" if there are not two pieces of bread. 

getSandwich("breadjambread") → "jam"
getSandwich("xxbreadjambreadyy") → "jam"
getSandwich("xxbreadyy") → ""



public String getSandwich(String str) {

  if (str.length()<11)
  {
   return "";
// if the string length is less than 11, then there can't be two "bread" and something in between, so return
// an empty string.
  }
 
  int first = str.indexOf("bread");
  int last = str.lastIndexOf("bread");
// create two integers, each marking the first appearance of "bread" and the last in the string str.
 

  return str.substring(first+5,last);
// return the part of the string which is after the first "bread", and before the last one.

}

----------------------------------------------------------------------------------------------------------------------------------


In the third part of class, Matan really wanted to show me his work that he was doing on GarageBand, so I went outside with him for a few minutes so he could show me his music. It was an interesting mix of sounds that he put together, and surprisingly, it actually sounded good.


And finally, during the last part of class, I got back to working on my own problems. I got started on another problem, and then encountered another problem (deja-vu). But that's another story, that I'll deal with next class.












No comments:

Post a Comment