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);

0 (product initialized to 0, so all multiplications result in 0) B. String fred = "cast orhotcakes";    for (int i = 0; i < fred.length(); i += 2)    {       System.out.print(fred.charAt(i));    } cs rocks C. String alphabet = "abcdefghijklmnopqrstuvwxyz";    int index = 0;    while (alphabet.charAt(index) != 'g') {       System.out.print(alphabet.charAt(index));       index = index + 2;    } ace // 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))); 3       System.out.println(g(f(5))); 1.0 (note g returns a double)    }    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 void printStars(int maxStars) {

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

    for (int i = maxStars; i > 0; i--) {
    
        for (int j = 0; j < i; 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.


public static int blackjack(int a, int b) {
    if (a > 21 && b > 21) {
        return 0;
    } else if (a > 21) {
        return b;
    } else if (b > 21) {
        return a;
    } else if (a > b) {
        return a;
    } else {
        return b;
    }
}

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.getWeight() < 2000) {

        c.eat(100);

        counter++;
    }
    
    return counter;
}