Week 10

Markov Text Generator

This week we completed a fun assignment called Markov Text Generator. In this assignment, we had to write a class that takes a file, stores all of the words in the file, and the words immediately following those words, in a HashMap, and generates random sentences using those words.

Once I finished the code, most of the unit tests failed, which was to be expected.What was surprising, was that I only had to fix two bugs to pass all of the tests. To fix the first bug I had to correctly get an array size while selecting random words, and to fix the second I had to move an condition checking for the end of a for loop into a separate if statement. If felt very satisfying to only have to fix two small bugs, out of all of the code I wrote.

Comparing Objects

In some of our lecture for this week, we learned about comparing objects in Java. Unlike primitive data types, we cannot use "==" to compare objects. This is because "==" checks to see if the objects are the same, which they will not be unless they are two variables pointing to the same object. Instead, we have to use methods for comparing objects. One example of this would be the String.equals method.

Inheritance

This week we talked about inheritance. In Java, classes can extend a class or implement an interface, which gives them access to the fields and methods of their parent class, although only with the public or protected access modifiers.

When a class inherits from another class, the original class is called the super class, parent class, or base class, while the new class is called the sub class, child class, or derived class.

When using classes that inherent from another, we can use type @override before a method to change it from the parent method. We can also use the keyword 'super' to access fields and methods from the parent class, such as Super.toString.

Comments

Popular posts from this blog

Week 4

Week 2

Week 1