-
displaying System out in an applet
Hi,
I have created a java application and need to display the output it in an
applet - is there an easy way to do this? (or any suggestions would be
appreciated) I have included the main class file below:
Thanks in advance,
Tom.
public class Personi
{
static int numberOfSandwichesEaten;
static int wallet = 400;
public static void main(String[] args)
{
Personi sandwichMan = new Personi();
SandwichMachine theMachine = new SandwichMachine();
SandwichMachine.setPrices(120,140);
SandwichMachine.setCreditLimit(wallet);
sandwichMan.makeInitialSandwiches(theMachine);
numberOfSandwichesEaten = sandwichMan.checkForSale(theMachine);
// The prices have been set and the machine has been filled
showContents(sandwichMan, theMachine);
Personi hungryStudent = new Personi();
printThis ("\nA student has just walked up to the machine......");
printThis ("He wants a cheese sandwich and has the correct change (140
pence)");
pauseForAShortMoment();
hungryStudent.buySandwich("cheese", 140, theMachine);
// A new person, hungryStudent has been created and he has purchased a
cheese sandwich (he had the right money)
showContents(sandwichMan, theMachine);
printThis ("\nThe sandwich was nice, but it didn't satisfy the hungry
student......");
printThis ("He wants another cheese sandwich, but, this time only puts
in 135 pence");
pauseForAShortMoment();
hungryStudent.buySandwich("cheese", 135, theMachine);
// The student has just put in too little money for the sandwich, the
error is displayed, and the money returned.
showContents(sandwichMan, theMachine);
printThis ("\nThe student then remembers he needs to buy 3 egg
sandwiches for his friends,");
printThis ("as well as the one cheese sandwich that he wanted");
printThis ("He puts in 3 lots of 120 pence and selects Egg for his
friends' sandwiches");
printThis ("and 140 pence for his cheese sandwich.....");
pauseForAShortMoment();
hungryStudent.buySandwich("egg", 120, theMachine);
pauseForAShortMoment();
hungryStudent.buySandwich("egg", 120, theMachine);
pauseForAShortMoment();
hungryStudent.buySandwich("egg", 120, theMachine);
pauseForAShortMoment();
hungryStudent.buySandwich("cheese", 140, theMachine);
// 3 egg sandwiches are bought, and one cheese exact money given, no
change needed
showContents(sandwichMan, theMachine);
printThis ("\nThat Student must have a serious eating disorder......");
printThis ("He wants another egg sandwich for himself, and puts in a two
pound coin");
pauseForAShortMoment();
hungryStudent.buySandwich("egg", 200, theMachine);
// The student has just put in too much money for the egg sandwich, so
his change is returned.
showContents(sandwichMan, theMachine);
printThis ("\nWho'd believe it, the greedy student is back for another
egg......");
printThis ("He puts in the correct change, but .... there's no egg left
in the machine ....");
pauseForAShortMoment();
hungryStudent.buySandwich("egg", 120, theMachine);
showContents(sandwichMan, theMachine);
}
public void makeInitialSandwiches(SandwichMachine machine)
{
machine.fillMachine();
}
public static void printThis(String output)
{
System.out.println(output);
}
// this method produces all output for the program
public static int showNumberOfCheese(SandwichMachine machine)
{
return machine.checkCheeseLeft();
}
public static int showNumberOfEgg(SandwichMachine machine)
{
return machine.checkEggLeft();
}
public static int showMoneyLeft(SandwichMachine machine)
{
return machine.checkMoneyLeft();
}
public static int checkForSale(SandwichMachine machine)
{
return machine.howManyBought();
}
public static void buySandwich (String flavour, int money, SandwichMachine
machine)
{
machine.dispenseSandwich (flavour, money);
}
public static void showContents(Personi tempBloke, SandwichMachine
tempMachine)
{
tempBloke.printThis("\nLets check the machine, what is in it at the
moment?....");
tempBloke.printThis(showNumberOfCheese(tempMachine)+ " Cheese Sandwiches
left in the Machine");
tempBloke.printThis(showNumberOfEgg(tempMachine)+ " Egg Sandwiches left
in the Machine");
tempBloke.printThis("Therefore, the total number of sandwiches in the
machine is " + (showNumberOfCheese(tempMachine) +
showNumberOfEgg(tempMachine) + " sandwiches"));
tempBloke.printThis("and .....");
tempBloke.printThis("There is "+ showMoneyLeft(tempMachine) + " Pence
left in the Machine");
numberOfSandwichesEaten = checkForSale(tempMachine);
tempBloke.printThis(numberOfSandwichesEaten + " Sandwiches have been
eaten since it was last filled");
}
}// end of class
-
Re: displaying System out in an applet
Tom,
Sorry I haven't been able to answer your other question.
If you are running your applet from the command line - it will go to your
command window.
If you are running your applet in IE with the default JVM (Bad thing) go
to View/Java Console and it will show up in the console. You might have
to go to internet options to get it to show up in the menu.
If you are using the Java Plugin, go to Start/Settings/Control Panel/Java
Plugin. On the Basic tab select Show Java Console.
Mark
"Tom" <tomee@bigfoot.com> wrote:
>Hi,
>
>I have created a java application and need to display the output it in an
>applet - is there an easy way to do this? (or any suggestions would be
>appreciated) I have included the main class file below:
>
>Thanks in advance,
>
>Tom.
>
>
>public class Personi
>{
>static int numberOfSandwichesEaten;
>static int wallet = 400;
>
> public static void main(String[] args)
> {
> Personi sandwichMan = new Personi();
> SandwichMachine theMachine = new SandwichMachine();
> SandwichMachine.setPrices(120,140);
> SandwichMachine.setCreditLimit(wallet);
> sandwichMan.makeInitialSandwiches(theMachine);
> numberOfSandwichesEaten = sandwichMan.checkForSale(theMachine);
> // The prices have been set and the machine has been filled
>
> showContents(sandwichMan, theMachine);
>
> Personi hungryStudent = new Personi();
> printThis ("\nA student has just walked up to the machine......");
> printThis ("He wants a cheese sandwich and has the correct change (140
>pence)");
> pauseForAShortMoment();
> hungryStudent.buySandwich("cheese", 140, theMachine);
> // A new person, hungryStudent has been created and he has purchased
a
>cheese sandwich (he had the right money)
>
> showContents(sandwichMan, theMachine);
>
> printThis ("\nThe sandwich was nice, but it didn't satisfy the hungry
>student......");
> printThis ("He wants another cheese sandwich, but, this time only puts
>in 135 pence");
> pauseForAShortMoment();
> hungryStudent.buySandwich("cheese", 135, theMachine);
> // The student has just put in too little money for the sandwich, the
>error is displayed, and the money returned.
>
>showContents(sandwichMan, theMachine);
>
> printThis ("\nThe student then remembers he needs to buy 3 egg
>sandwiches for his friends,");
> printThis ("as well as the one cheese sandwich that he wanted");
> printThis ("He puts in 3 lots of 120 pence and selects Egg for his
>friends' sandwiches");
> printThis ("and 140 pence for his cheese sandwich.....");
> pauseForAShortMoment();
> hungryStudent.buySandwich("egg", 120, theMachine);
> pauseForAShortMoment();
> hungryStudent.buySandwich("egg", 120, theMachine);
> pauseForAShortMoment();
> hungryStudent.buySandwich("egg", 120, theMachine);
> pauseForAShortMoment();
> hungryStudent.buySandwich("cheese", 140, theMachine);
> // 3 egg sandwiches are bought, and one cheese exact money given, no
>change needed
>
>showContents(sandwichMan, theMachine);
>
> printThis ("\nThat Student must have a serious eating disorder......");
> printThis ("He wants another egg sandwich for himself, and puts in a
two
>pound coin");
> pauseForAShortMoment();
> hungryStudent.buySandwich("egg", 200, theMachine);
> // The student has just put in too much money for the egg sandwich,
so
>his change is returned.
>
> showContents(sandwichMan, theMachine);
>
> printThis ("\nWho'd believe it, the greedy student is back for another
>egg......");
> printThis ("He puts in the correct change, but .... there's no egg left
>in the machine ....");
> pauseForAShortMoment();
> hungryStudent.buySandwich("egg", 120, theMachine);
>
> showContents(sandwichMan, theMachine);
>
> }
>
> public void makeInitialSandwiches(SandwichMachine machine)
> {
> machine.fillMachine();
> }
>
> public static void printThis(String output)
> {
> System.out.println(output);
> }
>// this method produces all output for the program
>
> public static int showNumberOfCheese(SandwichMachine machine)
> {
> return machine.checkCheeseLeft();
> }
>
> public static int showNumberOfEgg(SandwichMachine machine)
> {
> return machine.checkEggLeft();
> }
>
> public static int showMoneyLeft(SandwichMachine machine)
> {
> return machine.checkMoneyLeft();
> }
>
> public static int checkForSale(SandwichMachine machine)
> {
> return machine.howManyBought();
> }
>
> public static void buySandwich (String flavour, int money, SandwichMachine
>machine)
> {
> machine.dispenseSandwich (flavour, money);
> }
>
> public static void showContents(Personi tempBloke, SandwichMachine
>tempMachine)
>{
> tempBloke.printThis("\nLets check the machine, what is in it at the
>moment?....");
> tempBloke.printThis(showNumberOfCheese(tempMachine)+ " Cheese Sandwiches
>left in the Machine");
> tempBloke.printThis(showNumberOfEgg(tempMachine)+ " Egg Sandwiches left
>in the Machine");
> tempBloke.printThis("Therefore, the total number of sandwiches in the
>machine is " + (showNumberOfCheese(tempMachine) +
>showNumberOfEgg(tempMachine) + " sandwiches"));
> tempBloke.printThis("and .....");
> tempBloke.printThis("There is "+ showMoneyLeft(tempMachine) + " Pence
>left in the Machine");
> numberOfSandwichesEaten = checkForSale(tempMachine);
> tempBloke.printThis(numberOfSandwichesEaten + " Sandwiches have been
>eaten since it was last filled");
>}
>
>}// end of class
>
>
>
-
Re: displaying System out in an applet
add the JTextArea in your applet to display the message
"Tom" <tomee@bigfoot.com> ¼¶¼g©ó¶l¥ó news:3cb162ec@10.1.10.29...
> Hi,
>
> I have created a java application and need to display the output it in an
> applet - is there an easy way to do this? (or any suggestions would be
> appreciated) I have included the main class file below:
>
> Thanks in advance,
>
> Tom.
>
>
> public class Personi
> {
> static int numberOfSandwichesEaten;
> static int wallet = 400;
>
> public static void main(String[] args)
> {
> Personi sandwichMan = new Personi();
> SandwichMachine theMachine = new SandwichMachine();
> SandwichMachine.setPrices(120,140);
> SandwichMachine.setCreditLimit(wallet);
> sandwichMan.makeInitialSandwiches(theMachine);
> numberOfSandwichesEaten = sandwichMan.checkForSale(theMachine);
> // The prices have been set and the machine has been filled
>
> showContents(sandwichMan, theMachine);
>
> Personi hungryStudent = new Personi();
> printThis ("\nA student has just walked up to the machine......");
> printThis ("He wants a cheese sandwich and has the correct change (140
> pence)");
> pauseForAShortMoment();
> hungryStudent.buySandwich("cheese", 140, theMachine);
> // A new person, hungryStudent has been created and he has purchased a
> cheese sandwich (he had the right money)
>
> showContents(sandwichMan, theMachine);
>
> printThis ("\nThe sandwich was nice, but it didn't satisfy the hungry
> student......");
> printThis ("He wants another cheese sandwich, but, this time only puts
> in 135 pence");
> pauseForAShortMoment();
> hungryStudent.buySandwich("cheese", 135, theMachine);
> // The student has just put in too little money for the sandwich, the
> error is displayed, and the money returned.
>
> showContents(sandwichMan, theMachine);
>
> printThis ("\nThe student then remembers he needs to buy 3 egg
> sandwiches for his friends,");
> printThis ("as well as the one cheese sandwich that he wanted");
> printThis ("He puts in 3 lots of 120 pence and selects Egg for his
> friends' sandwiches");
> printThis ("and 140 pence for his cheese sandwich.....");
> pauseForAShortMoment();
> hungryStudent.buySandwich("egg", 120, theMachine);
> pauseForAShortMoment();
> hungryStudent.buySandwich("egg", 120, theMachine);
> pauseForAShortMoment();
> hungryStudent.buySandwich("egg", 120, theMachine);
> pauseForAShortMoment();
> hungryStudent.buySandwich("cheese", 140, theMachine);
> // 3 egg sandwiches are bought, and one cheese exact money given, no
> change needed
>
> showContents(sandwichMan, theMachine);
>
> printThis ("\nThat Student must have a serious eating
disorder......");
> printThis ("He wants another egg sandwich for himself, and puts in a
two
> pound coin");
> pauseForAShortMoment();
> hungryStudent.buySandwich("egg", 200, theMachine);
> // The student has just put in too much money for the egg sandwich, so
> his change is returned.
>
> showContents(sandwichMan, theMachine);
>
> printThis ("\nWho'd believe it, the greedy student is back for another
> egg......");
> printThis ("He puts in the correct change, but .... there's no egg
left
> in the machine ....");
> pauseForAShortMoment();
> hungryStudent.buySandwich("egg", 120, theMachine);
>
> showContents(sandwichMan, theMachine);
>
> }
>
> public void makeInitialSandwiches(SandwichMachine machine)
> {
> machine.fillMachine();
> }
>
> public static void printThis(String output)
> {
> System.out.println(output);
> }
> // this method produces all output for the program
>
> public static int showNumberOfCheese(SandwichMachine machine)
> {
> return machine.checkCheeseLeft();
> }
>
> public static int showNumberOfEgg(SandwichMachine machine)
> {
> return machine.checkEggLeft();
> }
>
> public static int showMoneyLeft(SandwichMachine machine)
> {
> return machine.checkMoneyLeft();
> }
>
> public static int checkForSale(SandwichMachine machine)
> {
> return machine.howManyBought();
> }
>
> public static void buySandwich (String flavour, int money,
SandwichMachine
> machine)
> {
> machine.dispenseSandwich (flavour, money);
> }
>
> public static void showContents(Personi tempBloke, SandwichMachine
> tempMachine)
> {
> tempBloke.printThis("\nLets check the machine, what is in it at the
> moment?....");
> tempBloke.printThis(showNumberOfCheese(tempMachine)+ " Cheese
Sandwiches
> left in the Machine");
> tempBloke.printThis(showNumberOfEgg(tempMachine)+ " Egg Sandwiches
left
> in the Machine");
> tempBloke.printThis("Therefore, the total number of sandwiches in the
> machine is " + (showNumberOfCheese(tempMachine) +
> showNumberOfEgg(tempMachine) + " sandwiches"));
> tempBloke.printThis("and .....");
> tempBloke.printThis("There is "+ showMoneyLeft(tempMachine) + " Pence
> left in the Machine");
> numberOfSandwichesEaten = checkForSale(tempMachine);
> tempBloke.printThis(numberOfSandwichesEaten + " Sandwiches have been
> eaten since it was last filled");
> }
>
> }// end of class
>
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks