Overview

Write a program to move the stack of blocks from source to specific targets based on the size of blocks. Movement of Robot arms, picking and dropping can be controlled using the Robot methods described below.

The methods of Robot class:

up(), down(), extend(), contract(), raise(), lower(), pick(), drop(),speedUp(), slowDown()

  • The height (h) of the main arm can be changed by 1 unit using methods up() and down(). The height (h) of the this arm must lie in the range 2 to 14, and is initially set to 2.
  • The second arm can be moved horizontally by 1 unit using extend() or contract(). The width (w) must lie in the range 1 to 10, and is initially set to 1.
  • The depth (d) of the third arm can be changed by 1 unit using lower() or raise(). The depth cannot be less than 0 but must be less than the height of the main arm ( d < h ). In the initial position depth d is set to 0 (it is not visible).
  • Robot class has two other methods slowDown(int factor) and speedUp(int factor) to adjust the time between moves by the specified factor.
  • An item can be picked from the top of the stack of blocks at source using pick().
  • It can be dropped at the top of the stack of blocks at one of the two targets or on top of the bars using drop(), depending on the size of the block. Blocks of heights 1 and 2 must be placed on columns 1 and 2 respectively while blocks of size 3 must be placed on top of the bars.

Other Constraints

There can only be one block placed on top of a bar and the blocks must be placed in order starting with the bar in column 3. For scenarios (a) and (b) you are required to deal with blocks of height 3 only.

Algorithms for moving the blocks must avoid collisions with others blocks and bars.

Height of bars and Blocks

The height of bars are preset for scenario A at 7 each but will be specified as a command line argument for scenarios B and C. Similarly the heights of blocks at the source are preset to 3 each for scenarios A and B, but can be specified as a command line argument for scenario C, where the height of blocks may vary from 1 to 3. The number of blocks for Scenario C may vary between 1 and 12, but the total sum of the block heights cannot exceed 12.

What you need to do

It is your job to implement the control mechanism for the robot arm so that it can safely move the blocks from the source pile on the left to their destination without colliding with any bars or other blocks that have already been moved.

The control mechanism is implemented in the control() method of the RobotControl class – this is where your implementation of an algorithm for moving the robot must be placed.

Scenario A – Fixed size blocks and bars

Initially you may assume that the bars and blocks will be of a fixed size – the four blocks are all of sizes 3 (the first block is always at the bottom of the source pile) and all bars are of size 7.

The robot will start off with code in the control() method which demonstrates how the different movements can be combined to transport the first block from the source pile to the its target bar.

You will need to update the control method that it picks up each block and moves it to one of the bars, starting from the last bar and working backwards.

You can do this by repeating the code segment for moving the first block for each new block and making the necessary adjustments to the degree the arm moves in each direction to reflect the location of the top of the next block in the source pile and the next vacant bar the block is to be dropped upon (if you can do it more efficiently then that’s ok too).

Example: java Robot See image.

Scenario B – Fixed sized blocks, variable sized bars

In this scenario the heights of the bars will be specified by the user on the command line when running the robot and thus their heights could be anything from 1 to 7.

You need to factor these variable bar heights when moving the arm into position to drop off each block (hint: the barHeights array contains the heights of each bar).

It is also a requirement that you implement the control method in an efficient manner, which means you should not repeat the code required for moving a block from the source pile to its destination and moving the arm back to the starting position more than once.

In order to do this you will need to nest the code for moving one block inside a loop which repeats that series of robot arm movements for each block in the source pile – you may find using variables to keep track of where the arm is, height of the source pile and target bar position and updating them appropriately whenever something happens helpful.

Note that marks will only be awarded for scenario B if you have implemented the control() method in an efficient manner as described above.

Note that the blocks at source are all of size 3 as for scenario A, but bar sizes can be specified in the range 1 to 7.

Example: java Robot 734561 See image.

Scenario C – Variable sized blocks, variable sized bars

In this scenario the heights of both the blocks and the bars will be specified by the user on the command line when running the robot and thus the block heights can be anything between 1 and 3 units and the bar heights can be anything between 1 and 7 units.

You need to factor these variable block and bar heights when moving the arm into position to pick up and drop off each block (hint: the blockHeights array contains the heights of each block with the last element in the array reflecting the height of the top block in the source pile and the barHeights array contains the heights of each bar).

It is also a requirement that you implement the control method in an efficient manner as was the case for scenario B, which means you should not repeat the code required for moving a block from the source pile to its destination and moving the arm back to the starting position more than once.

In order to do this you will need to nest the code for moving one block inside a loop which repeats that series of robot arm movements for each block in the source pile – you may find using variables to keep track of where the arm is, height of the source pile and target bar position and updating them appropriately whenever something happens helpful.

Note that marks will only be awarded for scenario C if you have implemented the control() method in an efficient manner as described for scenarios A and B above. Also note that your robot must work for any possible combination of block and bar heights (you may assume there will always be 6 bars that need to be cleared safely).

Note block sizes can be specified in the range 1-3 and bar sizes can be specified in the range 1 to 7. Example: java Robot 734561 231231 See image.

The code you need to write to control the Robot moves

For all three sections below you are required to complete the control() method of the RobotControl class shown below. The control() method will be automatically called when the program is run. RobotControl class has a reference r set to refer to the Robot object. Hence to move the first arm of the Robot upwards call r.up(). See image.

Arguments Passed to the method control()

This method is also passed two arrays containing the heights of (up to six) bars and blocks based on user input. If the instructions you specify for the Robot result in hitting an bar or going beyond the limits, program will terminate immediately with an appropriate Error Message.

Scenarios for your robot control mechanism

  • Assume all the bars are of height 7 units and all the blocks are of height 3 units respectively. Complete the control() method of the RobotControl class (Sample call: java Robot). You may study the skeleton code - which moves one block from source to code (assuming bars are of height 7 and blocks are of height 3) - and use it as the basis for your scenario A control mechanism.
  • Allow the user to supply the heights of the 6 bars as command line arguments where the heights of the bars may vary from 1 to 7 (Sample: java Robot 734561). Based on these arguments, int array barHeights[] will be automatically set as in: barHeights[0] = 7; barHeights[1] = 3; …. barHeights[5] = 1;
  • Allow the user to supply the heights of the bars (which can vary in number) as well as the height of the blocks to be moved (which can also vary in number) as command line arguments (Sample: java Robot 734561 231231). Based on these arguments int arrays barHeights[], blockHeights[]) will be automatically set as in . barHeights[0] = 7; barHeights[1] = 3; …. barHeights[5] = 1; blockHeights[0] = 2; blockHeights[0] = 3; … blockHeights[3] = 1;
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.