University of London / MSc Computer Science: Object-oriented programming(前半)

University of London / MSc Computer Science: Object-oriented programming(前半)

February 13, 2024

ロンドン大学で MSc Computer Science: Object-oriented programming モジュールを履修中。

講義内容に関して記録した個人的なスタディノートです。

全 12 週のうち 1〜5 週目の内容を記録します。(1 週目開始:2024 年 1 月 8 日 / 5 週目終了:2024 年 2 月 11 日)

モジュール概要 #

Aims

This module covers object-oriented programming, including the use of subclasses, modules, and library classes to create well-organised programs. A further aim is to enhance student’s understanding of making appropriate choices on the selection of algorithms, their implementation together with the required data structures (e.g. arrays, lists, trees, graphs, depth- and breadth-first search algorithms). To enable students to develop programs for modern multi-core architectures utilising functional programming constructs.

Weeks

講義は Week 10 まで。Week 11, 12 は最終課題を作成する期間。

  • Week1: Transition to object-oriented
  • Week2: Casting, collections and more flow control
  • Week3: Object-Oriented Programming: Classes, objects, and methods
  • Week4: Inheritance
  • Week5: Unit Testing
  • Week6: Packages and Interfaces
  • Week7: Exceptions and I/O
  • Week8: Concurrency
  • Week9: Generics
  • Week10: Lambda and method references

参考文書 #

Essential reading

  • Eck, D.J. (2021). “Introduction to - programming using Java” 8th Edition.
  • Schildt, H. “Java: a beginner’s guide”. (McGraw Hill, 2018) 8th edition.
  • Sedgewick, R. and K. Wayne “Computer science: interdisciplinary approach”. (Addison Wesley, - 2020).
  • Sierra, K. and B. Bates “Head first Java”. (O’Reilly Media, Inc, 2005).
  • Whittaker, J.A., J. Arbon and J. Carollo “How Google tests software”. (Addison Wesley, 2012).
  • The Java Language Environment
  • The Java Tutorials

Week 1: Transition to object-oriented programming #

レクチャー内容

  • Code structure, datatypes, variables, flow control, operators
    • Lecture 1: Coding basics and flow control
  • Your first complete Java program
    • Lecture 2: Datatypes, variable scope, operators
  • Labs
    • Lecture 3: Hands-on programming demo
    • Programming exercises: Find the max value
    • Programming exercises: Middle of three
    • Programming exercises: Intersecting intervals
    • Programming exercises: Add one second
    • Programming exercises: Reverse
    • Programming exercises: String sum
    • Quiz

ノート

  • 既知の内容がほとんどだったため、講義内容の記録は簡略化する。
  • 講義内容は Java の導入的な説明だった。
// HelloWorld.java

public class HelloWorld {

  public static void main(String[] args) {
    System.out.println("Hello world!");
  }

}

When Java tries to execute our application, it will look for the “main” method to begin running our code.

javac HelloWorld.java && java HelloWorld
>> Hello World!

“javac” command compiles HelloWorld.java to “HelloWorld.class” file then “java” command executes “HelloWorld.class” file.

Further material #

Week 2: Casting, collections and more flow control #

レクチャー内容

  • Casting and collections
    • Lecture 1: Casting, string, arrays, ArrayLists and other collections
  • Iteration
    • Lecture 2: Iteration statements in Java
  • Labs
    • Lecture 3: Hands-on programming demo
    • Programming exercises: Classification of letters
    • Programming exercises: Separating by commas
    • Programming exercises: The odd between
    • Programming exercises: Anagrams
    • Programming exercises: Roman numerals
    • Programming exercises: Knight’s tour
    • Quiz

ノート

  • 既知の内容がほとんどだったため、講義内容の記録は簡略化する。
  • 講義内容は前回に引き続き Java の文法などの説明だった。各種データ型や、分岐、ループ処理など。

Week 3: Object-oriented programming: classes, objects, and methods #

レクチャー内容

  • Classes and objects
    • Lecture 1: Classes and objects
  • Methods and constructors
    • Lecture 2: Methods, constructors and overloading
  • Access control, nested and inner classes
    • Lecture 3: Access control, nested and inner classes
  • Labs
    • Lecture 4: Hands-on programming demo
    • Programming exercises: Bank account
    • Programming exercises: Dates
    • Programming exercises: Triplicate
    • Programming exercises: Centered numbers
    • Quiz

ノート

  • 既知の内容がほとんどだったため、講義内容の記録は簡略化する。
  • 講義内容は前回に引き続き Java の文法などの説明だった。クラス、オブジェクト、メソッドやオーバーロードなど。

Memory management

  • When new is used an object is dynamically allocated from a pool of free memory.
  • new may fail due to insufficient free memory.
  • When objects are no longer used, the memory allocated should be freed.
  • Java employs garbage collection.
  • When there are no references to an object, the object is released.
  • It’s possible to specify what happens before an object is released using finalize().

Week 4: Inheritance #

レクチャー内容

  • Inheritance
    • Lecture 1: Inheritance, constructors and using superclass methods
  • Overriding and dynamic dispatch
    • Lecture 2: Overriding and dynamic dispatch
  • Labs
    • Lecture 3: Hands-on programming demo
    • Programming exercises: Rectangles
    • Programming exercises: Squares
    • Programming exercises: Chess attack
    • Programming exercises: Poker face
    • Quiz

ノート

  • 既知の内容がほとんどだったため、講義内容の記録は簡略化する。
  • 講義内容は前回に引き続き Java の文法の説明が主だった。抽象クラス、継承、オーバーライドや動的ディスパッチなど。これに加えて、IntelliJ IDEA の導入に関しての説明もあった。

Dynamic dispatch

オブジェクトのメソッド呼び出しを コンパイル時の型ではなく実行時の型により決定する仕組み。

Further material #

  • Composition over Inheritance - Fun Fun Function | YouTube
  • Banana, Gorilla, Jungle problem
    • The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.

      Joe Armstrong (the creator of Erlang)

    • 「オブジェクト指向言語の問題点は、暗黙的な環境をすべて持ち回ることです。あなたはバナナが欲しかったのですが、手に入れたのはバナナとジャングル全体を抱えたゴリラでした。」これは主として継承の問題点を指摘している。

Week 5: Unit testing #

レクチャー内容

  • JUnit
    • Lecture 1: Using JUnit classes to test Java programs
  • How to organise testing using JUnit classes
    • Lecture 2: Testing strategies
  • Labs
    • Lecture 3: Hands-on programming demo
    • Programming exercises: Replace Checkers with Unit tests
    • Quiz

ノート

  • 既知の内容がほとんどだったため、講義内容の記録は簡略化する。
  • 講義内容は JUnit を用いた Java コードのユニットテストについて。

Java annotations

  • Are a form of metadata i.e. provide data about a program but not part of the program itself.
  • Used by the compiler to detect errors or suppress warnings.
    • @SuppressWarnings("deprecation")
  • Software tools can process them to generate code, XML files, and so forth.

JUnit

import org.junit.Test;
import static org.junit.Assert.*;

public class MainTest {
    @Test
    public void testStrings()
    {
        String a = "Hi";
        String b = "there";
        String c = "Hi there";
        assertEquals(a + b, c);
    }
}
// To check an exception is thrown.

@Test(expected = NullPointerException.class)
public void testNull()
{
    String test = null;
    test.length();
}

Testing strategies

1 - Test “extreme” values:

  • Test that “small values” are processed correctly:
    • int: -1, 0, 1
    • positive int: 1, 2, 3
    • List: empty, single element
    • String: empty string, string of length 1
    • Day of year: first day of year
  • Test that “large values” are processed correctly:
    • int: Integer.MIN_VALUE, Integer.MAX_VALUE
    • Lists with many elements
    • Strings of high length
    • Day of year: last day of year

2 - Test a mix of inputs:

  • For a method that receives multiple ints, test that it works when we mix together:
    • big and small values
    • negative and positive values
    • duplicate/equal values
    • values close to each other
  • Apply this ideas to other datatypes e.g. for strings
    • long and short (in terms of length)
    • textually equal
    • almost textually equal etc

3 - Test unexpected/malformed inputs:

  • For a method that expects a String holding an email address, what happens when the string does not contain an @ character?
  • What happens when a negative int is passed to a method that expects positive int