中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > Web开发 > JavaScripts > 综合文章
一道JAVA作业题
作者:佚名 时间:2005-03-15 11:03 出处:互连网 责编:chinaitpower
              摘要:一道JAVA作业题
学JAVA的时候做的一道课后作业题,请打分!

Assignment:


This assignment involves writing a program, which simulates fishing. You must model the following situation: There are 4 fish species in the river, which you may catch:


Golden Perch - commonly less than 5 Kg; excellent eating, when between 1 and 2 Kg.
Silver Perch - commonly less than 7 Kg; excellent eating, when less then 1.5 Kg.
River Blackfish - Up to 5.5 kg; a superb eating fish with soft white flesh
Murray Cod - commonly less then 12 Kg; excellent eating when up to 7 Kg

For simplicity of the model, we assume that the chances to catch a fish of each of these four species are equal.


We also assume that within each species weights of fishes appear with equal chances, and range from 0 to the Maximal Weight. Maximal Weight for each of the species is specified in the species description given above.


You start fishing with an empty basket. If a caught fish¡¯s weight is within the recommended table range (specified above), you put the fish in the basket, otherwise release it. Fishes of weights less then 0.5 Kg also are released. Stop fishing as soon as the total weight of all the caught fishes exceeds 15 Kg.


To model the situation, you are supposed to use the random() method from the Math class. Fishes of different species must be implemented as objects of different classes, each extending an abstract class Fish. Whenever branching logic is required to handle fish objects of different species, you must do it polymorphically.



Task A
Create a design for your program from the following specifications:
The program requires you to create an abstract class called Fish. It can have the following class (minus constructor, which you will be required to supply).









Fish
protected double weight;
public abstract boolean acceptable()
public abstract void setWeight();
public double getWeight()
public abstract String getName()
public String toString()

The program also requires you to create four concrete classes ¨C GoldenPerch, SilverPerch, RiverBlackFish, MurrayCod, which inherit from the Fish class. These classes could have the following class diagrams.


















GoldenPerch SilverPerch RiverBlackFish MurrayCod
private static final String NAME = "Golden Perch";
private static final double MAX_WEIGHT = 5.0;
private static final String NAME = "Silver Perch";
private static final double MAX_WEIGHT = 7.0;
private static final String NAME = "River Black Fish";
private static final double MAX_WEIGHT = 5.5;
private static final String NAME = "Murray Cod";
private static final double MAX_WEIGHT = 12.0;
public boolean acceptable()
public void setWeight();
public String getName()
public boolean acceptable()
public void setWeight();
public String getName()
public boolean acceptable()
public void setWeight();
public String getName()
public boolean acceptable()
public void setWeight();
public String getName()

Task B
Create a class Angling,£¬ which randomly creates objects of GoldenPerch, SilverPerch, RiverBlackFish, MurrayCod types, and if suitable puts them into the basket, i.e.writes them to the basket.dat file. To write objects to the file you should use writeObject() method of ObjectOutputStream class (notice, that the Fish class must implement Serializable interface).


Current information about what is going on (i.e. what kind of fish was caught, what it¡¯s weight, was it put into the basket or released) must be printed to the BlueJ terminal window.


Task C
Create a class PrintBasket, which opens the file basket.dat, then reads objects from it one by one. It should then print that information to the BlueJ terminal window. Output should look like the following:


Golden Perch: 1.40 Kg
Golden Perch: 1.17 Kg
Golden Perch: 1.00 Kg
Golden Perch: 1.65 Kg
Golden Perch: 1.08 Kg
River Black Fish: 1.46 Kg
Murray Cod: 3.14 Kg
River Black Fish: 3.83 Kg
Murray Cod: 6.55 Kg

All created classes should be in a package called yourStudentId followed by Ass1. For example, UB2000345 would create a package called UB2000345Ass1.





My Answer Sheet:







Fish.java

package UB2000345Ass1;


import JAVA.io.*;


/**
 * <p>Title: Fish</p>
 * <p>Description: Fishing Class</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Longware Studio</p>
 * @author Longware
 * @version 1.0
 */


public abstract class Fish {
  protected double weight;


  public Fish() {
  }


  public abstract boolean acceptable();


  public abstract void setWeight(double w);


  public double getWeight() {
    return this.weight;
  }


  public abstract String getName();


  private void writeObject(ObjectOutputStream out) {
  }


  public String toString() {
    return "This Fish's weight is:" + this.weight;
  }
}








GoldenPerch.java

package UB2000345Ass1;


/**
 * <p>Title: Fish</p>
 * <p>Description: Fishing Class</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Longware Studio</p>
 * @author Longware
 * @version 1.0
 */


public class GoldenPerch
    extends Fish {
  private static final String NAME = "Golden Perch";
  private static final double MAX_WEIGHT = 5.0;


  public GoldenPerch(double w) {
    setWeight(w);
  }


  public boolean acceptable() {


    if ( (this.weight > 1) && (this.weight < 2)) {
      return true;
    }
    else {
      return false;
    }
  }


  public void setWeight(double w) {
    this.weight = w;
  }


  public String getName() {
    return NAME;
  }


}








SilverPerch.java

package UB2000345Ass1;


/**
 * <p>Title: Fish</p>
 * <p>Description: Fishing Class</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Longware Studio</p>
 * @author Longware
 * @version 1.0
 */


public class SilverPerch
    extends Fish {
  private static final String NAME = "Silver Perch";
  private static final double MAX_WEIGHT = 7.0;


  public SilverPerch(double w) {
    setWeight(w);
  }


  public boolean acceptable() {
    if ( (this.weight >= 0.5) && (this.weight < 1.5)) {
      return true;
    }
    else {
      return false;
    }
  }


  public void setWeight(double w) {
    this.weight = w;
  }


  public String getName() {
    return NAME;
  }
}








RiverBlackFish.java

package UB2000345Ass1;


/**
 * <p>Title: Fish</p>
 * <p>Description: Fishing Class</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Longware Studio</p>
 * @author Longware
 * @version 1.0
 */


public class RiverBlackFish
    extends Fish {
  private static final String NAME = "River Black Fish";
  private static final double MAX_WEIGHT = 5.5;


  public RiverBlackFish(double w) {
    setWeight(w);
  }


  public boolean acceptable() {
    if (this.weight >= MAX_WEIGHT) {
      return true;
    }
    else {
      return false;
    }
  }


  public void setWeight(double w) {
    this.weight = w;
  }


  public String getName() {
    return NAME;
  }


}








MurrayCod.java

package UB2000345Ass1;


/**
 * <p>Title: Fish</p>
 * <p>Description: Fishing Class</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Longware Studio</p>
 * @author Longware
 * @version 1.0
 */


public class MurrayCod
    extends Fish {
  private static final String NAME = "Murray Cod";
  private static final double MAX_WEIGHT = 12.0;


  public MurrayCod(double w) {
    setWeight(w);
  }


  public boolean acceptable() {
    if ( (this.weight >= 7) && (this.weight < MAX_WEIGHT)) {
      return true;
    }
    else {
      return false;
    }
  }


  public void setWeight(double w) {
    this.weight = w;
  }


  public String getName() {
    return NAME;
  }


}








Angling.java

package UB2000345Ass1;


import JAVA.io.*;


/**
 * <p>Title: Fish</p>
 * <p>Description: Fishing Class</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Longware Studio</p>
 * @author Longware
 * @version 1.0
 */


public class Angling {
  public float totalWeight = 0;
  public double rWeight;
  public int rFishing;


  public Angling() {
  }


  public static double getRandom() {
    return JAVA.lang.Math.random();
  }


  public Fish getRandomFish() {
    rFishing = (int) JAVA.lang.Math.ceil(getRandom() * 4);
    Fish fish = null;


    switch (rFishing) {
      case 1:
        fish = new GoldenPerch(rWeight);
        break;
      case 2:
        fish = new SilverPerch(rWeight);
        break;
      case 3:
        fish = new RiverBlackFish(rWeight);
        break;
      case 4:
        fish = new MurrayCod(rWeight);
        break;
    }
    return fish;
  }


  public String doFishing() {
    int i = 0;
    String str = "Begin Fishing:\n";
    do {
      i++;
      rWeight = JAVA.lang.Math.ceil(getRandom() * 120) / 10;
      Fish fish = getRandomFish();


      if (fish.acceptable()) {
        totalWeight += rWeight;
        str += i + "." + fish.getName() + ":" + rWeight + "Kg,put in basket.\n";
      }
      else {
        str += i + "." + fish.getName() + ":" + rWeight + "Kg,release it.\n";
      }
    }
    while (totalWeight < 15);


    str += "\nTotal weight:" + totalWeight +
        "Kg,bigger than 15Kg,Stop Fishing.\n";
    return str;
  }


  public void writeFile(String str) {
    try {
      FileOutputStream f = new FileOutputStream("basket.dat");
      ObjectOutputStream s = new ObjectOutputStream(f);
      s.writeObject(str);
      s.close();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }


  public static void main(String[] args) {
    Angling angling = new Angling();
    angling.writeFile(angling.doFishing());
  }
}








PrintBasket.java

package UB2000345Ass1;


import JAVA.io.*;


/**
 * <p>Title: Fish</p>
 * <p>Description: Fishing Class</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Longware Studio</p>
 * @author Longware
 * @version 1.0
 */


public class PrintBasket {
  public String strFileName = "basket.dat";


  public PrintBasket() {
  }


  public void readFile() {
    try {
      String text = null;


      FileReader fr = new FileReader(strFileName);
      BufferedReader br = new BufferedReader(fr);
      text = new String();
      while ( (text = br.readLine()) != null) {
        System.out.println(text);
      }


      br.close();
      fr.close();
    }
    catch (IOException e) {
      System.out.println("File Read Error!");
    }
  }


  public static void main(String[] args) {
    PrintBasket printBasket1 = new PrintBasket();
    printBasket1.readFile();
  }


}



[ END ]

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有