Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem: Help the Turtle This Program will be is used to control the robot (turt

ID: 3859569 • Letter: P

Question



Problem: Help the Turtle This Program will be is used to control the robot (turtle) to find its way to reach to the flsh. We assume this robot can either to move forward or to turn right. The robot should stand on position of (4, 2) at the beginning of the program. Write a java program contains a class of Robot with properties (Fields) and methods as shown in the following class diagram: Robot -startPos: int + Robot (sp: int) |+ 10 int) setStartPos (sp : + getStartPos ):int + move () + turnRight ) startPos: represents the start-position of the robot, startPos is an integer value between 11-88, where 52 means the position of 5, 2) which means 5th column and 2nd row. Move(): This method is prints out "Step Forward" on the screen. It means that the robot has moved one step forward. Don't care about the new address. turnRight():This method is prints out "Right" on the screen. It means that the robot has turned to right side in the same square turnRight ): This method is prints out "Right" on the screen. It means that the Now look at the main mthod in RobotTest class. An object 'turtle' of class Robot is created with position of (1, 1). Use methods of the robot to move him up to the fish. Expected Output Step Forward Step Forward Right Step Forward

Explanation / Answer

Answer:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Robot02{
Robot robot;

  
Robot02Slave slave = new Robot02Slave();


int keyInput[] = {
KeyEvent.VK_D,
KeyEvent.VK_O,
KeyEvent.VK_N,
KeyEvent.VK_E
};//end keyInput array
  

public static void main(String args[]){
System.out.println("Start test program");
new Robot02();
System.out.println(" End test program");
}

public Robot02(){
try{
robot = new Robot();
}catch(AWTException e){
e.printStackTrace();
}
while(!(slave.isShowing())){
  
}

Component[] components =
slave.getContentPane().getComponents();


System.out.println(" Traverse forward");
for(int cnt = 0;
cnt < (components.length - 1); cnt++){
robot.keyPress(KeyEvent.VK_TAB);
  
robot.delay(1000);
}


System.out.println(" Traverse backwards");

robot.keyPress(KeyEvent.VK_SHIFT);
for(int cnt = 0;
cnt < (components.length - 1); cnt++){
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(1000);
}
  
robot.keyRelease(KeyEvent.VK_SHIFT);

  
System.out.println(" Click all components");
for(int cnt = 0; cnt < components.length;
cnt++){
  
Point location =
components[cnt].getLocationOnScreen();
System.out.print("Click at: " +
location.x + ", " + location.y + " ");
mouseMoveAndClick(location.x,location.y);

  
if(components[cnt] instanceof JTextField
&& components[cnt].hasFocus()){
System.out.println(" Enter text");
robot.keyPress(KeyEvent.VK_SHIFT);
for (int cnt2 = 0;
cnt2 < keyInput.length; cnt2++){
if(cnt2 > 0){
robot.keyRelease(KeyEvent.VK_SHIFT);
}
robot.keyPress(keyInput[cnt2]);
robot.delay(1000);
}
  
robot.keyPress(KeyEvent.VK_ENTER);
}
}
}
  

public void mouseMoveAndClick(
int xLoc, int yLoc){
robot.mouseMove( xLoc,yLoc );
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);

robot.delay(1000);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote