Lab Assignment #6: Functions

Due by 11:59PM on Wednesday, March 10.


Reading Assignment

This lab deals with implementing functions in Java. This is material from Chapter 5 in the text. Read sections 5.1 - 5.2 and 5.4 - 5.11. The book discusses functions in the context of a protracted example involving the computation of compound interest on a savings balance. You do not need to understand the formula the book uses in this computation in order to understand the function mechanism.


In-Lab Work

This week during the lab you will finalize the formation of your project teams. If you do not yet have a team, or are in a team that has less than the minimum (4) number of members, please tell your AI and you will be able to choose your team members. Please make sure you exchange your contact information (name, email, phone numbers etc.) among you so that you can keep in touch easily. For the rest of the semester you will be working closely together, so it is a good idea to get used to this.

The main exercise in the lab this week will involve defining some functions. We are going to write another applet today, so you may want to find the templates in Lab 4 useful.

The basic idea

You will write a simple program that asks the user to click on a point on the canvas, and ask the user for a radius, and draw a circle (like you did in the in lab before). This time, in addition to drawing the circle, you are also going to calculate the area of the circle, and display the area at the center of the circle. Note that you may want to repeat this, so that the user can draw many circles. To be interesting, you may want to change the paint color before drawing subsequent circles.

Your AI will go over the "functional decomposition" of the problem. This is basically identifying the blocks of code that belong together, and moving them into functions. Pay close attention to how the parameters are passed, and how outputs are returned. You may want to use one function that draws the circle, and another one that calculates the area. Think of other ways to improve the program, and then see how you can easily extend this program to increase the number of circles.

Save this program as Circles.java and submit it by emailing it as an attachment to a201@cs.indiana.edu with the subject line given to you in class.


Lab Assignment

Okay, looks like everyone is pretty tired of the piglatin program, so we will stick to the one of the guessing games in the lab.

  1. For the first problem of this lab, you are going to take the guessing game you developed in the last lab and improve the design using a function. A description of the game is reproduced here for your convenience.

    The game will go like this. The program will randomly select a number between 1 and 15, and ask the user to guess what the number was. The user will then have four chances to guess this number. After each guess, the computer should say "Nope, too big" or "Nope, two small" (or something to that effect - be imaginative) or if the user guesses correctly, should say "Yes, you got it!" If after 4 chances, the user still hasn't guessed the number, then the computer should say a message telling the user what the number was.

    In this week's lab, you will create a function that deals with each of the choices. The function will do all the necessary inputs and outputs, and return a boolean value. The function should return true if the user guessed the number, and false if he didn't guess the number. The input to the function would be the number that the computer generated, and an integer ordinal (1, 2, 3, 4 etc.) indicating the current choice. The output of the program will be slightly different from last week:

        Okay, I have selected a number. You have four choices to guess it...
        Enter your guess #1 ....  10
        Nope, too big!
        Enter your guess #2.... 5
        Nope, too small!
        Enter your guess #3.... 7
        Nope, too small!
        Enter your guess #4.... 8
        Sorry, my selected number was 9! Thanks for playing.
        

    NOTE: You can play with the inputs and outputs of this function. As suggested in the class, this function may simply return the current guess which the main program may compare with the generated number, or any other way that you can come up with.

    Name this program Youguess2.java. Did the function help in reducing the size of your program?

  2. In this second program, you are going to design a rudimentary darts game. In this game, you will draw a darts board, and ask the player to "shoot" his/her darts by clicking on the board. The "bullseye" in the board is worth 50 points, the middle ring is worth 25 points, and the outer ring is worth 10 points. The area outside the ring is is worth zero points. The board should look like the following:

    A player may "throw" three dirts. Each dart is thrown by clicking somewhere on the canvas. After each dart is thrown, the player's score is displayed. After all three darts are thrown, the total score is displayed.

    Try to make your program as efficient as possible. That is, try to reuse old objects rather than always creating new ones.

    Demonstration:

    Here is a working demonstration of this program.

    Function design

    Look and Feel:


Submitting Your Lab Assignment

For this lab, you should submit two files:

Do not submit .prj, .class or .html files. Only the files named above should be submitted.

In order to submit files, click on the following link to send mail to:

a201@cs.indiana.edu

with the following Subject:

lab6

and then Attach your program files to the e-mail message. You may attach multiple files in the same message, but you don't have to. You can submit your programs in separate e-mails if you wish. Do not type anything in the body of the message! You must use the filenames specified in this assignment and you must attach the files to the mail message.