John Smith
John Smith

Reputation: 6259

Link to another java class

I have three classes in one folder:

Waehrung same as currency in Englisch!

public abstract class Waehrung {
    public abstract double dollarBetrag();
}

USDollar

public class UsDollar extends Waehrung {
    private double wert;

    public UsDollar(double wert){
        this.wert = wert;
    }

    public double dollarBetrag(){
        return wert;
    }
}

Yen

public class Yen extends Waehrung{

double wert;
private static double kurs;

public Yen(double wert){
    this.wert = wert;
}

public void setKurs(double kurs){
    this.kurs = kurs;
}

public double dollarBetrag(){
    return wert * kurs;
}

}

Now i worte another java Class that should make use of the others: Bank

public class Bank {
    public static void main(String[] args){
        Yen yen = new Yen(34);
        Yen.setKurse = (1.0/130);
        System.out.println(yen.dollarBetrag);
    }
}

My problem is that i do not understand how to link against the other classes! I made some tries with import package but it never compiled correctly! What do i wrong or how can i fix this!

Upvotes: 0

Views: 220

Answers (3)

JavaTechnical
JavaTechnical

Reputation: 9357

It seems that you haven't put your classes in any package. Look here.

com.currency.Waehrung

package com.currency;
public abstract class Waehrung {
    public abstract double dollarBetrag();
}

com.currency.USDollar class

package com.currency;
public class UsDollar extends Waehrung {
    private double wert;

    public UsDollar(double wert){
        this.wert = wert;
    }

    public double dollarBetrag(){
        return wert;
    }
}

com.currency.Yen

package com.currency;
public class Yen extends Waehrung{

double wert;
private static double kurs;

public Yen(double wert){
    this.wert = wert;
}

public void setKurs(double kurs){
    this.kurs = kurs;
}

public double dollarBetrag(){
    return wert * kurs;
}
}

Now you've put all those classes in com.currency package. That means you make sure that those class files (.class) files exist in the com\currency folder.

Now, you can write the class

import com.currency.*;
public class Bank {
    public static void main(String[] args){
        Yen yen = new Yen(34);
        yen.setKurs(1.0/130);
        System.out.println(yen.dollarBetrag());
    }
}

Now you can directly use these classes. Make sure that the folders com\currency exist.

The errors you've written in Bank class are:

Yen.setKurse = (1.0/130);
System.out.println(yen.dollarBetrag);

In the first line, you used Yen which is class name. You should use yen which is reference because setKurs() is a method. Another error in the same line is that setKurs() is the method name and not an instance variable. So you should call setKurs() also, setKurse is wrong 'e' is a typo.

Next, dollarBetrag is a method, so you should yen.dollarBetrag()

Thank you :)

Upvotes: 2

Girish
Girish

Reputation: 1717

If the class are not in the same package then you have to use import statements should be like that

import yourpackageNameofYen.Yen;
import yourpackageNameofUsDollar.USDollar;

you have to put these two import statements below the package declaration of your third class Bank

for example like

Package a;
import yourpackageNameofYen.Yen;
    import yourpackageNameofUsDollar.USDollar;
public class Bank {
        public static void main(String[] args){
            Yen yen = new Yen(34);
            Yen.setKurse = (1.0/130);
            System.out.println(yen.dollarBetrag);
        }
    }

Upvotes: 0

Rogue
Rogue

Reputation: 11483

If you want to import them all:

import com.yourpackage.money.*;

If you mean keeping some form of reference to them all, I usually store them in a Map or Array. Helps having a toString implementation or some way to get the money's name for this storage.

Map<String, Waehrung> currency = new HashMap<>();

Waehrung[] money = new Waehrung[]{
    new Yen(),
    new Dollar()
    // etc...
}

for (Waehrung m : money) {
    currency.put(m.getName(), m);
}

Upvotes: 0

Related Questions