CS107 - Lab 7

Java Programming (methods)

In this lab you will work on methods in Java.

General guidelines:


  1. Printing patterns

    Write a program that prints the following patterns on the screen:

    **********
     ********
      ******
       ****
        **
    
    The first line contains 10 stars. Use a constant variable to store this value.

    Your code should use the folowing two methods:

    /* This method prints n stars on the same line, no newline */
    void printStars(int n) {
       ...
    }
    
    /* This method prints n spaces on the same line, no newline */
    void printSpaces(int n) {
       ...
    }
    
    The body of your main shoud use these two methods to print the pattern. For instance, the code below
    printStars(8);
    System.out.println();
    int i = 0;
    while (i <= 4)  {
       printStars(2);
       printSpaces(4);
       printStars(2);
       System.out.println();
       i++;
    }
    printStars(8);
    System.out.println();
    
    gives the following pattern:
    ********
    **    **
    **    **
    **    **
    **    **
    ********
    

  2. I'm thinking of a number...

    Write a program that implements a binary-search-like guessing game: the program picks a random number and asks the user to guess it; the user is told, for each guess, whether it is too high or too low.

    Your program should:

    A skeleton of the program is given below. Design your program so that your main program looks exactly as below.

    /*
    A skeleton for the guessing problem.
    */
    
    //import  class (type) Random, which provides support for generating random nb
    import java.util.Random;
    
    public class Guess {
    	
    	// these variable are visible to all methods 
    	final static int GUESS_LIMIT = 1024; 
    	final static int NB_GUESSES = 10;
    
    	static Random rand = new Random(); // a random number generator
    
    
    	public static void main (String args[]) {
    				
    		//explain the program to the user. 
    		explainProgram();
    		
    		//generate a target from 1 to GUESS_LIMIT, inclusive
    		int target = generateTarget(GUESS_LIMIT);
    		
    		//allow the user to play the guessing game; if the user does not
    		//guess the answer in NB_GUESSES guesses they lose 
    		boolean won = playGuessingGame(NB_GUESSES, GUESS_LIMIT, target);
    		
    		//let them know whether they won or not
    		provideFeedback(won);
    
    	}
    
    
    	/* This method explains the program to the user. It does not ask
    	   whether they want to use it --- assume they do.  
    	*/
    	static	void explainProgram() { 
    
    	   // fill in 
    
    	}
    
    
    
    	/* This method generates a random number in the range 
    	   from 1 to upperLimit, inclusive.
    	*/
    	static int generateTarget(int upperLimit) {
    
    	   //read comments below 
    
    	}
    	
    
    	 /* This method  plays the guessing game; if the user does not
    		guess the answer in nguesses guesses they lose. The methods 
    		plays the game until it can decide whether the user won or lost. 
    		At that point it returns the result (without printing it). 
    	*/
    	 static boolean playGuessingGame(int nguesses, int guessLimit, int target) {
    		
    		// fill in
    		
    	}
    	
    	/* This method tells the user whether they won or lost */ 
    	static void provideFeedback(boolean won) {
    		
    		// fill in
    
    	}
    	
    
    } // end of class
    
    
    Note that the way the functions are used in main tells you exactly what the function header (the function header is the first line of a function that specifies the return type, function name and parameter list) should look like. For example, given this function:
    int target = generateTarget(GUESS_LIMIT); 
    
    you know that the function returns an integer value and has a single parameter which must be an integer. So the function will look like this:
    
    int generateTarget(int upperLimit) { 
    
    }
    
    Also, from the way it is used in main(), you know that this method is supposed to generate a random number in the range 1..upperLimit inclusive and return this value.

    In this problem, the headers and functionality of the methods are written for you. As you'll get more experience with methods, you will start figuring these out yourselves, just from the way they are called.

    To generate a random number, use the type (class) Random, more precisely the variable rand, which is declared and created at the top. The class Random has a method called nextInt(int n), which returns a random number in the range 0 ..(n-1). Thus, when calling

    rand.nextInt(10)
    
    this will return a random number in the range 0..9 inclusive.

    If you are interested, check out more documentation on Java Random class.

    Question: Does the user have a fair chance to win the game? How is that related to the value of GUESS_LIMIT? What happens if we make it smaller? larger?

  3. Writing poetry

    In this program you will generate (random) poetry. Take a look at the following skeleton program, Poet.java.

    Read it carefully and understand what each function does, and how all functions work together to generate verses and stanzas.

    There is (just) one rule for generating a verse:

    verse ==> 'The' adjective noun verb adjective object
    Your task is to extend this program to generate better poetry. For instance, you could add more words to the word lists, add more word lists (prepositions, interjections, et cetera), add different rules for creating verses, add rhyme, do not use the same word twice in a verse, add stanzas with different number of verses, add more stanzas, et cetera. This are just some examples, use your imagination!!


What to turn in:

Send me only the .java files (not the entire folder), all in the same email, as attachments.