Problem 3-1 Write appropriate definitions for the following new functions: moveMile, remembering that miles are 8 blocks
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, but leaves it facing the same direction, and
- 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.
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)
}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill