All Exercises in This Part
Create a new IDEA project named Week1 (see the Create a New Project guide).
Add a module named Task11 to the project (see the Create a Java Module guide).
Add a Java Compact File named MyDetails.java to the project. Write a program in the file that prints, each on a separate line:
- your name,
- the brand of your phone,
- the model of your phone.
Create a program that prints the number of words contained in a string entered through the console. You may assume that words are separated by a single space. Other punctuation marks do not need to be considered.
The program must print "No words" if the input is an empty string.
The program may terminate after displaying the output. It does not need to keep asking for input continuously.
Test your program by reading a string from the user and printing the number of words, for example:
Number of words: 5
Create a program that reads non-negative integers from the user, one line at a time, in a loop using the IO.readln() method, until the user enters an empty string.
Store these numbers in a list.
Then write the methods sum, average, smallest, and largest, which calculate the smallest number in the list, the largest number in the list, the sum of the numbers, and the average of the numbers. Print the values returned by these methods to the user.
Add appropriate handling for empty lists to the methods and document them.
You may assume that the user enters only non-negative integers as input. If a method receives an empty list as input, it must return the value -1.
The use of built-in methods from the Collections class, such as Collections.min() and Collections.max(), is prohibited.
Hint
You can convert a string to an integer using the
Integer.parseInt(number) method.
Create a method called compressString that takes a String as a parameter
and returns a new compressed String in such a way that consecutive identical
characters are represented by the character followed by its count.
For example:
compressString("aaaabbbccd")would return"a4b3c2d1",compressString("00666663332222222")would return"02653327",compressString("Niiiiiin")would return"N1i6n1",compressString("nnNNnnnN")would return"n2N2n3N1",compressString("ohjelmointi")would return"o1h1j1e1l1m1o1i1n1t1i1".
Also write an appropriate documentation comment for the method and add
at least one usage example to the main() method.
Tip 1
Use a StringBuilder and its methods to construct the resulting string.
Tip 2
Try solving the problem manually on paper first for a short string
(e.g., "aabb"). What information needs to be tracked (variables)?
Tip 3
It may be easier to start from the second character and always compare it to the previous one:
Create a function called isPasswordStrong that checks the strength of a password (String) passed as a parameter.
The password must be at least 8 characters long, contain one digit, and include at least one uppercase and one lowercase letter.
The function returns true if the password provided as a string meets the above requirements. Otherwise, it returns false.
Also write an appropriate documentation comment for the method and add at least one usage example to the main() method.
Hint
Take a look at the Character class method isDigit.
For example, by calling Character.isDigit(character), the return value is true if the character is a digit and false if it is not.
The methods isUpperCase and isLowerCase, which can also be found in the same class, work according to the same principle.
Create a method called printMatrix that prints all the numbers in the integer matrix given as a parameter on a single line.
The method does not return anything.
Example of how the method works:
int[][] matrix = {
{42, 2, 3},
{4, 67, 6},
{7, 8, 9}
};
printMatrix(matrix);
The output should then be:
42 2 3 4 67 6 7 8 9
Create a program that asks the user for input and prints how many times each digit (0-9) appears in the input.
For example, if the user enters:
12223
the program should print:
1: 1 pcs
2: 3 pcs
3: 1 pcs
Similarly, if the input is:
10002244412
the program should print:
0: 3 pcs
1: 2 pcs
2: 3 pcs
4: 3 pcs
After printing the results, the program asks the user for new input. If the user enters an empty input, the program terminates.
Tip
A single character can be converted to an integer by combining
Character.toString and Integer.parseInt:
int digitAsNumber = Integer.parseInt(Character.toString(a));
Note that Integer.parseInt assumes that the given string is actually a number. If it contains anything other than numeric characters, the function throws an exception.
You can check whether a single character is a digit by using the Character.isDigit method.
Create a function
int missingNumber(int[] array)
The function is given an array as a parameter that contains the numbers
1 through N in random order, but one of the numbers is missing.
The function must return the missing number.
The function must not cause any side effects.
Examples:
missingNumber(new int[] { 1, 6, 3, 4, 5 })returns 2missingNumber(new int[] { 8, 2, 4, 1, 3, 5, 6 })returns 7missingNumber(new int[] { })returns 1missingNumber(new int[] { 2 })returns 1
You can use the helper function below to generate a random array:
int[] generateInput(int maxNumber) {
Random r = new Random();
List<Integer> numbers = new ArrayList<>(IntStream.range(1, r.nextInt(2, maxNumber + 1)).boxed().toList());
Collections.shuffle(numbers);
numbers.remove(r.nextInt(numbers.size()));
return numbers.stream().mapToInt(Integer::intValue).toArray();
}
You can use the method as follows:
// Creates an random array of size 1-10, but with one number missing
int[] input = generateInput(10);
// Determine which number is missing
int missing = missingNumber(input);
Print all prime numbers between 1 and 100.
A prime number is an integer greater than 1 that is divisible only by itself and by 1.
Hint
First, create a function called isPrime that takes an integer as a parameter and returns a boolean value indicating whether the number is prime or not. Use this function in the main program, where you print all prime numbers between 1 and 100 using a loop.