Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 2-4 on replit In most cities and towns we can walk around the block by repeating the following actions four times: walk to the

Problem 2-4 on replit

In most cities and towns we can walk around the block by repeating the following actions four times:

  • walk to the nearest intersection
  • turn either right or left (the same one each time)

If done correctly we will return to our original starting place. Program karel to walk around the block. Will your program succeed for the initial situation in Figure 2-7? 

téléchargement.png

Figure 2-7: Initial Situation for the Walk Around the Block Task

 

Completed on programming for me (Kotlin)

 

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

fun main() {
 initializeWorld("Problem2-4.kwld")

 // Create your robot here and tell it 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 2-5

Every morning karel is awakened in bed when the newspaper, represented by a beeper, is thrown on the front porch of the house. Program karel to retrieve the paper and bring it back to bed. The initial situation is given in Figure 2-8, and the final situation must have karel back in bed (same corner, same direction) with the newspaper.

 

téléchargement (1).png


Figure 2-8: Initial Situation for the Newspaper Retrieval Task

 

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

fun main() {
 initializeWorld("Problem2-5.kwld")

 // Create your robot here and tell it 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 2-6

The wall sections in Figure 2-9 represent a mountain (north is up). Program karel to climb the mountain and then plant a flag, represented by a beeper, on the summit; karel must then descend to the other side of the mountain. Assume that karel starts with the flag-beeper in the beeper-bag. Remember that karel is not a super-robot who can leap to the top of the mountain, plant the flag, and then jump down in a single bound. As illustrated, karel must closely follow the mountain's face on the way up and down.

téléchargement (2).png

Figure 2-9: The Mountain Climbing Task

 

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

fun main() {
 initializeWorld("Problem2-6.kwld")

 // Create your robot here and tell it 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 2-7

On the way home from the supermarket, karel's shopping bag ripped slightly at the bottom, leaking a few expensive items. These groceries are represented by -you guessed it- beepers. The initial situation, when karel discovered the leak, is represented in Figure 2-10. Program karel to pick up all the dropped items and then return to the starting position.

 

téléchargement (3).png


Figure 2-10: Initial Situation for the Grocery Pickup Task

 

 

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

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

 // Create your robot here and tell it 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 2-8

Write a program that instructs karel to rearrange the beeper pattern as shown in Figure 2-11.

 

téléchargement (4).png

Figure 2-11: The Rearrange the Beepers Task

 

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

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

 // Create your robot here and tell it 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 2-9

Karel is practicing for the Robot Olympics. One of karel's events is the shuttle race. The shuttle race requires karel to move around two beepers in a figure 8 pattern. Write a program that instructs karel to walk a figure 8 pattern as fast as possible (fast implies as few instuctions as possible). Karel must stop in the same place it starts and must be facing the same direction.

 

téléchargement (5).png


Figure 2-12: The Shuttle Race and karel's Figure 8 Path

 

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

fun main() {
 initializeWorld("Problem2-9.kwld")

 // Create your robot here and tell it what to do!
 val karel = ShuttleRaceRobot(1, 1, North, 0)
 

// 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 2-11

A robot named karel is at the origin facing North with one beeper in its bag. Three blocks east of it is another robot named carl, facing East with no beepers. There are no wall sections or beepers in the world. Have karel walk to carl and give carl the beeper. Carl should then carry the beeper two blocks north and put it down. Both robots should then return to their original locations, facing the original directions.

Tip: To help differentiate the robots, add this code , Color.cyan immediately after specifiying how many beepers carl has.

 

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

fun main() {
 initializeWorld()

 // Create your robots 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)
}
 
 
 
 
 
 


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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials Of Services Marketing

Authors: Jochen Wirtz

4th Edition

1292425199, 9781292425191

More Books

Students also viewed these Algorithms questions

Question

The bonds pay interest on February 1 and August AppendixLO1

Answered: 1 week ago

Question

3. Show your interest in your students as individuals.

Answered: 1 week ago

Question

How do customers typically respond to service failures?

Answered: 1 week ago

Question

What are the key types of training service firms should conduct?

Answered: 1 week ago

Question

For each, show how or explain why not. If Can C = 0? Can C

Answered: 1 week ago

Question

For each, show how or explain why not. If Can Can

Answered: 1 week ago