2017-03-31 16 views
0

그래서 나는이 프로젝트를 얼마 동안 작업 해 왔으며 어떤 이유로 든 Java를 잡는 데 많은 어려움을 겪고 있습니다.Java 컴파일러 (접근 자, 돌연변이 유발 자, 생성자)

목표는 각각 3 개의 과일을 포함하는 3 개의 개체를 만들고 각 과일마다 가격/가치가 있습니다.

현재 값을 더하는 데 어려움이 있습니다. 지금까지 자바에 많은 문제가 있다는 말처럼 훨씬 더 잘못되었습니다.

가장 큰 문제는 현재 costofBox()입니다.

도움이되는 제안 사항에 대해서는 매우 감사 드리며, 일주일 넘게이 작업을 진행하고 있습니다.

public class Project8 
{ 

private String fruit1; 
private String fruit2; 
private String fruit3; 
private String Bundle1; 
private String Bundle2; 
private String Bundle3; 
private int costofBox; 
double total; 
int broccoli; 
int tomato; 
int kiwi; 
int kale; 
int orange; 

public String toString() 
{ 
    String output = "The box contains: " + Bundle1 + ", " + Bundle2 + ", " + Bundle3 + 
    "and the cost is $" + costofBox(); 
    return output; 
} 

public String getBundle1() 
{ 
    return Bundle1; 
} 
public String getBundle2() 
{ 
    return Bundle2; 
} 
public String getBundle3() 
{ 
    return Bundle3; 
} 


public void setBundle1(String Bundle1) 
{ 
    Bundle1=fruit1; 
} 
public void setBundle2(String Bundle2) 
{ 
    Bundle2=fruit2; 
} 
public void setBundle3(String Bundle3) 
{ 
    Bundle3=fruit3; 
} 

public double costofBox() 
{ 
    double total=0; 
    if(Bundle1.equals("broccoli")) 
     total+=6; 
    else if(Bundle1.equals("tomato")) 
     total+=5; 
    else if(Bundle1.equals("kiwi")) 
     total+=8; 
    else if(Bundle1.equals("kale")) 
     total+=4; 
    else if(Bundle1.equals("orange")) 
     total+=7; 
    if(Bundle2.equals("broccoli")) 
     total+=6; 
    else if(Bundle2.equals("tomato")) 
     total+=5; 
    else if(Bundle2.equals("kiwi")) 
     total+=8; 
    else if(Bundle2.equals("kale")) 
     total+=4; 
    else if(Bundle2.equals("orange")) 
     total+=7; 
    if(Bundle3.equals("broccoli")) 
     total+=6; 
    else if(Bundle3.equals("tomato")) 
     total+=5; 
    else if(Bundle3.equals("kiwi")) 
     total+=8; 
    else if(Bundle3.equals("kale")) 
     total+=4; 
    else if(Bundle3.equals("orange")) 
     total+=7; 

    return total; 
} 

public Project8() 
{  
    fruit1 = "broccoli" + "kale" + "orange"; 
    fruit2 = "kale" + "kiwi" + "orange"; 
    fruit3 = "broccoli" + "tomato" + "kiwi"; 
} 

public Project8(String fruit1, String fruit2, String fruit3) 
{ 
    String Bundle1=fruit1; 
    String Bundle2=fruit2; 
    String Bundle3=fruit3; 
} 

public static void main (String [] args) 
{ 
    Project8 Bundle1=new Project8 ("broccoli", "kale", "orange"); 
    Project8 Bundle2=new Project8 ("kale", "kiwi", "orange"); 
    Project8 Bundle3=new Project8 ("broccoli", "tomato", "kiwi"); 



    System.out.println("Week 1: " + Bundle1.toString()); 
    System.out.println("Week 2: " + Bundle2.toString()); 
    System.out.println("Week 3: " + Bundle3.toString()); 
    System.out.println("Week4: The box contains:,, and the cost is $0.0"); 
    } 
} 

가 나를 도울 수있는 당신의 사람들을 위해 시간을 미리 감사 :

여기에 전체 프로그램입니다!

+0

'Map '을 사용하여 각 항목의 가격을 표현하는 것이 더 좋을 것 같습니다. –

+0

'for'loop과'while'loop을보아야합니다 – jhamon

+0

'Bundle1'과 같은 변수는 항상 소문자로 시작해야합니다. 이것을 강제하는 것은 없지만 코드를 더 쉽게 이해할 수 있도록 널리 사용되는 규칙입니다. 마찬가지로 수업은 대문자로 시작해야합니다. – Michael

답변

3

귀하의 문제는이 생성자에 있습니다

때문에 이러한 과제 앞에있는 String 형의
public Project8(String fruit1, String fruit2, String fruit3) 
{ 
    String Bundle1=fruit1; 
    String Bundle2=fruit2; 
    String Bundle3=fruit3; 
} 

, 당신이 선언되어 새로운 지역 변수! 즉, 클래스 필드는 다음과 같습니다.

private String Bundle1; 
private String Bundle2; 
private String Bundle3; 

...이 값은 제공되지 않습니다. 액세스하려고하면 NULL이기 때문에보고 있던 예외가 발생합니다. 당신이 생성자를 변경하는 경우

:

public Project8(String fruit1, String fruit2, String fruit3) 
{ 
    Bundle1 = fruit1; 
    Bundle2 = fruit2; 
    Bundle3 = fruit3; 
} 

다음 프로젝트가 제대로 실행됩니다.


제쳐두고, 프로그램의 길이를 줄이고, 간결하게 만들고, 적은 것을 반복 할 수있는 많은 방법이 있습니다. StackOverflow의 자매 사이트 인 Code Review으로 넘어 가면 개선 할 제안을 해줄 것입니다. 그렇게하기로 결정했다면이 답변에 대한 의견을 나에게 남겨주세요!

+0

생성자가 모두 틀렸고, 이미 문자열 묶음을 만들었습니다. ***를 사용하여 가변 문자열을 전달해야합니다. this.Bundle1 = fruit1 *** –

+0

무엇을합니까? 평균? 이름 충돌이 없으므로'this '가 필요하지 않습니다. 게다가, 나는 컴파일하고 제안 된 변경 사항과 그것을 실행하고 제대로 작동합니다. – Michael

+0

사실이긴하지만 this.bundle을 사용하면 올바른 것입니다. 나는 그 정확한 해결책 때문에 아직도 upvoted했다 –