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

Introduction This assignment is the investigation into the use of various Java d

ID: 674534 • Letter: I

Question

Introduction

This assignment is the investigation into the use of various Java data structures (non-primitive data types). Sorting, printing, and iterating across these structures will also be investigated.

Procedure

This assignment will cover material that is discussed in the lab as well as in class next week. In addition, see the text and the Java API for the Arrays class and the LinkedList class. You must use packages.

Part A

The student will: load arrays into LinkedLists and print them out as Stacks or Queues.
                        : sort Lists using the Collections class.
                        : traverse lists.

1. Make a new class called StacksAndQueues to contain all the methods to be called from the main method in UseStacksAndQueues. Obviously you are creating a new project in the same manner as in the previous lab.

     Add import java.util.LinkedList ;
              
import java.util.ListIterator;

    public class StacksAndQueues      // 8 public methods to be written and tested
        1.    Load an array of String objects into a LinkedList starting with array[0].
                Method takes an array of type Object, returns a LinkedList. Use a for loop.
        2.    Display a list as a stack, so array[0] is displayed last.
                It takes a LinkedList . Use get(i);
        3.    Display a List as a queue, so array[0] is displayed first.
                Method takes a LinkedList. Use get(i);
        4.    Use a ListIterator object to traverse a list and display it as a Queue.
                Method takes a LinkedList. To get a ListIterator and go first to last you need to write:
                        ListIterator myIterator = myList.listIterator();
                        while( myIterator.hasNext() )
                                    System.out.print( " "+myIterator.next()+" " );
        
5.    Use a ListIterator object to traverse a list and display it as a Stack.
                Method takes a LinkedList. Check out the methods of the LinkedList class in the API; this requires a little care!
        6.    Load strings from the keyboard into a LinkedList and display it. Use the Scanner class.
                Method takes no parameters, returns a LinkedList. Note that the size of the LinkedList is dependent upon the number of strings entered.
        7. Sort a LinkedList using the Collections class then display it. Use Collections.sort(someList);  
                
Method takes a LinkedList
        8.    Displays a list, and allows the user to remove an element from the list and display again.
                Method takes a LinkedList

2. Make a new class called UseStacksAndQueues

     Add import java.util.LinkedList ;
              
import java.util.ListIterator;

            public class UseStacksAndQueues {
                public static void main(String[] args) {
                    StacksAndQueues sQ = new StacksAndQueues();
                    String [] days = {"mon","tue","wed","thur","fri", "sat","sun"};
                    LinkedList aList = new LinkedList();
                    LinkedList newList = new LinkedList();

      Then using StacksAndQueues methods:
            1. load aList with the array of 7 strings called days
            2. print aList out as Stack using an internal iterator ( get(i) )
            3. print aList out as Queue using an internal iterator ( get(i) )
            4. print aList out as Stack using an external iterator ( ListIterator )
            5. print aList out as Queue using an external iterator ( ListIterator )
            6. read in some strings from the keyboard and return as newList, display.
            7. display newList just loaded and remove a selected element , display.        
            8. sort newList with the Collections class and print it out.     

Part B

The student will sort and print arrays of Record objects.

1.    Write a Record class containing an integer field, a default constructor, a constructor to take a single int parameter, setters and getters as necessary, a toString() method and a compareTo() method for sorting (See Rectagle1.java comparable and compareTo). It must implement the CompareTo interface. See Rectangle1.java from class.

2. i) Make a UseRecords class for testing.
        Add import java.util.Arrays; to your code when using Arrays class.
   ii) Load 5 Record objects with a generated random positive integer less than 20 (0 <= x < 20).
   iii) Add them one at a time to an array of Records.
   iv) Print out the array using a for loop and System.out.println( array[i] ) . Remember that the toString() method is called by default in println() .
   v) Use the Arrays class to sort the array according to the integer value and print it again.
   vi) Load the sorted array into a Linked List.
   vii) Iterate through the list and add the value of the integers in the list using ((Record)(recordList.get(j))).getValue(); in a for loop.
        Note: each object must be cast into type Record after getting it from the list before you can access the method for returning the value of the Record object.
   viii) Print out the total.
   ix) Using the compareTo method, print a message to indicate if any two integers are equal. Find an efficient way to implement the code.
   Bonus mark: Implement this last part with just one ListIterator (i.e. no other variables). Explain why the compareTo method cannot be modified.

Explanation / Answer

//Program1:


package usestacksandqueues;

import java.util.LinkedList ;
import java.util.ListIterator;
public class UseStacksAndQueues {
public static void main(String[] args) {
StacksAndQueues sQ = new StacksAndQueues();
String [] days = {"mon","tue","wed","thur","fri", "sat","sun"};
LinkedList aList;
LinkedList newList;
/* 1. load aList with the array of 7 strings called days */
aList=sQ.ArrayToList(days);
/*2. print aList out as Stack using an internal iterator ( get(i) )*/
sQ.ListAsStack(aList);
/*3. print aList out as Queue using an internal iterator ( get(i) )*/
sQ.ListAsQueue(aList);
/*4. print aList out as Stack using an external iterator ( ListIterator )*/
sQ.PrintAsStack(aList);
/*5. print aList out as Queue using an external iterator ( ListIterator )*/
sQ.PrintAsQueue(aList);
/*6. read in some strings from the keyboard and return as newList, display.*/
newList=sQ.getLinkedList();
/* 7. display newList just loaded and remove a selected element , display. */
sQ.removeItem(newList);
/*8. sort newList with the Collections class and print it out. */
sQ.sorted(newList);

}
}

//============================================================


package usestacksandqueues;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList ;
import java.util.ListIterator;
import java.util.Scanner;
class StacksAndQueues {
/*1. Load an array of String objects into a LinkedList starting with array[0].
Method takes an array of type Object, returns a LinkedList. Use a for loop.*/
public LinkedList ArrayToList(String[] arr)
{
LinkedList L=new LinkedList();
for(int i=0;i<arr.length;i++)
L.add(arr[i]);
return L;
}
/*2. Display a list as a stack, so array[0] is displayed last.
It takes a LinkedList . Use get(i);*/
public void ListAsStack(LinkedList L)
{ System.out.println(" List to Stack: ");
for(int i=L.size()-1;i>=0;i--)
System.out.println(L.get(i));
}
/*3. Display a List as a queue, so array[0] is displayed first.
Method takes a LinkedList. Use get(i);*/
public void ListAsQueue(LinkedList L)
{ System.out.println(" List to Queue: ");
for(int i=0;i<L.size();i++)
System.out.print(" "+L.get(i)+" ");
}
/*4. Use a ListIterator object to traverse a list and display it as a Queue.
Method takes a LinkedList. To get a ListIterator and go first to last you need to write:
ListIterator myIterator = myList.listIterator();
while( myIterator.hasNext() )
System.out.print( " "+myIterator.next()+" " );*/
public void PrintAsQueue(LinkedList L)
{ System.out.println(" List to Queue: ");
ListIterator myIterator = L.listIterator();
while( myIterator.hasNext() )
System.out.print( " "+myIterator.next()+" " );
}
/* 5. Use a ListIterator object to traverse a list and display it as a Stack.
Method takes a LinkedList. Check out the methods of the LinkedList class in the API; this requires a little care!*/
public void PrintAsStack(LinkedList L)
{ System.out.println(" List to Stack: ");
if(!L.isEmpty())
{
ListIterator myIterator = L.listIterator();
while( myIterator.hasPrevious() )
System.out.print( " "+myIterator.previous()+" " );
}
else
System.out.println(" Empty Stack");
}
/*6. Load strings from the keyboard into a LinkedList and display it. Use the Scanner class.   
Method takes no parameters, returns a LinkedList. Note that the size of the LinkedList is dependent
upon the number of strings entered.*/
public LinkedList getLinkedList()
{
LinkedList L=new LinkedList();
Scanner scan=new Scanner(System.in);
System.out.println(" Enter number of Strings you want to insert");
int n=scan.nextInt();
for(int i=0;i<n;i++)
{
System.out.println(" Enter a String");
L.add(scan.next());
}
return L;
}
  
  
/* 7. Sort a LinkedList using the Collections class then display it. Use Collections.sort(someList);
Method takes a LinkedList*/
public void sorted(LinkedList L)
{
Collections.sort(L);
System.out.println("Sorted list entries: ");
for (Iterator it = L.iterator(); it.hasNext();) {
Object L1 = it.next();
System.out.println(L1);
}
}
/*8. Displays a list, and allows the user to remove an element from the list and display again.
Method takes a LinkedList */
public void removeItem(LinkedList L)
{
System.out.println("List elements: ");
for (Iterator it = L.iterator(); it.hasNext();) {
Object L1 = it.next();
System.out.println(L1);
}
System.out.println("Enter the index of item to remove:");
Scanner scan=new Scanner(System.in);
int index=scan.nextInt();
L.remove(index);
System.out.println("List elements after removal of index: ");
for (Iterator it = L.iterator(); it.hasNext();) {
Object L1 = it.next();
System.out.println(L1);
}
}
}

//Output1:

run:

List to Stack:
sun
sat
fri
thur
wed
tue
mon

List to Queue:
mon tue wed thur fri sat sun
List to Stack:

List to Queue:
mon tue wed thur fri sat sun
Enter number of Strings you want to insert
5

Enter a String
apple

Enter a String
zebra

Enter a String
bat

Enter a String
cat

Enter a String
rat
List elements:
apple
zebra
bat
cat
rat
Enter the index of item to remove:
1
List elements after removal of index:
apple
bat
cat
rat
Sorted list entries:
apple
bat
cat
rat
BUILD SUCCESSFUL (total time: 51 seconds)

//======================================================================

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote