lab 5

Description:  Create a program that asks you to choose a data type (string, integer, and double) then, ask for how many elements input. The result should display as:
display all the elements in the order they were entered.
display all the elements in the reverse order they were entered.
display all the elements in ascending order.
display the elements without repetitions.

 

Code:

 

import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;
import java.util.TreeSet;

	public class lab5 {
		public static void main(String[] args) {
			int choice = 0;
			int x = 0;
			int y = 0;
			
			Scanner input = new Scanner(System.in);
			System.out.println("Choose the data types of your elements:");
			System.out.println("Press 1 for String");
			System.out.println("Press 2 for Integer");
			System.out.println("Press 3 for Double");
			choice = input.nextInt();
			
			LinkedList list1 = new LinkedList();
			LinkedList list2 = new LinkedList();
			Stack stack = new Stack();
			TreeSet treeset = new TreeSet();
			
			if (choice == 1) {
				System.out.println("Enter the number of elements you want to use:");
				x = input.nextInt();
				System.out.println("Enter the elements:");
				
				while (y < x) {
					String a = input.next();
					list1.addFirst(a);
					stack.push(a);
					treeset.add(a);
					y++;
					}
				
				list2.addAll(list1);
				Collections.sort(list2);
				
				System.out.printf("Elements in the order they were entered: %s\n",stack);
				System.out.printf("Elements in the reverse order they were entered: %s\n",list1);
				System.out.printf("Elements in ascending order: %s\n",list2);
				System.out.printf("Elements in ascending order without repeating: %s\n",treeset);
				}
			
			if (choice == 2) {
				System.out.println("Enter the number of elements you want to use:");
				x = input.nextInt();
				System.out.println("Enter the elements:");
				
				while (y < x) {
					Integer a = input.nextInt();
					list1.addFirst(a);
					stack.push(a);
					treeset.add(a);
					y++;
					}
				
				list2.addAll(list1);
				Collections.sort(list2);
				
				System.out.printf("Elements in the order they were entered: %s\n",stack);
				System.out.printf("Elements in the reverse order they were entered: %s\n",list1);
				System.out.printf("Elements in ascending order: %s\n",list2);
				System.out.printf("Elements in ascending order without repeating: %s\n",treeset);
				}
			
			if (choice == 3) {
			System.out.println("Enter the number of elements you want to use:");
			x = input.nextInt();
			System.out.println("Enter the elements:");
			
				while (y < x) {
					Double a = input.nextDouble();
					list1.addFirst(a);
					stack.push(a);
					treeset.add(a);
					y++;
					}
				
				list2.addAll(list1);
				Collections.sort(list2);
				
				System.out.printf("Elements in the order they were entered: %s\n",stack);
				System.out.printf("Elements in the reverse order they were entered: %s\n",list1);
				System.out.printf("Elements in ascending order: %s\n",list2);
				System.out.printf("Elements in ascending order without repeating: %s\n",treeset);
			}
		}
	}

}

l5-1

l5-2

l5-3

Leave a Reply

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