Так что я пытаюсь понять, почему, когда я пытаюсь отредактировать метод логических равных, так что у него есть this.dollars == o.dollars, он дает мне код ошибки во второй переменной доллара в коде и говорит, что он не может найти переменная? Я знаю, что кроме этого есть много других ошибок.Не удается найти переменную ошибку?
Кроме того, для тех, кто готов ответить, для чего нужны методы getMoney и setMoney? Зачем мне это нужно, а не только доллары и центы?
public class Money
{
private int dollars;
private int cents;
private double money;
public Money (int dol) {
this.dollars = dol;
cents = 0;
if (dol < 0) {
throw new IllegalArgumentException ("Must be greater than 0.");
}
}
public Money (int dol, int cent) {
dol = this.dollars;
cent = this.cents;
if (dol < 0 || cent < 0) {
throw new IllegalArgumentException ("Must be greater than 0.");
}
}
public Money (Money other) {
this.dollars = other.dollars;
this.cents = other.cents;
this.money = other.money;
}
public int getDollars() {
return dollars;
}
public int getCents() {
return cents;
}
public void setMoney (int dollars, int cents) {
dollars = this.dollars;
cents = this.cents;
if (dollars < 0 || cents < 0) {
throw new IllegalArgumentException ("Must be greater than 0.");
}
if (cents > 100) {
int c = cents/100;
int m = dollars + c;
}
}
public double getMoney() {
return money;
}
public void add (int dollars) {
if (dollars < 0) {
throw new IllegalArgumentException ("Must be greater than 0.");
}
}
public void add (int dollars, int cents) {
if (dollars < 0 || cents < 0) {
throw new IllegalArgumentException ("Must be greater than 0.");
}
}
public void add (Money other) {
}
public boolean equals (Object o) {
}
public String toString() {
String c = String.format("%.02d",cents);
return "$" + dollars + "." + c;
}
}
Я не могу найти this.dollars == o.dollars в вашем вставленном коде, вставьте правильный код –
Извините, я взял его, потому что он не компилировался, когда он был там. Он должен быть общедоступным логическим равным (Object o) { return (this.dollars == o.dollars) && (this.cents = - o.cents); } –