Function Countdv Java Exercise

Lesson

Functions

Exercise

Function Countdv

Objetive

Create a function that calculates the amount of numeric digits and vowels that a text string contains. It will accept three parameters: the string that we want to search, the variable that returns the number of digits, and the number of vowels, in that order). The function should be called "CountDV". Use it like this:

CountDV ("This is the phrase 12", ref amountOfDigits, ref amountOfVowels)

In this case, amountOfDigits would be 2 and amountOfVowels would be 5

Example Code

public class Main
{
	public static void CountDV(String answer, int amountOfDigits, int amountOfVowels)
	{
		amountOfDigits = 0;
		amountOfVowels = 0;

		for (int i = 0; i < answer.length(); i++)
		{
			switch (answer.substring(i, i + 1).toLowerCase())
			{
				case "a":
				case "e":
				case "i":
				case "o":
				case "u":
					amountOfVowels++;
					break;
				case "0":
				case "1":
				case "2":
				case "3":
				case "4":
				case "5":
				case "6":
				case "7":
				case "8":
				case "9":
					amountOfDigits++;
					break;
			}
		}
	}

	public static void main(String[] args)
	{
		int amountOfDigits = 0;
		int amountOfVowels = 0;

		CountDV("This", amountOfDigits, amountOfVowels);

		System.out.println(amountOfDigits);
		System.out.println(amountOfVowels);
	}
}

More Java Exercises of Functions

Functions: Greeting + Farewell
Create a program whose Main must be like this: public static void Main() { SayHello(); SayGoodbye(); } SayHello and SayGoodbye are functions...
Function With Parameters
Create a program whose Main must be like this: public static void Main() { SayHello("John"); SayGoodbye(); } SayHello and SayGoodbye are fun...
Function Returning A Value
Create a program whose Main must be like this: public static void Main() { int x= 3; int y = 5; Console.WriteLine( Sum(x,y) ); } "Sum" is a...
Function Returning A Value V2
Create a program whose Main must be like this: public static void Main() { __Console.WriteLine("\"Hello, how are you\" contains {0} spaces", ____...
Function Write Centered
Create a function to write centered on screen the text that is indicated as a parameter (supposing a screen width of 80 characters): WriteCentered(...
Function Write Underlined
Create a function able to write centered on screen the text that is indicated as a parameter (supposing a screen width of 80 characters) and then unde...
Function Sum Of Array
Create a Java program to calculate the sum of the elements in an array. "Main" should be like this: public static void Main() { int[] example = {2...
Function Double
Create a function named "Double" to calculate and return an integer number doubled. For example. Double(7) should return 14....
Function Double Reference Parameter
Create a function named "Double" to calculate the double of an integer number, and modify the data passed as an argument. It must be a "void" function...
Function Swap Reference Parameters
Create a function named "Swap" to swap the values of two integer numbers, which are passed by reference. An example of use might be: int x=5, y=...
Function Power Local Variables
Create a function named "Power" to calculate the result of raising an integer number to another (positive integer) number. It must return another inte...
Function Recursive Power
Create a function that calculates the result of raising an integer to another integer (eg 5 raised to 3 = 53 = 5 × 5 × 5 = 125). This function must be...
Function Fibonacci
Create a Java program that uses recursion to calculate a number in the Fibonacci series (in which the first two items are 1, and for the other element...
Function Modify A Letter In A String
Create a function named "ChangeChar" to modify a letter in a certain position (0 based) of a string, replacing it with a different letter: string s...
Function Isprimetarea
Create a function named "IsPrime", which receives an integer number and retuns true if it is prime, or false if it is not: if (isPrime(127)) ......
Parameters Of Main, Sum
Create a program named "sum", which receives two integer numbers in the command line and displays their sum, as in this example: sum 5 3 8...
Function Sumdigits
Create a function SumDigits that receives a number and returns any results in the sum of its digits. For example, if the number is 123, the sum would ...
Function Factorial
The factorial of a number is expressed as follows: n! = n · (n-1) · (n-2) · (n-3) · ... · 3 · 2 · 1 For example, 6! = 6·5·4·3·2·1 Create a r...
Parameters Of Main, Reverse
Create a program named "reverse", which receives several words in the command line and displays them in reverse order, as in this example: reverse ...
Function Getint
Create a function named "GetInt", which displays on screen the text received as a parameter, asks the user for an integer number, repeats if the numbe...
Function Tasks Database
Create an improved version of the "tasks database", splitting it into functions....
Greatest Value In A Array
Create a function which returns the greatest value stored in an array of real numbers which is specified as parameter: float[] data={1.5f, 0.7f, 8....
Function Factorial (Iterative)
Create an iterative (non-recursive) function to calculate the factorial of the number specified as parameter: Console.Write ( Factorial (6) ); w...
Function Writetitle
Create a function named "WriteTitle" to write a text centered on screen, uppercase, with extra spaces and with a line over it and another line under i...
Function Return Value For Main
Create a Java program in which you write a title (using the previous WriteTitle function) which the user will specify in command line. If no text is s...
Function Isalphabetic
Create a function that tells if a character is alphabetic (A through Z) or not. It should be used like this: if (IsAlphabetic ("a")) System.Consol...
Function Isnumber
Create a function that tells if a string is an intenger number. It should be used like this: if (IsNumber ("1234")) System.Console.WriteLine ("It ...
Function Calculator, Params Of Main
Create a Java program to calculate a sum, subtraction, product or division, analyzing the command line parameters: calc 5 + 379 (Parameters must...
Function Calculator, Params And Return Value Of Main
Create a Java program to calculate a sum, subtraction, product or division, analyzing the command line parameters: calc 5 + 379 (Parameters must...
Function Minmaxarray
Create a function named MinMaxArray, to return the minimum and maximum values stored in an array, using reference parameters: float[] data={1.5f, 0...
Reverse, Recursive
Create a program that uses recursion to reverse a string of characters (for example, from "Hello" it would return "olleH")....
Function Writerectangle
Create a function WriteRectangle to display a (filled) rectangle on the screen, with the width and height indicated as parameters, using asterisks. Co...
Function Palindrome, Iterative
Create an iterative function to say whether a string is symmetric (a palindrome). For example, "RADAR" is a palindrome....
Palindrome, Recursive
Create a recursive function to say whether a string is symmetric (a palindrome). For example, "RADAR" is a palindrome....
Function Getminmax
Create a function named "GetMinMax", which will ask the user for a minimum value (a number) and a maximum value (another number). It should be called ...
Function Multiply & Multiplyr
Create two functions, Multiply and MultiplyR, to calculate the product of two numbers using sums. T he first version must be iterative, and the second...