-1

I started learning Java last week so bear with me as I'm just getting the hang of things. I'm trying to make a class that extends the Java Robot class.

I am getting an "Identifier Expected" on this line:

    public ChanseyRobot(bot)

Robot Class:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.MouseInfo;

public class ChanseyRobot extends Robot
{
private Robot bot;

public ChanseyRobot(bot)
{
    try
    {
        this.bot = new Robot();
    }
    catch (AWTException e)
    {
        throw new RuntimeException(e);
    }
}
}

Main Class:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.MouseInfo;

public class Main
{
public static void main(String args[])
    {
        ChanseyRobot robot = new ChanseyRobot(robot);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mbridges
  • 184
  • 1
  • 2
  • 12
  • 1
    Note that you'll also need to change how you're instantiating the `ChanseyRobot` class ...the current line (`ChanseyRobot robot = new ChanseyRobot(robot);`) is a recursive / tautological statement. – Daniel F. Thornton Feb 22 '13 at 15:57
  • Besides 'making a short example', is there any reason the code is extending robot as opposed to just using one? – Andrew Thompson Feb 22 '13 at 15:57

4 Answers4

1

Change this:

public ChanseyRobot(bot) { }

to

public ChanseyRobot(Robot bot) { }

You need to declare Data Type before the variable name, and it is a very basic of Java.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
1

Read up on Java Inheritance

Your class IS A robot. So it doesn't need to create a Robot bot internally too.

public class ChanseyRobot extends Robot
{
    public ChanseyRobot()
    {
    }
}

And then just :

ChanseyRobot robot = new ChanseyRobot();
David Lavender
  • 8,021
  • 3
  • 35
  • 55
0

You need to provide the type of the parameter in your constructor.

change

public ChanseyRobot(bot)

to

public ChanseyRobot(Robot bot)  throws AWTException

You also need to declare AWTException in your constructor declaration as Robot's default constructor throws AWTException.

public Robot()
      throws AWTException
PermGenError
  • 45,977
  • 8
  • 87
  • 106
0

Robot Class:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.MouseInfo;

public class ChanseyRobot extends Robot
{
private Robot bot;

public ChanseyRobot()
{
    try
    {
        this.bot = new Robot();
    }
    catch (AWTException e)
    {
        throw new RuntimeException(e);
    }
}
}

Main Class:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.MouseInfo;

public class Main
{
public static void main(String args[])
    {
        ChanseyRobot robot = new ChanseyRobot();
    }
}
Community
  • 1
  • 1