Coordinate.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | package jp.ac.utsunomiya_u.is; public class Coordinate { /** * X座標 */ private double x = 0.0 ; /** * Y座標 */ private double y = 0.0 ; /** * コンストラクタ(デフォルト) */ Coordinate() { x = y = 0.0 ; } /** * コンストラクタ * * @param x X座標 * @param y Y座標 */ Coordinate( double x, double y) { this .x = x; this .y = y; } /** * セッタ for X座標 * * @param x X座標 */ void setX( double x) { this .x = x; } /** * セッタ for Y座標 * * @param y Y座標 */ void setY( double y) { this .y = y; } /** * セッタ for X-Y座標 * * @param x X座標 * @param y Y座標 */ void set( double x, double y) { this .x = x; this .y = y; } /** * ゲッタ for X座標 * * @return X座標 */ double getX() { return x; } /** * ゲッタ for Y座標 * * @return Y座標 */ double getY() { return y; } } |
CoordinateTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 | package jp.ac.utsunomiya_u.is; class CoordinateTest { public static void main(String[] args) { Coordinate coordinate = new Coordinate( 1.0 , 2.0 ); System.out.println( "x = " + coordinate.getX() + " y = " + coordinate.getY()); coordinate.setX( 3.0 ); System.out.println( "x = " + coordinate.getX() + " y = " + coordinate.getY()); coordinate.set( 4.0 , 5.0 ); System.out.println( "x = " + coordinate.getX() + " y = " + coordinate.getY()); } } |
出力
x = 1.0 y = 2.0 x = 3.0 y = 2.0 x = 4.0 y = 5.0
CoordinateTest.javaのmainメソッドへの追加
1 2 3 4 5 6 7 | Coordinate[] coordinates = new Coordinate[ 4 ]; for ( int i = 0 ; i < coordinates.length; ++i) { coordinates[i].set(i, i); } for ( int i = 0 ; i < coordinates.length; ++i) { System.out.println( "x = " + coordinates[i].getX() + " y = " + coordinates[i].getY()); } |
CoordinateTest.javaのmainメソッドの修正
1 | coordinates[i].set(i, i); -> coordinates[i] = new Coordinate(i, i); |
1 | Coordinate[] coordinates = { new Coordinate( 0.0 , 0.0 ), new Coordinate( 1.0 , 1.0 ), new Coordinate( 2.0 , 2.0 ), new Coordinate( 3.0 , 3.0 )}; |
1 2 | Coordinate[] coordinates; coordinates = new Coordinate[]{ new Coordinate( 0.0 , 0.0 ), new Coordinate( 1.0 , 1.0 ), new Coordinate( 2.0 , 2.0 ), new Coordinate( 3.0 , 3.0 )}; |
1 2 3 4 5 6 | Coordinate[][] coordinateses = new Coordinate[ 3 ][ 2 ]; for ( int i = 0 ; i < coordinateses.length; ++i) { for ( int j = 0 ; j < coordinateses[i].length; ++j) { coordinateses[i][j] = new Coordinate(i, j); } } |
1 2 3 4 5 6 7 | Coordinate coordinate1 = new Coordinate( 1.0 , 1.0 ); Coordinate coordinate2 = coordinate1; System.out.println( "x1 = " + coordinate1.getX() + " y1 = " + coordinate1.getY()); System.out.println( "x2 = " + coordinate2.getX() + " y2 = " + coordinate2.getY()); coordinate1.setX( 2.0 ); System.out.println( "x1 = " + coordinate1.getX() + " y1 = " + coordinate1.getY()); System.out.println( "x2 = " + coordinate2.getX() + " y2 = " + coordinate2.getY()); |
出力
x1 = 1.0 y1 = 1.0 x2 = 1.0 y2 = 1.0 x1 = 2.0 y1 = 1.0 x2 = 2.0 y2 = 1.0上記のようなコードでコピーする場合の対処方法はいくつかありますが,例えば,Coordinateクラスに以下のようなコンストラクタを追加します.
Coordinate.javaの追加部分
1 2 3 4 5 6 7 8 | /** * コンストラクタ(コピー) * @param coordinate 点クラスインスタンス */ Coordinate(Coordinate coordinate) { this .x = coordinate.getX(); this .y = coordinate.getY(); } |
CoordinateTest.javaのmainメソッドの修正
1 | Coordinate coordinate2 = new Coordinate(coordinate1); |
出力
x1 = 1.0 y1 = 1.0 x2 = 1.0 y2 = 1.0 x1 = 2.0 y1 = 1.0 x2 = 1.0 y2 = 1.0
Line.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | package jp.ac.utsunomiya_u.is; public class Line { /** * 始点 */ private Coordinate begin = null ; /** * 終点 */ private Coordinate end = null ; /** * コンストラクタ(デフォルト) */ Line() { begin = end = null ; } /** * コンストラクタ * * @param begin 始点 * @param end 終点 */ Line(Coordinate begin, Coordinate end) { this .begin = new Coordinate(begin); this .end = new Coordinate(end); } /** * コンストラクタ(コピー) * * @param line 線クラスのインタンス */ Line(Line line) { this .begin = new Coordinate(line.begin); this .end = new Coordinate(line.end); } /** * セッタ for 始点 * * @param begin 始点 */ void setBegin(Coordinate begin) { this .begin = new Coordinate(begin); } /** * セッタ for 終点 * * @param end 終点 */ void setEnd(Coordinate end) { this .end = new Coordinate(end); } /** * セッタ for 始点 & 終点 * * @param line Lineクラスのインスタンス */ void set(Line line) { this .begin = new Coordinate(line.begin); this .end = new Coordinate(line.end); } /** * ゲッタ for 始点 * * @return 始点 */ Coordinate getBegin() { return begin; } /** * ゲッタ for 終点 * * @return 終点 */ Coordinate getEnd() { return end; } /** * 長さを計算 * * @return 長さ */ double getLength() { return Math.sqrt(Math.pow(begin.getX() - end.getX(), 2.0 ) + Math.pow(begin.getY() - end.getY(), 2.0 )); } } |
LineTest.java
1 2 3 4 5 6 7 8 9 10 11 12 | package jp.ac.utsunomiya_u.is; class LineTest { public static void main(String[] args) { Coordinate begin = new Coordinate( 1 , 1 ); Coordinate end = new Coordinate( 2 , 2 ); Line line = new Line(begin, end); System.out.println( "begin.x = " + line.getBegin().getX() + " begin.y = " + line.getBegin().getY()); System.out.println( "end.x = " + line.getEnd().getX() + " end.y = " + line.getEnd().getY()); System.out.println( "length = " + line.getLength()); } } |
Triangle.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | package jp.ac.utsunomiya_u.is; public class Triangle { /** * 三角形の三頂点を保持するためのフィールド */ private Coordinate[] coordinates = new Coordinate[ 3 ]; /** * コンストラクタ(ディフォルト) */ Triangle() { coordinates = new Coordinate[ 3 ]; } /** * コンストラクタ * * @param coordinates 3点座標 */ Triangle(Coordinate[] coordinates) { for ( int i = 0 ; i < Integer.min( this .coordinates.length, coordinates.length); ++i) { this .coordinates[i] = new Coordinate(coordinates[i]); } } /** * コンストラクタ(コピー) * * @param triangle */ Triangle(Triangle triangle) { for ( int i = 0 ; i < Integer.min( this .coordinates.length, triangle.coordinates.length); ++i) { this .coordinates[i] = new Coordinate(triangle.get()[i]); } } /** * セッタ * * @param coordinates 3点座標 */ void set(Coordinate[] coordinates) { for ( int i = 0 ; i < Integer.min( this .coordinates.length, coordinates.length); ++i) { this .coordinates[i] = new Coordinate(coordinates[i]); } } /** * ゲッタ * * @return 3点座標 */ Coordinate[] get() { return coordinates; } /** * 面積 * * ヘロンの公式 3辺の長さがa,b,cである三角形の面積は (s(s-a)(s-b)(s-c))^(1/2) where s = (a+b+c)/2 * * @return 面積 */ double getArea() { double a = new Line(coordinates[ 0 ], coordinates[ 1 ]).getLength(); double b = new Line(coordinates[ 1 ], coordinates[ 2 ]).getLength(); double c = new Line(coordinates[ 2 ], coordinates[ 0 ]).getLength(); double s = 0.5 * (a + b + c); return Math.sqrt(s * (s - a) * (s - b) * (s - c)); } } |
TriangleTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package jp.ac.utsunomiya_u.is; public class TriangleTest { public static void main(String[] args) { Coordinate[] coordinates = { new Coordinate( 0.0 , 0.0 ), new Coordinate( 2.0 , 0.0 ), new Coordinate( 1.0 , 1.0 )}; Triangle triangle = new Triangle(coordinates); int i= 0 ; for (Coordinate coordinate : triangle.get()) { System.out.println( "[" + i + "] x = " + coordinate.getX() + " y = " + coordinate.getY()); i++; } System.out.println( "area = " + triangle.getArea()); } } |
1 | for (型 変数名 : コレクション) {} |