Programming and Computer Science in Java:

Methods (How To Think, Ch 3.5 - 3.11)

See Course Home Page.

**Due
Date: Mon, Mar 10, 2003**

First, read Chapter
3.5 - 3.11 from "How To Think Like a Computer Scientist". Read it carefully (there can be a *pop quiz* anytime
after a chapter is assigned....).

Next, do the following problems. Be sure to submit a separate, working program for each of the following problems!

** Question 1**. Write a Java method with the following signature:

public static int readIntWithPrompt()

This method should, as its name implies, first print a suitable prompt, and then read an integer, and finally return the value of the integer read. Demonstrate this new method works by adding a main method which calls this new method three times and then exits, giving the following behavior:

Enter an integer: 2 You entered a 2. Enter an integer: 5 You entered a 5. Goodbye!

Note that readIntWithPrompt is outputting "Enter an integer", and your main method should be outputting "You entered a ...".

** Question
2.** Now we wish to make the prompt itself a parameter to the
method, so that the same method can provide different prompts. To do this,
change the signature to:

public static int readIntWithPrompt(String prompt)

Of course, you must change the method to actually use the newly-supplied prompt. Also, change your main method to demonstrate this new method by calling it twice, each time with a different prompt, as in:

```
Enter an integer: 2
You entered a 2.
Enter another integer: 5
You entered a 5.
Goodbye!
```

** Question 3:** Now add a min and a max...

public static int readIntWithPrompt(String prompt, int min, int max)

Call it twice to produce the following behavior:

Enter an integer in [0,100]: -3 That is out of range. Try again. Enter an integer in [0,100]: 5 You entered a 5. Enter another integer in [6,10]: 5 That is out of range. Try again. Enter another integer in [6,10]: 6 You entered a 6. Goodbye!

Note that readIntWithPrompt should print out the "in [0,100]" portion of the prompt (so the prompts from main are "Enter an integer" and "Enter another integer" in the example above).

** Question 4:**
First, write a Java method with the following signature:

public static boolean dividesEvenly(int numerator, int denominator)

This method should return true if and only if the "numerator" is divided evenly by the "denominator", and false otherwise.

Next, write a Java method with the following signature:

public static boolean isEven(int n)

This method should return true if and only if the argument is even, and false otherwise. Note that isEven must call dividesEvenly (which makes sense -- a number n is even if it is divided evenly by 2).

Finally, write a main function which uses readIntWithPrompt and isEven to repeatedly read in an integer n and output whether or not n is even.

** Question 5:**
Write a Java method with the following signature:

public static boolean isPrime(int n)

This method should return true if and only if the argument is prime, and false otherwise. This method must also call dividesEvenly (since a number n is prime if no number between 2 and n-1 inclusive evenly divides n).

Write a main function which uses readIntWithPrompt and isPrime to repeatedly read in an integer n and output whether or not n is prime.

Good luck!

DK

See Course Home Page.