Java学习笔记 2015/4/12

C3:Variables, values , and types Arithmetic expressions. 学习完毕。

C4:Expressions,预习完毕。Lecture_6未听。预计今晚上完。对应本章课后作业明天完成。

Assignment_1 完成。但是无法check.

以下是A1部分,代码,欢迎交流。

/*

 * File: CheckerboardKarel.java
 * ----------------------------
 * When you finish writing it, the CheckerboardKarel class should draw
 * a checkerboard using beepers, as described in Assignment 1.  You
 * should make sure that your program works for all of the sample
 * worlds supplied in the starter folder.
 */

import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {
	
	private int i = 0;
	
	public void run() {
		while((!leftIsBlocked())) {
			judgeFirstCorner();
			completeOneStreet();
			moveNextStreet();
		}
		judgeFirstCorner();
		completeOneStreet();
		moveNextStreet();
	}

	
	/*
	 * If the street is a odd street, the first corner should be put a beeper; else it is empty.
	 */
	 private void judgeFirstCorner() {
		 if(i==1){
			 i = 0;
			 if(frontIsClear()) {
				 move(); putBeeper();};
		 } else {
			 i = 1; 
			 putBeeper();
		 }
	 }	
	 
	/*
	 * control the Karel to check one street/
	 */
	 private void completeOneStreet() {
		 while(frontIsClear()) {
			 move();
			 if(frontIsClear()) {
				 move();
				 putBeeper();
			 };
		 } // while
		 if(facingEast()){
			 turnAround();
		 };
		 if(frontIsClear()) {
			 backToStart();
			 turnAround();
		 } else {
			 turnAround();
			 backToStart();
			 if(facingWest()) {
				 turnAround();};	
		 }
	 }
	 
	 /*
	  * when complete one street. control the karel back to its original port. 
	  */
      private void backToStart() {
		  while(frontIsClear()) {
			  move();
		  }
      }
	 
	/*
	 * given that one street is completed, we move to the next street.
	 */
	 private void moveNextStreet() {
         turnLeft();
		 if(frontIsClear()) {
			move();
		 	turnRight();
		 };	
	 }
	// You fill in this part

}
<pre name="code" class="java">/*
 * File: CollectNewspaperKarel.java
 * --------------------------------
 * At present, the CollectNewspaperKarel subclass does nothing.
 * Your job in the assignment is to add the necessary code to
 * instruct Karel to walk to the door of its house, pick up the
 * newspaper (represented by a beeper, of course), and then return
 * to its initial position in the upper left corner of the house.
 */

import stanford.karel.*;

public class CollectNewspaperKarel extends Karel {
	
	public void run() {
	    
		move();
		move();
	    turnRight();	
	    move();
	    turnLeft();
	    move();
	    pickBeeper();
	}
	
	/*
	 * Turns Karel 90 degrees to the right.
	 */
	   private void turnRight() {
		   turnLeft();
		   turnLeft();
		   turnLeft();
	   }
	    // You fill in this part

}

/*
 * 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.*;

public class MidpointFindingKarel extends SuperKarel {

	private int length;
	private int midLength;
	
	public void run() {
		int halfLength;
		moveToEnd();
		turnAround();
		halfLength = length/2;
		for(int i=0; i < halfLength ; i++){
			move();
		}
		judgeTheMid();
		if(midLength==halfLength){
			turnAround();
			for(int i=0; i < halfLength ; i++){
				move();
			}
			putBeeper();
		} else {
			turnAround();
			for(int i=0; i < halfLength ; i++){
				move();
			}
			putBeeper();
			move();
			putBeeper();	
		}
		
		
	}
	// You fill in this part

	/*
	 * when start running, move Karel to the end of the street.
	 * Use the "length" variables to count the length of the street. 
	 */
	  private void moveToEnd() {
		  
		  while(frontIsClear()){
			  move();
			  length++;
		  }
	  }
	 /*
	  * calculate the correct length of mid ,thinking of the two condition : odd or even.
	  */
	   private void judgeTheMid() {
		   while(frontIsClear()){
			   move();
			   midLength++;
		   }
	   }
	 
}

/*
 * File: StoneMasonKarel.java
 * --------------------------
 * The StoneMasonKarel subclass as it appears here does nothing.
 * When you finish writing it, it should solve the "repair the quad"
 * problem from Assignment 1.  In addition to editing the program,
 * you should be sure to edit this comment so that it no longer
 * indicates that the program does nothing.
 */

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {
	
    public void run() {
		
    	while(frontIsClear()) {
    		turnLeft();
    		completeOneAvenue();
    		turnAround();
    		backToOriginal();
    		turnLeft();
		
    		getNextAvenue();
    	}
    	
    	turnLeft();
		completeOneAvenue();
		turnAround();
		backToOriginal();
		turnLeft();
      	
	}
       
    
    /*
     *repair one avenue from one port to another port.If there
     *have been a beeper ,Karel neglect this corner. If haven't 
     *Karel put a beeper in that corner.
     */
        
	 private void completeOneAvenue() {
		
		while(frontIsClear()) {
			if(beepersPresent())
				move();
			else {
				putBeeper();
				move();
			}
		}
		if(!beepersPresent())
			putBeeper();
	}
	 
	 
	/*
	 * when Karel finish one avenue.For this method ,we control 
	 * Karel back to its original port.  
	 */
	 
	 private void backToOriginal(){
		 
		 while(frontIsClear()){
			 move();
		 }
	 }  

	 
	/*
	 *when complete one avenue ,we control Karel move to the next avenue. 
	 */
	 
	 private void getNextAvenue() {
		
        while(frontIsClear()) {
        	for(int i=1; i<5; i++)
        		move();
        	break;
        }
	}

	 
	
}


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值