背景・理由
立派なプログラマーになるためにThink Javaを学びます。
#リソース
Think Java: How to Think Like a Computer Scientist
Allen B. Downey https://www.amazon.co.jp/dp/1492072508/ref=cm_sw_r_tw_dp_THGFFJFBXVVCYQ54R9ES
但し書き エクササイズ主にThink Javaの前のバージョンを使います。
https://books.trinket.io/thinkjava/
chap 9
語彙
object-oriented:
object:
primitive:
immutable:
wrapper class:
parse:
empty array:
design process:
encapsulate:
generalize:
ex 9_1
省略 実際に手は動かしました。
ex 9_3
public static BigInteger pow(int x, int n){
BigInteger result;
{
result = BigInteger.ONE;
}
if (n == 0) return result;
// find x to the n/2 recursively
BigInteger t = pow(x, n / 2);
// if n is even, the result is t squared
// if n is odd, the result is t squared times x
if (n % 2 == 0) {
return t.multiply(t);
} else {
return t.multiply(t) * x;
}
}
ex 9_5
public double powArray(double[] array){
// 2番目の要素をとります。
// 累乗を返します。
double result = array[1] * array[1] ;
return result;
}
public int[] histogram(int size){
Random random = new Random();
int[] a = new int[size];
// 配列を取得します
// ヒストグラムを返します。
for (int i = 0; i < a.length; i++){
a[i] = random.nextInt(100);
}
return a;
}
chap 10
注 エクササイズはやれそうなのでテキストをやりました。
Think Java: How to Think Like a Computer Scientist
Allen B. Downey https://www.amazon.co.jp/dp/1492072508/ref=cm_sw_r_tw_dp_THGFFJFBXVVCYQ54R9ES
但し書き エクササイズ主にThink Javaの前のバージョンを使います。
語彙
attribute
dot notation
UML
class diagram
mutable
ex 10_1
public static int riddle(int x, Point p) {
x = x + 7;
return x + p.x + p.y;
} public static void main(String[] args) {
int x = 5;
Point blank = new Point(1, 2);
System.out.println(riddle(x, blank));
System.out.println(x);
System.out.println(blank.x);
System.out.println(blank.y);
}
◼️省略
◼️15
◼️mutable 理由 型がPointであるから。
ex 10_3
import java.awt.Rectangle;
import java.awt.Point;
public class ex10_3 {
public static void main(String[] args) {
Rectangle box1 = new Rectangle(2, 4, 7, 9);
Point p1 = findCenter(box1);
printPoint(p1);
box1.grow(1, 1);
// 2 -1 , 4- 1,7+ 2, 9 + 2
// 1 , 3,9, 11
Point p2 = findCenter(box1);
printPoint(p2);
}
/**
* Finds the center of a Rectangle and returns a new Point.
*/
public static Point findCenter(Rectangle box) {
int x = box.x + box.width / 2;
// 2 + 7/2 ans 5
// 1 + 9/2 ans 5
int y = box.y + box.height / 2;
// 4 + 9/2 ans 8
// 3 + 11/2 ans 8
return new Point(x, y);
}
/**
* Prints the attributes of a Point object.
*/
public static void printPoint(Point p) {
System.out.println("(" + p.x + ", " + p.y + ")");
}
}
◼️省略
◼️
(5,8)
(5,8)
◼️
p1 and p2 aliased? → yes
p2にbox1を代入しているから・・・・
chap 11
語彙
class
instance
instantiate
variable
instance variable
information hiding
constructor
shadowing
client
getter
override
instance method
identical
equivalent
注 エクササイズはやれそうなのでテキストをやりました。
Think Java: How to Think Like a Computer Scientist
Allen B. Downey https://www.amazon.co.jp/dp/1492072508/ref=cm_sw_r_tw_dp_THGFFJFBXVVCYQ54R9ES
ex 11_1
public static void increment(double seconds) {
Time sum = new Time();
sum.second += seconds;
sum.second = sum.second % 60;
sum.minute = (int) ((sum.second - sum.second) % 60);
sum.hour = (int) ((sum.second - sum.second) / 60);
}
ex 11_3
/**
* Represents a time of day.
*/
public class Date {
private int year;
private int month;
private int day;
/**
* Construct a Time object with default values.
*/
public Date() {
this.year = 0;
this.month = 0;
this.day = 0;
printDate(this.year, this.month, this.day);
}
/**
* Construct a Time object with given values.
*/
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
printDate(this.year, this.month,this.day);
}
private void printDate(int year, int month, int day) {
System.out.print(year);
System.out.print("/");
System.out.print(month);
System.out.print("/");
System.out.println(day);
}
public int getYear() {
return this.year;
}
public int getMonth() {
return this.month;
}
public double getDay() {
return this.day;
}
}