Lab Assignment, Page 1

Loop Lab Assignment

The purpose of this lab assignment is for you to become more comfortable using some of the concepts we have talked about recently (loops, if/else, comparison logic, and input/output) and in general to become better at programming.

Your task is to create a program that is capable of doing a variety of tasks at the request of a user. Later we will use these methods with our Menu object. To accomplish this task, for now you will build individual methods until the final program is complete.

Task 1: Calculate factorials (iteratively)

Your next task is to write a method that will get input from a user, and then calculate the factorial of the integer they provide. Recall that 0! = 1 and that the factorial is not defined for negative integers.

Task 2: Calculate the “additive factorial” (iteratively)

Your next task is to write a method that will get input from a user, and then calculate the “additive factorial” of the integer they provide. The “additive factorial” will calculate the sum of all numbers between 0 and the integer provided by the user. The “additive factorial” of 0 will obviously be 0, and the “additive factorial” is not defined for negative integers.

Task 3: Calculate Fibonacci numbers (iteratively)

Your next task is to write a method that will get input from a user, and then output the Fibonacci number the user has requested (if the user enters “5” then you should print out the fifth Fibonacci number). The nth Fibonacci number is defined as F(n) = F(n-1) + F(n-2), where F(0) = 0, F(1) = 1, and F(2) = 1 [because F(0) + F(1) = 0 + 1 = 1].

Task 4: Calculate the next perfect square (iteratively)

Your next task is to write a method that will get input from a user, and then output the first perfect square that is equal to or larger than the number input by the user. For instance, if the user enters “5” then you should print out “9”, since the prior perfect square “4” is smaller than the user input. But, if the user enters “4” then you should output “4”.

Task 0: Initialization

Your first task in this lab is to create the framework for running your solutions to the tasks that follow. You will create a general MathFramework class. In this class, you will have a constructor that creates an instance of the InputReader class called reader, which will be used to prompt the user for input. In addition, you will create a method that asks a user to select which of the tasks they want to do.

  1. First, in BlueJ, create a class declaration for the task at hand. Please name your class with your name followed by “Framework” (example: “KendraFramework”).
  2. Write the runMenu method. This method will:
  3. Print out an introduction
  4. Prints available tasks (by calling another method (getTasks) that returns a String of the available tasks)
  5. Prompt the user to choose which task they wish to select,
  6. Check to ensure that a valid task was selected,
  7. Run the task that was selected (if a valid task was selected),
  8. Print out the result of the task,
  9. Finally, the method should display the menu again after each task is completed, until the user chooses to quit the program.
  10. To test the runMenu method, I have written the following method to serve as the first task you will present your users. Type the following into your class, and then alter your runMenu method to allow you to choose this as an option. (After this step, you should have two available task options in your menu – running “Echo” or exiting the program).

public int echo () { return reader.getIntInput(); }

GENERAL HINTS:

  • When you ask for input, you should explain to the user what the task will accomplish once they enter input.
  • Your method must return an answer to the runMenu method to be printed. However, within each method you should explain to the user what the output represents.

Task 5: Creating a simple game (Guess the number)

The next task will be to create a series of methods that will represent more complex versions of the “guess the number” game. In general, the computer will generate a number within a given range, and then ask the user to guess the number.

In order for the computer to “pick” a number, we will use a random number generator built into java to select a number. I have provided the method you should call to select the number the computer has chosen.

public int chooseRandom (int range) {

double rand = Math.random();

// rand will have a value between 0.0 and 1.0

return (int) (rand * range);

}

First iteration

The first time you run your method, you should:

  1. Print out an introduction explaining what your game will do,
  2. Generate the number (providing a hard-coded range),
  3. Repeatedly ask the user to guess the number, telling them they got it either wrong or right, and exiting only when they get the correct answer.

I recommend that you hard-code a relatively small range for testing purposes.

Second iteration

In the second iteration, I will ask you to create a new method (use a different name) that will do everything that the first iteration will do, but will add the following two features.

  • This time, I want you to keep track of how many guesses the user takes to get to the correct answer, and tell the user how many tries it took them to get it right. (Optionally, you can create a way to “grade” their performance and tell them that too)
  • Also, each time the user makes a guess, I want you to tell them if the number they are looking for is higher or lower than their guess.

Third iteration

In the final iteration, you should allow the user to “set the difficulty” of the game by asking them to provide the range for the number generation phase. Again, please create a new method using a different name from the prior iterations.