Skip to the content.
« Previous task Practice tasks Next task »

3. Basic Java Syntax. Strings, Arrays, java.lang.Math

Create a new Eclipse project named task03. Use a package named ua.khpi.oop.your_first_name.task03 for all created classes.

1) Make a new class whose main method flips a coin K times, printing “heads” or “tails” each time.

Hint: java.lang.Math.random() returns a double between 0 and 1.

2) Make a new class whose main method creates an array of M random doubles (each between LOW and HIGH). Use one-step array allocation. Loop down the array and print out the values.

Hint: Declare and allocate array in one fell swoop:

int[] values = { 10, 100, 1_000 };
String[] names = { "Joe", "Jane", "Juan" };

3) Make a new class whose main method creates an array of N random numbers. Use two-step array allocation. After the array has been created, loop down the array, calculate and print out the following:

Hint: Building arrays in two steps:

4) Make a new class whose main method prints out the number of command line arguments: “You supplied x arguments.”.

Hint: How to specify command line parameters in Eclipse

Menu: Run / Run Configurations / Arguments -> Program arguments

5) Print the command line arguments in reverse order, converted to uppercase.

6) Modify the coin-flipping program of subtask #1 to flip a coin the number of times the user specifies as command line arguments.

7) Make a new class.

Your overall code will look roughly like this:

public static void main(String[] args) {
	String[] testStrings = { "Hello", "Hi", "Hola", "Howdy" };
	if (isStringInArray(testStrings, "Hola")) {
		// TODO
	}
	if (isStringInArray(testStrings, "Hey")) {
		// TODO
	}
}
public static boolean isStringInArray(String[] strings, String potentialMatch) {
	// TODO
}


« Previous task Practice tasks Next task »