Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class ArrayManipulation { public static void main(String[] args ) { Integ

ID: 3689068 • Letter: P

Question

public class ArrayManipulation

{

  public static void main(String[] args)

{

Integer[] a = { 1, 2, 3, 4, 5, 6, 7 };

Character[] b = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };

Double[] c = { 1.0, 2.0, 3.3, 4.4, 5.9, 8.7 };

print(a);

reverse(a);

print(a);

print(b);

reverse(b);

print(b);

print(c);

reverse(c);

print(c);

}

  public static <T> void print(T[] x)

{

// Fill in your code here

}

// Add a reverse method here

}

2. Build a class called LinkedListRunner with a main method that instantiates a LinkedList<String>.Add the following strings to the linked list:

aaa

bbb

ccc

ddd

eee

fff

ggg

hhh

iii

Build a ListIterator<String> and use it to walk sequentially through the linked list using hasNext and next, printing each string that is encountered. When you have printed all the strings in the list, use the hasPrevious and previous methods to walk backwards through the list. Along the way, examine each string and remove all the strings that begin with a vowel. When you arrive at the beginning of the list, use hasNext and next to go forward again, printing out each string that remains in the linked list.

2.  Use the class below as the driver for this lab called LinkedListRunner with a main method that instantiates a LinkedList<String>. Add the following strings to the linked list:

import java.util.LinkedList;

import java.util.Stack;

public class StackRunner

{

  public static void main(String[] args)

{

  LinkedList<String> myLinkedList1 = new LinkedList<String>();

  myLinkedList1.add("aaa");

  myLinkedList1.add("bbb");

  myLinkedList1.add("ccc");

  myLinkedList1.add("ddd");

  myLinkedList1.add("eee");

// print the first linked list

  System.out.println("My Linked List 1:");

// … your code goes here

Stack<String> myStack = new Stack<String>();

//Iterate through elements in the linked list (don't remove them), but

// push all the elements onto the stack

// … your code goes here

//Pop all the stack elements off the stack and add them to

// a new linked list

// … your code goes here

//print the second linked list

  System.out.println("My LinkedList 2:");

// … your code goes here

}

}

How is the second list ordered compared to the first? Why?

3.  Repeat Question 2, but replace the stack with a Queue. Use add() and remove() methods on the queue. Explain the ordering of elements in the second Linked list.

4. A stack is a useful tool for reversing a list of items. Pushing items onto a stack and then popping them off results in the items being returned in the reverse order from which they were pushed. Use the following class for this assignment.

public class PalindromeRunner

{

  public static void main(String[] args)

{

PalindromeTester pt = new PalindromeTester();

if (pt.isPalindrome("bob"))

{

System.out.println("bob is a palindrome");

}

else

{

  System.out.println("bob is not a palindrome");

}

if (pt.isPalindrome("amanaplanacanalpanama"))

{

  System.out.println("is a palindrome");

}

else

{

  System.out.println("amanaplanacanalpanama is not a palindrome");

}

if (pt.isPalindrome("abcdefghijklmnopqrstuvwxyz"))

}

  System.out.println("abcdefghijklmnopqrstuvwxyz is a palindrome");

}

else

{

  System.out.println("abcdefghijklmnopqrstuvwxyz is not a palindrome");

}

}

}

You will need to build the PalindromeTester class and provide it with a with the booleanisPalindrome(String s) method that returns true if the string s is a palindrome, otherwise it returns false. Populate the class with a Stack<Character> object. Push each character of the passed string onto the stack. Java will autobox each char into a Character object.

Explanation / Answer

Answer for Question#1

package assignment;

public class ArrayManipulation

{

public static void main(String[] args)

{

     Integer[] a = { 1, 2, 3, 4, 5, 6, 7 };

     Character[] b = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };

     Double[] c = { 1.0, 2.0, 3.3, 4.4, 5.9, 8.7 };

     print(a);

     reverse(a);

     print(a);

     print(b);

     reverse(b);

     print(b);

     print(c);

    reverse(c);

     print(c);

}

public static <T> void print(T[] x)

{

     // Fill in your code here
      for(T ele : x)
          System.out.print(ele+" ");
      System.out.println();

}

// Add a reverse method here
public static <T> void reverse(T[] x) {
   if(x.length > 0) {
       T temp;
       for(int i = 0; i < x.length/2; i++) {
           temp = x[i];
           x[i] = x[x.length-1-i];
           x[x.length-1-i] = temp;
       }
   }
}

}

------------------output-----------------------------

1 2 3 4 5 6 7
7 6 5 4 3 2 1
a b c d e f g
g f e d c b a
1.0 2.0 3.3 4.4 5.9 8.7
8.7 5.9 4.4 3.3 2.0 1.0