Lab 5

Description

In this lab we had to create a program to show a menu asking the user to input two things: the first thing is data type of the elements and the second thing is the amount of elements that will be processed. Using the scanner, we’ll let the program read the entered numbers the user typed to analyze, compiled and tested the results should displaying the following:

a. Display the sequence of the number entered
b. Display the reverse sequences of the numbers entered.
c. Display all numbers from highest to lowest.
d. display no repetitions

program

package lab5;
import java.util.Collections;
import java.util.Scanner;
import java.util.Stack;
import java.util.TreeSet;
import java.util.HashSet;
import java.util.LinkedList;

public class lab5 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int choice;
        Scanner input = new Scanner(System.in);
        HashSet Set = new HashSet();
        Stack Stack = new Stack();
        TreeSet treeset = new TreeSet();
        LinkedList Rev = new LinkedList();
        LinkedList Asc = new LinkedList();
        System.out.println("Enter Type of elements");
        System.out.println("   1) Strings");
        System.out.println("   2) Doubles");
        System.out.println("   3) Integers\n");
        choice = input.nextInt();
        if(choice ==1){
            int y = 0;
            System.out.print("Amount of elements you want to process: \n");
            int z = input.nextInt(); 
            while(y < z) {
                String X = input.next();
                Rev.addFirst(X);
                Stack.push(X);
                treeset.add(X);
                Set.add(X);
                y++; 
            }
        }
        else if(choice == 2){
            int y = 0;
            System.out.print("Amount of elements you want to process: ");
            int z = input.nextInt(); 
            while(y < z) {
                Double X = input.nextDouble();
                Rev.addFirst(X);
                Stack.push(X);
                treeset.add(X);
                Set.add(X);
                y++; 
            }
        }
        else if(choice == 3){
            int y = 0;
            System.out.print("Amount of elements you want to process: ");
            int z = input.nextInt(); 
            while(y < z) {
                Integer X = input.nextInt();
                Rev.addFirst(X);
                Stack.push(X);
                treeset.add(X);
                Set.add(X);
                y++; 
            }
        }
        Asc.addAll(Rev);
        Collections.sort(Asc);
        System.out.println("Elements in Order they were Entered:" + Stack);
        System.out.println("Elements in Reversed Order:" + Rev);
        System.out.println("Elements in Ascending Order:" + Asc);
        System.out.println("Elements without Repeating:" + Set);
    }
}

Screen Shot
lab 5

Leave a Reply

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