SlideShare a Scribd company logo
1 of 3
Download to read offline
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

More Related Content

Similar to Write a method called uniqueNumbers that takes an int array as param.pdf

JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdfJAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
arpaqindia
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
akkhan101
 
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdfWrite the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
arihantmum
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
ankit11134
 
import java.util.LinkedList; import java.util.Random; import jav.pdf
import java.util.LinkedList; import java.util.Random; import jav.pdfimport java.util.LinkedList; import java.util.Random; import jav.pdf
import java.util.LinkedList; import java.util.Random; import jav.pdf
aquastore223
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09
Terry Yoast
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
freddysarabia1
 
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdfStep 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
aloeplusint
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
aathiauto
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
aathmaproducts
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
Nooryaseen9
 

Similar to Write a method called uniqueNumbers that takes an int array as param.pdf (20)

Chap09
Chap09Chap09
Chap09
 
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdfJAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdfWrite the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Please add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxPlease add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docx
 
import java.util.LinkedList; import java.util.Random; import jav.pdf
import java.util.LinkedList; import java.util.Random; import jav.pdfimport java.util.LinkedList; import java.util.Random; import jav.pdf
import java.util.LinkedList; import java.util.Random; import jav.pdf
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09
 
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdfpublic static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
 
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdfStep 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
 
I need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdfI need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdf
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
 
Chap 2 Arrays and Structures.ppt
Chap 2  Arrays and Structures.pptChap 2  Arrays and Structures.ppt
Chap 2 Arrays and Structures.ppt
 
Chap 2 Arrays and Structures.pptx
Chap 2  Arrays and Structures.pptxChap 2  Arrays and Structures.pptx
Chap 2 Arrays and Structures.pptx
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 

More from fashioncollection2

All programming assignments should include the name, or names, of th.pdf
All programming assignments should include the name, or names, of th.pdfAll programming assignments should include the name, or names, of th.pdf
All programming assignments should include the name, or names, of th.pdf
fashioncollection2
 
Why is “Profit” a problematic target Please consider in your answer.pdf
Why is “Profit” a problematic target Please consider in your answer.pdfWhy is “Profit” a problematic target Please consider in your answer.pdf
Why is “Profit” a problematic target Please consider in your answer.pdf
fashioncollection2
 
What is the role of abiotic factors in the formation of biomesS.pdf
What is the role of abiotic factors in the formation of biomesS.pdfWhat is the role of abiotic factors in the formation of biomesS.pdf
What is the role of abiotic factors in the formation of biomesS.pdf
fashioncollection2
 
What are the major terrestrial adaptations of plants1- List the F.pdf
What are the major terrestrial adaptations of plants1- List the F.pdfWhat are the major terrestrial adaptations of plants1- List the F.pdf
What are the major terrestrial adaptations of plants1- List the F.pdf
fashioncollection2
 
The insatiable demand for everything wireless, video, and Web-enable.pdf
The insatiable demand for everything wireless, video, and Web-enable.pdfThe insatiable demand for everything wireless, video, and Web-enable.pdf
The insatiable demand for everything wireless, video, and Web-enable.pdf
fashioncollection2
 
Technology is an ever-growing system that provides advantages and in.pdf
Technology is an ever-growing system that provides advantages and in.pdfTechnology is an ever-growing system that provides advantages and in.pdf
Technology is an ever-growing system that provides advantages and in.pdf
fashioncollection2
 
Role of involvement in consumer decision-making. Identify the level .pdf
Role of involvement in consumer decision-making. Identify the level .pdfRole of involvement in consumer decision-making. Identify the level .pdf
Role of involvement in consumer decision-making. Identify the level .pdf
fashioncollection2
 
Questionsa What are some of the barriers to understanding an issu.pdf
Questionsa What are some of the barriers to understanding an issu.pdfQuestionsa What are some of the barriers to understanding an issu.pdf
Questionsa What are some of the barriers to understanding an issu.pdf
fashioncollection2
 
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdfRainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
fashioncollection2
 
Quality software project managementHow are tasks, activities, and .pdf
Quality software project managementHow are tasks, activities, and .pdfQuality software project managementHow are tasks, activities, and .pdf
Quality software project managementHow are tasks, activities, and .pdf
fashioncollection2
 

More from fashioncollection2 (20)

All programming assignments should include the name, or names, of th.pdf
All programming assignments should include the name, or names, of th.pdfAll programming assignments should include the name, or names, of th.pdf
All programming assignments should include the name, or names, of th.pdf
 
After a (not very successful) trick or treating round, Candice has 1.pdf
After a (not very successful) trick or treating round, Candice has 1.pdfAfter a (not very successful) trick or treating round, Candice has 1.pdf
After a (not very successful) trick or treating round, Candice has 1.pdf
 
Why steroids can cross the cell membraneWhy steroids can cross .pdf
Why steroids can cross the cell membraneWhy steroids can cross .pdfWhy steroids can cross the cell membraneWhy steroids can cross .pdf
Why steroids can cross the cell membraneWhy steroids can cross .pdf
 
Write a command in unixlinux to Display all the files whose names e.pdf
Write a command in unixlinux to Display all the files whose names e.pdfWrite a command in unixlinux to Display all the files whose names e.pdf
Write a command in unixlinux to Display all the files whose names e.pdf
 
Why is “Profit” a problematic target Please consider in your answer.pdf
Why is “Profit” a problematic target Please consider in your answer.pdfWhy is “Profit” a problematic target Please consider in your answer.pdf
Why is “Profit” a problematic target Please consider in your answer.pdf
 
Which of the following statements about protists is falsea) There a.pdf
Which of the following statements about protists is falsea) There a.pdfWhich of the following statements about protists is falsea) There a.pdf
Which of the following statements about protists is falsea) There a.pdf
 
Which cable type would be used to connect a router to a switchA. .pdf
Which cable type would be used to connect a router to a switchA. .pdfWhich cable type would be used to connect a router to a switchA. .pdf
Which cable type would be used to connect a router to a switchA. .pdf
 
What is the role of abiotic factors in the formation of biomesS.pdf
What is the role of abiotic factors in the formation of biomesS.pdfWhat is the role of abiotic factors in the formation of biomesS.pdf
What is the role of abiotic factors in the formation of biomesS.pdf
 
What information needs to be encoded in a loadstorebranchALU inst.pdf
What information needs to be encoded in a loadstorebranchALU inst.pdfWhat information needs to be encoded in a loadstorebranchALU inst.pdf
What information needs to be encoded in a loadstorebranchALU inst.pdf
 
What are the major terrestrial adaptations of plants1- List the F.pdf
What are the major terrestrial adaptations of plants1- List the F.pdfWhat are the major terrestrial adaptations of plants1- List the F.pdf
What are the major terrestrial adaptations of plants1- List the F.pdf
 
The total lung capacity of a patient is 5.5 liters. Find the patient.pdf
The total lung capacity of a patient is 5.5 liters. Find the patient.pdfThe total lung capacity of a patient is 5.5 liters. Find the patient.pdf
The total lung capacity of a patient is 5.5 liters. Find the patient.pdf
 
The insatiable demand for everything wireless, video, and Web-enable.pdf
The insatiable demand for everything wireless, video, and Web-enable.pdfThe insatiable demand for everything wireless, video, and Web-enable.pdf
The insatiable demand for everything wireless, video, and Web-enable.pdf
 
Technology is an ever-growing system that provides advantages and in.pdf
Technology is an ever-growing system that provides advantages and in.pdfTechnology is an ever-growing system that provides advantages and in.pdf
Technology is an ever-growing system that provides advantages and in.pdf
 
Suppose that the proportions of blood phenotypes in a particular pop.pdf
Suppose that the proportions of blood phenotypes in a particular pop.pdfSuppose that the proportions of blood phenotypes in a particular pop.pdf
Suppose that the proportions of blood phenotypes in a particular pop.pdf
 
Select all of the following that are true regarding evolution. Altho.pdf
Select all of the following that are true regarding evolution.  Altho.pdfSelect all of the following that are true regarding evolution.  Altho.pdf
Select all of the following that are true regarding evolution. Altho.pdf
 
Role of involvement in consumer decision-making. Identify the level .pdf
Role of involvement in consumer decision-making. Identify the level .pdfRole of involvement in consumer decision-making. Identify the level .pdf
Role of involvement in consumer decision-making. Identify the level .pdf
 
Questionsa What are some of the barriers to understanding an issu.pdf
Questionsa What are some of the barriers to understanding an issu.pdfQuestionsa What are some of the barriers to understanding an issu.pdf
Questionsa What are some of the barriers to understanding an issu.pdf
 
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdfRainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
 
Quality software project managementHow are tasks, activities, and .pdf
Quality software project managementHow are tasks, activities, and .pdfQuality software project managementHow are tasks, activities, and .pdf
Quality software project managementHow are tasks, activities, and .pdf
 
Part I Write the complete class definition (or server) for Unsorte.pdf
Part I Write the complete class definition (or server) for Unsorte.pdfPart I Write the complete class definition (or server) for Unsorte.pdf
Part I Write the complete class definition (or server) for Unsorte.pdf
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Recently uploaded (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

Write a method called uniqueNumbers that takes an int array as param.pdf

  • 1. 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) {
  • 2. 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;
  • 3. 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