抽象クラス | インターフェース | |
---|---|---|
インスタンス変数 | ○ | × |
クラス変数 | ○ | × |
定数 | ○ | ○ |
コンストラクター | ○ | × |
抽象メソッド | ○ | ○ |
具象メソッド | ○ | × |
- // 1102
- // 割引インターフェースです。
- // 割引される商品情報が持っていなければならないメソッドの
- // 実装を強制しています。
- interface DiscountInterface {
- // 商品割引率の値を設定します。
- void setDiscount(int discount);
- // 商品割引率の値を取得します。
- int getDiscount();
- }
- // 1102
- // 割引商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class DiscountProduct extends Product implements DiscountInterface {
- 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);
- }
- // 商品割引率に値を設定します。
- public void setDiscount(int discount) {
- if (discount < 0 || discount > 100) {
- this.discount = 0;
- return;
- }
- this.discount = discount;
- }
- // 商品割引率に値を設定します。
- public void setDiscount(String discount) {
- setDiscount(Integer.parseInt(discount));
- }
- // 商品割引率の値を取得します。
- public int getDiscount() {
- return this.discount;
- }
- // 商品の売値の値を取得します。
- public int getPrice() {
- return (int)(super.getPrice() * (1 - discount / 100.0));
- }
- }
- // 1102
- // セット商品クラスです。
- // ひとつまたは複数の商品を集めて、割引したものをセット商品として扱います。
- // セット商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class SetProduct extends AbstractProduct implements DiscountInterface {
- private int discount; // セット商品割引率
- private AbstractProduct[] products; // セット商品リスト
- // セット商品クラスのコンストラクター。
- SetProduct(String code, String name, int discount, AbstractProduct[] products) {
- setCode(code);
- setName(name);
- setDiscount(discount);
- setProducts(products);
- }
- // 商品の売値の値を取得します。
- public int getPrice() {
- int price = 0;
- for (int i = 0; i < products.length; i++) {
- price += products[i].getPrice();
- }
- return (int)(price * (1 - discount / 100.0));
- }
- // 商品割引率に値を設定します。
- public void setDiscount(int discount) {
- if (discount < 10 || discount > 100) {
- this.discount = 0;
- return;
- }
- this.discount = discount;
- }
- // 商品割引率に値を設定します。
- public void setDiscount(String discount) {
- setDiscount(Integer.parseInt(discount));
- }
- // 商品割引率の値を取得します。
- public int getDiscount() {
- return discount;
- }
- // 商品割引率に値を設定します。
- public void setProducts(AbstractProduct[] products) {
- this.products = products;
- }
- }
- // 1102
- // 商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) {
- AbstractProduct[] p = new AbstractProduct[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());
- }
- SetProduct s = new SetProduct("SA01", "パソコン・セット", 10, p);
- System.out.println("セット商品のインスタンスを生成しました。 s = " + s);
- System.out.println(" code = " + s.getCode());
- System.out.println(" name = " + s.getName());
- System.out.println(" price = " + s.getPrice());
- }
- }
□ 実行結果
$ java TestProduct 商品配列のインスタンスを生成しました。 p = [LProductInterface;@1e63e3d 商品のインスタンスを生成しました。 p[0] = DiscountProduct@1004901 code = A001 name = パソコン price = 150400 商品のインスタンスを生成しました。 p[1] = Product@1b90b39 code = P001 name = プリンター price = 24000 商品のインスタンスを生成しました。 p[2] = DiscountProduct@18fe7c3 code = C001 name = CD-ROM price = 5850 商品のインスタンスを生成しました。 p[3] = Product@b8df17 code = M001 name = USBメモリー price = 9800 セット商品のインスタンスを生成しました。 s = SetProduct@1172e08 code = SA01 name = パソコン・セット price = 171045
図 図形クラス階層
インターフェースが実装するのは、以下のメソッドです。
上記インターフェースを利用して、図形を描画するクラスを作成してください。
|
Maru(int x, int y, int r) | 中心(x, y)と半径r |
|
Sankaku(int[][] p) | 頂点の座標(x0, y0),(x1, y1), (x2, y2)の組 |
|
Shikaku(int x0, int y0, int x1, int y1) | 左上の座標(x0, y0)と右下の座標(x1, y1) |
□ "ZukeiTest01.java"は、変更ありません。
- public class Sankaku extends AbstractZukei {
- 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]);
- }
- }
- public double length() {
- return distance(p[0], p[1]) + distance(p[1], p[2]) + distance(p[2], p[0]);
- }
- private double distance(int[] x, int[] y) {
- return Math.sqrt(diff(x[0], y[0]) * diff(x[0], y[0]) + diff(x[1], y[1]) * diff(x[1], y[1]));
- }
- private double diff(int x, int y) {
- return Math.abs(y - x);
- }
- }
□ 実行結果
$ java ZukeiTest01 基準四角 の周の長さ = 400 基準三角 の周の長さ = 341 基準丸 の周の長さ = 628 三角形 の周の長さ = 752 丸 の周の長さ = 377 四角形 の周の長さ = 480