Saturday, 5 March 2016

Helsinki MOOC - 30 day months



Week 6. The first chapter talks about the use of multiple constructers, which allow for a new object to be created in different ways (i.e. different type of parameters). In the same way, methods can also have multiple versions. Although each method has to have different parameters, otherwise the two methods cannot be distinguished.

The following few chapters are about how objects are handled in java. Objects are at the ‘end of a wire’ and when a new variable is assigned to reference of the object, then any action taken on the new variable will also affect the object (i.e. multiple ‘wires’ to the same object).

In exercise 92, a class called ‘Mydate’ has to implement the method of finding the difference in years between two dates (to make it simple, all months have 30 days). What I found difficult about this exercise was when there was 364 days difference, which is technically still less than a year.
My solution to this problem, made easier by only having 30 day months, was to calculate the total number of days in a particular date. For example, 10.10.2016 would equate to 725,680 total days. I then found the difference of the two dates, and then took away 360 for each year.




Thursday, 3 March 2016

Helsinki MOOC - random.nextInt (x);



The introduction to this week was on how to write good code. When to use camelCase, PascalCase or CAPITAL_WITH_UNDERSCORES. That would be for variables, classes and final variables respectively. Also, the use of indentation of each statement and line breaks between code to separate out logical blocks.

Another important aspect related to object-oriented programming (OOP) was to not have repeating code. If the code is repeated, then it can have its own method/class, which is then called when needed.

 The next few chapters were about the meanings of Class, Method, Instance Variables and Objects in relation to each other. The class being the ‘Blueprint’ and the instance variables being the individual differences. It also goes on to say, how a constructor method is called when a New ‘Object’ is made and how that is useful when creating classes.

Lastly, we are introduced to the Random class in java. Random methods allow for a (nearly) random integer to be returned. The exercises at the end of the week were related to the use of this class. 

Exercise 80: Roll the dice – A dice class needs a method ‘roll’, which prints out (random) numbers in relation to how many sides on the dice (6-sided dice should roll numbers 1-6).

The method “random.nextInt (x)” allows a integer variable (x) to be given to it. And it will produce random integers between 0(inclusive) – x(exclusive). What I found difficult about this exercise was that I did not know how to get numbers 1-x instead. The solution was very simple.



The key here was that users will input according to standard one-based index (i.e. 6-sided dice = 1-6) while the method will use zero-based index, therefore reading ‘6’ as 0-6 (i.e. 0,1,2,3,4,5). By adding 1to the random integer, gives you the desired 1-6 output.

Wednesday, 2 March 2016

Helsinki MOOC - Method and object logistics



This week (week 3) starts off with an introduction to variables given to methods as parameters. Primitive type variables are copied as-is (the actual value), while reference type variables only copy the reference and give it to the method. It also talked about how methods can be used to assign work more easily, for example, having a lone method just for working out an average.
Again, the first few exercise started off simple. They were about using separate methods for the calculation of the largest/least number (of 3), average, and the sum.

The course then introduces ArrayList objects. These lists are used to store objects, which makes it easier when there are several objects to handle. The next two exercises used the methods of the list, plus the use of a for loop (for(String word: words)) to print out the contents of a String ArrayList.

The last exercise is quite popular for programming courses, the aim was to tell the user if the text they inputted was a palindrome or not. The goal of the exercise was for you to use different methods, which then called other methods. As well as this, the methods of the String object were used to reverse the text.





Tuesday, 1 March 2016

Helsinki MOOC - while(true)



Week 2 was again pretty straightforward, the first few exercise were to do with printing a statement a certain amount of times based on the while(condition). Further on, we are told about the for-loop which makes this use of the while loop pretty useless.
The main outline of the week was around methods, and how they use parameters given to them, and how different methods can call other methods as their parameters.

 The final few exercise were slightly tricky. ‘Printing Like A Boss’ exercise number 40 had you create a code, when called on, creates a Christmas tree like figure using ‘*’. The main issue I had with this exercise was the math, how many ‘stars’ did each row need. For example a tree of height  10. Along with this, each tree had to print a stand of fixed stars, in the middle of the tree.
This was the code that passed the tests, at this point, the code was very basic.