Intro Java Test #2

NAME_________________________ PERIOD ___________

INSTRUCTIONS

1. (5 points; 1 point each, except D which is worth 2 points--1 for each print.) What will the following output be for each of the following pieces of code? If you think that there is an error in the code, write "ERROR". Circle your answers. (Hint: Show your work.)

A. int product = 0;
   for (int i = 1; i < 5; i++)
   {
      product *= i;
   }
   System.out.print(product);


B. String fred = "cast orhotcakes";
   for (int i = 0; i < fred.length(); i += 2)
   {
      System.out.print(fred.charAt(i));
   }


C. String alphabet = "abcdefghijklmnopqrstuvwxyz";    
   int index = 0;

   while (alphabet.charAt(index) != 'g') {
      System.out.print(alphabet.charAt(index));
      index = index + 2;
   }


// Make sure to do both parts of part D!
D. public static void main(String args[]) // Assume this uses methods f and g, below
   {
      System.out.println(f(g(5)));
      System.out.println(g(f(5)));
   }

   
public static int f(double x)
   {
      return (int) (18 / x + 9);
   }

   public static double g(int x)
   {
      return x / 2 - 5;
   }

2. (5 points) We wish to write the printStars method such that printStars(4) prints:

*
**
***
****
***
**
*

Fill in the blanks so printStars takes an int as its lone argument and prints a pattern like the one above based on the value of the int. printStars does not return anything.

public _________________ printStars(_________________ maxStars) {

    for (int i = _________________; _________________; i++) {
        
        for (int j = 0; j < _________________; j++) {
        
            System.out.print("*");
        
        } 
        
        System.out.println();
    }

    for (int i = _________________; _________________; i--) {
    
        for (int j = 0; j < _________________; j++) {
    
            System.out.print("*");
    
        } 
    
        System.out.println();
    }
}


3. (5 points) Borrowed from Prof. Nick Parlante's CodingBat (http://codingbat.com):

Given 2 int values greater than 0, return whichever value is nearest to 21 without going over. Return 0 if they both go over.  In the case of a tie, return either value.

Sample output:

blackjack(19, 21) → 21
blackjack(19, 18) → 19
blackjack(17, 17) → 17
blackjack(23, 22) → 0
blackjack(19, 22) → 19
Write blackjack.



4. (5 points) Consider the following Cow class:

public class Cow {
	private double weight;
	
	public Cow(double w) {
		weight = w;
	}
	
	public double getWeight() {
		return weight;
	}
	
	// A random percentage of what is eaten by the cow
	// gets added to its weight.  Math.random() returns
	// a number on the interval [0,1).
	public void eat(double cud) {
		weight = weight + (Math.random() * cud);
	}
}

Write the method cowChow() that returns the number of times a Cow, that starts out weighing 1000 pounds, has to eat 100 pounds of cud before it weighs 2000 pounds. Note: a blank does not imply that a single word or number belongs there. It could be easily be a compound expression or a method with input(s), etc.

Here is a sample main() method that uses the Cow class and calls cowChow():

public static void main(String[] args) {
    Cow fatCow = new Cow(1000);

System.out.printl(cowChow(fatCow));

}

Now fill in the blanks to complete the cowChow() method.

public static int cowChow(Cow c) {

    int counter = 0;

    while (__________________________________ < _________________) {

        c.eat(_________________);

        counter++;
    }
    
	 __________________________________;
}