Lab 3 Robot Parlay - MDHSRobotics/TeamWiki GitHub Wiki
Home / References / [Introduction to Programming - Part 1](Introduction to Programming - Part 1)
Lab 3 - Robot Parlay
Objective
In this lab, you will use Java's Object Oriented capabilities to simulate the FRC framework
Key Concepts
- Java class files
- Abstract class
- Inheritance
- Encapsulation
- Objects
- Methods
- Container
Overview
In this lab, you will create a system that simulates the FRC system and models the FRC robot. You will create a class called Game. This will be the runtime container. It will randomly instantiate 2 robots. After the robots are initialized, the game will proceed by asking each robot who they are and to say something.
-
Use the tutorialspoint online editor It will load with the famous Hello World example already filled in
-
Create initial Game.java file
- use the file menu to create a new file. The file will be empty and will be named NewFile.java. Use the file menu to rename the file to
Game.java
- select the HelloWord.java file and use the file menu to delete it
- make the content of the Game.java file the following:
- use the file menu to create a new file. The file will be empty and will be named NewFile.java. Use the file menu to rename the file to
public class Game{ public static void main(String[] args){ System.out.println("Game starting ... "); } }
2. Change the project settings to compile and run your new java file
* using the project / compile options menu, change the settings to the following:

* click on the compile button and correct any errors you may have made
* click on the execute button. The program will execute and result in `Game state: starting` being printed in the console
2. Create the RobotBase class
* use the file menu to create a new file. The file will be empty and will be named NewFile.java. Use the file menu to rename the file to `RobotBase.java`
* make the content of the RobotBase.java file the following:
```java
public abstract class RobotBase{
public abstract void init();
public abstract void speak();
private String name;
public String getName(){
return name;
}
private String state;
public String getState(){
return state;
}
public void setState(String state){
this.state = state;
System.out.println("robot "+getName()+" state: "+getState());
}
private String thought;
public String getThought(){
return thought;
}
public void setThought(String thought){
this.thought = thought;
}
public RobotBase (String name){
this.name = name;
setState("created");
}
}
-
Create the C3PO class
- use the file menu to create a new file. The file will be empty and will be named NewFile.java. Use the file menu to rename the file to
C3PO.java
- make the content of the C3PO.java file the following:
- use the file menu to create a new file. The file will be empty and will be named NewFile.java. Use the file menu to rename the file to
public class C3PO extends RobotBase{ public C3PO(){ super("C3PO"); }
public void init(){
setThought("It's time like this that I really feel like shutting down.");
}
public void speak(){
System.out.println(getThought());
}
}
2. Create the WallE class
* use the file menu to create a new file. The file will be empty and will be named NewFile.java. Use the file menu to rename the file to `WallE.java`
* make the content of the WallE.java file the following:
```java
public class WallE extends RobotBase{
public WallE(){
super("Wall-E");
}
public void init(){
setThought("Wall-E!");
}
public void speak(){
System.out.println(getThought());
}
}
-
Create the Chappie class
- use the file menu to create a new file. The file will be empty and will be named NewFile.java. Use the file menu to rename the file to
Chappie.java
- make the content of the Chappie.java file the following:
- use the file menu to create a new file. The file will be empty and will be named NewFile.java. Use the file menu to rename the file to
public class Chappie extends RobotBase{ public Chappie(){ super("Chappie"); }
public void init(){
setThought("I've got blings!");
}
public void speak(){
System.out.println(getThought());
}
}
2. Create the Terminator class
* use the file menu to create a new file. The file will be empty and will be named NewFile.java. Use the file menu to rename the file to `Terminator.java`
* make the content of the Terminator.java file the following:
```java
public class Terminator extends RobotBase{
public Terminator(){
super("Terminator");
}
public void init(){
setThought("I'll be back!");
}
public void speak(){
System.out.println(getThought());
}
}
-
Create the B9 class
- use the file menu to create a new file. The file will be empty and will be named NewFile.java. Use the file menu to rename the file to
B9.java
- make the content of the B9.java file the following:
- use the file menu to create a new file. The file will be empty and will be named NewFile.java. Use the file menu to rename the file to
public class B9 extends RobotBase{ public B9(){ super("B9"); }
public void init(){
setThought("Danger, Will Robinson!");
}
public void speak(){
System.out.println(getThought());
}
}
2. Create the Joshua class
* use the file menu to create a new file. The file will be empty and will be named NewFile.java. Use the file menu to rename the file to `Joshua.java`
* make the content of the Joshua.java file the following:
```java
public class Joshua extends RobotBase{
public Joshua(){
super("Joshua");
}
public void init(){
setThought("How about a nice game of chess?");
}
public void speak(){
System.out.println(getThought());
}
}
-
Finish the Game class
- update the main method to the following:
public class Game{
public static void main(String[] args){
System.out.println("Game starting ... ");
RobotBase[] robotOptions = new RobotBase[6];
robotOptions[0] = new C3PO();
robotOptions[1] = new WallE();
robotOptions[2] = new Chappie();
robotOptions[3] = new Terminator();
robotOptions[4] = new B9();
robotOptions[5] = new Joshua();
RobotBase[] players = selectPlayers(robotOptions);
System.out.println("Initializing robots");
for(int i=0;i<players.length;i++){
players[i].init();
players[i].setState("initialized");
}
System.out.println("Parlay ...");
for(int i=0;i<players.length;i++){
players[i].setState("speaking");
players[i].speak();
}
}
}
* implement the selectPlayers method
```java
public class Game{
public static void main(String[] args){
System.out.println("Game starting ... ");
RobotBase[] robotOptions = new RobotBase[6];
robotOptions[0] = new C3PO();
robotOptions[1] = new WallE();
robotOptions[2] = new Chappie();
robotOptions[3] = new Terminator();
robotOptions[4] = new B9();
robotOptions[5] = new Joshua();
RobotBase[] players = selectPlayers(robotOptions);
System.out.println("Initializing robots");
for(int i=0;i<players.length;i++){
players[i].init();
players[i].setState("initialized");
}
System.out.println("Parlay ...");
for(int i=0;i<players.length;i++){
players[i].setState("speaking");
players[i].speak();
}
}
public static RobotBase[] selectPlayers(RobotBase[] options){
int count = options.length;
int player1 = (int)(Math.random()*count);
int player2 = player1;
while(player2 == player1){
player2 = (int)(Math.random()*count);
}
RobotBase[] players = new RobotBase[2];
players[0] = options[player1];
players[1] = options[player2];
return players;
}
}
- Run the simulation
- click on the compile button and correct any errors you may have made
- click on the execute button. The program will execute and result in an output similar to the following, though the speaking robots will be random:
Game starting ...
robot C3PO state: created
robot Wall-E state: created
robot Chappie state: created
robot Terminator state: created
robot B9 state: created
robot Joshua state: created
Initializing robots
robot C3PO state: initialized
robot Terminator state: initialized
Parlay ...
robot C3PO state: speaking
It's time like this that I really feel like shutting down.
robot Terminator state: speaking
I'll be back!