Function write underlined Java Exercise - Java Programming Course

 Lesson

Functions

 Exercise

Function write underlined

 Objetive

Write a java function able to write centered on screen the text that is indicated as a parameter (supposing a screen width of 80 characters) and then underline it (writing several hyphens under that word):

WriteUnderlined("Hello!");

 Example Code

public class Main
{
	public static void WriteUnder(String text)
	{
		int countSpaces = (80 - text.length()) / 2;
		int i = 0;

		for (; i < countSpaces; i++)
		{
			System.out.print(" ");
		}

		System.out.println(text);

		for (i = 0; i < countSpaces; i++)
		{
			System.out.print(" ");
		}

		for (i = 0; i < text.length(); i++)
		{
			System.out.print("_");
		}
	}

	public static void main(String[] args)
	{
		WriteUnder("Hello");
	}
}

More Java Exercises of Functions

 Functions: greeting + farewell
Write a Java program whose Main must be like this: public static void Main() { SayHello(); SayGoodbye(); } SayHello and SayGoodbye are funct...
 Function with parameters
Write a Java program whose Main must be like this: public static void Main() { SayHello("John"); SayGoodbye(); } SayHello and SayGoodbye are...
 Function returning a value
Write a Java program whose Main must be like this: public static void Main() { int x= 3; int y = 5; Console.WriteLine( Sum(x,y) ); } "Sum" ...
 Function returning a value V2
Write a Java program whose Main must be like this: public static void Main() { __Console.WriteLine("\"Hello, how are you\" contains {0} spaces", ...
 Function write centered
Write a Java function to write centered on screen the text that is indicated as a parameter (supposing a screen width of 80 characters): WriteCente...
 Function sum of array
Write a Java program to calculate the sum of the elements in an array. "Main" should be like this: public static void Main() { int[] example = {20...
 Function double
Write a Java function named "Double" to calculate and return an integer number doubled. For example. Double(7) should return 14....
 Function Double reference parameter
Write a Java function named "Double" to calculate the double of an integer number, and modify the data passed as an argument. It must be a "void" func...
 Function swap reference parameters
Write a Java 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...
 Function power local variables
Write a Java function named "Power" to calculate the result of raising an integer number to another (positive integer) number. It must return another ...
 Function recursive power
Write a Java function that calculates the result of raising an integer to another integer (eg 5 raised to 3 = 53 = 5 × 5 × 5 = 125). This function mus...
 Function Fibonacci
Write 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 elements...
 Function modify a letter in a string
Write a Java function named "ChangeChar" to modify a letter in a certain position (0 based) of a string, replacing it with a different letter: stri...
 Function IsPrimeTarea
Write a Java function named "IsPrime", which receives an integer number and retuns true if it is prime, or false if it is not: if (isPrime(127)) .....
 Function Parameters of Main, Sum
Write a Java 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
Write a Java 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 wo...
 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...
 Function Parameters of Main, Reverse
Write a Java program named "reverse", which receives several words in the command line and displays them in reverse order, as in this example: reve...
 Function GetInt
Write a Java function named "GetInt", which displays on screen the text received as a parameter, asks the user for an integer number, repeats if the n...
 Function tasks database
Write in Java an improved version of the "tasks database", splitting it into functions....
 Function Greatest value in a array
Write a Java function which returns the greatest value stored in an array of real numbers which is specified as parameter: float[] data={1.5f, 0.7f...
 Function factorial (iterative)
Write an Java iterative (non-recursive) function to calculate the factorial of the number specified as parameter: Console.Write ( Factorial (6) ); ...
 Function WriteTitle
Write a Java function named "WriteTitle" to write a text centered on screen, uppercase, with extra spaces and with a line over it and another line und...
 Function return value for Main
Write 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 sp...
 Function CountDV
Write a Java function that calculates the amount of numeric digits and vowels that a text string contains. It will accept three parameters: the string...
 Function IsAlphabetic
Write a Java function that tells if a character is alphabetic (A through Z) or not. It should be used like this: if (IsAlphabetic ("a")) System.Co...
 Function IsNumber
Write a Java function that tells if a string is an intenger number. It should be used like this: if (IsNumber ("1234")) System.Console.WriteLine (...
 Function calculator, params of Main
Write 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
Write a Java program to calculate a sum, subtraction, product or division, analyzing the command line parameters: calc 5 + 379 (Parameters must ...
 Function MinMaxArray
Write a Java function named MinMaxArray, to return the minimum and maximum values stored in an array, using reference parameters: float[] data={1.5...
 Function Reverse, recursive
Write a Java program that uses recursion to reverse a string of characters (for example, from "Hello" it would return "olleH")....
 Function WriteRectangle
Write a Java function WriteRectangle to display a (filled) rectangle on the screen, with the width and height indicated as parameters, using asterisks...
 Function Palindrome, iterative
Write an Java iterative function to say whether a string is symmetric (a palindrome). For example, "RADAR" is a palindrome....
 Function Palindrome, recursive
Write a Java recursive function to say whether a string is symmetric (a palindrome). For example, "RADAR" is a palindrome....
 Function GetMinMax
Write a Java function named "GetMinMax", which will ask the user for a minimum value (a number) and a maximum value (another number). It should be cal...
 Function Multiply & MultiplyR
Write two Java functions, Multiply and MultiplyR, to calculate the product of two numbers using sums. T he first version must be iterative, and the se...


Juan A. Ripoll - Programming Tutorials and Courses © 2024 All rights reserved.  Legal Conditions.