麻烦写数组

时间:2013-05-02 00:13:02

标签: java

我正在尝试编写一个数组。其描述最好用这个描述:

  

每天渔民最多可以称重10条鱼,其重量需要以一系列双重值存储。

这是我到目前为止所做的:

import java.util.*;
import java.text.*;
public class DailyCatch
{
    private int fishermanID, fisherID;
    private String dateOfSample, date;
    private double[] weights;
    private double[] fishCaught = new double[10];
    private int currWeight = 0;

public DailyCatch (int fishermanID, String dateOfSample, String weightsAsString)
{
    fisherID = fishermanID;
    date = dateOfSample;
    // Parse the the weigths string and store the list of weights in this array.
    weights = readWeights(weightsAsString);
}

 public DailyCatch (int fishermanID, String dateOfSample)
 {
    fisherID = fishermanID;
    date = dateOfSample;
 }

 public void addFish(double weight)
 {
    if (currWeight > 10)
    {
     // array full
 }
    else
{
  fishCaught[currWeight] = weight;
  currWeight += 1;  // update current index of array
    }
 }

private double[] readWeights(String weightsAsString) 
{
    String[] weightsArr = weightsAsString.split("\\s+");
    double[] weights = new double[weightsArr.length];

    for (int i = 0; i <  weights.length; i++) {
        double weight = Double.parseDouble(weightsArr[i]);
    }
    return weights;
}

 public void printWeights()
 {
    for (int i = 0; i < fishCaught.length; i++)
  {
    System.out.println(fishCaught[i]);
    } 
 }

public String toString()
{
    return "Fisherman ID: " + fisherID + "\nDate: " + date + "\nWeights: " + Arrays.toString(weights);
}

}

这是我在这个项目上使用的测试文件:

public class BigBass
{
public static void main (String[]args)
{
DailyCatch monday1 = new DailyCatch(32, "4/1/2013", "4.1 5.5 2.3 0.5 4.8 1.5");
System.out.println(monday1);

DailyCatch monday2 = new DailyCatch(44, "4/1/2013");
monday2.addFish(2.1);
monday2.addFish(4.2);
System.out.println(monday2);
}
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

public class DailyCatch
{
    private int fishermanID, fisherID;
    private String dateOfSample, date;
    private double[] weights;

    public DailyCatch (int fishermanID, String dateOfSample, String weightsAsString)
    {
        fisherID = fishermanID;
        date = dateOfSample;
        // Parse the string and store the list of weights in this array.
        weights = readWeights(weightsAsString);
    }

    private double[] readWeights(String weightsAsString) 
    {
        String[] weightsArr = weightsAsString.split("\s+");
        double[] weights = new double[weightsArr.length];

        for (int i = 0; i <  weights.length; i++) {
            double weight = Double.parseDouble(weightsArr[i]);
        }
        return weights;
    }

    public String toString()
    {
        return "Fisherman ID: " + fisherID + "\nDate: " + date + "\nweights: " + Arrays.toString(weights);
    }
}

答案 1 :(得分:0)

我更新了重载的构造函数以减少代码重复。构造函数能够调用自己,当多个构造函数设置相同的成员变量时,它就会出现。

我添加了addFish方法(如测试中所示),它只是根据数组的当前索引(从0开始)将权重添加到数组中。每当添加新的鱼体重时,currWeight会增加,以便下一条鱼可以在下一个指数处添加。

readWeights方法将传递的字符串解析为由空格分隔的标记。使用addFish方法将每个标记放入类的fishCaught数组中。

public class DailyCatch
{
  private int fishermanID, fisherID;
  private String dateOfSample, date;
  private double[] fishCaught = new double[10];
  private int currWeight = 0;

  public DailyCatch() {  }

  public DailyCatch (int fishermanID, String dateOfSample)
  {
    fisherID = fishermanID;
    date = dateOfSample;
  }

  public DailyCatch (int fishermanID, String dateOfSample, String weight)
  {
    this(fishermanID, dateOfSample);
    readWeights(weight);
  }

  public void addFish(double weight)
  {
    if (currWeight > 10)
    {
       // array full
    }
    else
    {
      fishCaught[currWeight] = weight;
      currWeight += 1;  // update current index of array
    }
  }

  private void readWeights(String weightsAsString) 
  {
    String[] weightsRead = weightsAsString.split("\\s+");
    for (int i = 0; i < weightsRead.length; i++) 
    {
       this.addFish(Double.parseDouble(weightsRead[i]));
    }
  }

  public String toString()
  {
    return "Fisherman ID: " + fisherID + "\nDate:" + date + "\nWeights: " + Arrays.toString(fishCaught);
  }

  public void printWeights()
  {
     for (int i = 0; i < fishCaught.length; i++)
     {
          System.out.println(fishCaught[i]);
     } 
  }
}