Description: This lab uses arrays alongside with its own capabilities given by the library. With it, you could sort the elements by using keyword Arrays.sort() and other capabilities alike. Another feature in this lab was the ArrayList, which works on an unfixed amount of elements. The biggest difference between an array and an ArrayList is that you have to forcefully give it a size before using it, after that, the size of array cannot be changed. However, with the ArrayList, you can keep adding or removing elements. This was really helpful when removing duplicate elements which saved a lot of time instead of creating logic behind the comparison behind an array to remove duplicate elements. The biggest issue I had with the lab was that I could not figure out the logic behind comparing the array to itself and removing the duplicates. However, after researching more into ArrayList, I was able to program it another way. This involved using contain and size. I had the contain check if an empty ArrayList had a color for example, I used contain to check, and if it didn't, it would add it to the ArrayList. This guaranteed that the program did not add any duplicates. Overall it made everything, easier. Code: Main Class: import java.util.InputMismatchException; import java.util.Scanner; import java.util.Arrays; import java.util.ArrayList; public class Elements { public static void main(String[] args) { int decision = 0, elements, counter, arraysize, intelement, loop = 1, loop2 = 1; double dblelement; boolean check; String word; Scanner input = new Scanner(System.in); while(loop == 1){ loop2 = 1; System.out.println("Which data type would you like to use for the elements?"); System.out.println("(1) String\n(2) Double\n(3) Integer\n(4) Exit"); System.out.print("Choice: "); try { decision = input.nextInt(); while(decision 5){ System.out.println("Please enter valid choice!"); decision = input.nextInt(); } if(decision == 4){ loop = 0; loop2 = 0; } }catch(InputMismatchException e) { System.out.println("This input is invalid!"); input.nextLine(); //clear input value continue; } while (loop2 == 1) { System.out.println("How many elements would you like to process?"); System.out.print("Choice: "); try{ elements = input.nextInt(); while (elements < 1) { System.out.print("Please enter values more than 0)"); elements = input.nextInt(); } }catch(InputMismatchException e) { System.out.println("This input is invalid!"); input.nextLine(); //clear input value continue; } ArrayList strarraylist = new ArrayList(); ArrayList strdupe = new ArrayList(); ArrayList dblarraylist = new ArrayList(); ArrayList dbldupe = new ArrayList(); ArrayList intarraylist = new ArrayList(); ArrayList intdupe = new ArrayList(); String[] strarray = new String[elements]; double[] dblarray = new double[elements]; int[] intarray = new int[elements]; switch(decision) { case 1: input.nextLine(); for(counter = 0; counter < elements; counter++) { System.out.print("Enter string value: "); word = input.nextLine(); strarray[counter] = word; strarraylist.add(counter, word); } System.out.println("\nString:"); System.out.print("Elements in order: "); for(counter = 0; counter = 0; counter--) { System.out.print(strarraylist.get(counter)+" "); } System.out.print("\nElements in ascending order: "); Arrays.sort(strarray); for(counter = 0; counter < elements; counter++) { System.out.print(strarray[counter]+" "); } System.out.print("\nRemoved duplicated elements list: "); for(counter = 0; counter < elements; counter++) { word = strarraylist.get(counter); check = strdupe.contains(word); if(check == false) { strdupe.add(word); } } arraysize = strdupe.size(); for(counter = 0; counter < arraysize; counter++) { System.out.print(strdupe.get(counter)+" "); } break; case 2: for(counter = 0; counter < elements; counter++) { System.out.print("Enter double value: "); dblarray[counter] = input.nextDouble(); dblarraylist.add(counter, dblarray[counter]); } System.out.println("\nDouble:"); System.out.print("Elements in order: "); for(counter = 0; counter = 0; counter--) { System.out.print(dblarray[counter]+" "); } System.out.print("\nElements in ascending order: "); Arrays.sort(dblarray); for(counter = 0; counter < elements; counter++) { System.out.print(dblarray[counter]+" "); } System.out.print("\nRemoved duplicated elements list: "); for(counter = 0; counter < elements; counter++) { dblelement = dblarraylist.get(counter); check = dbldupe.contains(dblelement); if(check == false) { dbldupe.add(dblelement); } } arraysize = dbldupe.size(); for(counter = 0; counter < arraysize; counter++) { System.out.print(dbldupe.get(counter)+" "); } break; case 3: for(counter = 0; counter < elements; counter++) { System.out.print("Enter integer value: "); intarray[counter] = input.nextInt(); } System.out.println("Integer:"); System.out.print("Elements in order: "); for(counter = 0; counter = 0; counter--) { System.out.print(intarray[counter]+" "); } System.out.print("\nElements in ascending order: "); Arrays.sort(intarray); for(counter = 0; counter < elements; counter++) { System.out.print(intarray[counter]+" "); } System.out.print("\nRemoved duplicated elements list: "); for(counter = 0; counter < elements; counter++) { intelement = intarraylist.get(counter); check = intdupe.contains(intelement); if(check == false) { intdupe.add(intelement); } } arraysize = intdupe.size(); for(counter = 0; counter < arraysize; counter++) { System.out.print(intdupe.get(counter)+" "); } break; } System.out.println("\n"); loop2 = 0; } } } }
Write a brief paragraph to introduce yourself to your visitors and explain what they will find on this site. Be sure to add more developed content to your About Me page. (Edit this paragraph in Dashboard > Widgets > Text)
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.