1. Write a main in a class called Explore that declares two different java.util.
ID: 3675396 • Letter: 1
Question
1. Write a main in a class called Explore that declares two different java.util.LinkedList objects. Make one's generic type String and the other's type Integer. Add a bunch of different String object to one and a bunch of Integer (wrapper class for int) objects to the other. Use the method that returns an Iterator for a LinkedList and then write a while loop that used the hasNext() and next() methods of the iterator to display all the values for one of the lists. Next, use the Java for-each statement with the other list to display all of the values in it without explicitly using hasNext(), next(), and certainly not the get() method of the list as that would be grossly inefficient (as discussed in lecture)!
2. Now write a class call Stack as a generic class (like ArrayList and LinkedList in the Java Standard Library). Here is the syntax for the class header: public class Stack { // Rest of the class code here, Element can be // used as a type for parameters and return types. }
3. The Stack class should have a single instance variable (private!) of type java.util.LinkedList. Write any constructors you want, a default constructor is sufficient.
4. Recall that a stack is a first-in-last-out data structure. Its classic operations are push to add stuff to the stack and pop to remove stuff from the stack. Both methods operate on the logical "top" of the stack, i.e., new stuff is added to the top of the stack using the push method (which "pushes" all the existing data "down") and stuff is retrieved from the list from the top using the pop method (which removes and returns the object at the top of the list and bumps everything "up"). For example, take a look at the following code using a Stack class and it associated output: Stack stack = new Stack(); stack.push("A"); stack.push("B"); stack.push("C") System.out.println(stack.pop()); stack.push("D)"; System.out.println(stack.pop()); System.out.println(stack.pop()); System.out.println(stack.pop()); The code above would produce the following output: CDBA
5. Implement push and pop methods to make your Stack class behave as the example above. You do not need to implement any other methods. DO NOT USE the push and pop methods found in the Java Standard Library LinkedList class. The goal of this lab is to expose you to how you can implement an abstract data type (stack) using a concrete data type (LinkedList). Instead, use the add and remove methods (or variants thereof – check what’s available) of the LinkedList to implement the stack push and pop methods of your Stack class.
6. Write a Tester class to test your Stack class. Be sure to include at least the code shown above.
Explanation / Answer
// Explore.java
import java.util.LinkedList;
import java.util.ListIterator;
public class Explore {
public static void main(String[] args) {
// string list
LinkedList<String> list1 = new LinkedList<String>();
//integer list
LinkedList<Integer> list2 = new LinkedList<Integer>();
// adding string into first list
list1.add("Alex");
list1.add("Book");
list1.add("Comapny");
list1.add("Delhi");
list1.add("Laptop");
list1.add("Network");
list1.add("Charger");
list1.add("Music");
list1.add("Video");
list1.add("GOOO");
// adding integers into list2
list2.add(44);
list2.add(32);
list2.add(12);
list2.add(6);
list2.add(78);
list2.add(90);
list2.add(5);
list2.add(76);
list2.add(22);
list2.add(43);
//printing lis1 using list iterator
ListIterator<String> iterator = list1.listIterator();
while(iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println(" ");
// printing list2 using for each loop
for(int i : list2){
System.out.print(i+" ");
}
}
}
/*
output:
Alex Book Comapny Delhi Laptop Network Charger Music Video GOOO
44 32 12 6 78 90 5 76 22 43
*/
// Stack.java
import java.util.ArrayList;
public class Stack <T> {
private ArrayList<T> stack = new ArrayList<T> ();
private int top = 0;
public int size () { return top; }
// push method
public void push (T item) {
stack.add (top++, item);
}
public T pop () {
return stack.remove (--top);
}
}
// tester class
class StackTester{
public static void main (String[] args) {
Stack stack = new Stack();
stack.push("A");
stack.push("B");
stack.push("C");
System.out.println(stack.pop());
stack.push("D");
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}
/*
Output:
C
D
B
A
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.