You are on page 1of 2

Method Exercises

5 programs each worth 5 marks = 25 marks

Project Name: Method Exercises


Classes: ☐ MagicSum.java ☐SmallestNum.java ☐ Vowels.java ☐ Password.java
☐ Exponent.java

MagicSum.java

Write a Boolean method called hasEight(), which takes an int as input and returns true
if the number contains the digit 8 (e.g., 18, 168, 1288).
Now write a main program called MagicSum, which prompts users for integers (or -1
to end) and produces the sum of numbers containing the digit 8. Your program
should use the above method. Here is a sample output.

Enter a positive integer (or -1 to end): 1


Enter a positive integer (or -1 to end): 2
Enter a positive integer (or -1 to end): 3
Enter a positive integer (or -1 to end): 8
Enter a positive integer (or -1 to end): 88
Enter a positive integer (or -1 to end): -1
The magic sum is: 96

SmallestNum.java
Write a Java method to find the smallest number among three numbers.  Here is
some sample output.

Input the first number: 25


Input the Second number: 37
Input the third number: 12
The smallest number is: 12
Vowels.java
Write a Java method to count all vowels in a string. 

Input the string: Banana


Here is how many vowels are in the word: 3

Challenge: You could add a counter that keeps track of how many of each vowel
there is. You could also allow your program to look at phrases, not just words.

Password.java (This is harder than you think, be careful!)


Write a Java method to check whether a string is a valid password. 

Password rules:

● A password must have at least ten characters.


● A password consists of only letters and digits. NO special characters.
● A password must contain at least two digits.

HINT: The ascii table will be a huge help here or even better regex!!!

Exponent.java

Write a method called exponent (int base, int exp) that returns an int value


of base raises to the power of exp. Assume that exp is a non-negative integer
and base is an integer. Do not use any Math library functions.

Sample Output:

Enter the base: 3


Enter the exponent: 4
3 raised to the power of 4 is: 81

You might also like