Question
Here is the question: Design and implement a class called Bug, which represents a bug moving along a horizontal wire. The bug can only move
Here is the question:
Design and implement a class called Bug, which represents a bug moving along a horizontal wire. The bug can only move for one unit of distance at a time, in the direction it is facing. The bug can also turn to reverse direction. For your design, create a UML Class diagram similar to Figure 5.5 on page 180 of the textbook. Note that you need to include the constructor in the methods section if you code a constructor. Bug will require a toString method to return the current position and which direction the bug is facing to the driver so it can be output.
Hint: Remember that a horizontal line has a zero position in the middle with positive to the right and negative to the left. Consider that a bug will land on the wire at some point before starting along the wire.
Write an interactive test driver that instantiates a Bug, then allows the user to manipulate it with simple commands like Output (to see the position and direction), Move, Turn, Exit ... single letters work just fine. All output should be via the driver not methods within Bug. You should use this driver to create screenshot exhibits for a number of scenarios (e.g., output original position, move a few times, output, move a few more times, output, turn, output, move, output, etc.)
Here is the code I wrote:
import java.util.Scanner;
public class Assignment4Q1 {
// Define a main method public static void main(String[] args) {
Scanner input = new Scanner(System.in); boolean exitFlag = false;
// create object for a class Bug bug = new Bug(0, Bug.RIGHT);
System.out.println("Welcome to the Bug zone"); System.out.println("Current position of Bug is: " + bug); System.out.println("M. To move Bug"); System.out.println("T. To change the direction of Bug"); System.out.println("E. To Exit"); System.out.println("O. To Output"); do {
String choice = input.nextLine();
switch (choice) { case "M": case "m": { bug.move(); break; } case "T": case "t": { bug.turn(); break; } case "E": case "e": { exitFlag = true; break; } case "O": case "o": { System.out.println(bug); break; } default: { System.out.println(" Wrong choice entered."); System.out.println("M. To move the insect"); System.out.println("T. To change the direction of insect"); System.out.println("E. To Exit"); System.out.println("O. To Output "); break; } }// End switch } while (!exitFlag); // End while
System.out.println(" Good Bye!!!");
// Finally, close this scanner. input.close();
}// End main }// End Assignment4Q1 class
class Bug { public static final int LEFT = -1; public static final int RIGHT = 1;
private int location; private int direction;
public Bug() { location = 0; direction = RIGHT; }
// for bug method public Bug(int location, int direction) { this.location = location; if (direction == RIGHT || direction == LEFT) this.direction = direction; else System.out.println("Invalid Direction"); }
// for set position method public void setPosition(int location) { this.location = location; }
// for to get the position public int getPosition() { return location; }
// for to set direction public void setDirection(int direction) { if (direction == RIGHT || direction == LEFT) this.direction = direction; else System.out.println("Invalid Direction"); }
// for to get direction public int getDirection() { return direction; }
// Method to change // Actually this will help to move the insect by 1 unit distance. public void move() { if (direction == RIGHT) location++; else if (direction == LEFT) location--; // location += direction; }
// Method for reversing the path public void turn() { if (direction == RIGHT) direction = LEFT; else if (direction == LEFT) direction = RIGHT; }
// TO string method public String toString() { String directionStr = "no"; if (direction == LEFT) directionStr = "left"; else if (direction == RIGHT) directionStr = "right";
return "Bug is in '" + directionStr + "' direction and at '" + location + "' location."; } }// End Bug class
Please fix any issues in my code or comment if it's all correct. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started