mikemcleanuk
April 20th, 2009, 05:20 PM
Hey im trying to complete some basic java questions and i have become stumped. I am trying to run an method of an object that is stored in a Vector. Below is my code:
class KillerRobot
{
String name, mainWeapon;
int numberOfKills;
KillerRobot(String n, String mw, int k)
{
name = n;
mainWeapon = mw;
numberOfKills = k;
}
void robotSummary()
{
System.out.println("The Robot" + name + " uses a " + mainWeapon + " and has " + numberOfKills + " total kills.");
}
}
import java.util.Vector;
import java.util.Scanner;
public class TestVector2
{
public static void main(String[] args)
{
Scanner myInput = new Scanner(System.in);
String name ="", mainWeapon;
int numberOfKills;
Vector <Object> robotCollection = new Vector <Object>();
while (!name.equals("Q"))
{
System.out.println("Please enter the name of the Killer Robot or Q to finish.");
name = myInput.next();
if(!name.equals("Q"))
{
System.out.print("Robot Weapon: ");
mainWeapon = myInput.next();
System.out.print("Robot Kills: ");
numberOfKills = myInput.nextInt();
KillerRobot robot = new KillerRobot(name, mainWeapon, numberOfKills);
robotCollection.add(robot);
}
}
for(Object robots : robotCollection)
{
robots.robotSummary();
}
}
}
I am getting the following error on compile:
C:\JPR\Lab13>javac TestVector2.java
TestVector2.java:32: cannot find symbol
symbol : method robotSummary()
location: class java.lang.Object
robots.robotSummary();
^
1 error
Any help would be apprechiated.
Thanks
Mike
class KillerRobot
{
String name, mainWeapon;
int numberOfKills;
KillerRobot(String n, String mw, int k)
{
name = n;
mainWeapon = mw;
numberOfKills = k;
}
void robotSummary()
{
System.out.println("The Robot" + name + " uses a " + mainWeapon + " and has " + numberOfKills + " total kills.");
}
}
import java.util.Vector;
import java.util.Scanner;
public class TestVector2
{
public static void main(String[] args)
{
Scanner myInput = new Scanner(System.in);
String name ="", mainWeapon;
int numberOfKills;
Vector <Object> robotCollection = new Vector <Object>();
while (!name.equals("Q"))
{
System.out.println("Please enter the name of the Killer Robot or Q to finish.");
name = myInput.next();
if(!name.equals("Q"))
{
System.out.print("Robot Weapon: ");
mainWeapon = myInput.next();
System.out.print("Robot Kills: ");
numberOfKills = myInput.nextInt();
KillerRobot robot = new KillerRobot(name, mainWeapon, numberOfKills);
robotCollection.add(robot);
}
}
for(Object robots : robotCollection)
{
robots.robotSummary();
}
}
}
I am getting the following error on compile:
C:\JPR\Lab13>javac TestVector2.java
TestVector2.java:32: cannot find symbol
symbol : method robotSummary()
location: class java.lang.Object
robots.robotSummary();
^
1 error
Any help would be apprechiated.
Thanks
Mike