[アクセス修飾子] [修飾子] class クラス名 { フィールド メソッド }
クラス名 変数名 = new クラス名(); 例: MyClass myObj = new MyClass(); または MyClass myObj; myObj = new MyClass();
図 商品管理システムのクラス図
クラス | 役割 | オブジェクトの例 |
---|---|---|
Product | 商品の情報を持つ | パソコン |
TestProduct | テストするためのアプリケーション | - |
- // 0901
- // 商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- class Product {
- }
- // 0901
- // 商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) {
- Product p1 = new Product();
- System.out.println("商品のインスタンスを生成しました。 p1 = " + p1);
- Product p2 = new Product();
- System.out.println("商品のインスタンスを生成しました。 p2 = " + p2);
- }
- }
□ 実行結果
$ java TestProduct 商品のインスタンスを生成しました。 p1 = Product@f6a746 商品のインスタンスを生成しました。 p2 = Product@15ff48b"Product@f6a746"の"f6a746"は、環境により異なります。
クラス名@ハッシュ・コードの16進表現 getClass().getName() + '@' + Integer.toHexString(hashCode()) (例) Product@f6a746
[アクセス修飾子] [修飾子] データ型 フィールド名;
オブジェクト名.フィールド名 例: myObj.fieldName
フィールド | 役割 | 値の例 |
---|---|---|
code | 商品コードの情報を持つ | A001 |
name | 商品名の情報を持つ | パソコン |
price | 商品定価の情報を持つ | 188000 |
- // 0902
- // 商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class Product {
- String code; // 商品コード
- String name; // 商品名
- int price; // 商品定価
- }
- // 0902
- // 商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) {
- Product p1 = new Product();
- p1.code = "A001";
- p1.name = "パソコン";
- p1.price = 188000;
- System.out.println("商品のインスタンスを生成しました。 p1 = " + p1);
- System.out.println(" code = " + p1.code);
- System.out.println(" name = " + p1.name);
- System.out.println(" price = " + p1.price);
- }
- }
□ 実行結果
$ java TestProduct 商品のインスタンスを生成しました。 p1 = Product@f6a746 code = A001 name = パソコン price = 188000
- public class Book {
- // ここにコードを記述してください。
- }
□ Book.javaクラスをテストするアプリケーションBookTest.java
- public class BookTest {
- public static void main(String[] args) {
- Book b1 = new Book();
- b1.title =args[0];
- b1.author = args[1];
- System.out.println(" Title:" + b1.title);
- System.out.println("Author:" + b1.author);
- }
- }
□ 実行結果
$ java BookTest JavaBook Maruno Title:JavaBook Author:Maruno
[アクセス修飾子] [修飾子] 戻り値の型 メソッド名(引数リスト) { : return [戻り値]; }
フィールド名 | セッター | ゲッター | boolean型のときのゲッター |
---|---|---|---|
fieldName | setFieldName() | getFieldName() | isFieldName() |
例 (メソッドの中は一般的なもの)
フィールド | セッター | ゲッター |
---|---|---|
int number; | void setNumber(int number) { this.number = number; } |
int getNumber() { return this.number; } |
boolean empty; | void setEmpty(boolean empty) { this.empty = empty; } |
boolean isEmpty() { return this.empty; } |
オブジェクト名.メソッド名() 例: myObj.methodName()
メソッド | 役割 | 戻り値 | 引数 |
---|---|---|---|
getCode | 商品コードを取得する | 商品コード | なし |
setCode | 商品コードを設定する | なし | 商品コード |
getName | 商品名を取得する | 商品名 | なし |
setName | 商品名を設定する | なし | 商品名 |
getPrice | 商品定価を取得する | 商品定価 | なし |
setPrice | 商品定価を設定する。要求された値が負の場合は、0を設定する。 | なし | 商品定価 |
- // 0903
- // 商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class Product {
- String code; // 商品コード
- String name; // 商品名
- int price; // 商品定価
- // 商品コードに値を設定します。
- void setCode(String value) {
- code = value;
- }
- // 商品コードの値を取得します。
- String getCode() {
- return code;
- }
- // 商品名に値を設定します。
- void setName(String value) {
- name = value;
- }
- // 商品名の値を取得します。
- String getName() {
- return name;
- }
- // 商品定価に値を設定します。
- void setPrice(int value) {
- if (value < 0) {
- price = 0;
- return;
- }
- price = value;
- }
- // 商品定価の値を取得します。
- int getPrice() {
- return price;
- }
- }
- // 0903
- // 商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) {
- Product p1 = new Product();
- p1.setCode("A001");
- p1.setName("パソコン");
- p1.setPrice(188000);
- System.out.println("商品のインスタンスを生成しました。 p1 = " + p1);
- System.out.println(" code = " + p1.getCode() );
- System.out.println(" name = " + p1.getName() );
- System.out.println(" price = " + p1.getPrice());
- Product p2 = new Product();
- p2.setCode("P001");
- p2.setName("プリンター");
- p2.setPrice(-24000);
- System.out.println("商品のインスタンスを生成しました。 p2 = " + p2);
- System.out.println(" code = " + p2.getCode() );
- System.out.println(" name = " + p2.getName() );
- System.out.println(" price = " + p2.getPrice());
- }
- }
□ 実行結果
$ java TestProduct 商品のインスタンスを生成しました。 p1 = Product@f6a746 code = A001 name = パソコン price = 188000 商品のインスタンスを生成しました。 p2 = Product@15ff48b code = P001 name = プリンター price = 0
[アクセス修飾子] [修飾子] class クラス名 [extends スーパークラス名] [implements インターフェース名 [, インターフェース名...]] { 内部クラス フィールド コンストラクター メソッド 静的初期化子 インスタンス初期化子 }
- public class StaticInit01 {
- static double d;
- double e;
- // 静的初期化子
- static {
- d = Math.random();
- System.out.println("Static Initializer");
- }
- // インスタンス初期化子
- {
- System.out.println("Instance Initializer");
- }
- // コンストラクター
- StaticInit01() {
- e = Math.random();
- System.out.println("Constructor");
- }
- public static void main(String[] args) {
- StaticInit01 a1 = new StaticInit01();
- System.out.println("a1.d = " + a1.d + " a1.e = " + a1.e);
- StaticInit01 a2 = new StaticInit01();
- System.out.println("a2.d = " + a2.d + " a2.e = " + a2.e);
- }
- }
□ 実行結果
$ java StaticInit01 Static Initializer Instance Initializer Constructor a1.d = 0.35048261315527396 a1.e = 0.5217058001158968 Instance Initializer Constructor a2.d = 0.35048261315527396 a2.e = 0.7077484318238487
フィールド | ゲッター | セッター |
---|---|---|
private String name; |
public String getName() { return this.name; } |
public void setName(String name) { this.name = name; } |
private int userCount; |
public int getUserCount() { return this.userCount; } |
public void setUserCount(int userCount) { this.userCount = userCount; } |
private boolean empty; |
public boolean isEmpty() { return this.empty; } |
public void setEmpty(boolean empty) { this.empty = empty; } |
- // 0904
- // 商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) {
- Product p1 = new Product();
- p1.setCode("A001");
- p1.setName("パソコン");
- p1.setPrice(-188000);
- System.out.println("商品のインスタンスを生成しました。 p1 = " + p1);
- System.out.println(" code = " + p1.getCode());
- System.out.println(" name = " + p1.getName());
- System.out.println(" price = " + p1.getPrice());
- Product p2 = new Product();
- p2.code = "P001";
- p2.name = "プリンター";
- p2.price = -24000;
- System.out.println("商品のインスタンスを生成しました。 p2 = " + p2);
- System.out.println(" code = " + p2.getCode());
- System.out.println(" name = " + p2.getName());
- System.out.println(" price = " + p2.getPrice());
- }
- }
□ 実行結果
$ java TestProduct 商品のインスタンスを生成しました。 p1 = Product@f6a746 code = A001 name = パソコン price = 0 商品のインスタンスを生成しました。 p2 = Product@15ff48b code = P001 name = プリンター price = -24000
- // 0905
- // 商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class Product {
- private String code; // 商品コード
- private String name; // 商品名
- private int price; // 商品定価
- :
- }
$ javac TestProduct.java TestProduct.java:15: code は Product で private アクセスされます。 p2.code = "P001"; ^ TestProduct.java:16: name は Product で private アクセスされます。 p2.name = "プリンター"; ^ TestProduct.java:17: price は Product で private アクセスされます。 p2.price = -24000; ^ エラー 3 個
- public class Args {
- static boolean isValid(int count, String[] args, String message) {
- // ここにコードを記述してください。
- }
- public static void main(String[] args) {
- if (!isValid(1, args, "引数をひとつ指定してください。")) {
- System.exit(1); // アプリケーションを終了します
- }
- System.out.println("引数: " + args[0]);
- }
- }
□ 実行結果
$ java Args 引数をひとつ指定してください。 $ java Args Hello 引数: Hello $ java Args Hello World 引数をひとつ指定してください。 $ java Args "Hello World" 引数: Hello World
- public class Graph {
- String getStars(int n) {
- // ここにコードを記述してください。
- }
- }
- public class TestGraph {
- public static void main(String[] args) {
- Graph g = new Graph();
- System.out.println(g.getStars(Integer.parseInt(args[0])));
- }
- }
□ 実行結果
$ java TestGraph 5 *****
- public class GCD {
- int getGCD(int a, int b) {
- int r;
- while ((r = a % b) != 0) {
- a = b;
- b = r;
- }
- // ここにコードを記述してください。
- }
- }
- public class TestGCD {
- public static void main(String[] args) {
- // ここにコードを記述してください。
- System.out.println(args[0] + " と " + args[1] + " の最大公約数は, " + gcd.getGCD(Integer.parseInt(args[0]), Integer.parseInt(args[1])) + " です。" );
- }
- }
□ 実行結果
$ java TestGCD 12 32 12 と 32 の最大公約数は, 4 です。
- public class PrimeNumber {
- static boolean isPrimeNumber(int n) {
- //
- // ここにコードを記述してください。
- //
- return true;
- }
- }
- public class TestPrimeNumber {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]);
- if (PrimeNumber.isPrimeNumber(n)) {
- System.out.println(n + " is a prime number.");
- } else {
- System.out.println(n + " is not a prime number.");
- }
- }
- }
□ 実行結果
$ java TestPrimeNumber 7 <-- 素数の場合、 7 is a prime number. $ java TestPrimeNumber 8 <-- 素数でない場合、 8 is not a prime number.
- public class PrimeNumber10 {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]);
- // ここにコードを記述してください。
- System.out.println();
- }
- }
□ 実行結果
$ java PrimeNumber10 100 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
□ カプセル化前のBook.java
- public class Book {
- String title; // 書名
- String author; // 著作者
- }
□ カプセル化後のBook.javaクラスをテストするアプリケーションBookTest.java
- public class BookTest {
- public static void main(String[] args) {
- Book b1 = new Book();
- b1.setTitle(args[0]);
- b1.setAuthor(args[1]);
- System.out.println(" Title:" + b1.getTitle());
- System.out.println("Author:" + b1.getAuthor());
- }
- }
□ 実行結果
$ java BookTest JavaBook Maruno Title:JavaBook Author:Maruno
- public class JankenTe {
- // ここにコードを記述してください。
- }
□ JankenTe.javaクラスをテストするアプリケーションJankenAppl.java
- public class JankenAppl {
- public static void main(String[] args) {
- JankenTe te = new JankenTe();
- te.setTe(Integer.parseInt(args[0]));
- System.out.println("手: " + te.getTe());
- }
- }
□ 実行結果
$ java JankenAppl 0 手: 0 $ java JankenAppl 1 手: 1 $ java JankenAppl 2 手: 2 $ java JankenAppl 3 手: 0
- public class JankenTe {
- // ここにコードを記述してください。
- }
□ JankenTe.javaクラスをテストするアプリケーションJankenAppl.java
- public class JankenAppl {
- public static void main(String[] args) {
- JankenTe te = new JankenTe();
- te.setTe(args[0]);
- System.out.println("手: " + te.getTe());
- }
- }
□ 実行結果
$ java JankenAppl ぐー 手: 0 $ java JankenAppl ちょき 手: 1 $ java JankenAppl ぱー 手: 2 $ java JankenAppl ちゃー 手: 0
Shikaku(int x0, int y0, int x1, int y1) | 左上の座標(x0, y0)と右下の座標(x1, y1)。デフォルトの名前は、"四角形"とします。 |
Shikaku(String name, int x0, int y0, int x1, int y1) | 四角形の名前と左上の座標(x0, y0)、右下の座標(x1, y1) |
このクラスには、四角形の周の長さを戻すメソッドlength()を作成してください。周の長さは、つぎの式によります。
四角形 2 * (x1 - x0 + y1 - y0)
- public class Shikaku {
- private int x0, y0; // 左上の頂点の座標
- private int x1, y1; // 左上の頂点の座標
- private int w, h; // 幅と高さ
- private String name; // 名前
- Shikaku(String name, int x0, int y0, int x1, int y1) { // コンストラクター
- setName(name);
- this.x0 = x0;
- this.y0 = y0;
- this.x1 = x1;
- this.y1 = y1;
- w = Math.abs(x0 - x1);
- h = Math.abs(y0 - y1);
- }
- Shikaku(int x0, int y0, int x1, int y1) { // コンストラクター
- this("四角形", x0, y0, x1, y1);
- }
- String getName() { // 名前の取得
- return name;
- }
- void setName(String name) { // 名前の設定
- this.name = name;
- }
- double length() { // 周の長さ
- // ここにコードを記述してください。
- }
- }
なお、これらをテストするアプリケーション"ZukeiTest01.java"は、以下のとおりです。
- public class ZukeiTest01 {
- public static void main(String[] args) {
- int x0 = Integer.parseInt(args[0]);
- int y0 = Integer.parseInt(args[1]);
- int x1 = Integer.parseInt(args[2]);
- int y1 = Integer.parseInt(args[3]);
- Shikaku s0 = new Shikaku(x0, y0, x1, y1);
- System.out.println(s0.getName() + " の周の長さ = " + (int)(s0.length()));
- Shikaku s1 = new Shikaku("しかく", x0, y0, x1, y1);
- System.out.println(s1.getName() + " の周の長さ = " + (int)(s1.length()));
- }
- }
□ 実行結果
$ java ZukeiTest01 0 0 100 100 四角形 の周の長さ = 400 しかく の周の長さ = 400