15-110 FALL 2009 (CORTINA)

HOMEWORK 4 - due Friday, October 2, 2009 in class

Please remember that homework must be handed in on time. No late homework will be accepted.

Review the suggested readings from chapter 5 of your textbook and answer the following questions based on your textbook and the course lecture material.

  1. (2 pts) How many asterisks do the following code fragments print out? Explain each of your answers.

    (a)	int i = 1;
    	while (i != 45) {
    		System.out.println("*");
    		i++;
    	}
    
    (b)	int i = 0;
    	do {
    		System.out.println("*");
    		i += 7;
    	} while (i <= 35);
    
    (c)	for (int i = 1; i < 200; i*=2) {
    		System.out.println("*");
    	}
    
    (d)	for (int i = 1; i <= 9; i++) {
    		for (int j = i; j <= 10-i; j++) {
    			System.out.println("*");
    		}
    	}
    

  2. (1 pt) Write a Java code fragment that uses a do-while loop to ask the user to input an integer representing an hour between 1 and 12 (inclusive). If the user enters an integer out of range, ask the user again until a valid integer is read in. If the user enters an invalid hour, you should output an error message also. You may assume that the user enters integers only.

  3. (2 pts) Consider the sum of the first n positive odd integers: 1 + 3 + 5 + ... + (2n-1)

    1. Complete the following static method that requires an integer parameter for the value of n. This method computes and returns the sum of the first n positive odd integers using a loop. You may assume that n is positive when this method is executed.

      public static _____________ computeSum(int n) {
      	int sum = 0;
      	int nextValue = 1;
      	for (int i = 0; ___________________; i++) {
      		sum += nextValue;
      		nextValue = ______________________;
      	}
      	return ____________________;
      }
      

    2. Trace the method above for n = 7 to check your answer. Show the values of sum and nextValue after each iteration of the loop.

    3. Assume this method is in the same class as the main method. Show how to call this method from the main method to print out the value of the sum 1 + 3 + 5 + 7 + 9 + 11 + 13.

    4. Show that the following method is equivalent to the method given above for any positive integer n.

      public static int computeSum(int n) {
      	return n * n;
      }
      

      HINT: Show that 1 + 3 + 5 + ... + (2n-1) = n2.

  4. (1 pt) Complete the static method below that counts and returns the number of occurrences of a desired character in a message. The method requires two parameters, a string containing the message and a char containing the desired character that we are looking for.

    public static ________ count(String message, char desiredChar) {
    	int counter = 0;
    	for (int i = _________; i < _______________; i++) {
    		if (message.charAt(i) == desiredChar) {
    			_____________________;
    		}
    	}
    	return counter;
    }
    

  5. (1 pt) Using the method from the previous problem, write a main method that asks the user for a message, converts the message to uppercase only, and then outputs the number of occurrences of each letter of the alphabet in the message. HINT: After you read in the message, create a string with the letters of the alphabet. Then in a loop, use the charAt method to extract each letter of the alphabet one at a time and call the method from the previous problem to get the count of each letter in the message.

    Example: If the input message is GATES HILLMAN CENTER, then the output would be:

    A 2
    B 0
    C 1
    D 0
    E 3
    
    etc.

  6. (1 pt) Complete the method below. The method uses a loop to print random integers in the range 1 to 9 (inclusive) until the number 9 is printed. This method does not return anything when it finishes.

    public static void printRandomSingleDigits() {
    
    
    }
    

  7. (2 pts) Using the Robot API given in class (or click HERE ), write static methods for the following operations. Think about the return type needed (does it return any computed answer?) and if it needs any parameters (does it need any additional information to do its job?).

    1. Write a method spinAround that causes the robot to spin around on the current tile n times. For example, if n = 2, the robot will turn left 8 times so it spins around twice. You may assume n is a positive integer.

    2. Write a method countDark that moves the robot forward, counting the number of dark tiles it moves over until it cannot move forward anymore (i.e. when it is not clear to move forward). NOTE: It is possible that the robot might be currently in a position where it cannot move forward.