Write a method called uniqueNumbers that takes an int array

Write a method called uniqueNumbers that takes an int array as parameter, and returns a different int array, which only contains the list of unique numbers in the original array. Hint: Use the isPresent method defined below to accomplish your

here is the code we need to add too

Solution

solution

package com.mt.classes;

import java.util.ArrayList;
import java.util.List;

public class Lab8e {
   public static void main(String[] args) {
       int[] numbers = new int[20];

       // Generate random numbers between 0 and 99 and fill up the array
       for (int i = 0; i < numbers.length; i++) {
           numbers[i] = (int) (Math.random() * 50);
       }

       System.out.println(\"The list is:\");
       printNumbers(numbers);

       // Task 1
       System.out.println(\"The smallest number in the list is \"
               + smallestNumber(numbers));

       // Task 2
       System.out.println(\"The largest number in the list is \"
               + largestNumber(numbers));

       // Task 3
       System.out.println(\"The average of numbers in the list is \"
               + averageOfNumbers(numbers));

       // Task 4: Extra Credit. Uncomment the following
       // two lines if you complete this task.
       System.out.println(\"The list of unique numbers is:\");
       uniqueNumbers(numbers);

   }

   private static void uniqueNumbers(int[] numbers) {

       for (int i = 0; i < numbers.length; i++) {
           boolean isDuplicate = false;
           for (int j = 0; j < i; j++) {
               if (numbers[i] == numbers[j]) {
                   isDuplicate = true;
                   break;
               }
           }
           if (!isDuplicate) {
               System.out.print(numbers[i] + \" \");

           }
       }

   }

   public static void printNumbers(int[] list) {
       for (int i = 0; i < list.length; i++) {
           System.out.print(list[i] + \" \");
       }
       System.out.println();
   }

   // 1. Write a method called smallestNumber that
   // takes an int array as parameter, and returns the
   // smallest number
   public static int smallestNumber(int[] list) {
       int small = 100;
       for (int i = 0; i < list.length; i++) {
           if (list[i] < small) {
               small = list[i];
           }
       }
       return small;
   }

   // 2. Write a method called largestNumber that
   // takes an int array as parameter, and returns the
   // largest number
   public static int largestNumber(int[] list) {
       int large = 0;
       for (int i = 0; i < list.length; i++) {
           if (list[i] > large) {
               large = list[i];
           }
       }
       return large;
   }

   // 3. Write a method called averageOfNumbers that
   // takes an int array as parameter, and returns the
   // average of the numbers
   public static double averageOfNumbers(int[] list) {
       double average = 0.0;
       for (int i = 0; i < list.length; i++) {
           average = average + list[i];
       }
       average = average / list.length;
       return average;
   }

   public static boolean isPresent(int[] list, int target) {
       boolean found = false;
       for (int i = 0; i < list.length && !found && list[i] != 0; i++) {
           if (list[i] == target) {
               found = true;
           }
       }
       return found;
   }

}

output

The list is:
16 14 11 31 7 8 18 49 27 43 9 39 38 18 6 16 8 30 32 15
The smallest number in the list is 6
The largest number in the list is 49
The average of numbers in the list is 21.75
The list of unique numbers is:
16 14 11 31 7 8 18 49 27 43 9 39 38 6 30 32 15

Write a method called uniqueNumbers that takes an int array as parameter, and returns a different int array, which only contains the list of unique numbers in t
Write a method called uniqueNumbers that takes an int array as parameter, and returns a different int array, which only contains the list of unique numbers in t
Write a method called uniqueNumbers that takes an int array as parameter, and returns a different int array, which only contains the list of unique numbers in t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site