クラス | 役割 | オブジェクト |
---|---|---|
Product | 商品の情報を持つ | パソコン |
DiscountProduct | 割引商品の情報を持つ | CD-ROMドライブ |
TestProduct | テストするためのアプリケーション | - |
フィールド | 役割 | 値の例 |
---|---|---|
discount | 値引き率(単位%) | 20 |
メソッド | 役割 | 戻り値 | 引数 |
---|---|---|---|
getDiscount | 値引き率を取得する | 値引き率 | なし |
setDiscount | 値引き率を設定する | なし | 値引き率 |
- // 1011
- // 割引商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class DiscountProduct extends Product {
- private int discount; // 商品割引率
- // 割引商品クラスのコンストラクター。
- DiscountProduct(String code, String name, int price) {
- setCode(code);
- setName(name);
- setPrice(price);
- setDiscount(0);
- }
- // 割引商品クラスのコンストラクター。
- DiscountProduct(String code, String name, String price) {
- setCode(code);
- setName(name);
- setPrice(price);
- setDiscount(0);
- }
- // 割引商品クラスのコンストラクター。
- DiscountProduct(String code, String name, int price, int discount) {
- setCode(code);
- setName(name);
- setPrice(price);
- setDiscount(discount);
- }
- // 割引商品クラスのコンストラクター。
- DiscountProduct(String code, String name, String price, String discount) {
- setCode(code);
- setName(name);
- setPrice(price);
- setDiscount(discount);
- }
- // 商品割引率に値を設定します。
- void setDiscount(int discount) {
- if (discount < 0 || discount > 100) {
- this.discount = 0;
- return;
- }
- this.discount = discount;
- }
- // 商品割引率に値を設定します。
- void setDiscount(String discount) {
- setDiscount(Integer.parseInt(discount));
- }
- // 商品割引率の値を取得します。
- int getDiscount() {
- return this.discount;
- }
- }
$ java TestProduct DiscountProduct.java:7: シンボルを解決できません。 シンボル: コンストラクタ Product () 場所 : Product の クラス DiscountProduct(String code, String name, int price) { ^ DiscountProduct.java:15: シンボルを解決できません。 シンボル: コンストラクタ Product () 場所 : Product の クラス DiscountProduct(String code, String name, String price) { ^ DiscountProduct.java:23: シンボルを解決できません。 シンボル: コンストラクタ Product () 場所 : Product の クラス DiscountProduct(String code, String name, int price, int discount) { ^ DiscountProduct.java:31: シンボルを解決できません。 シンボル: コンストラクタ Product () 場所 : Product の クラス DiscountProduct(String code, String name, String price, String discount) { ^ エラー 4 個
- // 1012
- // 商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class Product {
- private String code; // 商品コード
- private String name; // 商品名
- private int price; // 商品定価
- // 商品クラスのコンストラクター。
- Product() {
- }
- // 商品クラスのコンストラクター。
- Product(String code, String name, int price) {
- setCode(code);
- setName(name);
- setPrice(price);
- }
- // 商品クラスのコンストラクター。
- Product(String code, String name, String price) {
- setCode(code);
- setName(name);
- setPrice(price);
- }
- // 商品コードに値を設定します。
- void setCode(String code) {
- this.code = code;
- }
- // 商品コードの値を取得します。
- String getCode() {
- return this.code;
- }
- // 商品名に値を設定します。
- void setName(String name) {
- this.name = name;
- }
- // 商品名の値を取得します。
- String getName() {
- return this.name;
- }
- // 商品定価に値を設定します。
- void setPrice(int price) {
- if (price < 0) {
- this.price = 0;
- return;
- }
- this.price = price;
- }
- // 商品定価に値を設定します。
- void setPrice(String price) {
- setPrice(Integer.parseInt(price));
- }
- // 商品定価の値を取得します。
- int getPrice() {
- return this.price;
- }
- }
- // 1012 = 1011
- // 割引商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) {
- DiscountProduct p1 = new DiscountProduct("A001", "パソコン", 188000, 20);
- System.out.println("割引商品のインスタンスを生成しました。 p1 = " + p1);
- System.out.println(" code = " + p1.getCode());
- System.out.println(" name = " + p1.getName());
- System.out.println(" price = " + p1.getPrice());
- System.out.println(" discount = " + p1.getDiscount());
- DiscountProduct p2 = new DiscountProduct("P001", "プリンター", 24000);
- System.out.println("割引商品のインスタンスを生成しました。 p2 = " + p2);
- System.out.println(" code = " + p2.getCode());
- System.out.println(" name = " + p2.getName());
- System.out.println(" price = " + p2.getPrice());
- System.out.println(" discount = " + p2.getDiscount());
- }
- }
□ 実行結果
$ java TestProduct 割引商品のインスタンスを生成しました。 p1 = DiscountProduct@15ff48b code = A001 name = パソコン price = 188000 discount = 20 割引商品のインスタンスを生成しました。 p2 = DiscountProduct@affc70 code = P001 name = プリンター price = 24000 discount = 0
- // 1013
- // 割引商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class DiscountProduct extends Product {
- private int discount; // 商品割引率
- // 割引商品クラスのコンストラクター。
- DiscountProduct(String code, String name, int price) {
- this(code, name, price, 0);
- }
- // 割引商品クラスのコンストラクター。
- DiscountProduct(String code, String name, String price) {
- this(code, name, price, "0");
- }
- // 割引商品クラスのコンストラクター。
- DiscountProduct(String code, String name, int price, int discount) {
- super(code, name, price);
- setDiscount(discount);
- }
- // 割引商品クラスのコンストラクター。
- DiscountProduct(String code, String name, String price, String discount) {
- super(code, name, price);
- setDiscount(discount);
- }
- // 商品割引率に値を設定します。
- void setDiscount(int discount) {
- if (discount < 0 || discount > 100) {
- this.discount = 0;
- return;
- }
- this.discount = discount;
- }
- // 商品割引率に値を設定します。
- void setDiscount(String discount) {
- setDiscount(Integer.parseInt(discount));
- }
- // 商品割引率の値を取得します。
- int getDiscount() {
- return this.discount;
- }
- }
□ 実行結果
$ java TestProduct 割引商品のインスタンスを生成しました。 p1 = DiscountProduct@15ff48b code = A001 name = パソコン price = 188000 discount = 20 割引商品のインスタンスを生成しました。 p2 = DiscountProduct@affc70 code = P001 name = プリンター price = 24000 discount = 0
- // 1013
- // 商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class Product {
- private String code; // 商品コード
- private String name; // 商品名
- private int price; // 商品定価
- // 商品クラスのコンストラクター。
Product() {}- :
- }
- public class A {
- String mA() {
- return "A#mA()";
- }
- String mB() {
- return "A#mB()";
- }
- }
- public class B extends A {
- String mB() {
- return "B#mB()";
- }
- }
- public class ThisSuper01 extends B {
- String mA() {
- return "C#mA()";
- }
- String mB() {
- return "C#mB()";
- }
- String mC() {
- return "C#mC()";
- }
- void doMethod() {
- System.out.println("this.mA() : " + this.mA());
- System.out.println("super.mA(): " + super.mA());
- System.out.println("this.mB() : " + this.mB());
- System.out.println("super.mB(): " + super.mB());
- System.out.println("this.mC() : " + this.mC());
- // System.out.println("super.mC(): " + super.mC());
- }
- public static void main(String[] args) {
- new ThisSuper01().doMethod();
- }
- }
□ 実行結果
$ java ThisSuper01 this.mA() : C#mA() super.mA(): A#mA() this.mB() : C#mB() super.mB(): B#mB() this.mC() : C#mC()
- // 1014
- // 割引商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class DiscountProduct extends Product {
- private int discount; // 商品割引率
- :
- // 商品の売値の値を取得します。
- int getPrice() {
- return (int)(super.getPrice() * (1 - discount / 100.0));
- }
- }
$ java TestProduct 割引商品のインスタンスを生成しました。 p1 = DiscountProduct@15ff48b code = A001 name = パソコン price = 150400 discount = 20
class A { ... } class B extends A { ... }
型 | オブジェクト | ... = new A( ); | ... = new B( ); |
---|---|---|
A 型 | A a1 = new A(); a1.field // A のフィールド a1.doMethod(); // A のメソッド |
A a2 = new B(); a2.field // A のフィールド a2.doMethod(); // B のメソッド |
B 型 | - |
B b1 = new B(); b1.field // B のフィールド b1.doMethod(); // B のメソッド |
- public class A {
- int n = 10;
- void m() {
- System.out.println("Superclass A");
- }
- }
- public class B extends A {
- double n;
- B() {
- n = 50.0;
- }
- void m() {
- System.out.println("Subclass B");
- }
- }
- public class Inheri01 {
- public static void main(String[] args) {
- A a = new B();
- System.out.println("a.n = " + a.n);
- System.out.print("a.m(); ");
- a.m();
- B b = new B();
- System.out.println("b.n = " + b.n);
- System.out.print("b.m(); ");
- b.m();
- System.out.println("((A)b).n = " + ((A)b).n);
- System.out.print("((A)b).m(); ");
- ((A)b).m();
- }
- }
□ 実行結果
$ java Inheri01 a.n = 10 ... クラスA型のフィールド a.m(); Subclass B ... オブジェクトbのメソッド b.n = 50.0 ... クラスB型のフィールド b.m(); Subclass B ... オブジェクトbのメソッド ((A)b).n = 10 ... クラスA型のフィールド ((A)b).m(); Subclass B ... オブジェクトbのメソッドTop
- // 1015
- // 商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) {
- Product[] p = new Product[4];
- p[0] = new Product("A001", "パソコン", 188000);
- p[1] = new Product("P001", "プリンター", 24000);
- p[2] = new Product("C001", "CD-ROM", 6500);
- p[3] = new Product("M001", "USBメモリー", 9800);
- System.out.println("商品配列のインスタンスを生成しました。 p = " + p);
- for (int i = 0; i < p.length; i++) {
- System.out.println("商品のインスタンスを生成しました。 p[" + i + "] = " + p[i]);
- System.out.println(" code = " + p[i].getCode());
- System.out.println(" name = " + p[i].getName());
- System.out.println(" price = " + p[i].getPrice());
- }
- }
- }
□ 実行結果
$ java TestProduct 商品配列のインスタンスを生成しました。 p = [LProduct;@131f71a 商品のインスタンスを生成しました。 p[0] = Product@15601ea code = A001 name = パソコン price = 188000 商品のインスタンスを生成しました。 p[1] = Product@197d257 code = P001 name = プリンター price = 24000 商品のインスタンスを生成しました。 p[2] = Product@7259da code = C001 name = CD-ROM price = 6500 商品のインスタンスを生成しました。 p[3] = Product@16930e2 code = M001 name = USBメモリー price = 9800
- // 1016
- // 商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) {
- Product[] p = new Product[4];
- p[0] = new DiscountProduct("A001", "パソコン", 188000, 20);
- p[1] = new Product("P001", "プリンター", 24000);
- p[2] = new DiscountProduct("C001", "CD-ROM", 6500, 10);
- p[3] = new Product("M001", "USBメモリー", 9800);
- System.out.println("商品配列のインスタンスを生成しました。 p = " + p);
- for (int i = 0; i < p.length; i++) {
- System.out.println("商品のインスタンスを生成しました。 p[" + i + "] = " + p[i]);
- System.out.println(" code = " + p[i].getCode());
- System.out.println(" name = " + p[i].getName());
- System.out.println(" price = " + p[i].getPrice());
- }
- }
- }
□ 実行結果
$ java TestProduct 商品配列のインスタンスを生成しました。 p = [LProduct;@15ff48b 商品のインスタンスを生成しました。 p[0] = DiscountProduct@affc70 code = A001 name = パソコン price = 150400 商品のインスタンスを生成しました。 p[1] = Product@1e63e3d code = P001 name = プリンター price = 24000 商品のインスタンスを生成しました。 p[2] = DiscountProduct@1004901 code = C001 name = CD-ROM price = 5850 商品のインスタンスを生成しました。 p[3] = Product@1b90b39 code = M001 name = USBメモリー price = 9800
- public class Book {
- private String title; // 書名
- private String author; // 著作者
- // コンストラクター
- public Book(String title, String author) {
- setTitle(title);
- setAuthor(author);
- }
- // title のゲッター
- public String getTitle() {
- return this.title;
- }
- // author のゲッター
- public String getAuthor() {
- return this.author;
- }
- // title のセッター
- public void setTitle(String title) {
- this.title = title;
- }
- // author のセッター
- public void setAuthor(String author) {
- this.author = author;
- }
- }
- public class SeriesBook extends Book {
- // コードを記述してください。
- }
- public class Magazine extends Book {
- // コードを記述してください。
- }
□ SeriesBook.javaクラス、Magazine.java クラスをテストするアプリケーションBookTest.java
(*) new java.util.Date().toString() は、現在日時の文字列表現をあらわします。
- public class BookTest {
- public static void main(String[] args) {
- Book b1 = new Book(args[0], args[1]);
- System.out.println("Book1 = [" + b1.getTitle() + ", " + b1.getAuthor() + "]");
- SeriesBook b2 = new SeriesBook(args[0], args[1], args[2], Integer.parseInt(args[3]));
- System.out.print("Book2 = [" + b2.getTitle() + ", " + b2.getAuthor());
- System.out.println(", " + b2.getSeries() + ", " + b2.getVolume() + "]");
- Magazine b3 = new Magazine(args[0], args[1], new java.util.Date().toString()(*));
- System.out.print("Book3 = [" + b3.getTitle() + ", " + b3.getAuthor());
- System.out.println(", " + b3.getDate() + "]");
- }
- }
□ 実行結果
$ java BookTest JavaBook Maruno Java 3 Book1 = [JavaBook, Maruno] Book2 = [JavaBook, Maruno, Java, 3] Book3 = [JavaBook, Maruno, Wed Jun 22 21:28:12 JST 2005]
SeriesBook.java ... "シリーズ名 - 書名 (通番)" Magazine.java ... "書名 (日付)"
□ 実行結果
$ java BookTest JavaBook Maruno Java 3 Book1 = [JavaBook, Maruno] Book2 = [JavaBasic - JavaBook (3), Maruno, JavaBasic, 3] Book3 = [JavaBook (Thu Jun 23 12:41:09 JST 2005), Maruno, Thu Jun 23 12:41:09 JST 2005]
□ テストするアプリケーションBookTest.java
- public class BookTest {
- public static void main(String[] args) {
- Book b1 = new Book(args[0], args[1]);
- System.out.println("Book1 = " + b1.getTitle());
- Book b2 = new SeriesBook(args[0], args[1], args[2], Integer.parseInt(args[3]));
- System.out.println("Book2 = " + b2.getTitle());
- Book b3 = new Magazine(args[0], args[1], new java.util.Date().toString());
- System.out.println("Book3 = " + b3.getTitle());
- }
- }
□ 実行結果
$ java BookTest JavaBook Maruno Java 3 Book1 = JavaBook Book2 = Java - JavaBook (3) Book3 = JavaBook (Thu Jun 23 12:36:17 JST 2005) T 2005]
図 図形クラス階層
この図形クラスを継承して円、三角形、四角形の図形を処理するクラスを作ります。コンストラクターはつぎのとおりです。なお、図形の座標の原点は左上にあるものとします。
- public class Zukei {
- private String name; // 名前
- String getName() { // 名前の取得
- return name;
- }
- void setName(String name) { // 名前の設定
- this.name = name;
- }
- double length() { // 周の長さ
- return 0.0;
- }
- }
|
Maru(String name, int x, int y, int r) | 中心(x, y)と半径r |
Maru(int x, int y, int r) | 中心(x, y)と半径r | |
|
Sankaku(String name, int[][] p) | 頂点の座標(x0, y0),(x1, y1), (x2, y2)の組 |
Sankaku(int[][] p) | 頂点の座標(x0, y0),(x1, y1), (x2, y2)の組 | |
|
Shikaku(String name, int x0, int y0, int x1, int y1) | 左上の座標(x0, y0)と右下の座標(x1, y1) |
Shikaku(int x0, int y0, int x1, int y1) | 左上の座標(x0, y0)と右下の座標(x1, y1) |
また、図形の周の長さを戻すメソッドlength()をオーバーライドしてください。周の長さは、つぎの式によります。Math.PIは、Mathクラスのdouble型定数で、円周率をあらわします。
円 2 * Math.PI * r 四角形 2 * (x1 - x0 + y1 - y0)
なお、これらをテストするアプリケーション"ZukeiTest01.java"と、三角形クラス"Sankaku.java"のサンプル・ソース・ファイルをつけます。
- public class ZukeiTest01 {
- static Zukei[] z;
- ZukeiTest01() {
- z = new Zukei[] {
- new Shikaku("基準四角", 0, 0, 100, 100)
- , new Sankaku("基準三角", new int[][]{{0, 0}, {0, 100}, {100, 0}})
- , new Maru("基準丸", 0, 0, 100)
- , new Sankaku(new int[][]{{100, 100}, {10, 250}, {300, 300}})
- , new Maru(150, 170, 60)
- , new Shikaku(120, 130, 300, 190)
- };
- }
- public static void main(String[] args) {
- ZukeiTest01 zt = new ZukeiTest01();
- for (int i = 0; i < z.length; i++) {
- System.out.println(z[i].getName() + " の周の長さ = " + Math.round(z[i].length()));
- }
- }
- }
- public class Maru extends Zukei {
- private int x0, y0; // 中心
- private int r; // 半径
- Maru(String name, int x0, int y0, int r) {
- setName(name);
- this.x0 = x0;
- this.y0 = y0;
- this.r = r;
- }
- Maru(int x0, int y0, int r) {
- this("丸", x0, y0, r);
- }
- double length() { // 周の長さ
- // ここにコードを記述してください。
- }
- }
- public class Sankaku extends Zukei {
- private int[][] p; // 頂点
- private double[] l = new double[3]; // 辺の長さ
- Sankaku(int[][] p) {
- this("三角形", p);
- }
- Sankaku(String name, int[][] p) {
- setName(name);
- this.p = p;
- for (int i = 0; i < l.length; i++) {
- l[i] = distance(p[i], p[(i + 1) % l.length]);
- }
- }
- double length() { // 周の長さ
- return sum(l);
- }
- private double sum(double[] x) { // 要素値の合計
- double s = 0;
- for (int i = 0; i < l.length; i++) {
- s += l[i];
- }
- return s;
- }
- private double distance(int[] x, int[] y) { // 頂点間の距離
- return Math.sqrt(sq(diff(x[0], y[0])) + sq(diff(x[1], y[1])));
- }
- private int sq(int x) { // 2乗
- return x * x;
- }
- private int diff(int x, int y) { // 差
- return Math.abs(y - x);
- }
- }
- public class Shikaku extends Zukei {
- private int x0, y0; // 左上の頂点の座標
- private int x1, y1; // 左上の頂点の座標
- private int w, h; // 幅と高さ
- 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);
- }
- double length() { // 周の長さ
- // ここにコードを記述してください。
- }
- }
□ 実行結果
$ java ZukeiTest01 基準四角 の周の長さ = 400 基準三角 の周の長さ = 341 基準丸 の周の長さ = 628 三角形 の周の長さ = 752 丸 の周の長さ = 377 四角形 の周の長さ = 480