This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
My friend and I (and may I stress that we are beginners) have been trying to complete this assignment but don't know how to fix the run-time errors for the following code. Error message received: The class CyberPet does not exist Any input would be greatly appreciated. FYI - we are using JBuilder5. ************************************************************ CyberPet code: public class CyberPet { private boolean isEating = true; // CyberPet's state private boolean isSleeping = false; private boolean isThinking = false; private String name = "no name"; // CyberPet's name public CyberPet (String str) // Constructor method { name = str; } public void setName (String str) // Access method { name = str; } // setName() public String getName() { return name; // Return CyberPet's name } // getName() public void eat() // Start eating { isEating = true; // Change the state isSleeping = false; isThinking = false; return; } // eat() public void sleep() // Start sleeping { isSleeping = true; // Change the state isEating = false; isThinking = false; return; } // sleep() public void think() // Start thinking { isThinking = true; // Change the state isSleeping = false; isEating = false; return; } // think() public String getState () { if (isEating) return "Eating"; // Exit the method if (isSleeping) return "Sleeping"; // Exit the method if (isThinking) return "Thinking"; // Exit the method return "Error in State"; // Exit the method } // getState() public String toString() { return name + " is " + getState(); } } // CyberPet ******************************************************** CyberPetApplet code: import java.applet.*; import java.awt.*; import java.awt.event.*; public class CyberPetApplet extends Applet implements ActionListener { // Declare instance variables private CyberPet pet1; // The CyberPet private Label nameLabel; // A Label private TextField stateField; // A TextField private Button eatButton, sleepButton, thinkButton; // Three Buttons private Image eatImg, sleepImg, thinkImg; /* * The init() method instantiates the instance variables, including both the * CyberPet (pet1) and the GUI elements that are displayed on the applet. */ public void init() { System.out.println("In the init() method"); // INSTANTIATE THE INSTANCE VARIABLES. THIS // CREATES BOTH THE CyberPet, pet1, AND THE // GUI ELEMENTS THAT ARE DISPLAYED ON THE APPLET pet1 = new CyberPet("Socrates"); // CyberPet // Create the GUI components nameLabel = new Label("Hi! My name is " + pet1.getName() + " and currently I am : "); stateField = new TextField(12); eatButton = new Button("Eat!"); // Buttons eatButton.addActionListener(this); // Assign the listeners. sleepButton = new Button("Sleep!"); sleepButton.addActionListener(this); thinkButton = new Button("Thinking!"); thinkButton.addActionListener(this); eatImg = getImage( getDocumentBase(), "spidereat.gif" ); sleepImg = getImage( getDocumentBase(), "spidersleep.gif" ); thinkImg = getImage( getDocumentBase(), "spiderthink.gif" ); // Initialize the TextField stateField.setText(pet1.getState()); stateField.setEditable(false); // Add the components to the applet. add(nameLabel); add(stateField); add(eatButton); add(sleepButton); add(thinkButton); setSize(300,150); // Set the applet's size to 300 x 150 pixels } // init /* * The actionPerformed() method is called whenever * one of the buttons is pressed. */ public void paint( Graphics g ) { String str = pet1.getState(); if (str.equals("Eating")) g.drawImage(eatImg,20,100,this); else if (str.equals("Sleeping")) g.drawImage(sleepImg,20,100,this); else if (str.equals("Thinking")) g.drawImage(thinkImg,20,100,this); else System.out.println("Error"); } public void actionPerformed( ActionEvent e) { System.out.println("In the actionPerformed Method"); if (e.getSource() == eatButton) pet1.eat(); else if (e.getSource() == sleepButton) pet1.sleep(); else if (e.getSource() == thinkButton) pet1.think(); stateField.setText(pet1.getState()); repaint(); // call the paint() method }//actionPerformed } // CyberPetApplet
Manfred Leonhardt
Ranch Hand
Joined: Jan 09, 2001
Posts: 1492
posted
0
Hi Mirta, There doesn't seem to be anything wrong with your code. I couldn't see the images because they don't exist on my site. I was able to generate a simple HTML file to run the code. The only line that was required is: < APPLET CODE="CyberPetApplet" WIDTH=800 HEIGHT=600 >< /APPLET > without the spaces after and before the brackets. (Those are required to show you what it looks like.) Make sure you compile both CyberPet and CyberPetApplet java files into class files and place them into the same directory as the HTML file that contains the line above. Regards, Manfred.