Reputation: 33
I'm not sure why Eclipse is giving me this error, I cannot run the exchange method for any of my desired classes:
The method exchange(Currency, double) is undefined for the type Currency
Some of the code methods listed have not been fully implemented due to my issues compiling to begin with.
What simple mistake am I making?
public abstract class Currency {
String currencyName;
double totalFunds;
//Exchange money between planets
public abstract double toEarthDollars(double amount);
public abstract double fromEarthDollars(double EarthDollars);
public static void main(String[] args) {
//TEST INPUT CODE
Currency mars = new Mars(100.00);
Currency neptune = new Neptune(100.00);
Currency saturn = new Saturn(100.00);
System.out.println("----- Exchanges -----");
mars.exchange(saturn,25.0);
neptune.exchange(saturn, 10.0);
saturn.exchange(mars, 122.0);
saturn.exchange(mars, 121.0);
}
}
public interface Exchangeable {
//rates should be encapsulated and accessed from here
double EarthDollar = 1;
double MarsMoney = 1.3;
double SaturnSilver = 0.87;
double NeptuneNuggets = 2;
public void exchange(Exchangeable other, double amount);
}
public class Mars extends Currency implements Exchangeable {
public Mars(double amount) {
currencyName = "MarsMoney";
totalFunds = amount;
}
public double toEarthDollars(double amount) {
return amount * (Exchangeable.EarthDollar/Exchangeable.MarsMoney);
}
public double fromEarthDollars(double EarthDollars) {
return EarthDollars * (Exchangeable.MarsMoney/Exchangeable.EarthDollar);
}
public void exchange(Exchangeable other, double amount) {
System.out.println("Hello");
//double transfer = this.toEarthDollars(amount);
//transfer = ((Mars) other).fromEarthDollars(transfer);
}
}
Upvotes: 3
Views: 347
Reputation: 169
I think your Currency
class does not know a method of exchange
only Exchangeable
has that.
So you might want to cast:
Currency mars = new Mars(100.00);
Exchangeable marsEx = (Exchangeable)mars;
Exchangeable saturnEx = (Exchangeable)saturn;
marsEx.exchange(saturnEx,25.0);
You also might want to check if that Currency
can be casted to Exchangeable
as maybe not all currencies are exchangeable.
Also you might need to cast saturn, as Currency
can not be passed to the exchange method.
Upvotes: 2
Reputation: 201447
Your abstract Currency
class does not implement the Exchangeable
interface. Change
public abstract class Currency {
to
public abstract class Currency implements Exchangeable {
Upvotes: 4