public class OldMacDonald
{
public void hadAFarm(Cow moo, Cow moomoo)
throws EIEIOException
{
if (!moo.here() && !moo.there() && !moomoo.everywhere())
{
throw new EIEIOException("Trouble on the farm!");
}
}
public static void main(String args[])
{
// Need some cows that fail.
Cow cow1 = new Cow(false, false, false);
Cow cow2 = new Cow(false, false, false);
OldMacDonald farmer = new OldMacDonald();
try
{
farmer.hadAFarm(cow1, cow2);
}
catch (EIEIOException ex)
{
System.out.println(ex);
ex.printStackTrace();
System.out.println("Better get some pigs.");
}
}
}
// Need a class with those method names!
class Cow
{
private boolean here;
private boolean there;
private boolean everywhere;
public Cow(boolean here, boolean there, boolean everywhere)
{
this.here = here;
this.there = there;
this.everywhere = everywhere;
}
public boolean here()
{
return here;
}
public boolean there()
{
return there;
}
public boolean everywhere()
{
return everywhere;
}
}
EIEIOException: Trouble on the farm! EIEIOException: Trouble on the farm! at java.lang.Throwable.fillInStackTrace(Native Method) at java.lang.Throwable.(Throwable.java:94) at java.lang.Exception. (Exception.java:42) at java.io.IOException. (IOException.java:47) at EIEIOException. (EIEIOException.java:10) at OldMacDonald.hadAFarm(OldMacDonald.java:8) at OldMacDonald.main(OldMacDonald.java:20) Better get some pigs.
--- Output Ends ---
- javac OldMacDonald.java
- java OldMacDonald