Question: Problem 3-1 Write appropriate definitions for the following new functions: moveMile, remembering that miles are 8 blocks long; moveBackward, which moves karel one block backward,

Problem 3-1

Write appropriate definitions for the following new functions:

  1. moveMile, remembering that miles are 8 blocks long;
  2. moveBackward, which moves karel one block backward, but leaves it facing the same direction, and
  3. moveKiloMile which moves karel 1000 miles forward.

This last problem is difficult but a fairly short solution does exist. You may use the moveMile  message in this problem without redefining it.

Note: The point of this exercise is to write new member functions; no object creation or code execution is necessary. However, a good programming practice is to always test your code. The easiest way to do that is to create objects and run the code. For the moveKiloMile function, the robot will run off the edge of the screen, but you can still look at the information in the Console tab to track the robot's location. If you want to speed the robot up, change initializeWorld("") to initializeWorld("", 0)

 

 

import kareltherobot.*
import java.awt.Color
import kareltherobot.Directions.*

// Create your new robot classes here


fun main() {
 initializeWorld("")

 // Create your robot objects here and tell them what to do!
 
 
}

// You don't need to change anything below.
fun initializeWorld(worldFile:String="", delay:Int=50)
{
 javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel")
 javax.swing.JOptionPane.showMessageDialog(null, "Click OK to continue.")
 World.reset()
 if (!worldFile.isEmpty()) World.readWorld(worldFile)
 World.setBeeperColor(Color.magenta)
 World.setStreetColor(Color.blue)
 World.setNeutroniumColor(Color.green.darker())
 World.setDelay(delay)
 World.setVisible(true)
}

 

Problem 3-2

Karel sometimes works as a pin-setter in a bowling alley. Write a program that instructs karel to transform the initial situation in Figure 3-4 into the final situation. Karel starts the task with ten beepers in its beeper-bag.

tter in a bowling alley. Write a program that instructs karel to transform the initial situation in Figure 3-4 into the final situation. Karel starts the task with ten beepers in its beeper-bag.


10 9 8 7 4 3 2 1 N 4 844 9

Figure 3-4: A Pin Setting Task

 

import kareltherobot.*
import java.awt.Color
import kareltherobot.Directions.*

// Create your new robot classes here


fun main() {
 initializeWorld("")

 // Create your robot objects here and tell them what to do!
 
 
}

// You don't need to change anything below.
fun initializeWorld(worldFile:String="", delay:Int=50)
{
 javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel")
 javax.swing.JOptionPane.showMessageDialog(null, "Click OK to continue.")
 World.reset()
 if (!worldFile.isEmpty()) World.readWorld(worldFile)
 World.setBeeperColor(Color.magenta)
 World.setStreetColor(Color.blue)
 World.setNeutroniumColor(Color.green.darker())
 World.setDelay(delay)
 World.setVisible(true)
}

 

Problem 3-7

Karel has taken a part time job as a gardner. Karel's specialty is planting beepers. Karel's current task is to plant one and only one beeper on each corner around the "+" shaped wall arrangement as shown in the situation in Figure 3-8. This task should use 28 beepers in total.

7 00 8 9 1 9 8 7 6 5 4 3

Figure 3-8: A Gardening Task

 

import kareltherobot.*
import java.awt.Color
import kareltherobot.Directions.*

// Create your new robot classes here


fun main() {
 initializeWorld("Problem3-7.kwld")

 // Create your robot objects here and tell them what to do!

 
}

// You don't need to change anything below.
fun initializeWorld(worldFile:String="", delay:Int=50)
{
 javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel")
 javax.swing.JOptionPane.showMessageDialog(null, "Click OK to continue.")
 World.reset()
 if (!worldFile.isEmpty()) World.readWorld(worldFile)
 World.setBeeperColor(Color.magenta)
 World.setStreetColor(Color.blue)
 World.setNeutroniumColor(Color.green.darker())
 World.setDelay(delay)
 World.setVisible(true)
}

 

Problem 3-8

Karel got bored with gardening, so the robot decided to try a different part time job. The robot now installs carpets (made from beepers) in buildings in its world. Write a program that instructs karel to install a carpet in the building shown in Figure 3-9. There must be no "lumps" in the carpet so be sure that karel places one and only one beeper on each intersection in the room.

2 IN 2 3 4 5 6 00 8 9

Figure 3-9: A Carpet Task

 

import kareltherobot.*
import java.awt.Color
import kareltherobot.Directions.*

// Create your new robot classes here


fun main() {
 initializeWorld("Problem3-8.kwld")

 // Create your robot objects here and tell them what to do!

 
}

// You don't need to change anything below.
fun initializeWorld(worldFile:String="", delay:Int=50)
{
 javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel")
 javax.swing.JOptionPane.showMessageDialog(null, "Click OK to continue.")
 World.reset()
 if (!worldFile.isEmpty()) World.readWorld(worldFile)
 World.setBeeperColor(Color.magenta)
 World.setStreetColor(Color.blue)
 World.setNeutroniumColor(Color.green.darker())
 World.setDelay(delay)
 World.setVisible(true)
}

10 9 8 7 4 3 2 1 N 4 844 9 7 00 8 9 1 9 8 7 6 5 4 3 2 IN 2 3 4 5 6 00 8 9

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithms Questions!