AI robot


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: AI robot

  1. #1
    Join Date
    Dec 2005
    Posts
    11

    AI robot

    hi,
    I am making a website. I need to put a java ai robot on the website. I am very new to this so i have a few questions.

    1) does this robot need to be an applet or can i just put a java program on a website?
    2) does anyone have a link to an already made java ai robot, so i don't need to start all over and can just edit it as i need to?

    thanks for all the help in advance

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    Has to be an applet to run from a webpage. What do you want the bot to do?

  3. #3
    Join Date
    Dec 2005
    Posts
    11
    I am making a tech support site, i want it to be able to talk to users and answer their questions with AI. I have made one in past but i made a java program, i have never made a java applet. Can you help me get started?

  4. #4
    Join Date
    Dec 2005
    Posts
    11
    update:
    here is a simple porgram that i put together, i need someone to make this exact program, wont take long, i made it as small as i could. I need someone to turn this into an applet. If you can post the code for the applet i can then get the idea and make my own applet that's very long and well made.

    code:
    Code:
    public class New_bot
    {
        public static void main(String[] args)
        {
                EasyReader input=new EasyReader();
                String question;
                           
               {
               System.out.println("Hi, I am the P.A.I.B.(Plaine Artificial Intellegence Bot!)");
               System.out.println("What can I do for you today?");
               question = input.readLine();
                
                question=question.toLowerCase(); //all lower case
                
                if (question.indexOf("hi")!=-1) 
                System.out.println("Hello!");
                
                else if (question.indexOf("hello")!=-1)
                System.out.println("Hi!");
                
                
                else
                        System.out.println("I don't know. I am still learning. Try some other question.");
                
              }
    
            }
        }

  5. #5
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Quote Originally Posted by linexpert
    update:
    here is a simple porgram that i put together, i need someone to make this exact program, wont take long, i made it as small as i could. I need someone to turn this into an applet. If you can post the code for the applet i can then get the idea and make my own applet that's very long and well made.
    Here is a link to a tutorial that will explain exactly how to create an applet:
    http://java.sun.com/docs/books/tutorial/applet/

    Good luck.

  6. #6
    Join Date
    Dec 2005
    Posts
    11
    thanks for the help. I have one problem,that link stopped working.

  7. #7
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Quote Originally Posted by linexpert
    thanks for the help. I have one problem,that link stopped working.
    Ok, then I'll post the 2nd hit from Google's search for you:
    http://www.realapplets.com/tutorial/

  8. #8
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Here ya go... the code is heavily commented to help you ( which it hopefully will ). Oh and by the way, I did this in AWT. It's up to you if you want to use AWT or Swing.
    Code:
    /**
     * Robot.java
     *
     * Robot that responds to questions
     *
     * @version	  1.0 18 Dec 2005
     * @author 	  destin / linexpert
     */
    
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class Robot extends Applet implements ActionListener {
    	/** questionField will be the user input */
    	TextField questionField;
    	
    	/** 
    	 * askQuestion will be a button the users hits 
    	 * to submit the question
    	 */
    	Button askQuestion;
    	
    	/** answer label will be the computer's response */
    	Label answerLabel;
    	
    	/** question will be the contents of questionField */
    	String question;
    	
    	/** initializing */
    	public void init() {
    		/** set initial size */
    		setSize( 400, 350 ); // x:400 y:350
    		
    		/** instantiate objects */
    		questionField = new TextField( 10 ); // create a TextField with 10 columns
    								  // (a column is an average character width)
    		askQuestion = new Button( "Ask" ); // creates a Button with a label "Ask"
    		answerLabel = new Label( "Hi, I am the P.A.I.B. (Plaine Artificial Intellegence Bot!)\n"
    			+ "What can I do for you today?" ); // creates a Label with initial text
    		
    		/** add the objects to the component */
    		add( answerLabel ); // adds answerLabel to the component
    		add( askQuestion ); // adds askQuestion to the component
    		add( questionField ); // adds questionField to the component
    		
    		/** 
    		 * add an ActionListener to the askQuestion button
    		 * this will "listen" to see if it is clicked
    		 */
    		askQuestion.addActionListener( this );
    	}
    	
    	/** paint the component */
    	public void paint( Graphics g ) {
    		/** 
    		 * set the location of the objects 
    		 * ( you will probably want to change the location to fit your preference )
    		 */
    		answerLabel.setLocation(5, 5); // x:5 y:5
    		askQuestion.setLocation(5, 50); // x:5 y:50
    		questionField.setLocation(5, 100); // x:5 y:100
    		
    		/** set question to the lowercase String value of the questionField */
    		question = questionField.getText().toLowerCase();
    	}
    	
    	/**
    	 * this will only execute when askQuestion is clicked
    	 * because of askQuestion.addActionListener( this ); in init()
    	 *
    	 * this method is required since we are implementing the ActionListener
    	 * interface
    	 */
    	public void actionPerformed( ActionEvent ae ) {
    		/** 
    		 * check to see if the button they clicked has the same memory location
    		 * as askQuestion
    		 */
    		if ((( Button ) ( ae.getSource() )).equals( askQuestion )) {
    			/** parse for Strings */
    			if ( question.indexOf( "hi" ) != -1 ) {
    				answerLabel.setText( "Hello!" );			
    			} else if ( question.indexOf( "hello" ) != -1 ) {
    				answerLabel.setText( "Hi!" );		
    			} else {
    				answerLabel.setText( "I don't know. I am still learning.\n" +
    					"Try some other question." );			
    			}
    			/**
    			 * this will repaint the component by
    			 * calling the paint( Graphics ) method
    			 *
    			 * we don't actually need to do this in this program but
    			 * I threw it in so that if you end up adding something
    			 * that you will need to repaint the component, you'll know how
    			 */
    			repaint();
    		}
    	}
    }
    Feel free to ask anymore questions.
    Last edited by destin; 12-25-2005 at 10:37 AM.

  9. #9
    Join Date
    Dec 2005
    Posts
    11
    thanks

    This is an awsome christmas gift.

    thank you very very much

  10. #10
    Join Date
    Dec 2005
    Posts
    11
    Quote Originally Posted by destin
    Here ya go... the code is heavily commented to help you ( which it hopefully will ). Oh and by the way, I did this in AWT. It's up to you if you want to use AWT or Swing.
    Code:
    /**
     * Robot.java
     *
     * Robot that responds to questions
     *
     * @version	  1.0 18 Dec 2005
     * @author 	  destin / linexpert
     */
    
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class Robot extends Applet implements ActionListener {
    	/** questionField will be the user input */
    	TextField questionField;
    	
    	/** 
    	 * askQuestion will be a button the users hits 
    	 * to submit the question
    	 */
    	Button askQuestion;
    	
    	/** answer label will be the computer's response */
    	Label answerLabel;
    	
    	/** question will be the contents of questionField */
    	String question;
    	
    	/** initializing */
    	public void init() {
    		/** set initial size */
    		setSize( 400, 350 ); // x:400 y:350
    		
    		/** instantiate objects */
    		questionField = new TextField( 10 ); // create a TextField with 10 columns
    								  // (a column is an average character width)
    		askQuestion = new Button( "Ask" ); // creates a Button with a label "Ask"
    		answerLabel = new Label( "Hi, I am the P.A.I.B. (Plaine Artificial Intellegence Bot!)\n"
    			+ "What can I do for you today?" ); // creates a Label with initial text
    		
    		/** add the objects to the component */
    		add( answerLabel ); // adds answerLabel to the component
    		add( askQuestion ); // adds askQuestion to the component
    		add( questionField ); // adds questionField to the component
    		
    		/** 
    		 * add an ActionListener to the askQuestion button
    		 * this will "listen" to see if it is clicked
    		 */
    		askQuestion.addActionListener( this );
    	}
    	
    	/** paint the component */
    	public void paint( Graphics g ) {
    		/** 
    		 * set the location of the objects 
    		 * ( you will probably want to change the location to fit your preference )
    		 */
    		answerLabel.setLocation(5, 5); // x:5 y:5
    		askQuestion.setLocation(5, 50); // x:5 y:50
    		questionField.setLocation(5, 100); // x:5 y:100
    		
    		/** set question to the lowercase String value of the questionField */
    		question = questionField.getText().toLowerCase();
    	}
    	
    	/**
    	 * this will only execute when askQuestion is clicked
    	 * because of askQuestion.addActionListener( this ); in init()
    	 *
    	 * this method is required since we are implementing the ActionListener
    	 * interface
    	 */
    	public void actionPerformed( ActionEvent ae ) {
    		/** 
    		 * check to see if the button they clicked has the same memory location
    		 * as askQuestion
    		 */
    		if ((( Button ) ( ae.getSource() )).equals( askQuestion )) {
    			/** parse for Strings */
    			if ( question.indexOf( "hi" ) != -1 ) {
    				answerLabel.setText( "Hello!" );			
    			} else if ( question.indexOf( "hello" ) != -1 ) {
    				answerLabel.setText( "Hi!" );		
    			} else {
    				answerLabel.setText( "I don't know. I am still learning.\n" +
    					"Try some other question." );			
    			}
    			/**
    			 * this will repaint the component by
    			 * calling the paint( Graphics ) method
    			 *
    			 * we don't actually need to do this in this program but
    			 * I threw it in so that if you end up adding something
    			 * that you will need to repaint the component, you'll know how
    			 */
    			repaint();
    		}
    	}
    }
    Feel free to ask anymore questions.
    I have three questions:
    1) how do i change the size of the text input box? In the code it only has the location (x,y) for the bar but no size.
    2) There is one error in the code. I see the error but don't know how i can solve it. The error is when we run the program and let's say we type in "hi" and click "ask" it says "I don't know. I am still learning. Try some other question." and when you click it one more time it says what it's supposed to "hi". How can i fix this so i don't have to click twice and shows me the correct response the first time. I don't see why we have to click twice.
    3) If i want to add a line of code so that insted of clicking "ask" the user can just press enter, what do i have to do?


    thank you very very very much

  11. #11
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    1.
    /** instantiate objects */
    questionField = new TextField( 10 ); // create a TextField with 10 columns
    // (a column is an average character width

    Just change the contructor's argument to a greater amount.
    questionField = new TextField( columns );
    where columns is the approx. amount of characters you want the textfield to hold.

    If you wanted to make it longer as well as wider, you should use a TextArea instead of a TextField.

    2. ahh... this is because i got the value of the TextField in the paint method, which isn't called until after it does repaint(); in the actionPerformed( ActionEvent ) method (fixed code will be posted with the answer to question 3)

    3. You'll need to add a KeyListener. here's the code for this:
    Code:
    /**
     * Robot.java
     *
     * Robot that responds to questions
     *
     * @version	  1.0 Dec 2005
     * @author 	  destin / linexpert
     */
    
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    /** 
     * we need to implement ActionListener for the Ask button and
     * KeyListener for when they hit enter 
     */
    public class Robot extends Applet implements ActionListener, KeyListener {
    	/** questionField will be the user input */
    	TextField questionField;
    	
    	/** 
    	 * askQuestion will be a button the users hits 
    	 * to submit the question
    	 */
    	Button askQuestion;
    	
    	/** answer label will be the computer's response */
    	Label answerLabel;
    	
    	/** question will be the contents of questionField */
    	String question;
    
    	/** initializing */
    	public void init() {
    		/** set initial size */
    		setSize( 400, 350 ); // x:400 y:350
    		
    		/** instantiate objects */
    		questionField = new TextField( 20 ); // create a TextField with 20 columns
    										   // (a column is an average character width)
    		askQuestion = new Button( "Ask" ); // creates a Button with a label "Ask"
    		answerLabel = new Label( "Hi, I am the P.A.I.B. (Plaine Artificial Intellegence Bot!)\n"
    			+ "What can I do for you today?" ); // creates a Label with initial text
    		
    		/** add the objects to the component */
    		add( answerLabel ); // adds answerLabel to the component
    		add( askQuestion ); // adds askQuestion to the component
    		add( questionField ); // adds questionField to the component
    		
    		/** 
    		 * add an ActionListener to the askQuestion button
    		 * this will "listen" to see if it is clicked
    		 */
    		askQuestion.addActionListener( this );
    		
    		
    		/** 
    		 * add a KeyListener to the questionField textfield
    		 * this will "listen" for keys being pressed
    		 */
    		questionField.addKeyListener( this );
    	}
    	
    	/** paint the container */
    	public void paint( Graphics g ) {
    		/** 
    		 * set the location of the objects 
    		 * ( you will probably want to change the location to fit your preference )
    		 */
    		answerLabel.setLocation( 5, 5 ); // x:5 y:5
    		askQuestion.setLocation( 5, 50 ); // x:5 y:50
    		questionField.setLocation( 5, 100 ); // x:5 y:100
    		
    		/** set question to the lowercase String value of the questionField */
    		question = questionField.getText().toLowerCase();
    	}
    	
    	
    	/** 
    	 * added this method since we will have two instances when
    	 * we will be recieving a question ( the button and when they
    	 * hit enter
         */
    	public void answerQuestion() {
    		/** set question to the lowercase String value of the questionField */
    		question = questionField.getText().toLowerCase();
    		
    		/** parse for Strings */
    		if ( question.indexOf( "hi" ) != -1 ) {
    			answerLabel.setText( "Hello!" );			
    		} else if ( question.indexOf( "hello" ) != -1 ) {
    			answerLabel.setText( "Hi!" );		
    		} else {
    			answerLabel.setText( "I don't know. I am still learning.\n" +
    								 "Try some other question." );			
    		}
    		/**
    		* this will repaint the component by
    		 * calling the paint(Graphics) method
    		 *
    		 * we don't actually need to do this in this program but
    		 * I threw it in so that if you end up adding something
    		 * that you will need to repaint the component, you'll know how
    		 */
    		repaint();
    	}
    	
    	/**
    	 * this will only execute when askQuestion is clicked
    	 * because of askQuestion.addActionListener( this ); in init()
    	 *
    	 * this method is required since we are implementing the ActionListener
    	 * interface
    	 */
    	public void actionPerformed( ActionEvent ae ) {
    		/** 
    		 * check to see if the button they clicked has the same memory location
    		 * as askQuestion
    		 */
    		if ((( Button ) ( ae.getSource() )).equals( askQuestion )) {
    			answerQuestion();
    		}
    	}
    	
    	/**
    	 * these following methods will only execute when a key is pressed
    	 * because of questionField.addKeyListener( this ); in init()
    	 *
    	 * these methods are required since we are implementing the KeyListener
    	 * interface
    	 */
    	public void keyPressed( KeyEvent ke ) {
    		/** 
    		 * the following code is the same as:
    		 * if ( ke.getKeyCode == KeyEvent.VK_ENTER ) {
    		 *     answerQuestion();
    		 * }
    		 * i just prefer switch-case statements.. seems more organized
    		 */
    		switch ( ke.getKeyCode() ) {
    		/** 
    		 * VK_ENTER is a static final int in the KeyEvent class 
    		 * it's value is 10 ( which is the number that is read
    		 * when you press enter )
    		 * So you could really just do case 10: but then no one
    		 * would have any idea what that's doing ( which is the
    		 * point of final variables =P )
    		 */
    		case KeyEvent.VK_ENTER:
    			answerQuestion();
    			break;
    		/** default is just like else */
    		default:
    			break;
    		}
    	}
    	
    	/** required by the interface, but not needed */
    	public void keyTyped( KeyEvent ke ) { }
    	
    	/** required by the interface, but not needed */
    	public void keyReleased( KeyEvent ke ) { }
    }
    The new stuff is in bold.
    Last edited by destin; 01-05-2006 at 05:29 PM.

  12. #12
    Join Date
    Dec 2005
    Posts
    11
    thank you very much

  13. #13
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    anytime

  14. #14
    Join Date
    Dec 2005
    Posts
    11
    I need your help with one more thing...
    How do i clear the text box everytime they press enter or click 'ask'?

    thank you

  15. #15
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Code:
    questionField.setText("");
    Stick that at the end of the answerQuestion() method.
    Last edited by destin; 01-02-2006 at 06:23 PM.

Similar Threads

  1. Replies: 1
    Last Post: 11-26-2005, 11:19 PM
  2. Strange behaviour of "CreateProcess" in Win2K
    By Jürgen Thümmler in forum VB Classic
    Replies: 4
    Last Post: 11-05-2001, 04:27 AM
  3. Strange behaviour of "CreateProcess" in Win2K
    By Jürgen Thümmler in forum VB Classic
    Replies: 0
    Last Post: 11-02-2001, 05:55 AM
  4. robot teleoperation
    By zfan in forum Web
    Replies: 3
    Last Post: 05-31-2001, 07:56 PM
  5. Programming
    By Paul Vatish in forum Java
    Replies: 2
    Last Post: 03-24-2000, 05:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center