2014-07-12 6 views
1

의 값을 변경하려고 시도하지만 출력이 변경되지 않습니다. NetBeans에서이 두 프로그램을 동일한 프로젝트 및 패키지에 저장했습니다. 코드를 너무 길게 만들었 기 때문에 나는 여기에 포함시키지 않았지만 잘 작동하는 접근 자 메서드를 작성했기 때문에 왜 뮤 테이터 메서드가 작동하지 않는지 혼란 스럽습니다.뮤 테이터 메서드가 작동하지 않습니다. NetBeans

클래스 코드 :

package auto; 

public class Auto 
{ 
    private String model; 
    private int milesDriven; 
    private double gallonsOfGas; 

    public Auto(String startModel, 
       int startMilesDriven, 
       double startGallonsOfGas) 
    { 
    model = startModel; 
    setMilesDriven(startMilesDriven); 
    setGallonsOfGas(startGallonsOfGas); 
    } 

    public void setModel(String newModel) 
    { 
     model = newModel; 
    } 

    public void setMilesDriven(int newMilesDriven) 
    { 
     if (newMilesDriven >= 0) 
      milesDriven = newMilesDriven; 
     else 
     { 
      System.err.println("Miles driven cannot be negative."); 
      System.err.println("Value not changed."); 
     } 
    } 

    public void setGallonsOfGas(double newGallonsOfGas) 
    { 
     if (newGallonsOfGas >= 0.0) 
      gallonsOfGas = newGallonsOfGas; 
     else 
     { 
      System.err.println("Gallons of gas cannot be negative."); 
      System.err.println("Value not changed."); 
     } 
    } 
} 

클라이언트 클래스 코드 :

package auto; 

import java.text.DecimalFormat; 

public class AutoClient 
{ 
    public static void main(String [] args) 
    { 
     DecimalFormat milesPattern = new DecimalFormat("#,###"); 

     Auto coupe = new Auto("Corvette", 300000, 0.0); 

     String coupeModel = coupe.getModel(); 
     int coupeMiles = coupe.getMilesDriven(); 
     double coupeGallons = coupe.getGallonsOfGas(); 

     System.out.println("coupe:" 
          + "\nmodel: " + coupeModel 
          + "\nmiles: " + milesPattern.format(coupeMiles) 
          + "\ngallons: " + coupeGallons);  

     coupe.setModel("Viper"); 
     coupe.setMilesDriven(10000); 
     coupe.setGallonsOfGas(50.0); 

     System.out.println("coupe:" 
          + "\nmodel: " + coupeModel 
          + "\nmiles: " + milesPattern.format(coupeMiles) 
          + "\ngallons: " + coupeGallons); 
    } 
} 
+2

Netbeans이 문제와 관련이 있다고 생각하는 이유를 설명 할 수 있습니까? –

+0

원래 파일을 저장 한 방법 (예 : 동일한 패키지에 있는지 여부)에 문제가 있다고 생각했습니다. 나는 IDE와 프로그래밍에 익숙하지 않다. – pez

+2

충분합니다. 그러나 그게 문제 였다면 실행하는 프로그램보다는 컴파일 오류가 발생하지만 잘못된 대답을 줄 수 있습니다. –

답변

3

당신이 가치 당신은 다시

그들을 얻을 필요가
coupe.setModel("Viper"); 
coupe.setMilesDriven(10000); 
coupe.setGallonsOfGas(50.0); 

을 변경 한 후, 현재 코드를 감안할 때
coupeModel = coupe.getModel(); 
coupeMiles = coupe.getMilesDriven(); 
coupeGallons = coupe.getGallonsOfGas(); 

당신이

System.out.println("coupe:" 
         + "\nmodel: " + coupeModel 
         + "\nmiles: " + milesPattern.format(coupeMiles) 
         + "\ngallons: " + coupeGallons); 

를 호출하기 전에 당신이

으로

System.out.println("coupe:" 
      + "\nmodel: " + coupeModel 
      + "\nmiles: " + milesPattern.format(coupeMiles) 
      + "\ngallons: " + coupeGallons); 

Auto를 업데이트하고 (두 곳 모두에서) 당신이 대체 할 수있는 그런 다음 toString()

@Override 
public String toString() { 
    return "coupe:" 
      + "\nmodel: " + coupeModel 
      + "\nmiles: " + milesPattern.format(coupeMiles) 
      + "\ngallons: " + coupeGallons; 
} 

를 추가 제안

System.out.println(coupe); // <-- Java will call toString() for you 
+0

고마워요! 어떻게'toString'을 수정하여 같은 형식을 반환 하겠지만'Auto' 클래스의 다양한 인스턴스 (예 :'sedan'과'suv')를 어디에 호출했는지에 따라 어떻게 할 수 있습니까? – pez

+1

왜 'String vehicleType = "coupe";'필드를 추가하고 "coupe :"를 toString()의'vehicleType + ":"'로 변경하지 않겠습니까? 그런 다음'setVehicleType (String)'뮤 테이터를 가질 수 있습니다. –