(30 marks) Write a class of static ArrayListMethods according to the UML descrip
ID: 3531193 • Letter: #
Question
(30 marks) Write a class of static ArrayListMethods according to the UML description below and a GUI to test them. ArrayListMethods + printIntegerList(list: ArrayList): void + printStringList(list: ArrayList): void + getIntegerMax(list: ArrayList): int + getStringMax(list: ArrayList): String * + getIntegerPosition(list: ArrayList, int element): int ** + getStringPosition(list: ArrayList, String element): int ** +sortIntegerList(list: ArrayList): void *** + sortStringList(list: ArrayList): void *** *Max of a list of strings is the one that has highest alphabetical order - i.e., zebra is maximum over aardvark. **Return the position of the first occurrence of the element, -1 if it is not found. Use linear search here. ***You can use any sorting method we discussed in COSC 1046 - selection or insertion are good choices BUT you may NOT use any built-in sorting from the java libraries. When you have made sure that these methods work - I suggest writing a small hard-coded command line tester program just to make sure they work - continue to the next step. (30 marks) Write a GUI or command line application that does the following: Allow the user to specify whether or not they want to work with Strings or Integers. Create an appropriate empty list. Allow the user to add elements or remove elements from the list. Allow the user to call the functions from your static class. If you are using a GUI you can use menus or buttons or enter keys on text fields as you see fit but make sure it is clear to the markers how your GUI works. If you are using command line you'll need to build some sort of menu that the user can select from. The current list (either integers or strings) should print any time the list is updated (add, remove, sort, etc). Make sure your tester calls the static methods from their own class - do not re-write them in your tester class. Remember, once the user has selected what type of list they want (int or String) your tester should enforce that type by calling the right methods and producing an error if the user tries to do something that is not appropriate. ***Hint: You cannot specify an ArrayList without a type - eg. ArrayList list = new ArrayList(); This is a bit of a problem if we don't know what type of list we'll need. You have two options, 1. Create data fields of both types ArrayList and ArrayList and simply use the one you need and ignore the other. 2, Create your list as a local variable and simply pass it to the static class. (minus 10 marks) - Hard code a tester that shows all of your methods working for array lists of strings and integers.Explanation / Answer
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ArrayListMethodsTest {
static class ArrayListMethods {
public void printIntegerList(List<Integer> integerList) {
for (int i : integerList) {
System.out.println(i + " ");
}
}
public void printStringList(List<String> stringList) {
for (String i : stringList) {
System.out.println(i + " ");
}
}
public int getIntegerMax(List<Integer> integerList) {
int max = 0;
for (int i : integerList) {
if (i > max)
max = i;
}
return max;
}
public String getStringMax(List<String> stringList) {
String max = "";
for (String i : stringList) {
if (i.compareTo(max) < 0)
max = i;
}
return max;
}
public int getIntegerPosition(List<Integer> integerList, int element) {
int position = -1;
for (int i = 0; i < integerList.size(); i++) {
if (element == integerList.get(i)) {
position = i;
break;
}
}
return position;
}
public int getStringPosition(List<String> stringList, String element) {
int position = -1;
for (int i = 0; i < stringList.size(); i++) {
if (element.equals(stringList.get(i))) {
position = i;
break;
}
}
return position;
}
public void sortIntegerList(List<Integer> integerList) {
for (int i = 0; i < integerList.size(); i++) {
for (int j = i; j < integerList.size(); j++) {
if (integerList.get(i) > integerList.get(j)) {
int t = integerList.get(i);
integerList.set(i, integerList.get(j));
integerList.set(j, t);
}
}
}
}
public void sortStringList(List<String> stringList) {
for (int i = 0; i < stringList.size(); i++) {
for (int j = i; j < stringList.size(); j++) {
if (stringList.get(i).compareTo(stringList.get(j)) > 0) {
String t = stringList.get(i);
stringList.set(i, stringList.get(j));
stringList.set(j, t);
}
}
}
}
}
public static void main(String[] args) {
Scanner inScanner = new Scanner(System.in);
System.out.println("Options are: ");
System.out.println("1. Want to work with Strings ");
System.out.println("2. Want to work with Integer ");
System.out.println("Enter your choice: ");
int choice1 = inScanner.nextInt();
while (true) {
System.out.println("Options are: ");
System.out.println("1. Insert ");
System.out.println("2. Delete ");
System.out.println("3. Print ");
System.out.println("4. Get Max ");
System.out.println("5. Get Position ");
System.out.println("6. Sort");
System.out.println("7. Exit");
System.out.println("Enter your choice: ");
int choice2 = inScanner.nextInt();
ArrayListMethodsTest.ArrayListMethods am = new ArrayListMethodsTest.ArrayListMethods();
switch (choice1) {
case 1:
List<String> stringList = new ArrayList<String>();
switch (choice2) {
case 1:
System.out.println("Enter element to add:");
String element = inScanner.next();
stringList.add(element);
break;
case 2:
System.out.println("Enter element to remove:");
String element2 = inScanner.next();
stringList.remove(element2);
break;
case 3:
am.printStringList(stringList);
break;
case 4:
System.out.println("max item "
+ am.getStringMax(stringList));
break;
case 5:
System.out.println("enter element");
String element3 = inScanner.next();
System.out.println("position : "
+ am.getStringPosition(stringList, element3));
break;
case 6:
am.sortStringList(stringList);
am.printStringList(stringList);
break;
case 7:
return;
}
break;
case 2:
List<Integer> integerList = new ArrayList<Integer>();
switch (choice2) {
case 1:
System.out.println("Enter element to add:");
int element = inScanner.nextInt();
integerList.add(element);
break;
case 2:
System.out.println("Enter element to remove:");
int element2 = inScanner.nextInt();
integerList.remove(element2);
break;
case 3:
am.printIntegerList(integerList);
break;
case 4:
System.out.println("max item "
+ am.getIntegerMax(integerList));
break;
case 5:
System.out.println("enter element");
int element3 = inScanner.nextInt();
System.out.println("position : "
+ am.getIntegerPosition(integerList, element3));
break;
case 6:
am.sortIntegerList(integerList);
am.printIntegerList(integerList);
break;
case 7:
return;
}
break;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.