Mr I Never Use an Umbrella

TC201 #CourseReview – Mr. I Never Use an Umbrella

This course has been something really new to me.

I learned the more fundamental concepts of Object Oriented Programming, as well as putting most of these into practice. Classes, objects, attributes and methods. APIE: Abstraction, polymorphism, inheritance, encapsulation and delegation. Has-a, is-a relationships. Visibility modifiers. Overloading and Overriding. Also I learned about software development tools and practices like UML and CRC cards. I still have to learn about the metaobject protocol. I also learned a lot about the Unity game engine and about video game networks and protocols, the Authoritative Server approach, latency compensation, client prediction, server reconciliation and other interesting concepts. this and this  are some really nice articles about the subject.

First off, I thought I wasn’t doing a lot of things, the course was really relaxed, doing the assignments whenever I wanted and learning whatever I wanted. A lot of people coursing OOP with Salinas told me that I was learning nothing, because the way the class works. But at the end of the first partial I realized I had learned more than my fellows from the class of Salinas, and they are still saying “you didn’t learn about Java user interfaces”,  but honestly, I don’t give a duck about user interfaces. I was learning all the theory and doing the programming wsq’s by, which is quicker than having the teacher trying to put the concepts in everyone’s head in one class, because I think everyone has it’s own learning rhythm.

I personally think this teaching method is really good and could work really well, but first, ken has to polish some aspects of the course:

First, there must be more emphasis on doing the assignments, because people (at least most I know) is not very interested in self-education, they don’t do the things they should until they realize they have one or two days left for finishing everything. They don’t manage their time correctly, they prefer to procrastinate all the time instead of focusing in what is really important.

Second, I think there should be more programming assignments, sometimes I learned the concepts, did the posts about what I learned and everything but never put these concepts into practice, making me feel like I did not master the topic completely, and I don’t mean learning Java, I mean reinforcing the concepts learned with something practical, because I definitively think that this class (and every programming class) should be language agnostic, focusing on learning the theory and putting these concepts in practice in whichever language the student wants.

I think that if ken really efforts improving his course in these aspects he will have a really, really good course, keep the good work!

James Gosling interview

Just finished watching Triangulation 245, featuring James Gosling, the creator of Java, one of the most popular programming languages in the world, and the Java Virtual Machine.

Glossing staring right into your soul

Gosling staring right into your soul

He was interested in programming at an early age. The idea of creating Java came from observing how everything was switching from hardware to software, writing code instead of inventing new machines. I also want to point out that he is Canadian.

Canadian-Flag.jpg

Ward Cunningham

Ward Cunningham

This post is about my thoughts on the Triangulation Chapter 239, where Ward Cunningham is interviewed. He is the inventor of the wiki, he contributed to the development of Eclipse 3x2_Collective_CUNNINGHAM.jpgand Relic and is part of the Smalltalk community.

Wikis are the biggest knowledge source in the world, where anyone can write, read and edit any publication. Extreme Programming is a methodology for agile software development where there is more emphasis in adaptability  than predictability.

367px-Extreme_Programming.svg.pngThe interview showed me how a simple man can make a huge contribution to humanity, such as wikis; where people share their knowledge just to make that knowledge public for everyone, and how we can work together for a better society.

 

tutu sparkles

#WSQ11-Library

And this is the most boring wsq that I have ever done!
Because all it asked of me was filling the blank methods with just some return statements in most cases.
The most interesting I did in this assignment was doing a for loop and throwing some exceptions.

So i entered the WSQ11 blog post in kenscourses and clicked the first link because I’m too lazy to read.
And the first thing I notice is that I don’t know what to do here, obviously, instead of go back and read the instructions I started doing the first assignment.
wut

After some dedication and hard work doing that thing about gravity I realized it was too stupid, so I deleted all the files, including System32.

So now I had the tough task of reading the instructions.
According to the English dictionary of Cambridge, the word “to” is pronounced as “tu”.
The instructions said that I should try to “tutu” the Library assignment.
tutu

All I did in the book class was add some fields, getters and setters.
part1

And in the Library class I just used the Book class to create the simulation of a library.
It also asked me to create a static method, I was so excited.
final

After that I kept reading and clicked on a link, and the first thing a saw was a big message saying “Buy my book!”, it was so gross that I had to leave the site.
buyMyBook

github

#WSQ09-TheMovies

See the assignment on github.

This assignment has been the hardest so far, I had to read a lot of documentation to complete this one.
I had to learn about these things:

  • About the Scanner class, for reading files and receiving input from the console.
  • About the Set interface and HashSet class, to use it like a dictionary in Python.
  • About the Map interface and HashMap class.
  • About how the String class operates and some methods.
  • About CharSequence, just because String is an implementation of CharSequence.
  • About handling Exceptions, for the file reading and the MovieQuery class.
  • And finally, a little bit about regular expressions, for reading the query.

So when I read the the document in the link of the assignment, it says that we need to do set operations, and I found this on stack overflow.
It turns out that I didn’t need to implement any of the code for set operations, just imported the Set interface and the HashSet class.

SetMethonds

 

The Set interface contains the methods addAll, retainAll and removeAll, which are equivalent to union, intersection and difference set operations, respectively. But the problem said: find the set of actors that the given movies don’t have in common. (or something by the way).
And so, this is equivalent to the difference of the union and the intersection of two sets. (A ∪ B) – (A ∩ B). And this is called the symmetric difference.

So I first created the Movie class and wrote several constructors supporting different data types, because why not. It has two fields, a String; representing the name of the movie, and a Set of Strings; representing the name of the actors. I wrote the methods for the operations I described above. It doesn’t makes much sense because it’s just returning the result of the call to the set methods, but at least I gave these methods a fancier name. But it makes sense in the getActorsNotInCommon method, because the symmetric difference is not implement in the Set interface. I also added getters and setters and bla bla bla.

Before using the File class I tested it.
throwMissing

But the code would not compile, and then, reading the docs of the File class, I realized I had to add “throws Exception” after the method parameters.
justAddthrows
And there are a lot of different implementations of the Exception interface.

In the MovieLibrary class I used a Scanner to read the file where are the actors and movies. And the constructor can throw a FileNotFoundException, and it has to, because the File class can, and so the code where it is used has to as well. The class has a field for storing the movie objects in a Map, using its name as the key, so the program has to store the name of the movie twice, and I though it was stupid, but I didn’t found a better way to do it, but now I’m wondering if it is possible to use something like a pointer to the variable inside the Movie object, to use it as the key of the HasMap, but whatever.

And the code for reading the file with the scanner:
usingScanner

In the constructor of the MovieLibrary two scanners are used, one for reading the lines of the file, and another one for reading that line and separate the actor and movies, and then instantiating a Movie object -only if the name was not already in the map- and then pushing it into that map.
This class use the methods of the Movie class in all the methods.
movieLib

And I tested what I had written up to this point, before moving into implementing the user input:
testsOfTheFinalCode

Then I was writing the main class, TheMovies, according to the description in the assignment (with the menus and option and shit). When I reached the part where says that I should ask the user for an input like: “movieName symbol moviewName”, I realized that it would be too much code, and it would be a mess with all the logic of user input, String processing and the use of the MovieLibrary class. So I decided to write another class.

In the MovieQuery class, given the description of the problem, I though I would need to use Regular Expressions somehow. I found this nice page to learn and test regular expressions.
I didn’t wanted for this class to be instantiated, and I was thinking of something like a “static class”, but it turns out there is no such thing, at least in the Java programming language, but you can simulate this behavior by adding some modifiers, like making the constructor private and some static methods and fields.
The class use a Scanner, with a regex delimiter like this: “( )|&^“. And what this is telling to the scanner is that it should identify a substring of that format, “()” means that the thing inside the parenthesis must appear in the string and the “[]” means that there must be any of the characters inside the braces. And by doing this, when I call the next() method of the scanner it returns the name of the first movie and calling next() again will return the second movie name.
When it finds the two name of the movies it searches for them in the MovieLibrary, and, if some of the movies is not there, throws an RuntimeException, because I don’t want neither keep executing the code nor return an empty Set.
It searches for which command is in the input, calls the appropriate method in the MovieLibrary instance and returns the result.
MovieQuery

And so the only thing the TheMovies class does is using the previous classes to ask and execute the user input.
TheMovies

And here’s the output produced by the command line.

output

#WSQ08-Yo Soy 196

This thing was really weird, I mean, it’s useless but it was interesting.
So a lychrel number is a special number, if the repetitive addition of a number with the number formed with inverse of its digits form a palindrome then it’s not a lychrel number.
So, in example: 196 + 691 = 887, 887 + 788 = 1575, … and so forth.

So here I first programmed the YoSoy186 class, before programming LychrelSequence and LychrelNumber, because I wanted to define how the class would work instead of programming it in the first place.
yosoy196

So because I needed to flip the digits of the numbers, I converted the number to a String. And because in this lynda Java course they said I should use the StringBuilder class to work with string, because it’s expensive to do the string operations with the normal String operators.

And then, in the Lychrel number class, I wrote a lot some fields, getters and setter then I wrote the above algorithm to evaluate if the number is a candidate to be a lychrel number:

public class LychrelNumber {
  private int value;
  private boolean isPalindromeNumber;
  private boolean isLychrel;
 
  public LychrelNumber() {
  }
 
  public LychrelNumber(int number) {
    setValue(number);
  }
 
  public boolean isLychrelNumber() {
    return this.isLychrel;
  }
 
  public boolean isPalindrome() {
    return this.isPalindromeNumber;
  }
 
  public int getValue() {
    return this.value;
  }
 
  private long[] evaluateHelper(long val) {
    StringBuilder reverseStr = (new StringBuilder(Long.
    toString(val))).reverse();
    long reverse = Long.parseLong(reverseStr.toString());
    long add = val + reverse;
    return new long[] {reverse, add};
  }
 
  private void evaluate(long value) {
    long[] r = this.evaluateHelper(value);
    String addStr = Long.toString(r[1]);
    this.isPalindromeNumber = (value == r[0]);
    if (!this.isPalindrome()) {
      for (int i = 0; i < 30; i++) {
        r = evaluateHelper(r[1]);
        addStr = Long.toString(r[1]);
        if (addStr.equals(new StringBuilder(addStr).reverse().toString())) {
          this.isLychrel = false;
          break;
        }
        this.isLychrel = true;
      }
    }
  }
 
  public void setValue(int number) {
    this.value = number;
    this.evaluate((long)this.value);
  }
}

In the code I wrote a private method, which is only called when the setter of the value is called, and another method to reverse and add these numbers. I used a long number because the repetitive addition of the numbers generated huge numbers and the program would run out of memory.

So first, before checking if the number is a lychrel number, I check if it is a palindrome, and then, if it is not, run the code to check if it is a lychrel number (because if it is a palindrome then it’s not a lychrel number). If at some point the addition of the number and the inverse of its digits is a palindrome, the code breaks and sets the isLychrel number variable to true.

For the LychrelNumber class, I used the ArrayList class to be able to append elements to the list, something you can’t do to a normal array easily. I used three lists, for storing the lychrel, non-lychrel, and palindrome sequences. The link says that since the List class is an Interface, you need to declare a specific implementation of this Interface.
All this class does is iterate from the lower bound to the upper bound, and instantiating the LychrelSequence class with that value, and if the number is a palindrome, it’s added to the palindromes list and the non-lychrel list, else, if the number is a lychrel number it is added to the lychrel list, but not all non-lychrel numbers are palindromes).

import java.util.List;
import java.util.ArrayList;
 
public class LychrelSequence {
  private int lower;
  private int upper;
  private List<Integer> lyqSeq;
  private List<Integer> nonLyqSeq;
  private List<Integer> palindromeSeq;
 
  public LychrelSequence(int lowerBound, int upperBound) {
    this.lower = lowerBound;
    this.upper = upperBound;
 
    this.lyqSeq = new ArrayList<>();
    this.nonLyqSeq = new ArrayList<>();
    this.palindromeSeq = new ArrayList<>();
 
    LychrelNumber ln = new LychrelNumber();
    for (int i = this.lower; i <= this.upper; i++) {
      ln.setValue(i);
      if (ln.isPalindrome()) {
        palindromeSeq.add(ln.getValue());
        nonLyqSeq.add(ln.getValue());
      } else if (ln.isLychrelNumber()) {
        lyqSeq.add(ln.getValue());
        System.out.println(“Found a Lychrel number candidate: ” + ln.getValue());
      } else {
        nonLyqSeq.add(ln.getValue());
      }
    }
  }
 
  public int getLowerBound() {
    return this.lower;
  }
 
  public int getUpperBound() {
    return this.upper;
  }
 
  public List<Integer> getPalindromeSequence() {
    return this.palindromeSeq;
  }
 
  public List<Integer> getLychrelSequence() {
    return this.lyqSeq;
  }
 
  public List<Integer> getNonLychrelSequece() {
    return this.nonLyqSeq;
  }
 
 
  public int getNumPalindromes() {
    return this.palindromeSeq.size();
  }
 
  public int getNumLychrels() {
    return this.lyqSeq.size();
  }
 
  public int getNumNonLychrels() {
    return this.nonLyqSeq.size();
  }
}

Something weird I noticed is that you have to use the Integer class when declaring the list, instead of the primitive type int.

onlyInteger

And the here’s the Github repository of this homework.

#WSQ07-Babylonian Square Root


First of all, the Babylonian square root is a Babylonian algorithm for calculating the Babylonian Square Root (duh…) of a given number, say n, but in a non conventional way. This algorithm consists of making a guess (i.e.: think of an approximate value to the actual square root), say g, then, you add g, to the ratio of the number and the guess, and divide that by 2.
So, this is:

Babylonian square root equation

And the result of this equation is the new guess.
We do this process several times and then the final guess is the result of the square root.

This is the class, without the method for calculating the square root.

Screenshot from 2016-02-04 12:14:39

So the algorithm says that we need to pick a near number to the actual square root. So I imported the Random class to pick some random number near the half of the number, because YOLO. Then I used that number as the first guess and executed the algorithm.

And here’s the final code.

finalwsq07

And the github repository.