Friday, January 16, 2015

Friday, January 16th

Today was a very good class. I managed to get many problems done. Mostly strings though, because I was really into strings and I just kept going on and on with strings and forgot to do array until the end of the class.  I don't have anything else to say really besides that I am getting closer and closer to finishing level 1, which makes me happy. I am happy at the pace that I am going at.

The string problems that I did today weren't very long, but they required a lot of thinking about really the technical things.

The problem for today:

Given a string, return true if it ends in "ly". 

endsLy("oddly") → true
endsLy("y") → false
endsLy("oddy") → false


public boolean endsLy(String str) {
  
  if (  str.length()>=2 && str.substring(str.length()-2, str.length()).equals("ly"))
// if the string length is 2 or larger, then we can always have the possibility of it ending with ly. if the last 2 characters of the string is equals to the string "ly"
  {
    return true;
// then return true.
  }
  
  return false;
// if the above conditions are not met, then return false.

}

I learned something with this problem. The first one being, that I had to indicate the length of the string before anything else. In Java, unlike some other coding languages, the computer checks the first thing, and if the first thing is not even true then it won't even check the second one. So when I had the line that ordered to check if the string ended in "ly" before checking the length of the string, it returned to me out of bounds if the string was an empty one.

I am doing my presentation for the end of the semester on Tuesday. I am looking forward to it because even though I don't have one big project to present, it will be an interesting sort of presentation.



No comments:

Post a Comment