I have a program in which I studied and tried out the Observer Pattern.
I wrote it with nano on my terminal, and I saved it like this:
kneipe
(location
(Show.java))
(leute
(Beobachter.java
Comedian.java))
I compiled correctly all of them but it keeps saying that in my main the references to the classes aren't right so it can't make new Object
s from the other classes.
Here is the deal:
I tried it out on eclipse and it works fine with import package2.*;
And all of the tutorials I found online or even in books are only with one package-folder.
So, how do I organise my project and how do I import correctly because I tried (almost) everything and nothing worked so far (I even tried copying the second package into the first, which didn't work either).
ClassA:
package leute;
import java.util.*;
public class Comedian extends Observable {
String name = "";
int numGags = 0;
String joke = "abc";
boolean toldjoke = false;
public Comedian(String name, int numGags) {
this.name = name;
this.numGags = numGags;
}
public void telljoke() {
this.toldjoke = true;
System.out.println(joke);
setChanged();
notifyObservers();
}
private String getname() {
return this.name;
}
private int getnumgags() {
return this.numGags;
}
}
Thats ClassB:
package leute;
import java.util.*;
public class Besucher implements Observer {
String name = "";
int intus = 0;
boolean lacht = false;
public Besucher(String name, int intus) {
this.name = name;
this.intus = intus;
}
private String getname() {
return this.name;
}
public void enought(Besucher a) {
a.lacht = false;
}
public void update(Observable o, Object arg) {
lacht = true;
System.out.println("HAHAHAH!!!");
}
}
Thats the Main:
package location;
import leute.*;
import java.util.*;
public class Show {
public static void main(String args[]) {
Observer franz = new Besucher("Franz", 6);
Observer karl = new Besucher("Karl", 1);
Comedian mark = new Comedian("Mark", 5);
mark.addObserver(franz);
mark.telljoke();
mark.addObserver(karl);
mark.telljoke();
mark.deleteObserver(karl);
mark.deleteObserver(franz);
mark.telljoke();
}
}
As mentioned it works fine on eclipse but the terminal tells me this:
lisaundamelia@lisaundamelia ~/Schreibtisch/Alle Projekte $ javac
kneipe/location/Show.java
kneipe/location/Show.java:3: error: package leute does not exist
import leute.Besucher;
^
kneipe/location/Show.java:4: error: package leute does not exist
import leute.Comedian;
^
kneipe/location/Show.java:10: error: cannot find symbol
Observer franz = new Besucher("Franz", 6);
^
symbol: class Besucher
location: class Show
kneipe/location/Show.java:11: error: cannot find symbol
Observer karl = new Besucher("Karl", 1);
^
symbol: class Besucher
location: class Show
kneipe/location/Show.java:12: error: cannot find symbol
Comedian mark = new Comedian("Mark", 5);
^
symbol: class Comedian
location: class Show
kneipe/location/Show.java:12: error: cannot find symbol
Comedian mark = new Comedian("Mark", 5);
^
symbol: class Comedian
location: class Show
6 errors