2013-05-02 2 views
0

배열을 쓰려고합니다. 그 설명은 가장이에 의해 설명 :트러블 쓰는 배열

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); 
} 

}

이 나는이 프로젝트에 함께 일하고 있어요 테스트 파일 :

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.

이것은 내가 지금까지 무엇을 가지고

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); 
} 
} 

도움을 주시면 대단히 감사하겠습니다.

+0

먼저 기본 생성자가 필요합니다. 그리고 그것은 물고기 배열을 정의하는 것 같습니다. – David

+0

예. 하지만 그것은 이중을 요구하고 문자열을 발견하면 오류가 발생합니다. –

+0

그의 물고기에 대한 double 값의 배열을 할당하는 남자를 가르치면 그는 일생 동안 먹을 것이다. – Patashu

답변

0

코드 중복을 줄이기 위해 오버로드 된 생성자를 업데이트했습니다. 생성자는 자신을 호출 할 수 있습니다. 여러 생성자가 동일한 멤버 변수를 설정하면 생성자가 직접 호출 할 수 있습니다.

배열의 현재 색인 (0에서 시작)을 기반으로 배열에 가중치를 단순히 추가하는 addFish 메서드를 추가했습니다. 새로운 물고기 무게가 추가 될 때마다 다음 물고기가 다음 색인에 추가 될 수 있도록 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]); 
    } 
    } 
} 
+0

도움에 감사드립니다. 내가 왜 배열을 인쇄 할 수 없는지 아십니까? –

+0

@DevonFreese 수업 내외부에서 인쇄하고 싶습니까? –

+0

거기에 toString을 사용하여 인쇄하고 싶습니다. 그러나 나는 그 일을 얻었다. 그러나, 선언 된 가중치를 인쇄하는 대신 "0.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); 
    } 
} 
+0

"\ s +"와 관련이있는 불법 탈출 문자가 있다는 에러가 난다 –

+0

나는 분명히이 형태로이 대답을 upvoting하지 않습니다. 코드가 무엇인지 또는 어떻게 사용하는지 설명하지 않고 코드를 덤프하면됩니다. – Doorknob

+0

@DevonFreese 대신' "\\ s +"'를 사용하십시오 – Doorknob