2016-10-31 11 views
1

私は現在、Java(Mocha)の学校でAp Computer Scienceというクラスに登録しています。私はステージ4.12.4で立ち往生しています。ここに練習があります衣料品店舗CodeHSのレッスン

この問題では、衣料品店のさまざまな衣服を表すいくつかのクラスを設計します。

あなたはTShirt、Jeans、Sweatshirt、Clothingのクラスを書くでしょう。

Clothingクラスには、衣服のサイズ(String)と衣服の色(String)の2つのインスタンス変数が必要です。

public String getSize() 
public String getColor() 

トレーナークラスは、それがフードを有するか否かを記憶するためのプライベートインスタンス変数(またはフィールド)を持つべきであり、対応するゲッター:

衣類は、二つの同様にアクセッサ(ゲッターメソッド)を有するべきです方法

public boolean hasHood() 

Tシャツクラスが呼ばれ、そのためのファブリックと対応するgetterを格納するためのプライベートフィールドを持っている必要があり

public String getFabric() 

すべてのジーンズは青色である必要があります。

コンストラクタはこの形式でなければなりません。ここで

public Clothing(String size, String color) 
public TShirt(String size, String color, String fabric) 
public Sweatshirt(String size, String color, boolean hasHood) 
public Jeans(String size) 

は私が持っているコードです。

TShirt.java:

public class TShirt extends Clothing 
{ 
private String size; 
private String color; 
private String fabric; 


public TShirt(String Size, String Color, String Fabric) 
{ 
    size = Size; 
    color = Color; 
    fabric = Fabric; 
} 


public String fabric() 
{ 
    return this.fabric; 
} 

} 

Jeans.java:

public class Jeans extends Clothing 
{ 
private String size; 
private String color = "blue"; 

public Jeans(String Size) 
{ 
    size = Size; 
} 



public String getColor() 
{ 
    return this.color; 
} 
} 

Sweatshirt.java:

public class Sweatshirt extends Clothing 
{ 
private String size; 
private String color; 
private boolean hasHood; 

public Sweatshirt(String Size, String Color, boolean HasHood) 
{ 
    //super(Clothing); 
    size = Size; 
    color = Color; 
    hasHood = HasHood; 

} 


public boolean hasHood() 
{ 
    return this.hasHood; 
} 
} 

Clothing.java:

public abstract class Clothing 
{ 
private String size; 
private String color; 
private String fabric; 

public Clothing(String Size, String Color) 
{ 
    this.size = size; 
    this.color = color; 
} 

public String getSize() 
{ 
    return this.size; 
} 

public String getColor() 
{ 
    return this.color; 
} 

public String getFabric() 
{ 
    return this.fabric; 
} 

} 
ここ

は、エラーのリストはとてもいずれかが、私は数日、すでにこのレッスンで立ち往生私を助けてください、私はプログラム

root/TShirt.java:9: error: constructor Clothing in class Clothing cannot be applied to given types; 
{ 
^ 
required: String,String 
found: no arguments 
reason: actual and formal argument lists differ in length 
root/Jeans.java:7: error: constructor Clothing in class Clothing cannot be applied to given types; 
{ 
^ 
required: String,String 
found: no arguments 
reason: actual and formal argument lists differ in length 
root/Sweatshirt.java:8: error: constructor Clothing in class Clothing cannot be applied to given types; 
{ 
^ 
required: String,String 
found: no arguments 
reason: actual and formal argument lists differ in length 
3 errors 
Error: Could not find or load main class ClothingTester 

を実行したときに私は私が間違っていたかわからないてしまったということです。

答えて

2

Clothingクラスを拡張するクラスは、エラーを投げかけているようです。 1つはTShirt(コードは表示されません)、もう1つはJeans、もう1つはSweatshirt(あなたの投稿コードに2回あります)です。

クラスを拡張するたびに、継承されたクラスのコンストラクタを直ちに初期化しないと、コンパイラはエラーになります。表示されているすべての拡張クラスで、これを行うことはありません。だから、私はsuperコンストラクタを初期化するスウェットシャツクラスを最低限提供しました。スウェットシャツオブジェクトを作成するたびにClothingが初期化されます。この場合、superClothingです。また、Clothingクラスから拡張しているので、拡張クラスのSizeプロパティとColorプロパティを再定義する必要はありません。

同じ名前の異なる変数を使用する場合は「Case」が異なる、例の場合も注意してください。あなたが指定したコードを見て、Clothingクラスのコンストラクタを見てください。そこにあなたの着信値:サイズと色は使用されていません。 this.size = Sizeおよびthis.color = Colorである必要があります。クラス自体の変数を使用して自分自身を設定するので、私はあなたがエラーを得るか、単に正しい値を取得しないと確信しています。大文字と小文字の違いだけが異なる変数を命名することはお勧めしません。

public class Sweatshirt extends Clothing 
{ 
    //private String size; 
    //private String color; 
    private boolean hasHood; 

    public Sweatshirt(String inSize, String inColor, boolean inHasHood) 
    { 
    super(inSize, inColor); 
    //super(Clothing); 
    //size = Size; 
    //color = Color; 
    hasHood = inHasHood; 
    } 

    public boolean getHasHood() 
    { 
    return this.hasHood; 
    } 

    @Override 
    public String toString() 
    { 
    return "Sweatshirt [hasHood=" + getHasHood() + ", getSize()=" + getSize() + ", getColor()=" + getColor() + "]"; 
    } 

} 

ご希望の場合は、こちらをご覧ください。

1

これは私がやったことです:

トレーナー

public class Sweatshirt extends Clothing 
{ 

    private boolean hood; 

    public Sweatshirt(String size, String color, boolean hood) 
    { 
     super (size,color); 
     this.hood=hood; 
    } 

    public boolean hasHood() 
    { 
     return this.hood; 
    } 
} 

衣料品店

public class Jeans extends Clothing 
{ 
    public Jeans(String size) 
    { 
     super(size,"blue"); 
    } 
} 

Tシャツ

public class TShirt extends Clothing 
{ 
    private String fabric; 

    public TShirt(String size, String color, String fabric) 
    { 
     super(size,color); 
     this.fabric=fabric; 
    } 

    public String getFabric() 
    { 
     return this.fabric; 
    } 

public class Clothing 
{ 
    // all instance variables should be private 
    private String size; 
    private String color; 
    // private int x; 

    // default constructor 
    /* 
    public Clothing() 
    { 
     // intialize all instance variables 
     size=""; 
     color=""; 
     x=0; 
    } 
    */ 
    // only intialize when the parameter is empty or zero argument 

    public Clothing(String size, String color) 
    { 
     this.size=size; 
     this.color=color; 
     // this.x=0; 

     // always initialize all the instance variables 
     //even though the parameters are incomplete 
    } 


    public String getSize() 
    { 
     return this.size; 
    } 
    public String getColor() 
    { 
     return this.color; 
    } 

} 

あなたはおそらくすでにこれを行っていますが、質問に答えるしようとしている場合は、単に

+0

場合には、彼らは将来の訪問者にその有用でないように、コードがダンプ主に答えを与えないようです。代わりに、元のポスターが持つ問題の背後にある理由とその問題の解決方法を説明するように努めます。あなたの答えは、元のポスターだけでなく、同様の問題を抱えている将来の訪問者に役立ちます。 –

+0

@HovercraftFullOfEelsうん、それだ。これは私が最初に答えたので、私はそれを覚えていませんでした。申し訳ありません、それを指摘してくれてありがとう – CodingBat