Learn Java Easy. Chapter 7 Exercises

Chapter 7 Exercises

Exercise 1. Create an array containing the numbers from 0 to 9. Then, display them on the screen in reverse order.

 

Solution:

 

public class Main {
    public static void main(String[] args) {

        int a [] = new int [10];

        for (int i=0;i<=9;i++) {

            a[i] = i;
           
        }
 
        for (int i =9; i>=0;i--) {
            System.out.println(a[i]);
        }
    }
}



πŸ’‘ Initialize Arrays Properly: Before using an array, make sure to initialize it correctly with the appropriate size and necessary values. This prevents index out of range errors and ensures that the array has the data you need.

Exercise 2. Enter 10 numbers from the keyboard and then display them in reverse order, using an array.

 

Solution:

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int[] numbers = new int[10];

        for (int i = 0; i <= 9; i++) {
            System.out.println("Enter number " + (i + 1));
            numbers[i] = input.nextInt();
        }
        
        System.out.println("The reverse order of the entered numbers is:");
        for (int i = 9; i >= 0; i--) {
            System.out.println(numbers[i]);
        }

    }
}

Exercise 3. Generate 50 random numbers between 0 and 100, and then display their squares and cubes, using an array.

 

Solution:

 

public class Main {
    public static void main(String[] args) {

        int[] numbers = new int[50];

        for (int i = 0; i < 50; i++) {
            numbers[i] = (int)(Math.random() * 100);
            int square = numbers[i] * numbers[i];
            int cube = numbers[i] * numbers[i] * numbers[i];
            System.out.println("Number " + numbers[i] + ", Square " + square + 
                               ", Cube " + cube);
        }
    }
}

πŸ’‘ Always verify that the indices used are within the array’s bounds. Accessing indices outside theallowed range can cause runtime errors.

Exercise 4. Create a program that displays 10 random numbers from 0 to 100. Next to each number, show whether it is even or odd.

 

Solution:

 

public class Main {
    public static void main(String[] args) {

        int a[] = new int [10];

        for (int i=0;i<10;i++) {

            a[i] = (int)(Math.random()*100);


            if (a[i]%2==0) {
                System.out.println(a[i] + " its even");
            }
            if (a[i]%2!=0) {
                System.out.println(a[i] + " its odd");
            }

        }
    }
}

πŸ’‘ Use Loops to Traverse Arrays: Use for loops to efficiently traverse array elements. Loops facilitate the manipulation and access of elements, allowing operations such as additions, searches, and updates to be performed quickly.

Exercise 5. Create a program that displays 10 numbers entered from the keyboard. Next to each number, show whether it is even or odd.

 

Solution:

 


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int[] numbers = new int[10];

        for (int i = 0; i <= 9; i++) {
            System.out.println("Enter a number");
            numbers[i] = input.nextInt();
        }

        for (int i = 0; i <= 9; i++) {
            if (numbers[i] % 2 == 0) {
                System.out.println(numbers[i] + " is even");
            }
            if (numbers[i] % 2 != 0) {
                System.out.println(numbers[i] + " is odd");
            }
        }
    }
}

Exercise 6. Exercise where we enter 10 numbers from the keyboard and move the last number to the first position.

 

Solution:

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int[] numbers = new int[10];

        // We create a for loop to enter the ten numbers
        for (int i = 0; i < 10; i++) {
            System.out.println("Enter a number");
            numbers[i] = input.nextInt();
        }

        // Now we will display the last number as the first one
        System.out.println(" ");
        System.out.println("The modified list is ");
        System.out.println(" ");

        System.out.println(numbers[9]);
        for (int i = 0; i < 9; i++) {
            System.out.println(numbers[i]);
        }
    }
}

Exercise 7. Program that creates 50 random numbers between 0 and 100. We create a searcher that looks for a specific number and replaces it with another number that we will also specify.

 

Solution:

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter the number you want to replace");
        int original = input.nextInt();

        System.out.println("Enter the number you want to replace it with");
        int replacement = input.nextInt();

        int[] numbers = new int[50];

        // We create a for loop to generate 50 random numbers
        for (int i = 0; i < 50; i++) {
            numbers[i] = (int)(Math.random() * 100);
            if (numbers[i] == original) {
                numbers[i] = replacement;
            }
        }

        System.out.println("Here's the modified list of numbers");

        for (int i = 0; i < 50; i++) {
            System.out.println(numbers[i]);
        }
    }
}

Exercise 8. Create a Java Program that classifies 10 numbers greater than 0, entered via keyboard, into even or odd, using an array.

 

Solution:

 


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        // Create an array to store the 10 numbers
        int[] numbers = new int[10];
        // Create an array to store even numbers
        int[] evenNumbers = new int[10];
        // Create an array to store odd numbers
        int[] oddNumbers = new int[10];

        // Counters for even and odd numbers
        int evenCount = 0;
        int oddCount = 0;

        // Create a loop to enter 10 numbers
        // They will be stored in our numbers array
        // Depending on whether they are even or odd,
        // they will also be stored in evenNumbers/oddNumbers

        for (int i = 0; i < 10; i++) {
            System.out.println("Enter a number");
            numbers[i] = input.nextInt();

            if (numbers[i] % 2 == 0) {
                evenNumbers[evenCount++] = numbers[i];
            } else {
                oddNumbers[oddCount++] = numbers[i];
            }
        }

        System.out.println(" ");
        System.out.println("Even numbers");

        // Display even numbers
        // In positions where there's no data for the array,
        // the default value is 0.
        // That's why we use an if statement to only show numbers greater than 0

        for (int i = 0; i < evenCount; i++) {
            if (evenNumbers[i] > 0) {
                System.out.print(evenNumbers[i] + " ");
            }
        }
        System.out.println(" ");
        System.out.println(" ");
        System.out.println("Odd numbers");
        for (int i = 0; i < oddCount; i++) {
            if (oddNumbers[i] > 0) {
                System.out.print(oddNumbers[i] + " ");
            }
        }
    }
}

Exercise 9. Java program that displays 50 numbers from 1 to 100. Then, shows which is the maximum value, using an array.

 

Solution:

 

public class Main {
    public static void main(String[] args) {

        int[] numbers = new int[50];
        int maximum = 0;

        for (int i = 0; i < 50; i++) {
            numbers[i] = (int)(Math.random() * 100 + 1);

            if (numbers[i] > maximum) {
                maximum = numbers[i];
            }
        }

        System.out.println("The largest number is " + maximum);

    }
}

Exercise 10. Array exercises with char and String type variables. Create an array containing these words: “Hello”, “friends”, “how”, “are”. Then, create another array containing the following symbols: ^, &, *, %. Finally, display everything on screen.

 

Solution:

 

public class Main {
    public static void main(String[] args) {

        String[] words = new String[4];
        char[] symbols = new char[4];

        words[0] = "hello";
        words[1] = "friends";
        words[2] = "how";
        words[3] = "are";

        symbols[0] = '*';
        symbols[1] = '&';
        symbols[2] = '^';
        symbols[3] = '%';

        for (int i = 0; i < 4; i++) {
            System.out.println(words[i]);
        }
        for (int i = 0; i < 4; i++) {
            System.out.println(symbols[i]);
        }
    }
}

Did you like it? Don’t keep it to yourself β€” share it like juicy gossip! 😏