Objective: Using the provided code, write a Java program that simulates the movement of different types of robots using

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Objective: Using the provided code, write a Java program that simulates the movement of different types of robots using

Post by answerhappygod »

Objective:
Using the provided code, write a Java program that simulates the movement of different types of robots using multiple threads on a graphical user interface (GUI). There are 4 types of robots (simple (provided), left and right, diagonal, and wavy) that must update their position on a separate thread, and their movement is determined by the type of robot detailed in the requirements.
Requirements:
Functionality. (80pts)No Syntax Errors, Major Run-Time, or Major Logic Errors. and works with the Provided Code. (80pts*)
*Code that cannot be compiled due to syntax errors is nonfunctional code and will receive no points for this entire section.
*Code that cannot be executed or tested due to major run-time or logic errors is nonfunctional code and will receive no points for this entire section.
Robot Panel. (20pts)
There must be 8 different Robots created: 2 Simple (provided), 2 Left and Right, 2 Diagonal, and 2 Wavy
Each of the 8 must have different properties. X/Y Position, Speed, Amplitude, etc.
Each of these robots must be added to the array or Robots in the provided code.
Once added to the Array, each robot’s thread needs to be started.
All the above must apply for full credit.
Left and Right Robot. (20pts)
This type of robot only moves left and right.
The robot’s left/right speed (in pixels) is assumed to be assigned when the robot is created.
The robot updates its position based on its current location plus the given speed.
Once the robot’s position reaches either the right side while traveling to the right or the left side while traveling to the left of the GUI’s frame, then it must move in the opposite direction.
Its color must be set to Blue.
All the above must apply for full credit.
Diagonal Robot. (20pts)
This type of robot moves left, right, up, and down diagonally.
The robot’s left/right and up/down speed (in pixels) is assumed to be assigned when the robot is created. These two speeds may be different.
Once the robot’s position reaches either the right side while traveling to the right or the left side while traveling to the left of the GUI’s frame, then it must move in the opposite direction.
Additionally, once the robot’s position reaches either the top side while traveling to the up or the bottom side while traveling to down of the GUI’s frame, then it must move in the opposite direction.
Its color must be set to Orange.
All the above must apply for full credit.
Wavy Robot. (20pts)
This type of robot moves is based on a horizontal sine wave.
The robot’s X position is determined by a given speed left/right (in pixels).
X = X + Speed_X
The robot’s Y position is determined by
Y = Initial_Y + SIN(X * Period) * Amplitude
The robot’s left/right speed, period, and amplitude is assumed to be assigned when the robot is created.
Once the robot’s position reaches either the right side while traveling to the right or the left side while traveling to the left of the GUI’s frame, then it must move in the opposite direction.
Its color must be set to Pink.
All the above must apply for full credit.
Coding Style. (5pts)
Code functionality organized within multiple methods other than the main method. (2pts)
Readable Code (3pts)
Meaningful identifiers for data and methods.
Proper indentation that clearly identifies statements within the body of a class, a method, a branching statement, a loop statement, etc.
All the above must apply for full credit.
Comments. (5pts)
Your name in the file. (2pts)
At least 5 meaningful comments in addition to your name. These must describe the function of the code it is near. (3pts)
Provided code:
import java.lang.Thread;import java.awt.*;
public class SimpleRobot extends Thread //Extends (inheritance) the functionality of Thread in order to update the Robot's position{private int x,y;//Location of the Robotprivate Color rColor = Color.DARK_GRAY;//Default Colorpublic static final int ROBOT_SIZE = 15;//Robot's are 15 pixel circlespublic static final int TIME_DELAY = 30;//Update is called every 30 milliseconds.public SimpleRobot(int aX, int aY, Color aC){this.setX(aX);this.setY(aY);this.setrColor(aC);}public int getX() {return x;}
public void setX(int x) {this.x = x;}
public int getY() {return y;}
public void setY(int y) {this.y = y;}
public Color getrColor() {return rColor;}
public void setrColor(Color rColor) {this.rColor = rColor;}/*** Overrides the method run in Thread. This calls the method update and sleeps the thread for 30 milliseconds before calling update again.*/public void run(){while(true){//Calls update on each robotthis.update();//Sleeps this thread for 30 milliseconds before updating againtry {Thread.sleep(TIME_DELAY);}catch(Exception e) {e.printStackTrace();}}}/*** This updates the position of the robot based on the robot's type.* This is meant to be overridden for other extended robot types.*/public void update(){//TODO determine how the robot will move. This type of robot does not move but other may need to override this, and set the X and Y coordinates.}}
Provided code:
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;
public class RobotThreadSimulator{public static final int FRAME_DIM = 512; //Image dimension assumed to be fixedpublic static RobotPanel rP = new RobotPanel();//Creates a new Robot Panel to be added to the Frame.public static JFrame frame = new JFrame("Robot Thread Simulation");public static final int FRAME_TIMER_DELAY = 10;//10 millisecondspublic static void main(String[] args) {//Frame for the programframe.setLayout(new FlowLayout());//Simple Layout for the Componentsframe.setBounds(0,0,FRAME_DIM,FRAME_DIM);//Initializes the Robot PanelrP.init();//Adds it to the frameframe.add(rP);//Ensures that it closes all threads after the frame is closedframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Now we can see it!frame.setVisible(true);//Begin AnimatingstartAnimation();}/*** Used to call repaint on the frame using a timer (another thread) with a delay.*/public static void startAnimation(){//Sets up an ActionListener to be used by the TimerActionListener al = new ActionListener(){//Overrides ActionListener's method actionPerformed using an Anonymous Classpublic void actionPerformed(ActionEvent e){frame.repaint();//Calls repaint on the Frame to redraw it and its components}};//Creates a timer with the timer delay and then action listenerTimer timer = new Timer(FRAME_TIMER_DELAY,al);//Starts the timer threadtimer.start();}
}
Provided code:
import java.awt.*;import javax.swing.*;
public class RobotPanel extends JPanel //Extends (inheritance) the functionality of the JPanel Component in order to draw Robots.{public static final int DEF_R_SIZE = 8; //Default number of Robotsprivate SimpleRobot[] robots = new SimpleRobot[DEF_R_SIZE]; //Creates the array of robots to be updated and drawn/*** Initializes elements of the JPanel. This method is to be called before added to a JFrame.*/public void init(){//Set's the JPanel's Preferred Size to be the same as the Frame.super.setPreferredSize(new Dimension(RobotThreadSimulator.FRAME_DIM,RobotThreadSimulator.FRAME_DIM));//TODO Add Robots to the Array//2 Simple Robots
//2 Left Right Robots
//2 Diagonal Robots
//2 Wavy Robots
//See assignment requirements for more details//TODO Start each robot thread
}/***Overrides JPanel's paintComponent method in order to draw each robot.*/public void paintComponent(Graphics g){//Calling super class' paintComponents firstsuper.paintComponent(g);//For each robot in the array of robotsfor(SimpleRobot r : robots){if(r == null)continue;//Sets the drawing colorg.setColor(r.getrColor());//Draws the oval to the JPanelg.fillOval(r.getX(),r.getY(),SimpleRobot.ROBOT_SIZE,SimpleRobot.ROBOT_SIZE);}}}
PLEASE SIMULATE SOMETHING LIKE BELOW DO NOT GIVE AN OUTPUT.
Use the Robot Thread Simulation.
Objective Using The Provided Code Write A Java Program That Simulates The Movement Of Different Types Of Robots Using 1
Objective Using The Provided Code Write A Java Program That Simulates The Movement Of Different Types Of Robots Using 1 (110.71 KiB) Viewed 50 times
Robot Thread Simulation
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply