Methods 创建构造函数
我有一些代码需要帮助。我是AP CS的学生(这是介绍性的),所以请不要评判我Methods 创建构造函数,methods,constructor,Methods,Constructor,我有一些代码需要帮助。我是AP CS的学生(这是介绍性的),所以请不要评判我 // name: // // program: CoinsTester // // purpose: public class CoinsTester { public static void main(String[] args) { //create a new Coins object named money and pass i
// 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 ******
}
}
事情是这样的,我为我不正确的代码格式道歉。所以当我运行它时,它说Coins money=newcoins()找不到代码的构造函数。我需要帮助创建一个合适的对象。这里的问题是,我必须为“CoinsTester”创建一个对象,然后它告诉我我没有链接到该对象的构造函数。我现在真的找不到解决办法。有人能告诉我如何为CoinsTester类创建构造函数吗 添加一个名为CoinsTester的“方法”(如果您需要类CoinsTester的构造函数,您的Q不清楚您需要什么构造函数)。如果希望使用添加的显式构造函数,请确保参数与调用序列相对应。有两行需要检查:
Coins();
这将调用名为Coins的方法,而不是构造函数。我建议你删除这个,因为你很可能不会使用/需要它
Coins money = new Coins();
没有与此链接的构造函数的原因是您已经有了硬币的构造函数:
public Coins(int change) {
myChange = change;
}
创建此构造函数时,删除了默认构造函数newcoins()。如果您希望能够在没有参数的情况下使用Coins构造函数,可以再次声明它。下面是一个例子:
public Coins() {
myChange = 0;
}
我该如何再次申报?用铸币厂还是用硬币?是否只是重新声明了“public Coins(){myChange=0;}”您将在public类Coins中声明public Coins()构造函数。然后,在铸币商的主要方法中,您只需使用硬币money=newcoins();线路。