Lab 5

Lab Description

In this lab, I was suppose to create a class that will first ask the user to input two things: the data type of the elements and how many data to process. Next, the program was suppose to analyze all four of the following functions: 1- Display all elements in the order they were entered in, 2- Display all the elements in the reverse order they were entered in, 3- Display all elements in ascending order, and 4-display all element only once without repetition. However, my program was unable to produce all the following tasks. My program can only ask the use to enter which data type they will like to use and how many data to process. The user would be able to input the number of elements to process then the program would display the numbers under the commands of each string i made.

Source Code

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.TreeSet; 

public class DataStructures {
	public static void main(String[] args) {

		Queue qone = new LinkedList();
		Stack sone = new Stack();
		TreeSet Tone = new TreeSet();
		Set lone = new HashSet();

		System.out.println("Choose the data type you would like to use: 1-String, 2-Double, and 3-Intergers");
		Scanner input = new Scanner(System.in);
		String input1 = input.next();
		int block = Integer.parseInt(input1);

		for(int i = 0; i<block; i++){ 			System.out.println("Input elements"); 			Scanner input3 = new Scanner(System.in); 			String input2 = input3.next(); 			qone.offer(input2); 			sone.push(input2); 			Tone.add(input2); 			lone.add(input2); 		} 		System.out.println("Display this"); 		while (qone.size() > 0){
			System.out.println(""+qone.peek());
			qone.poll();
			System.out.println(""+sone.pop());
			System.out.println(""+Tone.last());
			System.out.println(""+lone.iterator());

		}
	}
}

Screenshot

pic lab

Leave a Reply

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