this is the oracle GUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class OracleGUI extends JFrame implements ActionListener
{
// Define constants
public static final String GET_QUESTION =
"I will answer any question. Let me know yours.\n" +
"Type your question in the following and hit Submit.";
public static final String GET_ADVICE =
"That's a difficult question. \n" +
"I need some advice from you first.\n";
public static final String GIVE_ADVICE =
"Here is my advice to your problem.\n" +
"Hope it helps.\n" +
"Hit Reset button to ask another question.";
public static final String BT_SUBMIT = "Submit";
public static final String BT_RESET = "Reset";
public static final int CHAR_PER_LINE = 40;
public static final int LINES_USER_INPUT = 10;
public static final int LINES_PROMPT = 5;
public static final int STAGE_GET_QUESTION = 0;
public static final int STAGE_GET_ADVICE = 1;
public static final int STAGE_GIVE_ADVICE = 2;
// GUI components
private JTextArea prompt;
private JTextArea userInput;
private JButton btSubmit;
private JButton btReset;
//
private int stage;
private Oracle ora;
public OracleGUI ()
{
// Create the oracle to be used
ora = new Oracle ();
// Set window size, location and title
setSize (400, 250);
setLocation (250, 250);
setTitle ("I'm the Oracle");
// Create all components here
Container contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
// Add prompt label; set its background color, and font
prompt = new JTextArea (LINES_PROMPT, CHAR_PER_LINE);
prompt.setText (GET_QUESTION);
prompt.setBackground (contentPane.getBackground ());
prompt.setEditable (false);
Font oldFont = prompt.getFont ();
prompt.setFont (new Font (oldFont.getFontName (), Font.BOLD, oldFont.getSize ()));
contentPane.add (prompt, BorderLayout.NORTH);
// Add user input textarea
userInput = new JTextArea (LINES_USER_INPUT, CHAR_PER_LINE);
contentPane.add (userInput, BorderLayout.CENTER);
// Add a panel to hold two buttons
JPanel btPane = new JPanel ();
contentPane.add (btPane, BorderLayout.SOUTH);
btPane.setLayout (new FlowLayout ());
btSubmit = new JButton (BT_SUBMIT);
btSubmit.addActionListener (this);
btPane.add (btSubmit);
btReset = new JButton (BT_RESET);
btReset.addActionListener (this);
btPane.add (btReset);
// Make everything visible
setVisible (true);
}
public void actionPerformed (ActionEvent e)
{
String actionCommand = e.getActionCommand ();
if (actionCommand.equals ("Submit"))
{
switch (stage)
{
case STAGE_GET_QUESTION: //user submitted a question
updateForAdvice ();
break;
case STAGE_GET_ADVICE: // user submitted an advice
giveAdvice ();
break;
default:
break;
}
}
else if (actionCommand.equals ("Reset"))
reset ();
}
/*
* Main method to start the window
*/
public static void main (String[] args)
{
OracleGUI oraGUI = new OracleGUI ();
oraGUI.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
/*
* Private helper methods
*/
private void updateForAdvice ()
{
String userQ = userInput.getText ().trim ();
if (userQ.length () == 0)
{
JOptionPane.showMessageDialog (null,
"You need to tell me more about your problem\n"+
"for me to give you any help");
return;
}
// Change the prompt message
prompt.setText (GET_ADVICE);
// Clear the user-input textarea
userInput.setText ("");
stage ++;
}
private void giveAdvice ()
{
String userAdvice = userInput.getText ().trim ();
if (userAdvice.length () == 0)
{
// Tell the user to enter something
JOptionPane.showMessageDialog (null,
"Nothing is free.\n"+
"You need to give me some help so that I can help you.");
return;
}
// Retrive an advice from the oracle
String oraAdvice = ora.getAdvice ();
// Add user's advice to oracle
ora.addAdvice (userAdvice);
// Update GUI
prompt.setText (GIVE_ADVICE);
userInput.setText (oraAdvice);
btSubmit.setEnabled (false);
stage ++;
}
private void reset ()
{
// Reset stage
stage = STAGE_GET_QUESTION;
// Reset prompt message
prompt.setText (GET_QUESTION);
// Clear user-input textarea
userInput.setText ("");
// Reset button status
btSubmit.setEnabled (true);
}
}
then heres the oracle (code) which i have to fix...
/**
* Oracle.java: An Oracle object keeps a list of advices.
* It can learn a new advice by adding it to its list,
* and it can give advice by retrieving one randomly from its list.
*/
public class Oracle
{
/**
* Adds a new piece of advice to its list
*/
public void addAdvice (String advice)
{
// This is a stub. Nothing is done here
return;
}
/**
* Retrieves randomly a piece of advice from its list
*/
public String getAdvice ()
{
// This is a stub. It returns a fixed advice
return "Keep on searching, and you'll find the answer.";
}
/**
* Returns a random number in the range of 0 - (range-1)
*/
private static int getRandomIndex (int range)
{
return ((int) (Math.random () * range));
}
}
and this is the intruction...
"Implement the Oracle class. More specifically, you need to write the following three: a default constructor, method addAdvice () and method getAdvice ().
Use an array to keep a list of advices. Make the size of the array 5 (define a named constant for this number). In the default constructor of the Oracle class, initialize the oracle with 2 pieces of advices of your choice.
"
ok..thank you for reading this far...
im not asking anyone of you to do my homework, i want to do it my self..but i need advice where to start, and how to start..
im stuck fromt he begginning which is bad...
if i get a start i think i should be ok...i may ask questtions and all...but it would be great if i get help.
thank you very much.
any help (sites) or anything will be greatly appriciated...