I'm attempting to write an array. Its description is best described by this:
Each day a fisherman will weigh in at most 10 fish, the weight of which you are required to store in an array of double values.
This is what I have so far:
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);
}
}
This is the test file I'm working with on this project:
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);
}
}
Any help is greatly appreciated.