2016-10-26 4 views
0

Так что я пытаюсь понять, почему, когда я пытаюсь отредактировать метод логических равных, так что у него есть 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; 
} 

}

+0

Я не могу найти this.dollars == o.dollars в вашем вставленном коде, вставьте правильный код –

+0

Извините, я взял его, потому что он не компилировался, когда он был там. Он должен быть общедоступным логическим равным (Object o) { return (this.dollars == o.dollars) && (this.cents = - o.cents); } –

ответ

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 (int dol, int cent) { 
    this.dollars = dol; 
    this.cents = cent; 
    if (dol < 0 || cent < 0) { 
     throw new IllegalArgumentException ("Must be greater than 0."); 
    } 
} 

также для public void setMoney (int dollars, int cents) {

Что касаетсяequals

В equalso объект должен быть проверен, чтобы увидеть, если это Money объект с помощью instanceof и если это так, то может быть отлиты как деньги объекта

if(o instanceof Money) { 
    return this.dollars == ((Money)o).dollars && this.cents == ((Money)o).cents; // etc 
} 
+0

Я попытался изменить их вокруг, и это не повлияло; Что касается экземпляра, это было бы так? public boolean equals (Object o) { if (o instanceof Money) { Деньги, которые = (Деньги) o; return this.dollars == that.dollars && this.cents == that.cents) } return false; } –

+0

Да, это почти правильно. удалите ')' в '== that.cents)}' –

+0

Он все еще говорит, что он не может найти переменные доллары! –

Смежные вопросы