Java Exercises for programmers with intermediate advanced knowledge. In these Java Exercises,
you will begin to use functions, Classes, OOP object-oriented programming, and file management.
Functions: Greeting + Farewell Create a program whose Main must be like this:
public static void Main()
{
SayHello();
SayGoodbye();
}
SayHello and SayGoodbye are functions that you must define and that will be called from inside Main. |
Function With Parameters Create a program whose Main must be like this:
public static void Main()
{
SayHello("John");
SayGoodbye();
}
SayHello and SayGoodbye are functions that you must define and that will be called from inside Main. As you can see in the example. SayHello must accept an string as a parameter. |
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 that you must define and that will be called from inside Main. As you can see in the example, it must accept two integers as parameters, and it must return an integer number (the sum of those two numbers).
|
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", ____CountSpaces("Hello, how are you") );
}
CountSpaces is a function that you must define and that will be called from inside Main.
As you can see in the example, it must accept an string as a parameter, and it must return an integer number (the amount of spaces in that string). |
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("Hello!"); |
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 underline it (writing several hyphens under that word):
WriteUnderlined("Hello!"); |
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 = {20, 10, 5, 2 };
Console.WriteLine(
__"The sum of the example array is {0}", __Sum(example));
}
}
|
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 and you must use "refererence parameters". For example.
x = 5;
Double(ref x);
Console.Write(x);
would display 10 |
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=3;
Swap(ref x, ref y);
Console.WriteLine("x={0}, y={1}", x, y);
(which should write "x=3, y=5") |
Showing 10 to 98 java exercises
List of Java Exercises for Intermediate Grouped
All levels of the Java language and for everyone
Java exercises for all levels. Whether you are a beginning programmer or an advanced programmer. You can practice Java exercises online, easy and fast