Question

Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists...

Queues, Lists, and Stacks Assignment:

Write client methods to do the following

Assume that there exists in main a list   declared as :

List<Integer> aList   =   new   List<Integer>(50);

Write a client method called bubbleSort which given a list as a parameter uses the bubble sort algorithm to sort the contents of the list.

YOU MAY ASSUME that there exists a method called swap (with the following signature) which swaps the contents of two elements:

// swap

public static void swap(List<Integer> list1, int position1, int position2)

The heading for the method should be:

public static void bubbleSort( List<Integer> list1)

Assume that in main there exists a queue declared as:

Queue<String> aQueue =   new Queue<String>(50);

Write a client method called removeAll   that given a String as a parameter removes all occurrences of the String from the queue, leaving the other values in the queue.

The heading for the method should be:

public static void removeAll(Queue<String> q1,   String word)

Assume that in main there exists 2 queue’s declared as:

Queue<String> aQueue =   new Queue<String>(50);

Queue<String> bQueue = new Q<String>(50);

Write a client method called concatenate that concatenates two queues together and creating and returning a new queue.

the heading should be

public static Queue<String> concat( Queue<String> q1, Queue<String> q2)

Assuming that in main there exists a Stack declared as:

Stack<Integer> aStack = new Stack<Integer>(50);

Write a client method called reverseStack that given a stack as a parameter, reverses the contents of the stack. In this method you can declare additional instances of queues and stacks as needed.

Its heading should be:

public static void reverseStack(Stack<Integer> s1)

Assuming that in main there exists a Stack declared as:

Stack<Integer> aStack = new Stack<Integer>(50);

Write a client method called getLast that given a stack as a parameter, returns the value on the bottom of the stack, leaving the stack unchanged when the method is finished. In this method you can declare additional instances of queues and stacks as needed.

Its heading should be:

public static int getLast(Stack<Integer> s1)

Homework Answers

Answer #1

public static void bubbleSort( List<Integer> list1)
   {
       //loop till the list size
       for (int i=0; i<list1.size(); i++)
       {
           if(list1.get(i) > list1.get(i+1) )
           {
               //if current pointer element of list is greater than next element then call method swap to swap the elements
               swap(list1, i, i+1);
           }
       } // end of for loop
   } // end of method

public static void removeAll(Queue<String> q1, String word)
   {
       //loop through the queue till size times
       for (int i = 0; i < q1.size(); i++)
       {
           //check if string word is present in the string
           if(q1.element().equals(word))
           {
               //remove the element
           q1.remove();  
           }
       }
   } //end of method

public static Queue<String> concat( Queue<String> q1, Queue<String> q2)
   {
       //declare the output Queue object
       Queue<String> q3 = new LinkedList<String>();
      
       //assign the first Q values
       q3=q1;
      
       //append the second Q
       q3.addAll(q2);
      
       return q3;
          
   } //end of method

public static void reverseStack(Stack<Integer> s1)
   {
       //declare output stack
       Stack<Integer> s2 = new Stack<Integer>();
      
       //loop while stack is empty
       while (!s1.empty())
   {
   //add element to the output stack
           s2.push(s1.pop());
   }
      
       //print the stack
       while(!s2.isEmpty())
       {
           System.out.println(s2.pop());
       }
   }

public static int getLast(Stack<Integer> s1)
   {
       //declare output stack
       Stack<Integer> s2 = new Stack<Integer>();
      
       //variable to store the last element
       int lastElement=0;
      
      
       //loop while stack is empty
       while (!s1.empty())
       {
           //assign the element, at the end of the loop, this variable will have the last element
           lastElement=s1.peek();
          
           //add element to the output stack
           s2.push(s1.pop());
         
   }
      
       //print the last element
       System.out.println("LastElement " + lastElement);
      
       //assign the stack to the original that is s1
       s1=s2;
      
       //print the stack to show the stack is intact
       while(!s1.isEmpty())
       {
           System.out.println(s1.pop());
       } //end of while loop
      
       return lastElement;
   } //end of method

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
Write the following method in java: public static void reverse(Queue<Integer> q) which uses a stack to...
Write the following method in java: public static void reverse(Queue<Integer> q) which uses a stack to reverse the order of all the items in q.
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
Write a method named changeStack that takes in a Stack of Integer objects named stackIn as...
Write a method named changeStack that takes in a Stack of Integer objects named stackIn as a parameter and returns another Stack of Integer objects. The returned Stack contains contents that is the result of switching the top half and the bottom half of the stackIn. But the ordering of integers in each half is NOT changed. In the case of odd-size stackIn, the middle element remains in the same position before and after the switch. This method is OUTSIDE...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT