C. define the robot 1. Create a robot class that has the following data and func
ID: 3750024 • Letter: C
Question
C. define the robot 1. Create a robot class that has the following data and function members: - a point object to store the robot's current location in the world an enumerated data type, called orientation type, that defines the four directions that the robot could be facing (north, south, east or west), i.e., its "orientation" - a variable to store the robot's current orientation . void init0: This function initializes the robot's current location to (0, 0) and its current orientation to east + void print0 const This function prints the robot's current location and orientation in a pretty format, such as: I am at (0,0) and I am facing east. - void setOrientation( orientation type orientation): This function sets the value of the robot's orientation data member . bool forward0 This function simulates the robot moving forward one step in the direction that it is facing.It checks to make sure that the robot is not at the edge of its world. It returns true if the robotExplanation / Answer
Here is the ROBOT class:
import java.util.*;
import java.lang.*;
import java.io.*;
import java.awt.Point;
class Robot{
//point class is defined in java.awt.Point or you can implement your own.
private Point currentLocation;
//not possible without these two:
private Point worldStarting;
private Point worldEnding;
//construuctor, initializes the world cordinates:
public Robot(Point worldStarting, Point worldEnding){
this.worldStarting = worldStarting;
this.worldEnding = worldEnding;
}
enum orientation_type{
east, west, north, south;
}
private orientation_type currentOrientation;
public void init(){
currentLocation.setLocation(0, 0);
}
public void print(){
System.out.println("I am at " + currentLocation.getLocation().toString() + "and I am facing " + currentOrientation + ".");
}
public void setOrientation(orientation_type newOrientation){
currentOrientation = newOrientation;
}
public void forward(){
Point temproryLocation;
temproryLocation = currentLocation;
switch(currentOrientation){
case east:
temproryLocation.setLocation(currentLocation.getX() + 1, currentLocation.getY());
break;
case west:
temproryLocation.setLocation(currentLocation.getX() - 1, currentLocation.getY());
break;
case north:
temproryLocation.setLocation(currentLocation.getX(), currentLocation.getY() + 1);
break;
case south:
temproryLocation.setLocation(currentLocation.getX() + 1, currentLocation.getY() - 1);
break;
}
if((temproryLocation > worldEnding) || (temproryLocation < worldStarting)){
System.out.println("Robot has gone out of the world. Restoring previous location.");
} else {
currentLocation = temproryLocation;
}
}
public void turnCW(){
switch(currentOrientation){
case east:
currentOrientation = south;
break;
case west:
currentOrientation = north;
break;
case north:
currentOrientation = east;
break;
case south:
currentOrientation = west;
break;
}
}
public void turnAntiCW(){
switch(currentOrientation){
case east:
currentOrientation = north;
break;
case west:
currentOrientation = south;
break;
case north:
currentOrientation = west;
break;
case south:
currentOrientation = east;
break;
}
}
public boolean eastEnd(){
return (currentLocation.getX() == worldEnding.getX() );
}
public boolean westEnd(){
return ( currentLocation.getX() == worldStarting.getX() );
}
public boolean northEnd(){
return ( currentLocation.getY() == worldEnding.getY() );
}
public boolean southEnd(){
return ( currentLocation.getY() == worldStarting.getY() );
}
public void zag(){
if(currentLocation.getX() == worldEnding.getX() && currentOrientation == east){
turnCW();
forward();
turnCW();
forward();
}
}
public void zig(){
if(currentLocation.getX() == worldStarting.getX() && currentOrientation == west){
turnAntiCW();
forward();
turnAntiCW();
forward();
}
}
}
****************************************************************************
If you don't want to use two Point objects worldStarting and worldEnding in Robot class then you must provide the implementation of World class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.