2013-09-04 2 views
-1

도움이 필요한 코드가 있습니다. 나는 AP CS 학생이며 입문자이기 때문에 저를 판단하지 마십시오.생성자 만들기

// name: 
    // 
    // program: CoinsTester 
    // 
    // purpose: 

    public class CoinsTester { 



     public static void main(String[] args) { 
    //create a new Coins object named money and pass it the amount of cents as a parameter 
    //***** STUDENTS NEED TO COMPLETE ****** 
     Coins(); 
     Coins money = new Coins(); 

     // call the correct method in the Coins class to find your answer 
    //***** STUDENTS NEED TO COMPLETE ****** 
     money.calculate(); 


    } 

} 



// name: 
// 
// program: CoinsTester 
// 
// purpose: This class accepts a certain number of monetary change. 
//   The output is a list of the number of quarters, dimes, nickels, 
// and pennies that will make that amount of change with the least 
// number of coins possible. This is a skeleton that will be finished 
//  by the students 

    public class Coins { 

//private variable to store the only attribute of a "Coins" object - how many cents the 
// user wants to find change for. 
private int myChange; 

//constructor that accepts an initial value for the private data member 
public Coins(int change) { 
    myChange = change; 
} 

    // the method calculate will 
    // 1. use modular and regular division to determine the quantity of each type of coin 
    // 2. prints the quantity of the coins needed to make the entered amount 
     public void calculate(){ 
     int quarters=25, dimes=10, nickels=5, pennies=1; 
     int temp1, temp2, temp3, temp4; 
     int remainquar1, remaindime2, remainnick3, remainpenn4; 

     //variable declarations to hold the values needed for different coin types 
     // make sure you use descriptive identifiers! 
     //***** STUDENTS NEED TO COMPLETE ****** 

     // calculations for the various coin types 
     //***** STUDENTS NEED TO COMPLETE ****** 


     // output statements, formatted as shown on specs 
     //***** STUDENTS NEED TO COMPLETE ****** 

     } 

    } 

여기 내 잘못 지정된 형식의 코드에 대해 사과드립니다. 그래서 나는 그것을 실행할 때 동전 돈 = 새로운 동전()이 코드의 생성자를 찾을 수 없다고 말합니다. 나는 적절한 물건을 만드는 데 도움이 필요하다. 여기서 "CoinsTester"개체를 만들어야한다는 것은 객체에 연결된 생성자가 없다는 것을 말해줍니다. 나는 지금 당장 해결책을 찾을 수 없다. 누군가가 CoinsTester 클래스의 생성자를 만드는 방법에 대한 팁을 주시겠습니까?

답변

0

CoinsTester라는 이름의 "메소드"를 추가하십시오 (CoinsTester 클래스의 생성자가 필요한 경우 Q에서 필요한 생성자가 명확하지 않음). 추가 된 명시 적 생성자를 사용하려면 인수가 호출 순서와 일치하는지 확인하십시오.

Coins(); 

이 아닌 생성자, 메소드 이름 동전을 부를 것이다 :

0

당신은 검토 할 필요가 두 줄이있다. 나는 당신이 그것을 사용하지 않거나 필요로하지 않기 때문에 이것을 제거하는 것을 sugggest합니다.

Coins money = new Coins(); 

이 링크 생성자가없는 이유는 이미 동전 생성자를 가지고있다 :이 생성자를 생성

public Coins(int change) { 
    myChange = change; 
} 

, 당신은) (새 동전 기본 생성자를 제거;. 매개 변수없이 Coins 생성자를 사용하려면 다시 선언 할 수 있습니다. 다음은 그 예입니다.

public Coins() { 
    myChange = 0; 
} 
+0

어떻게 다시 선언하겠습니까? CoinsTester 또는 동전에서만? "Public Coins() { myChange = 0; }" – allenlistar

+0

공용 클래스 동전에 공용 Coins() 생성자를 선언합니다. 그런 다음 CoinsTester의 Main 메서드에서 동전 돈 = 새로운 Coins()를 사용하면됩니다. 선. – miguelarcilla