University of London / MSc Computer Science: Object-oriented programming(後半)
April 3, 2024
ロンドン大学で MSc Computer Science: Object-oriented programming モジュールを履修中。
講義内容に関して記録した個人的なスタディノートです。
全 12 週のうち 6〜12 週目の内容を記録します。(6 週目開始:2024 年 2 月 12 日 / 12 週目終了:2024 年 4 月 1 日)
Week 6: Packages and interfaces #
レクチャー内容
- Packages
- Lecture 1: Using Java packages: basics, access, importing
- Interfaces
- Lecture 2: Interfaces: basics, referencing and extending
- Labs
- Lecture 3: Hands-on programming demo
- Quiz
ノート
- 既知の内容がほとんどだったため、講義内容の記録は簡略化する。
- 講義内容は Java の言語機能のパッケージとインターフェースについて。
Packages
- All classes in Java belong to a package.
- When no package statement is specified, the default (“global”) package is used.
- Two classes cannot use the same name within the same namespace.
- Packages provide a way to avoid such naming clashes.
- To indicate inclusion in a package, place the following keywords at the top of a Java source file (
package packagename;
). - The class(es) defined in the file will belong to the package.
- The names of these classes become part of the package’s namespace.
- More than one file can include the same package statement.
- Package names are case sensitive.
- Lowercase names are often used.
- Source file(s) for a packae should be placed in a directory whose name is the package name.
- Example: package “mypack” should be placed in a directory called “mypack”.
- A package hierachy can be created: separate package names with a period
- Example: package “alpha.beta.gamma” should be in directory with path “alpha/beta/gamma”
Access control
- A top level class has two access levels: default or public.
- When a class is declared as public, it’s accessible by any other code.
- When a class has default access, it can be accessed only by other code in the same package.
The way to access a class defined in a different package.
package mybookpackage;
public class Book {
// ...
}
package mylibrarypackage;
public class myShelf {
mybookpackage.Book b = new mybookpackage.Book("Secret of the sea");
}
// OR
package mylibrarypackage;
import mybookpackage.Book;
public class myShelf {
Book b = new Book("Secret of the sea");
}
// OR
package mylibrarypackage;
import mybookpackage.*;
public class myShelf {
Book b = new Book("Secret of the sea");
}
import
brings one or more classes in a package into view.- Asterisk (
*
) can be used in place of the class name to fully import a package.
Java class libraries
- Java defines a large number of standard classes that are available to all programs.
- Often referred to as the Java API.
- Java API classes are stored in different packages, for example:
- “java.lang” - contains general-purpose classes
- “java.io” - contains classes for performing input/output
- “java.net” - contains classes that support networking
Interfaces
- In OOP, sometimes helpful to define what a class must do (the API) without specifying how it will do it (the implementation).
- Interfaces allow to fully separate a class’s “what” from the class’ “how”.
- Attention: JDK 8 added the option to provide a default implementation for an interface method.
- One interface can inherit from another, using the keyword extends.
- A class implementing an interface inheriting another interface must implement all methods on the interface inheritance chain.
// Interface
interface Animal {
// interface method (does not have a body)
public void animalSound();
// interface method (does not have a body)
public void sleep();
// from JDK 8, we can provide a default implementation for an interface method
default void jump() {
System.out.println("Jumping!");
}
}
// Pig "implements" the Animal interface
class Pig implements Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
public void sleep() {
// The body of sleep() is provided here
System.out.println("Zzz");
}
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig();
myPig.animalSound();
myPig.sleep();
myPig.jump();
}
}
Further material #
- Java Interfaces Explained - 040 - YouTube
Week 7: Exceptions and I/O #
レクチャー内容
- Raising and handling exceptions
- Lecture 1: Exceptions and exception handling
- Input and output streams
- Lecture 2: Java I/O with Streams
- Labs
- Lecture 3: Hands-on programming demo
- Programming Problem: Count Words
- Quiz
ノート
- 既知の内容がほとんどだったため、講義内容の記録は省略する。
- 講義内容は Java の例外処理、およびプログラム外部との入出力とストリームについて。
Week 8: Concurrency #
レクチャー内容
- Introduction to Java threads
- Lecture 1: Multithreading
- Concurrency and synchronisation
- Lecture 2: Synchronisation
- Labs
- Lecture 3: Hands-on programming demo
- Programming Problem: Bounded Queue
- Quiz
ノート
- 既知の内容がほとんどだったため、講義内容の記録は省略する。
- 講義内容は Java のマルチスレッドによる並列処理について。
Week 9: Generics #
レクチャー内容
- Introduction to generics
- Lecture 1: Using generics
- Using generics effectively
- Lecture 2: More generics
- Labs
- Lecture 3: Hands-on programming demo
- Programming Problem: Pair, Append, Replace
- Quiz
ノート
- 既知の内容がほとんどだったため、講義内容の記録は省略する。
- 講義内容は Java のジェネリクスについて。
Further material #
- Java Generics Tutorial - 053 - YouTube
Week 10: Lambda and method references #
レクチャー内容
- Lambdas, Functional interfaces and variable capture
- Lecture 1: Lambdas and functional interfaces
- Labs
- Lecture 2: Hands-on programming demo
- Programming Problem: Sorting
- Quiz
ノート
- 既知の内容がほとんどだったため、講義内容の記録は省略する。
- 講義内容は Java のラムダ式、クロージャについて。
Further material #
- Overview of Java 8 Functional Interfaces - YouTube
Week 11, 12 #
最終課題の期間。課題は指定された要件を満たす Java プログラム(とその単体テスト)を作成するもの。ランダムに数字が配置された2次元テーブルから、参加者 A は列を、参加者 B は行を指定し、列と行が交差するところの数字に書かれた数字によって A もしくは B に点数が入る。このとき、A と B がぞれぞれゲーム理論のマクシミン原理に基づいて行動する場合に選択されることになる列と行を求めるプログラムを作る。