Use Java Write a class that provides the following three methods: 1. Iteratively
ID: 3838554 • Letter: U
Question
Use Java
Write a class that provides the following three methods: 1. Iteratively reverse a list public static LList iterativeReverseList(LList list) that accepts a reference to a LList and returns a reference to another LList that contains the data of the original list in reverse order. Your method should be implemented using iteration. Recursively Reverse a list public static LList recursiveREverseList(LList list) that accepts a reference to a LListand returns a reference to another LList that contains the data of the original list in reverse order. Your method should be implemented using recursion Sort Using ArrayList public static ArrayList insertSort(ArrayList list) that accepts a reference to an ArrayList and returns another ArrayList that contains the data of the original list in ascending order. You can implement your method as insertion sort or selection sort. You cannot use the Java sort methods. Write a test class to tExplanation / Answer
PROGRAM CODE:
package arrays;
import linkedlist.LList;
public class ArrayListTester<T extends Comparable<? super T>> {
public LList<T> iterativeReverseList(LList<T> list)
{
LList<T> newList = new LList<>();
for(int i=list.size-1; i>=0; i--)
{
newList.add(list.get(i));
}
return newList;
}
public LList<T> recursiveReverseList(LList<T> list, LList<T> newList)
{
if(list.size == 0)
return newList;
T data = list.get(list.size-1);
list.remove(data);
newList.add(data);
return recursiveReverseList(list, newList);
}
public ArrayList<String> insertsort(ArrayList<String> list)
{
for(int i=0; i<list.size(); i++)
{
for(int j=i+1; j<list.size(); j++)
{
if(list.get(i).compareTo(list.get(j))>0)
{
String temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
return list;
}
public static void main(String[] args) {
ArrayListTester<String> tester = new ArrayListTester<>();
String data[] = {"Zebra", "Monkey", "Ant", "Cat", "Elephant", "Dog", "Antelope", "Lion","Deer", "Tiger", "Wolf", "Rabbit", "Snake", "Sparrow"
, "Parrot", "Spider", "Caterpillar", "Butterfly", "Sheep", "Panda"};
ArrayList<String> list = new ArrayList<>();
LList<String> reverselist = new LList<>();
LList<String> reverselist2 = new LList<>();
System.out.print("Initial List: [ ");
for(String string: data)
{
list.add(string);
reverselist.add(string);
reverselist2.add(string);
System.out.print(string + ", ");
}
System.out.print("] ");
//System.out.println(" Size of reverse list: " + reverselist.size);
System.out.println(" Sorted list: " + tester.insertsort(list));
System.out.print(" Recursive reverse list: " );
tester.recursiveReverseList(reverselist, new LList<>()).print();
System.out.print(" Iterative reverse list: ");
tester.iterativeReverseList(reverselist2).print();
}
}
OUTPUT:
Initial List: [ Zebra, Monkey, Ant, Cat, Elephant, Dog, Antelope, Lion, Deer, Tiger, Wolf, Rabbit, Snake, Sparrow, Parrot, Spider, Caterpillar, Butterfly, Sheep, Panda, ]
Sorted list: [Ant, Antelope, Butterfly, Cat, Caterpillar, Deer, Dog, Elephant, Lion, Monkey, Panda, Parrot, Rabbit, Sheep, Snake, Sparrow, Spider, Tiger, Wolf, Zebra]
Recursive reverse list: [ Panda, Sheep, Butterfly, Caterpillar, Spider, Parrot, Sparrow, Snake, Rabbit, Wolf, Tiger, Deer, Lion, Antelope, Dog, Elephant, Cat, Ant, Monkey, Zebra, ]
Iterative reverse list: [ Panda, Sheep, Butterfly, Caterpillar, Spider, Parrot, Sparrow, Snake, Rabbit, Wolf, Tiger, Deer, Lion, Antelope, Dog, Elephant, Cat, Ant, Monkey, Zebra, ]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.