Lab # 5

Lab Description:

This Lab entailed ripping away from the intricacies of Java that were required in the last labs and just create a simple program to sort and display some data. It was a nice way to relax and write out a bunch of code which allowed me to dislike Java less. In fact it made me realize that Java has a lot of functionality when it comes to simple coding. Further into the details of this lab:

We were required to ask the user to decide what type of data to work with; strings, doubles, or integers- then decide how many of those data elements they want to work with, and then have them type in their data. The program then prints the data entered utilizing the extensive Java Collection libraries to sort and convert data as needed. The required print out has to conform to the following guidelines:

1-     Display the data in the order it was entered
2-     Display the data in reverse order
3-     Sort the data (alphabetically or in ascending order)
4-     Display the data without duplicates

Code:

//Yevgeniy Babkin - CET 3640 Lab#5

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

public class data_structures {

    public data_structures() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner scan = new Scanner(System.in);

        //declare basic variables
        int datatype;
        int number;

        //1st menu
        System.out.println("Please select data type by entering:");
        System.out.println(" 1 for string datatype:");
        System.out.println(" 2 for double datatype:");
        System.out.println(" 3 for integer datatype:");
        //scan 1st menu choice and store to "datatype"
        datatype = scan.nextInt();

        //2nd menu 
        System.out.println("enter the number of data elements to process");
        //scan 2nd choice and store to "number"
        number = scan.nextInt();

        //declare all arrays and sets which will be used
        ArrayList < String > strings = new ArrayList< String >();
        ArrayList < Double > doubles = new ArrayList< Double >();
        ArrayList < Integer > integers = new ArrayList< Integer >();

        Set<String> setstrings = new HashSet<String>();
        Set<Double> setdoubles = new HashSet<Double>();
        Set<Integer> setintegers = new HashSet<Integer>();

        //main code starts below consisting of 3 if statements 

        //the 1st if deals with strings
        //the 2nd if deals with doubles
        //3rd if deals with integers

        if(datatype == 1)
        {
            //once user enters this string if statement
            //they will be asked to enter their strings
            //according to the number they had selected earlier

            System.out.println("enter " + number + " strings");

            //for loop to scan the correct amount of data
            for(int i=-1; i < number; i++)
            {
                strings.add(scan.nextLine());

            }
            //the following for loop prints the strings as they were entered
            System.out.print("\n1. ");
            for(int i=0; i < strings.size(); i++ ) {

                System.out.printf("%s ", strings.get(i));

            }
            //the 2nd print reverses the order of the original strings
            System.out.print("\n2. ");
            for(int i = strings.size()-1; i > 0; i-- ) {

                System.out.printf("%s ", strings.get(i));

            }  

            //the 3rd print copies the array into a set then sorts and prints in order
            System.out.print("\n3. ");
            setstrings.clear();
            setstrings.addAll(strings);
            Collections.sort(strings);
            for(int i=0; i < strings.size(); i++) {

                System.out.printf("%s ", strings.get(i));
            }

            // the last print uses the unsorted set and then copies it to a list
            // ultimately eliminating duplicate entries

            System.out.print("\n4. ");

            ArrayList list = new ArrayList(setstrings);

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

                System.out.printf("%s ", list.get(i));
            }

        }

        // the next two if statements follow the same pattern as the first
        if(datatype ==2)
        {
            System.out.println("enter " + number + " doubles");

            for(int i=0; i < number; i++)
            {
                doubles.add(scan.nextDouble());

            }
            System.out.print("\n1. ");
            for(int i=0; i < doubles.size(); i++ ) {

                System.out.printf("%s ", doubles.get(i));

            }

            System.out.print("\n2. ");
            for(int i = doubles.size()-1; i > -1; i-- ) {

                System.out.printf("%s ", doubles.get(i));

            }    

            System.out.print("\n3. ");
            setdoubles.clear();
            setdoubles.addAll(doubles);
            Collections.sort(doubles);
            for(int i=0; i < doubles.size(); i++) {

                System.out.printf("%s ", doubles.get(i));
            }

            System.out.print("\n4. ");

            ArrayList list2 = new ArrayList(setdoubles);

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

                System.out.printf("%s ", list2.get(i));
            }

        }
        if(datatype ==3)
        {
            System.out.println("enter " + number + " integers");

            for(int i=0; i < number; i++)
            {
                integers.add(scan.nextInt());

            }
            System.out.print("\n1. ");
            for(int i=0; i < integers.size(); i++ ) {

                System.out.printf("%s ", integers.get(i));

            }

            System.out.print("\n2. ");
            for(int i = integers.size()-1; i > -1; i-- ) {

                System.out.printf("%s ", integers.get(i));

            }    

            System.out.print("\n3. ");
            setintegers.clear();
            setintegers.addAll(integers);
            Collections.sort(integers);
            for(int i=0; i < integers.size(); i++) {

                System.out.printf("%s ", integers.get(i));
            }

            System.out.print("\n4. ");

            ArrayList list3 = new ArrayList(setintegers);

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

                System.out.printf("%s ", list3.get(i));
            }

        }

    }
}

Screenshot:

eclipse lab#5

Leave a Reply

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