{"id":951,"date":"2010-05-10T13:08:41","date_gmt":"2010-05-10T13:08:41","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=951"},"modified":"2011-01-14T14:00:22","modified_gmt":"2011-01-14T14:00:22","slug":"problem-4-2","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/10\/problem-4-2\/","title":{"rendered":"Problem 4"},"content":{"rendered":"<p><span id=\"zipfile\"><a href=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/05\/Assignment1_Code.zip\"><\/a><\/span>This is the problem 4 &#8220;As an exercise in solving algorithmic problems, program Karel to place a single beeper at the center of 1st Street. For example, if Karel starts in the world&#8221;.  In solving this problem, you may count on the following facts about the world:<\/p>\n<ul>\n<li>Karel starts at 1st Avenue and 1st Street, facing east, with an infinite number of beepers in its bag.<\/li>\n<li>The initial state of the world includes no interior walls or beepers.<\/li>\n<li>The world need not be square, but you may assume that it is at least as tall as it is wide.<\/li>\n<li>Your program, moreover, can assume the following simplifications:<\/li>\n<ol>\n<li>If the width of the world is odd, Karel must put the beeper in the center square. <\/li>\n<li>If the width is even, Karel may drop the beeper on either of the two center squares.<\/li>\n<\/ol>\n<li>It does not matter which direction Karel is facing at the end of the run.<\/li>\n<\/ul>\n<p>To me the answer is when you move from one side to the other and count the number of steps, and then once you hit the right side wall, go back length \/2 steps and then place the beeper, and below is my answer in code, once again the zip file attached is the full assignment 1 with each problem and also the PDF file which includes the assignment in full.<\/p>\n<pre lang=\"java\">\r\n\/*\r\n * File: MidpointFindingKarel.java\r\n * -------------------------------\r\n * When you finish writing it, the MidpointFindingKarel class should\r\n * leave a beeper on the corner closest to the center of 1st Street\r\n * (or either of the two central corners if 1st Street has an even\r\n * number of corners).  Karel can put down additional beepers as it\r\n * looks for the midpoint, but must pick them up again before it\r\n * stops.  The world may be of any size, but you are allowed to\r\n * assume that it is at least as tall as it is wide.\r\n *\/\r\n\r\nimport stanford.karel.*;\r\n\r\npublic class MidpointFindingKarel extends SuperKarel {\r\n\r\n\tprivate int middleI =0;\r\n\t\r\n\t\/\/ find the middle by move across until hitting the end\r\n\tprivate void findTheMiddle()\r\n\t{\r\n\t\twhile (!frontIsBlocked())\r\n\t\t{\r\n\t\t\tmove();\r\n\t\t\tmiddleI++;\r\n\t\t}\r\n\t\t\r\n\t}\r\n\t\r\n\t\/\/ move half the way back, and place the beeper\r\n\tprivate void placeBeeperInTheMiddle()\r\n\t{\r\n\t\tfor (int i=0; i < (middleI \/2); i++)\r\n\t\t\tmove();\r\n\t\tputBeeper();\r\n\t}\r\n\t\r\n\t\/\/ You fill in this part\r\n\tpublic void run()\r\n\t{\r\n\t\tfindTheMiddle();\r\n\t\tturnAround();\r\n\t\tplaceBeeperInTheMiddle();\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is the problem 4 &#8220;As an exercise in solving algorithmic problems, program Karel to place a single beeper at the center of 1st Street. For example, if Karel starts in the world&#8221;. In solving this problem, you may count on the following facts about the world: Karel starts at 1st Avenue and 1st Street, &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/10\/problem-4-2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Problem 4<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-951","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=951"}],"version-history":[{"count":8,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/951\/revisions"}],"predecessor-version":[{"id":1339,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/951\/revisions\/1339"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}