SlideShare a Scribd company logo
Illegal numbers.
a. Complete the method find which accepts a collection of integers and a target, and returns the
position that the integer is found in, or a flag of -1 if the integer is not in the collection. (DO
NOT use the indexOf method on ArrayList.)
public static int find(ArrayList numbers, int target) {
b. Complete the method filter which accepts two collections of integers, one set of input integers
(numbers) and one set of integers (illegal) that should be removed from the list. Your method
should use the find method above. (DO NOT use the removeAll method on ArrayList.)
public static ArrayList filter( ArrayList numbers, ArrayList illegal) {
Solution
import java.util.ArrayList;
import java.util.Scanner;
public class IllegalNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList list = addElementsToList();
System.out.println("List : ");
print(list);
System.out.println("Enter the element to search in the list : ");
int searchElement = sc.nextInt();
int pos = find(list, searchElement);
if(pos == -1){
System.out.println("Element is not in the list");
}
else{
System.out.println("Element found in the position : "+pos);
}
System.out.println("Enter illegal integer list : ");
ArrayList illegal = addElementsToList();
System.out.println("First list : ");
print(list);
System.out.println("Illegal list : ");
print(illegal);
System.out.println("New List After performing Filter is : ");
list = filter(list, illegal);
for(Integer i : list){
System.out.print(i+" " );
}
}
public static int find(ArrayList numbers, int target) {
for(int i = 0; i < numbers.size(); i++){
if( numbers.get(i) == (Integer)target) return (i+1);
}
return -1;
}
public static ArrayList filter( ArrayList numbers, ArrayList illegal) {
ArrayList filteredArrayList = new ArrayList();
for (int i = 0; i < numbers.size(); i++){
if (!illegal.contains(numbers.get(i))){
filteredArrayList.add((Integer) numbers.get(i));
}
}
return filteredArrayList;
}
public static ArrayList addElementsToList (){
ArrayList list = new ArrayList ();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the numbers in the list press 'q/Q to Stop ");
for(;;){
String newInt = sc.next();
if(newInt.equalsIgnoreCase("q")){
break;
}
else{
list.add(Integer.parseInt(newInt));
}
}
return list;
}
public static void print(ArrayList< Integer> someList) {
for(Integer i:someList)
System.out.print(i + " ");
System.out.println();
}
}

More Related Content

PDF
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
PDF
Collections 1 Insert an element at desired location Take th.pdf
PDF
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
PDF
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
PDF
Hitchhiker's Guide to Functional Programming
DOCX
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
PPTX
Java Foundations: Lists, ArrayList<T>
DOCX
A- Gets a number as an input from the user and stores it in a variable.docx
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
Collections 1 Insert an element at desired location Take th.pdf
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
Hitchhiker's Guide to Functional Programming
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
Java Foundations: Lists, ArrayList<T>
A- Gets a number as an input from the user and stores it in a variable.docx

Similar to Illegal numbers.a. Complete the method find which accepts a collec.pdf (20)

PDF
Question 1 MiniList collection 20 marks In this question .pdf
PDF
In three of the exercises below there is given a code of a method na.pdf
PDF
Refer to my progress on this assignment belowIn this problem you w.pdf
PDF
A popular implementation of List is ArrayList- Look up how to instanti.pdf
PDF
In this homework- you will write a program modify program you wrote in.pdf
PDF
helpInstructionsAdd the function max as an abstract function to .pdf
PPT
Lecture2
PDF
CS3381 OBJECT ORIENTED PROGRAMMINGLABS_1.pdf
PDF
In C++Add the function min as an abstract function to the classar.pdf
PDF
Simple Java Program for beginner with easy method.pdf
PPTX
Chapter 15 Lists
DOCX
Write a program that will test a name) method no sorting routine from.docx
DOCX
#include fstream#include iostream#include cstdlib#includ.docx
PDF
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
DOCX
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
PDF
What will be the output from the following code if the user enters 4.pdf
PPTX
lists8.pptx of chapter 12 IP Basic Python
PDF
PDF
Array notes
PDF
import java.util.ArrayList; import java.util.List;public class S.pdf
Question 1 MiniList collection 20 marks In this question .pdf
In three of the exercises below there is given a code of a method na.pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
A popular implementation of List is ArrayList- Look up how to instanti.pdf
In this homework- you will write a program modify program you wrote in.pdf
helpInstructionsAdd the function max as an abstract function to .pdf
Lecture2
CS3381 OBJECT ORIENTED PROGRAMMINGLABS_1.pdf
In C++Add the function min as an abstract function to the classar.pdf
Simple Java Program for beginner with easy method.pdf
Chapter 15 Lists
Write a program that will test a name) method no sorting routine from.docx
#include fstream#include iostream#include cstdlib#includ.docx
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
What will be the output from the following code if the user enters 4.pdf
lists8.pptx of chapter 12 IP Basic Python
Array notes
import java.util.ArrayList; import java.util.List;public class S.pdf

More from gopalk44 (20)

PDF
My protease appears to affect bacteria, but not my own cells. What t.pdf
PDF
Name the phases in interphase, and describe what happens during those.pdf
PDF
Look for at least four definitions of accounting (from four differen.pdf
PDF
Match the word or phrase with the best description of it. ;An express.pdf
PDF
JAVAThe mean of a list of numbers is its arithmetic average. The .pdf
PDF
Know and understand the contributions of Hooke, van Leeuwenhoek, Cohn.pdf
PDF
ImplementDijkstra’s algorithm using the graph class you implemente.pdf
PDF
how many chromosomes will be found in each cell at the end of anapha.pdf
PDF
how are the technological approaches of cloning and IPSCs similar to.pdf
PDF
HELP! Thought I can figure this one out but got it wrong. I have one.pdf
PDF
From a recent statistical analysis for the last five years, on an av.pdf
PDF
Find asymptotic upperlower bounds. Find asymptotic upperlower boun.pdf
PDF
Explain how a wireless device can help an organization perform busin.pdf
PDF
Exam scores were normal in BIO 200. Jasons exam score was one stan.pdf
PDF
Discuss the various types of business crimes, a business liability f.pdf
PDF
Describe the different types of ribs. Which ribs are considered “fal.pdf
PDF
Circle the best answer to the following questions. From the Bohr mod.pdf
PDF
C++ ProgramN-number queue rotations.[16] Write methods void Que.pdf
PDF
Below are two possible tree topologies. How many clades are different.pdf
PDF
WriteBelow are a list of descriptions that apply to either post s.pdf
My protease appears to affect bacteria, but not my own cells. What t.pdf
Name the phases in interphase, and describe what happens during those.pdf
Look for at least four definitions of accounting (from four differen.pdf
Match the word or phrase with the best description of it. ;An express.pdf
JAVAThe mean of a list of numbers is its arithmetic average. The .pdf
Know and understand the contributions of Hooke, van Leeuwenhoek, Cohn.pdf
ImplementDijkstra’s algorithm using the graph class you implemente.pdf
how many chromosomes will be found in each cell at the end of anapha.pdf
how are the technological approaches of cloning and IPSCs similar to.pdf
HELP! Thought I can figure this one out but got it wrong. I have one.pdf
From a recent statistical analysis for the last five years, on an av.pdf
Find asymptotic upperlower bounds. Find asymptotic upperlower boun.pdf
Explain how a wireless device can help an organization perform busin.pdf
Exam scores were normal in BIO 200. Jasons exam score was one stan.pdf
Discuss the various types of business crimes, a business liability f.pdf
Describe the different types of ribs. Which ribs are considered “fal.pdf
Circle the best answer to the following questions. From the Bohr mod.pdf
C++ ProgramN-number queue rotations.[16] Write methods void Que.pdf
Below are two possible tree topologies. How many clades are different.pdf
WriteBelow are a list of descriptions that apply to either post s.pdf

Recently uploaded (20)

PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
Hazard Identification & Risk Assessment .pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
Unit 4 Skeletal System.ppt.pptxopresentatiom
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
IGGE1 Understanding the Self1234567891011
PDF
RMMM.pdf make it easy to upload and study
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Supply Chain Operations Speaking Notes -ICLT Program
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
History, Philosophy and sociology of education (1).pptx
Cell Types and Its function , kingdom of life
Hazard Identification & Risk Assessment .pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Weekly quiz Compilation Jan -July 25.pdf
Computing-Curriculum for Schools in Ghana
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Unit 4 Skeletal System.ppt.pptxopresentatiom
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
IGGE1 Understanding the Self1234567891011
RMMM.pdf make it easy to upload and study
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
UNIT III MENTAL HEALTH NURSING ASSESSMENT

Illegal numbers.a. Complete the method find which accepts a collec.pdf

  • 1. Illegal numbers. a. Complete the method find which accepts a collection of integers and a target, and returns the position that the integer is found in, or a flag of -1 if the integer is not in the collection. (DO NOT use the indexOf method on ArrayList.) public static int find(ArrayList numbers, int target) { b. Complete the method filter which accepts two collections of integers, one set of input integers (numbers) and one set of integers (illegal) that should be removed from the list. Your method should use the find method above. (DO NOT use the removeAll method on ArrayList.) public static ArrayList filter( ArrayList numbers, ArrayList illegal) { Solution import java.util.ArrayList; import java.util.Scanner; public class IllegalNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList list = addElementsToList(); System.out.println("List : "); print(list); System.out.println("Enter the element to search in the list : "); int searchElement = sc.nextInt(); int pos = find(list, searchElement); if(pos == -1){ System.out.println("Element is not in the list"); } else{ System.out.println("Element found in the position : "+pos); } System.out.println("Enter illegal integer list : "); ArrayList illegal = addElementsToList(); System.out.println("First list : ");
  • 2. print(list); System.out.println("Illegal list : "); print(illegal); System.out.println("New List After performing Filter is : "); list = filter(list, illegal); for(Integer i : list){ System.out.print(i+" " ); } } public static int find(ArrayList numbers, int target) { for(int i = 0; i < numbers.size(); i++){ if( numbers.get(i) == (Integer)target) return (i+1); } return -1; } public static ArrayList filter( ArrayList numbers, ArrayList illegal) { ArrayList filteredArrayList = new ArrayList(); for (int i = 0; i < numbers.size(); i++){ if (!illegal.contains(numbers.get(i))){ filteredArrayList.add((Integer) numbers.get(i)); } } return filteredArrayList; } public static ArrayList addElementsToList (){ ArrayList list = new ArrayList (); Scanner sc = new Scanner(System.in); System.out.println("Enter the numbers in the list press 'q/Q to Stop "); for(;;){
  • 3. String newInt = sc.next(); if(newInt.equalsIgnoreCase("q")){ break; } else{ list.add(Integer.parseInt(newInt)); } } return list; } public static void print(ArrayList< Integer> someList) { for(Integer i:someList) System.out.print(i + " "); System.out.println(); } }