Lab 5

Description:

Lab 5 requires us to create a java program that must analyze data elements which a user will enter. Unlike Lab 1 and 2, this program will manipulate data with the use of data structures. After an introduction to data structures, we used what was learned and approached the data manipulation with the use of the predefined data structures: ArrayList (Collection class), Collections (Collection class), Collection (root interface in the collection hierarchy) and HashSet (Set implementation). We used ArrayList to create a list which would be filled with the user’s elements that they would enter and because of the add method it contained which we needed for adding the user elements to the list. Once the list was filled with the user elements, it could then be displayed in the order they were entered. With the Collections class, we used its methods sort and reverse to do exactly what they mean; sort and reverse the list of elements. This must also be displayed. Lastly, we used the Collection interface to create a new list; a list that will contain no repetition of elements. However, we had to set this list to HashSet because only then it would not allow repetition of elements. If repetition of elements are inserted by he user, those repeated elements will be eliminated and only one will be left. So, only one of the repeated elements will be displayed along with other elements that may have not been repeated. Overall, lab 5 helped understand data structures and their purposes in java programming.

Code:

package edu.cuny.citytech.CET3640.f13.Lab5;

import java.util.HashSet;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Collection;

public class DataAnalyzer {

	public static void main(String[] args) {

	@SuppressWarnings("resource")
	Scanner input = new Scanner(System.in);	

	int num;
	int choice;

	System.out.println("Enter 1 to enter integer type data elements ");
	System.out.println("Enter 2 to enter string type data elements ");
	System.out.println("Enter 3 to enter double type data elements ");
	choice = input.nextInt();

	if (choice == 1)
	{
		System.out.println("Enter how many elements to process: ");
		num = input.nextInt();	

		ArrayList myList1 = new ArrayList();

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

		for (int i = 0; i < num; i++){
			myList1.add(input.nextInt());
		}

		System.out.println("All elements displayed in the order they were entered (I used the List data structure): " + myList1);

		Collections.reverse(myList1);
		System.out.println("All elements displayed in the reverse order they were entered (I used the List data Structure): " + myList1);

		Collections.sort(myList1);
		System.out.println("All elements displayed in the ascending order (I used the List data structure):" + myList1);

		Collection myList2 = new HashSet(myList1);

		System.out.println("All elements displayed only once (I used the Collection data structure): " + myList2);

		}

	if (choice == 2)
	{
		System.out.println("Enter how many elements to process: ");
		num = input.nextInt();	

		ArrayList myList1 = new ArrayList();

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

		for (int i = 0; i < num; i++){
			myList1.add(input.next());
		}

		System.out.println("All elements displayed in the order they were entered (I used the List data structure): " + myList1);

		Collections.reverse(myList1);
		System.out.println("All elements displayed in the reverse order they were entered (I used the List data Structure): " + myList1);

		Collections.sort(myList1);
		System.out.println("All elements displayed in the ascending order (I used the List data structure):" + myList1);

		Collection myList2 = new HashSet(myList1);

		System.out.println("All elements displayed only once (I used the Collection data structure): " + myList2);

		}

	if (choice == 3)
	{
		System.out.println("Enter how many elements to process: ");
		num = input.nextInt();	

		ArrayList myList1 = new ArrayList();

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

		for (int i = 0; i < num; i++){
			myList1.add(input.nextDouble());
		}

		System.out.println("All elements displayed in the order they were entered (I used the List data structure): " + myList1);

		Collections.reverse(myList1);
		System.out.println("All elements displayed in the reverse order they were entered (I used the List data Structure): " + myList1);

		Collections.sort(myList1);
		System.out.println("All elements displayed in the ascending order (I used the List data structure):" + myList1);

		Collection myList2 = new HashSet(myList1);

		System.out.println("All elements displayed only once (I used the Collection data structure): " + myList2);

		}
	}
}

Screenshots of results:

Lab 5 running program

Lab 5 result1

Lab 5 result2

Lab 5 result3

Lab 5 result4

Leave a Reply

Your email address will not be published. Required fields are marked *