SlideShare a Scribd company logo
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

PDF
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
PDF
An object of class StatCalc can be used to compute several simp.pdf
DOCX
Write a program that will test a name) method no sorting routine from.docx
PPTX
Chapter 7.1
PDF
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
PDF
Create a menu-driven program that will accept a collection of non-ne.pdf
PPTX
Arrays in Java with example and types of array.pptx
PDF
In this homework- you will write a program modify program you wrote in.pdf
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
An object of class StatCalc can be used to compute several simp.pdf
Write a program that will test a name) method no sorting routine from.docx
Chapter 7.1
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
Arrays in Java with example and types of array.pptx
In this homework- you will write a program modify program you wrote in.pdf

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

DOCX
Array assignment
PPT
Chap09
PDF
Simple 27 Java Program on basic java syntax
PDF
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
PDF
OrderTest.javapublic class OrderTest {       Get an arra.pdf
PDF
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
PPTX
arrays in c programming - example programs
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
PDF
Please help solve this in C++ So the program is working fin.pdf
PPTX
6_Array.pptx
DOCX
Please add-modify the following to the original code using C# 1- Delet.docx
PDF
import java.util.LinkedList; import java.util.Random; import jav.pdf
PPT
9781439035665 ppt ch09
PDF
Simple Java Program for beginner with easy method.pdf
PDF
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
PDF
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
PDF
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
PDF
C++ Nested loops, matrix and fuctions.pdf
PDF
Need to be done in C++ Please Sorted number list implementation wit.pdf
PDF
I need help with the 2nd TODO comment and the other comments Ill.pdf
Array assignment
Chap09
Simple 27 Java Program on basic java syntax
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
arrays in c programming - example programs
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Please help solve this in C++ So the program is working fin.pdf
6_Array.pptx
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
9781439035665 ppt ch09
Simple Java Program for beginner with easy method.pdf
public static ArrayListInteger doArrayListInsertAtMedian(int nu.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
C++ Nested loops, matrix and fuctions.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
Ad

More from fashioncollection2 (20)

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

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Classroom Observation Tools for Teachers
PDF
Insiders guide to clinical Medicine.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Cell Structure & Organelles in detailed.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Basic Mud Logging Guide for educational purpose
PDF
01-Introduction-to-Information-Management.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Lesson notes of climatology university.
PPTX
Institutional Correction lecture only . . .
PPTX
Pharma ospi slides which help in ospi learning
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Classroom Observation Tools for Teachers
Insiders guide to clinical Medicine.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Cell Structure & Organelles in detailed.
O7-L3 Supply Chain Operations - ICLT Program
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Basic Mud Logging Guide for educational purpose
01-Introduction-to-Information-Management.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
GDM (1) (1).pptx small presentation for students
Lesson notes of climatology university.
Institutional Correction lecture only . . .
Pharma ospi slides which help in ospi learning
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPH.pptx obstetrics and gynecology in nursing
The Lost Whites of Pakistan by Jahanzaib Mughal.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