Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Written in Java Your task is to write a program that reads any number of int

ID: 3599985 • Letter: L

Question

Lab Written in Java

Your task is to write a program that reads any number of integers from the console (via a Scanner), stores them in an ArrayList, removes duplicates from the list, and then outputs the remaining distinct values. First, implement the removeRepeated method, which removes duplicates from a supplied list – note that you are not allowed to use advanced data structures (e.g. sets) to do this for you. Then implement the main method, which reads as many integers as the user desires into an ArrayList, adds them to an ArrayList, uses the removeRepeated method, and then outputs the distinct values. When you are testing your code in Eclipse, and are done typing integers, press enter (i.e. to proceed to a new line) and then press CTRL+D (Mac) or CTRL+Z (Windows) to indicate to Eclipse that you are done typing (this is code for EOF, or end-of-file; sometimes you may need to change focus to the code window and then back to the console for Eclipse to properly register the EOF).

Below are example runs of a completed program...

Enter integers:
No values entered.

Enter integers: 1 2 3 2 1
The distinct integer(s): 1 2 3

Enter integers: 1 1 1 2 1 2 2 1 2 3 2 1 2 3 1 The distinct integer(s): 1 2 3

Enter integers: 8 8 8 8 8 8 8 6 6 6 6 8 8 8 8 6 6 6 7 7 7 7 7 6 6 6 8 8 8 7 7 7 5 8 8 7666675533876500093098675
The distinct integer(s): 8 6 7 5 3 0 9

______________________________

import java.util.ArrayList;

//TODO: document this class

public class PA4c {

/**

* Removes all duplicate values

* from the supplied list

*

* @param list list from which to remove repeated elements

*/

public static void removeRepeated(ArrayList<Integer> list) {

// replace with your code

}

/**

* Reads numbers from the keyboard and

* outputs the list of distinct values

*

* @param args command-line arguments, ignored

*/

public static void main(String[] args) {

// replace with your code

}

}

Explanation / Answer

PA4c.java

import java.util.ArrayList;

import java.util.Scanner;

//TODO: document this class

public class PA4c {

/**

* Removes all duplicate values

* from the supplied list

*

* @param list list from which to remove repeated elements

*/

public static void removeRepeated(ArrayList<Integer> list) {

// replace with your code

for(int i=0;i<list.size();i++) {

for(int j=i+1;j<list.size();j++){

if(list.get(i) == list.get(j)) {

list.remove(j);

j--;

}

}

}

}

/**

* Reads numbers from the keyboard and

* outputs the list of distinct values

*

* @param args command-line arguments, ignored

*/

public static void main(String[] args) {

// replace with your code

System.out.println("Enter integers:");

Scanner scan = new Scanner(System.in);

String s = scan.nextLine();

ArrayList<Integer> list = new ArrayList<Integer>();

while(!s.trim().equals("")) {

list.add(Integer.parseInt(s));

s = scan.nextLine();

}

if(list.size() > 0) {

removeRepeated(list);

System.out.print("The distinct integer(s): ");

for(Integer i: list){

System.out.print(i+" ");

}

System.out.println();

} else {

System.out.println("No values entered.");

}

}

}

Output:

Enter integers:
1
1
1
2
1
2
2
1
2
3
2
1
2
3
1

The distinct integer(s): 1 2 3