Learn Java Easy. Chapter 7 Exercises

Chapter 7 Exercises

Chapter 7 Exercises

Exercise 1. Create an array that contains the numbers from 0 to 9. Then make them display 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 Appropriately: Before using an array, make sure to initialize it correctly with the appropriate size and necessary values. This prevents out-of-range index 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 exercise = new Scanner(System.in); 

             int a[] = new int[10];

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

                   System.out.println("Enter number " + (i+1)); 

                   a[i] = exercise.nextInt();

               }

             System.out.println("The reverse order of the entered numbers is:"); 

               for (int i =9; i>=0;i--) {

                        System.out.println(a[i]);

               }

       }

}

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

Solution:

public class Main {

        public static void main(String[] args) { 

                         int a[] = new int[50];

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

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

                                 int square = a[i]* a[i];

                                 int cube = a[i] * a[i] * a[i];

                                 System.out.println("Number " + a[i] + ", Square " + square + ", Cube " + cube);

                         }
                             
         }

}

💡Check Array Bounds: Always verify that the indices used are within the array bounds. Accessing indices outside the allowed range can cause runtime errors.

Exercise 4. Create a program that displays 10 random numbers from 0 to 100. Next to each one, 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] + " is even");

                     }

                     if (a[i]%2!=0) {

                         System.out.println(a[i] + " is odd");

                       }

               }

       }

}

💡Use Loops to Traverse Arrays: Use for loops to efficiently traverse array elements. Loops facilitate manipulation and access to elements, allowing operations such as sums, searches, and updates to be performed quickly.

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

Solution:

import java.util.Scanner; 

public class Main {

       public static void main(String[] args) {

             Scanner exercise = new Scanner(System.in); 

             int a[] = new int[10];

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

                    System.out.println("Insert a number"); 

                    a[i] = exercise.nextInt();

              }

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

                       if (a[i]%2==0) {

                            System.out.println(a[i] + " is even");

                       }

                        if (a[i]%2!=0) {

                            System.out.println(a[i] + " is odd");

                       }

               }

        }

}

Exercise 6. Exercise in which we enter 10 numbers from the keyboard and move the last one to the first position.

Solution:

import java.util.Scanner; 

public class Main {

        public static void main(String[] args) {

             Scanner exercise = new Scanner(System.in); 

             int a[] = new int[10];

             // We create a for loop to enter the ten data points. 

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

                    System.out.println("Enter a number"); 
                    a[i] = exercise.nextInt();

              }

             //Now we will display the last data point as the first 

             System.out.println(" ");

            System.out.println("The modified list is "); 

            System.out.println(" ");

            System.out.println(a[9]); 

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

                   System.out.println(a[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 indicate.

Solution:

import java.util.Scanner; 

public class Main {

       public static void main(String[] args) {

            Scanner exercise = new Scanner(System.in);

            System.out.println("Enter the number you want to replace"); 

            int original = exercise.nextInt();

            System.out.println("Enter the number you want to replace it with"); 

            int replacement = exercise.nextInt();

            int a[] = new int[50];

            // We create a for loop to generate 50 random data points 

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

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

                   if (a[i]==original) {

                            a[i]=replacement;

                   }

             }

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

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

                    System.out.println(a[i]);

             }

      }

}

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

Solution:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner exercise = new Scanner(System.in);

        // We create an array where the 10 data points will be stored
        int a[] = new int[10];

        // We create an array where the even data points will be stored
        int evens[] = new int[10];

        // We create an array where the odd data points will be stored
        int odds[] = new int[10];

        // Counters for even and odd numbers
        int numEvens = 0;
        int numOdds = 0;

        // We create a loop to enter 10 data points
        // They will be stored in our int a
        // Depending on whether they are even or odd, they will also be stored in evens/odds

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

            System.out.println("Enter a number");
            a[i] = exercise.nextInt();

            if (a[i] % 2 == 0) {
                evens[numEvens++] = a[i];
            } else {
                odds[numOdds++] = a[i];
            }
        }

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

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

        for (int i = 0; i < numEvens; i++) {
            if (evens[i] > 0) {
                System.out.print(evens[i] + " ");
            }
        }

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

        for (int i = 0; i < numOdds; i++) {
            if (odds[i] > 0) {
                System.out.print(odds[i] + " ");
            }
        }
    }
}

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

Solution:

public class Main {

       public static void main(String[] args) {

            int a[] = new int[50]; 

            int largest = 0;

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

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

                    if (a[i]>largest) {
      
                          largest=a[i];

                    }      

             }

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

     }

}

Exercise 10. Array exercises with char and String type variables. Create an array that contains these words: “Hello”, “friends”, “how”, “are”. Then, another array that contains the following symbols: ^, &, *, %. Then, display everything on the 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! 😏