class Bunsu 1 public static void mainString args
class Bunsu 1 public static void main(String args[]) { Bunsu(1, 2); 12 Bunsubun 1= =new Bunsu(1, 2); Bunsu bun 2 = new Bunsu(1, 3); Bunsu wa = bun 1. plus(bun 2); bun 1. print(); System. out. print("+"); bun 2. print(); System. out. print("="); wa. print(); bun 1 System. out. println(); } class Bunsu { private int bunshi; private int bunbo; Bunsu(int a, int b) { //コンストラクタ void print() { //分数を表示 Bunsu plus(Bunsu other) { //足し算 コピー Bunsu オブジェクト Bunsu(int a, int b) { bunshi void print() { bunbo Bunsu plus(Bunsu other) { Bunsu(int a, int b) { if(b < 0) { …} bunshi = a; bunbo = b; } a b
class Bunsu 1 public static void main(String args[]) { Bunsu(1, 2); Bunsubun 1= =new Bunsu(1, 2); Bunsu bun 2 = new Bunsu(1, 3); Bunsu wa = bun 1. plus(bun 2); bun 1. print(); System. out. print("+"); bun 2. print(); System. out. print("="); wa. print(); bun 1 System. out. println(); } class Bunsu { private int bunshi; private int bunbo; Bunsu(int a, int b) { //コンストラクタ void print() { //分数を表示 Bunsu plus(Bunsu other) { //足し算 Bunsu オブジェクト bunshi void print() { bunbo Bunsu plus(Bunsu other) { Bunsu(int a, int b) { if(b < 0) { …} bunshi ==a; a; bunshi bunbo = b; } a 1 b 2
class Bunsu 1 public static void main(String args[]) { Bunsu(1, 2); Bunsubun 1= =new Bunsu(1, 2); Bunsu bun 2 = new Bunsu(1, 3); Bunsu wa = bun 1. plus(bun 2); bun 1. print(); System. out. print("+"); bun 2. print(); System. out. print("="); wa. print(); bun 1 System. out. println(); } class Bunsu { private int bunshi; private int bunbo; Bunsu(int a, int b) { //コンストラクタ void print() { //分数を表示 Bunsu plus(Bunsu other) { //足し算 Bunsu オブジェクト bunshi bunbo 1 void print() { Bunsu plus(Bunsu other) { Bunsu(int a, int b) { if(b < 0) { …} bunshi = a; bunbo ==b; b; bunbo } a 1 b 2
class Bunsu 1 public static void main(String args[]) { Bunsu(1, 2); Bunsubun 1= =new Bunsu(1, 2); Bunsu bun 2 = new Bunsu(1, 3); Bunsu wa = bun 1. plus(bun 2); bun 1. print(); System. out. print("+"); bun 2. print(); System. out. print("="); wa. print(); bun 1 System. out. println(); } class Bunsu { private int bunshi; private int bunbo; Bunsu(int a, int b) { //コンストラクタ void print() { //分数を表示 Bunsu plus(Bunsu other) { //足し算 Bunsu オブジェクト bunshi 1 void print() { bunbo 2 Bunsu plus(Bunsu other) { Bunsu(int a, int b) { if(b < 0) { …} bunshi = a; bunbo = b; } a 1 b 2
class Bunsu 1 public static void main(String args[]) { Bunsu bun 1 = new Bunsu(1, 2); Bunsu bun 2 Bunsu(1, 3); Bunsu bun 2= =new Bunsu(1, 3); Bunsu wa = bun 1. plus(bun 2); bun 1. print(); System. out. print("+"); bun 2. print(); bun 1 System. out. print("="); wa. print(); System. out. println(); } bun 2 class Bunsu { private int bunshi; private int bunbo; Bunsu(int a, int b) void print() { Bunsu plus(Bunsu other) Bunsu オブジェクト Bunsu(int a, int b) bunshi 1 void print() { bunbo 2 Bunsu plus(Bunsu other) Bunsu オブジェクト Bunsu(int a, int b) bunshi 1 void print() { bunbo 3 Bunsu plus(Bunsu other)
class Bunsu 1 public static void main(String args[]) { Bunsu bun 1 = new Bunsu(1, 2); Bunsu bun 2 = new Bunsu(1, 3); Bunsu wa==bun 1. plus(bun 2); bun 1. print(); System. out. print("+"); bun 2. print(); bun 1 System. out. print("="); wa. print(); System. out. println(); } wa bun 2 class Bunsu { private int bunshi; private int bunbo; Bunsu(int a, int b) void print() { Bunsu plus(Bunsu other) Bunsu オブジェクト Bunsu(int a, int b) bunshi 1 void print() { bunbo 2 Bunsu plus(Bunsu other) { int new. Bunshi = this. bunshi*other. bunbo +. . . int new. Bunbo = this. bunbo*other. bunbo; Bunsu p = new Bunsu(new. Bunshi, … return p; other } Bunsu オブジェクト Bunsu(int a, int b) bunshi 1 void print() { bunbo 3 Bunsu plus(Bunsu other)
class Bunsu { private int bunshi; private int bunbo; class Bunsu 1 Bunsu(int a, int b) void print() { Bunsu plus(Bunsu other) bun 1 Bunsu オブジェクト bunshi 1 bunbo 2 this Bunsu plus(Bunsu other) { int new. Bunshi= = this. bunshi*other. bunbo + + this. bunshi * other. bunbo other. bunshi+this. bunbo; other. bunshi * this. bunbo; int new. Bunbo = this. bunbo*other. bunbo; Bunsu p = new Bunsu(new. Bunshi, new. Bunbo); return p; } new. Bunshi bun 2 wa other Bunsu オブジェクト bunshi 1 bunbo 3 5 1× 3=3 1× 2=2 3+2=5
class Bunsu { private int bunshi; private int bunbo; class Bunsu 1 Bunsu(int a, int b) void print() { Bunsu plus(Bunsu other) bun 1 Bunsu オブジェクト bunshi 1 bunbo 2 this Bunsu plus(Bunsu other) { int new. Bunshi = this. bunshi*other. bunbo + other. bunshi+this. bunbo; this. bunbo*other. bunbo; int new. Bunbo = this. bunbo * other. bunbo; Bunsu p = new Bunsu(new. Bunshi, new. Bunbo); return p; new. Bunshi new. Bunbo } bun 2 wa other Bunsu オブジェクト bunshi 1 bunbo 3 5 6 2× 3=6
class Bunsu { private int bunshi; private int bunbo; class Bunsu 1 Bunsu(int a, int b) void print() { Bunsu plus(Bunsu other) bun 1 Bunsu オブジェクト bunshi 1 bunbo 2 this Bunsu plus(Bunsu other) { int new. Bunshi = this. bunshi*other. bunbo + other. bunshi+this. bunbo; int new. Bunbo = this. bunbo*other. bunbo; Bunsu p = new Bunsu(new. Bunshi, new. Bunbo); return p; new. Bunshi new. Bunbo } bun 2 wa other Bunsu オブジェクト bunshi 1 bunbo 3 5 6 p Bunsu オブジェクト bunshi 5 bunbo 6
class Bunsu 1 Bunsu オブジェクト public static void main(String args[]) { Bunsu bun 1 = new Bunsu(1, 2); Bunsu bun 2 = new Bunsu(1, 3); Bunsu wa = bun 1. plus(bun 2); bun 1. print(); System. out. print("+"); bun 2. print(); bun 1 System. out. print("="); wa. print(); System. out. println(); } bunshi 1 bunbo 2 Bunsu plus(Bunsu other) { int new. Bunshi = this. bunshi*other. bunbo + other. bunshi+this. bunbo; int new. Bunbo = this. bunbo*other. bunbo; Bunsu p = new Bunsu(new. Bunshi, new. Bunbo); return p; new. Bunshi new. Bunbo } 5 other wa bun 2 Bunsu オブジェクト p 6 Bunsu オブジェクト bunshi 1 bunshi 5 bunbo 3 bunbo 6
class Bunsu 1 public static void main(String args[]) { Bunsu bun 1 = new Bunsu(1, 2); Bunsu bun 2 = new Bunsu(1, 3); Bunsu wa = bun 1. plus(bun 2); bun 1. print(); System. out. print("+"); bun 2. print(); bun 1 System. out. print("="); wa. print(); System. out. println(); } bun 2 wa (1/2) Bunsu オブジェクト Bunsu(int a, int b) bunshi 1 void print() { bunbo 2 Bunsu plus(Bunsu other) void print() { if(bunbo==1) { … } else { System. out. print(“(“+bunshi+”/”+bunbo+”)”); } } Bunsu オブジェクト bunshi 1 bunshi 5 bunbo 3 bunbo 6
class Bunsu 1 public static void main(String args[]) { Bunsu bun 1 = new Bunsu(1, 2); Bunsu bun 2 = new Bunsu(1, 3); Bunsu wa = bun 1. plus(bun 2); bun 1. print(); System. out. print("+"); System. out. print(“+”); bun 2. print(); bun 1 System. out. print("="); wa. print(); System. out. println(); } bun 2 wa (1/2) + Bunsu オブジェクト Bunsu(int a, int b) bunshi 1 void print() { bunbo 2 Bunsu plus(Bunsu other) Bunsu オブジェクト bunshi 1 bunshi 5 bunbo 3 bunbo 6
class Bunsu 1 public static void main(String args[]) { Bunsu bun 1 = new Bunsu(1, 2); Bunsu bun 2 = new Bunsu(1, 3); Bunsu wa = bun 1. plus(bun 2); bun 1. print(); System. out. print("+"); bun 2. print(); System. out. print("="); System. out. print(“=”); bun 1 wa. print(); System. out. println(); } bun 2 wa (1/2)+ (1/3)= Bunsu オブジェクト Bunsu(int a, int b) bunshi 1 void print() { bunbo 2 Bunsu plus(Bunsu other) Bunsu オブジェクト bunshi 1 bunshi 5 bunbo 3 bunbo 6
class Bunsu 1 public static void main(String args[]) { Bunsu bun 1 = new Bunsu(1, 2); Bunsu bun 2 = new Bunsu(1, 3); Bunsu wa = bun 1. plus(bun 2); bun 1. print(); System. out. print("+"); bun 2. print(); bun 1 System. out. print("="); wa. print(); System. out. println(); } bun 2 Bunsu オブジェクト Bunsu(int a, int b) bunshi 1 void print() { bunbo 2 Bunsu plus(Bunsu other) Bunsu オブジェクト bunshi 1 bunshi 5 bunbo 3 bunbo 6 wa (1/2)+(1/3)= (5/6)
- Slides: 14