Another problem that I thought would kinda be simple, and in reality it was if I could have used more of Java, but turned out to be really hard. I am going to post two results here. One using some other attributes of Java and the other is going to be what it took to satisfy the class.
Simple/fast way:
/* * File: MidpointFindingKarel.java * ------------------------------- * When you finish writing it, the MidpointFindingKarel class should * leave a beeper on the corner closest to the center of 1st Street * (or either of the two central corners if 1st Street has an even * number of corners). Karel can put down additional beepers as it * looks for the midpoint, but must pick them up again before it * stops. The world may be of any size, but you are allowed to * assume that it is at least as tall as it is wide. */import stanford.karel.*;
/* * Name: * Section Leader: */
public class MidpointFindingKarel extends SuperKarel {
private int countI; public void run() { /* * Find the middle of any size map. */ countI =0; findMiddle(); moveMiddle(); putBeeper(); } private void findMiddle() { while (frontIsClear()) { move(); countI++; } } private void moveMiddle() { turnAround(); for (int i=0; i < (countI /2); i++) move(); }}
To satisfy class
/* * File: MidpointFindingKarel.java * ------------------------------- * When you finish writing it, the MidpointFindingKarel class should * leave a beeper on the corner closest to the center of 1st Street * (or either of the two central corners if 1st Street has an even * number of corners). Karel can put down additional beepers as it * looks for the midpoint, but must pick them up again before it * stops. The world may be of any size, but you are allowed to * assume that it is at least as tall as it is wide. */import stanford.karel.*;
/* * Name: Ken Swain * Section Leader: */
public class MidpointFindingKarel extends SuperKarel {
public void run() { /* * Find the middle of any size map. * * Pre-Condition: On corner of 1:1 facing east. * * Post-Condition: Have one beeper sitting in the middle of the 1st row. * */ layBeepers(); findMiddle(); putBeeper(); faceEast(); } private void faceEast() { /* * Turn and face east */ while (notFacingEast()) { turnRight(); } } private void layBeepers() { /* * Pre-Condition: Karel is at 1:1 facing east. * * Post-Condition: Karel is at the end of the row facing west. */ while (noBeepersPresent()) { putBeeper(); if (frontIsClear()) { move(); } } turnAround(); } private void findMiddle() { /* * Pre-condition: Karel starts on row 1 facing west * * Post-condition: Karel is standing in the center of row 1 * */ for (int i=0; i < 2; i++) { moveUntilHitWallOrNoBeepers(); } move(); while (beepersPresent()) { cleanEnds(); } turnAround(); move(); } private void cleanEnds() { /* * Pre-condition: beepers are still on the map and we are starting on one. * * Post-condition: we are standing in the center of the map with no beepers. */ while (beepersPresent()) { move(); } turnAround(); move(); pickBeeper(); move(); } private void moveUntilHitWallOrNoBeepers() { /* * Move to the end of the line. */ while (frontIsClear()) { move(); } turnAround(); pickBeeper(); }}
