ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

Javaの課題丸投げコミュの教えてください

  • mixiチェック
  • このエントリーをはてなブックマークに追加
大学の課題です
わかる方いればお願いします

1. “Goods”というクラスを作成し、変数(フィールド)に商品名、
価格を少なくとも用意し、コンストラクタとして引数をもたない
ものと、持つものを用意しオーバーロードしなさい
2. 変数の値を出力するメソッドを用意し、クラスをカプセル化し
なさい
3. 1、2で作成したクラスを“Candy”というサブクラスと“Paper”と
いうサブクラスで継承し、Candyクラスには内容量、賞味期限
という変数を、Paperクラスには新聞の分野(一般紙,スポー
ツ紙,経済紙など)、発売日という変数を新たに加え、これら
の値を表示するメソッドをオーバーライドしなさい
4. スーパークラス(Goodsクラス)の配列を用いて、Candyクラ
ス、Paperクラスのオブジェクトをそれぞれ2つ程度作成し(コ
ンストラクタで適当な値を設定する)、オーバーライドした関数
で情報を出力しなさい

コメント(7)

public class Goods {
private String productName;
private Integer price;
public Goods(){}
public Goods(String productName,Integer price){
this.productName=productName;
this.price=price;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}

}
public class Candy extends Goods{
private Integer qty;
private Date limit;
public Candy(){}
public Candy(String productName,Integer price){
super(productName, price);
}
public Integer getQty() {
return qty;
}
public void setQty(Integer qty) {
this.qty = qty;
}
public Date getLimit() {
return limit;
}
public void setLimit(Date limit) {
this.limit = limit;
}
}
public class Paper extends Goods{
public enum Category{General,Sport,Economic}
private Category category;
private Date release;
public Paper(){}
public Paper(String productName,Integer price){
super(productName, price);
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public Date getRelease() {
return release;
}
public void setRelease(Date release) {
this.release = release;
}
}
public class Test {
public static void main(String[] args) {
Goods[] goods=new Goods[2];
goods[0]=new Candy("peropero",100);
goods[1]=new Paper("mixisports",120);
System.out.println(goods[0].getProductName());
System.out.println(goods[0].getPrice());
System.out.println(goods[1].getProductName());
System.out.println(goods[1].getPrice());
}
}
出力メソッドをカプセル化しろといわれてるので、こんな感じかな、と。
chunさんのコードを改造


import java.io.PrintStream;

public class Goods {
private String productName;
private Integer price;

public Goods() {
}

public Goods(String productName, Integer price) {
this.productName = productName;
this.price = price;
}

public String getProductName() {
return productName;
}

public void setProductName(String productName) {
this.productName = productName;
}

public Integer getPrice() {
return price;
}

public void setPrice(Integer price) {
this.price = price;
}

public void output(PrintStream stream){
stream.println("名前 : " + this.getProductName());
stream.println("価格 : " + this.getPrice());
}
}
同じの貼っちゃった…

import java.io.PrintStream;
import java.util.Date;

public class Candy extends Goods {
private Integer qty;
private Date limit;

public Candy() {
}

public Candy(String productName, Integer price, Integer qty, Date limit) {
super(productName, price);
this.qty = qty;
this.limit = limit;
}

public Integer getQty() {
return qty;
}

public void setQty(Integer qty) {
this.qty = qty;
}

public Date getLimit() {
return limit;
}

public void setLimit(Date limit) {
this.limit = limit;
}

@Override
public void output(PrintStream stream) {
super.output(stream);
stream.println("内容量 : " + this.getQty());
stream.println("賞味期限 : " + this.getLimit());
}
}

import java.io.PrintStream;
import java.util.Date;

public class Paper extends Goods {
public enum Category {
General, Sport, Economic
}

private Category category;
private Date release;

public Paper() {
}

public Paper(String productName, Integer price, Category category, Date release) {
super(productName, price);
this.category = category;
this.release = release;
}

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

public Date getRelease() {
return release;
}

public void setRelease(Date release) {
this.release = release;
}

@Override
public void output(PrintStream stream) {
super.output(stream);
stream.println("分野 : " + this.getCategory());
stream.println("発売日 : " + this.getRelease());
}
import java.util.Date;
import 適切なパッケージに書き換えてね.Paper.Category;

public class Test {
public static void main(String[] args) {
Goods[] goods = new Goods[4];
goods[0] = new Candy("peropero", 100, 200, new Date(2008, 5, 30));
goods[1] = new Paper("mixisports", 120, Category.Economic, new Date(2008, 5, 20));
goods[2] = new Candy("hogehoge", 140, 240, new Date(2008, 5, 25));
goods[3] = new Paper("fugafuga", 150, Category.Sport, new Date(2008, 5, 10));

for (int i = 0; i < goods.length; i++) {
goods[i].output(System.out);
System.out.println();
}
}
}
>seraphさん
出力処理を飛ばしてました…。
コンストラクタ、ちゃんと書いたんですね。

ログインすると、みんなのコメントがもっと見れるよ

mixiユーザー
ログインしてコメントしよう!

Javaの課題丸投げ 更新情報

Javaの課題丸投げのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。