MOOC.fi Java Programming I Part 7
Fundamentals of Java
Last part of MOOC.fi Java Programming I!
- Programming paradigms
-
Object-oriented programming(OOP): info is represented as classes that descibre concepts of problem domain and logic of application. Classes define the methods that determine how information is handled. During program execution, objects are instantiated from classes that contain runtime information and that also have an effect on program execution: program execution typically proceeds through a series of method calls related to the objects. As mentioned a few weeks ago, “the program is built from small, clear, and cooperative entities.”
-
Advantage of OOP is is how problem-domain concepts are modeled through classes and objects, which makes programs easier to understand. In addition, structuring the problem domain into classes facilitates the construction and maintenance of programs. Disadvantage is inherently cannot solve all problems. For example, scientific computing and statistics applications use Python or R.
-
Procedural programming: Whereas in object-oriented programming, the structure of a program is formed by the data it processes, in procedural programming, the structure of the program is formed by functionality desired for the program: the program acts as a step-by-step guide for the functionality to be performed. The program is executed one step at a time, and subroutines (methods) are called whenever necessary.
-
In procedural programming, the state of the program is maintained in variables and tables, and any methods handle only the values provided to them as parameters. The program tells the computer what should happen.
-
Differences: In OOP, the state of an object can, in principle, change with any object method, and that change of state can also affect the working of the methods of other objects. As a consequence, other aspects of a program’s execution may also be affected since objects can be used in multiple places within the program.
-
Procedural style:
int hours = 0; int minutes = 0; int seconds = 0; while (true) { // 1. printing the time print(hours, minutes, seconds); System.out.println(); // 2. advancing the second hand seconds = seconds + 1; // 3. advancing the other hands when necessary if (seconds > 59) { minutes = minutes + 1; seconds = 0; if (minutes > 59) { hours = hours + 1; minutes = 0; if (hours > 23) { hours = 0; } } } }
public static void print(int hours, int minutes, int seconds) { print(hours); print(minutes); print(seconds); } public static void print(int value) { if (value < 10) { System.out.print("0"); System.out.print(value); }
- OOP style:
public class Hand { private int value; private int upperBound; public Hand(int upperBound) { this.upperBound = upperBound; this.value = 0; } public void advance() { this.value = this.value + 1; if (this.value >= this.upperBound) { this.value = 0; } } public int value() { return this.value; } public String toString() { if (this.value < 10) { return "0" + this.value; } return "" + this.value; } }
public class Clock() { private Hand hours; private Hand minutes; private Hand seconds; public Clock() { this.hours = new Hand(24); this.minutes = new Hand(60); this.seconds = new Hand(60); } public void advance() { this.seconds.advance(); if (this.seconds.value() == 0) { this.minutes.advance(); if (this.minutes.value() == 0) { this.hours.advance(); } } } public String toString() { return hours + ":" + minutes + ":" + seconds; } } Clock clock = new Clock(); while (true) { System.out.println(clock); clock.advance(); }
-
- Algorithms
-
At the start of the course, all of our methods included the static modifier, but when we started using objects, use of the static modifier was banned. Methods in Java can be divided into two groups, based on whether they have the static modifier or not. Methods without the static modifier are instance methods. Methods with the static modifier are class methods
-
Instance methods are methods that are associated with an object, can process the objects variables and can call the object’s other methods. Instance methods specifically CAN use the this modifier, which refers to the variables associated with the specific object, that is calling the instance method. Class methods can’t use the this modifier, meaning that they can only access the variables they are given as parameters or that they create themselves.
-
In reality class methods can also access class variable, among other things. But advanced
-
To print array in Java: It formats an array into an easily readable string.
System.out.println(Arrays.toString(numbers));
-
Info retrieval - linear search, binary search
-
Linear search is a search algorithm that searches for information in an array by going through every value in the array one by one. When the value that was searched for is found, its index is immediately returned. If the requested value cannot be found, linear sort returns the information that the value was not found — typically this means returning -1 instead of a valid index. Inefficient for large-sized arrays
-
Binary search (aka half-interval search or logarithmic search): Start looking for the searched value in the middle index of the array (or list), compare the value found there to the searched value, and if needed (i.e, when the value isn’t found there) eliminate half of the search area. If there is no single middle value, round down. ONLY for ordered list/array
-
- Larger programming exercises
- Some more exercises