-3

im relatively new to java since im attending my first year at university. currently we are doing OOP in class and i have the following problem:

i am creating a "Train", but when i try to compile it , it gives the following error: https://i.gyazo.com/2d4f3ccc68f45419a9439ab9adb1a499.png[1]

which is really confusing to me because i have tried to run the main method in eclipse's console and it ran just fine.

my waggon class:

    package sheet08;



//a)
public class Waggon {
    private int SitzGesamt, SitzReserviert, SitzFrei ;
    private int Klasse;
    private String Doppelwagen;
    private int WCbesetzt, WCfrei , WCdefekt;
    private String WaggonHinten;





//b)
//Getter & Setter


public int getSitzGesamt() {
    return SitzGesamt;
}

public void setSitzGesamt(int sitzGesamt) {
    SitzGesamt = sitzGesamt;
}

public int getSitzReserviert() {
    return SitzReserviert;
}

public void setSitzReserviert(int sitzReserviert) {
    SitzReserviert = sitzReserviert;
}

public int getSitzFrei() {
    return SitzFrei;
}

public void setSitzFrei(int sitzFrei) {
    SitzFrei = sitzFrei;
}

public int getKlasse() {
    return Klasse;
}

public void setKlasse(int klasse) {
    Klasse = klasse;
}

public String getDoppelwagen() {
    return Doppelwagen;
}

public void setDoppelwagen(String doppelwagen) {
    Doppelwagen = doppelwagen;
}

public int getWCbesetzt() {
    return WCbesetzt;
}

public void setWCbesetzt(int wCbesetzt) {
    WCbesetzt = wCbesetzt;
}

public int getWCfrei() {
    return WCfrei;
}

public void setWCfrei(int wCfrei) {
    WCfrei = wCfrei;
}

public int getWCdefekt() {
    return WCdefekt;
}

public void setWCdefekt(int wCdefekt) {
    WCdefekt = wCdefekt;
}

public String getWaggonHinten() {
    return WaggonHinten;
}

public void setWaggonHinten(String waggonHinten) {
    WaggonHinten = waggonHinten;
}

//default Constructor
public Waggon() {

}
//Constructor
public Waggon(int sitzGesamt, int sitzReserviert, int sitzFrei, int klasse, String doppelwagen, int wCbesetzt,
        int wCfrei, int wCdefekt, String waggonHinten) {
    super();
    SitzGesamt = sitzGesamt;
    SitzReserviert = sitzReserviert;
    SitzFrei = sitzFrei;
    Klasse = klasse;
    Doppelwagen = doppelwagen;
    WCbesetzt = wCbesetzt;
    WCfrei = wCfrei;
    WCdefekt = wCdefekt;
    WaggonHinten = waggonHinten;
}

// e)
public String toString(){
    String strSitzGesamt = String.valueOf(SitzGesamt);
    String strSitzReserviert = String.valueOf(SitzReserviert);
    String strSitzFrei = String.valueOf(SitzFrei);
    String strKlasse = String.valueOf(Klasse);
    String strWCbesetzt = String.valueOf(WCbesetzt);
    String strWCfrei = String.valueOf(WCfrei);
    String strWCdefekt = String.valueOf(WCdefekt);

    return strSitzGesamt + "-" + strSitzReserviert + "-" + strSitzFrei + "-" + strKlasse + "-" + Doppelwagen + "-" + strWCbesetzt + "-" + 
            strWCfrei + "-" + strWCdefekt + WaggonHinten;

}


}

my "Train" class:

 package sheet08;

    public class Train {

        int Baureihe;
        String Antriebsart;
        int PS;
        int Höchstgeschwindigkeit;
        int WaggonDahinter;




        //Getter&Setter
        public int getBaureihe() {
            return Baureihe;
        }
        public void setBaureihe(int baureihe) {
            Baureihe = baureihe; //this.baurihe = baureihe
        }
        public String getAntriebsart() {
            return Antriebsart;
        }
        public void setAntriebsart(String antriebsart) {
            Antriebsart = antriebsart;
        }
        public int getPS() {
            return PS;
        }
        public void setPS(int pS) {
            PS = pS;
        }
        public int getHöchstgeschwindigkeit() {
            return Höchstgeschwindigkeit;
        }
        public void setHöchstgeschwindigkeit(int höchstgeschwindigkeit) {
            Höchstgeschwindigkeit = höchstgeschwindigkeit;
        }


        public int getWaggonDahinter() {
            return WaggonDahinter;
        }
        public void setWaggonDahinter(int waggonDahinter) {
            WaggonDahinter = waggonDahinter;
        }


        //default Constructor
        public Train() {
        }


        //Constructor
        public Train(int baureihe, String antriebsart, int pS, int höchstgeschwindigkeit, int waggonDahinter) {
            super();
            Baureihe = baureihe;
            Antriebsart = antriebsart;
            PS = pS;
            Höchstgeschwindigkeit = höchstgeschwindigkeit;

        }

    //e)

        public String toString(){
            String strBaureihe = String.valueOf(Baureihe);
            String strPS = String.valueOf(PS);
            String strHöchstgeschwindigkeit = String.valueOf(Höchstgeschwindigkeit);

            return strBaureihe + "-" + Antriebsart + "-" + strPS + "-" + strHöchstgeschwindigkeit;
        }

    }

and the main method itself :

package sheet08;

import java.util.Random;

public class Test {

    public static void main( String[] args ) {
        Random WCkaputt = new Random();
        int WCdefekt = WCkaputt.nextInt(100)+1;
        System.out.println("Die Toilette ist zu " + WCdefekt + " % kaputt");

        Train t1 = new Train (412, "elektrisch", 13500, 250, 1); 
        Waggon w1 = new Waggon(50, 24, 3, 1, "doppelstock", 0, 0, 0, "1 Waggon dahinter"); //keine Angabe über die WC-Anzahl
        Waggon w2 = new Waggon(100, 12, 64, 2, "doppelstock", 0, 0, 0, "1 Waggon dahinter");
        Waggon w3 = new Waggon(100, 32, 11, 2, "doppelstock", 0, 0, 0, "1 Waggon dahinter");
        Waggon w4 = new Waggon(50, 17, 3, 1, "doppelstock", 0, 0, 0, " kein Waggon dahinter");

        System.out.println(t1);
        System.out.println(w1);
        System.out.println(w2);
        System.out.println(w3);
        System.out.println(w4);

    //cannot find symbol Train & Waggon 
        //Variable als Train1 im typ train abspeichern



}
}

i compiled both the waggon and train class, got no errors there and i simply cant figure out why my main method doesnt find the symbol. id appreciate any tips as im stuck at this error since yesterday!

  • It showing error because you haven't imported Train and Wagon class . Important both class your code will compile – Ganesh Gudghe Dec 07 '19 at 12:11
  • Try compiling from the `src` folder instead; Java is looking for the packages in subfolders and it cannot find them because of that. – Maarten Bodewes Dec 07 '19 at 12:16
  • i have thought about it , but i thought i didnt have to import both classes since they were in the same package. i guess i was wrong –  Dec 07 '19 at 12:23
  • And you weren't wrong, the others are. You need to compile from the `src` folder. – Maarten Bodewes Dec 07 '19 at 12:51

2 Answers2

0

You forgot the import statement in the class that contains main method.

import sheet08.Train;
import sheet08.Waggon;
  • All classes are in the same package in above question. So you created an answer from a comment by somebody else without looking to the question, did you not? – Maarten Bodewes Dec 07 '19 at 12:51
0

From your screenshot, it looks like you have the three source files in the directory 'sheet08', and they are all in the package 'sheet08'. But your compile command is also being run from the same directory.

You need to "cd .." and compile from the parent directory, "src".

Your code will then run using "java sheet08.Test"

Think of it this way: the package (and directory) are a relative path from where you are running the java commands. When you identify a file as in a package, Java expects that file to be in a relative path from it equivalent to that package: in your case, your files are in package "sheet08", and so the relative path to them must be a directory "sheet08/".

This is why, when running the code, you need to identify the package: just as when referring to the file, you have to identify which directory it is in.