Question

In: Computer Science

Problem 4 • Use Java Collections to write a small program using NetBeans to do the...

Problem 4 •

Use Java Collections to write a small program using NetBeans to do the following:

1. Create a queue object of Integer items, myQueue.

2. Create a stack object of Integer items, myStack.

3. Enqueue the integers 10, 20. 30, .., 100 into myQueue, in that order.

4. Push the integers 10, 20, 30, ..., 100 into myStack, in that order.

5. Display the message: "Here are the elements of myQueue:".

6. Remove and display the front element of the queue until myQueue becomes empty.

7. Display the message: "And here are the elements of myStack:".

8. Remove and display the top element of the stack until myStack becomes empty.

Solutions

Expert Solution

CODE:

import java.util.*;
public class Main {

public static void main(String[] args) {
   Queue<Integer> myQueue = new LinkedList<Integer>();
       Stack<Integer> myStack= new Stack<Integer>();
for (int i = 10; i <=100 ; i+=10)
{ myQueue.add(i);
myStack.push(i);
}
System.out.println("Here are the elements of myQueue:"+ myQueue);

while (!myQueue.isEmpty()){
int remele = myQueue.remove();
System.out.println("removed element from the front:"+ remele);

if(!myQueue.isEmpty())
{
System.out.println(myQueue);
int front = myQueue.peek();
System.out.println("front element of myQueue:" + front);
System.out.println("-----------------------------------------");
}

}
System.out.println("================================================");
System.out.println("And here are the elements of myStack: " + myStack);
while (!myStack.isEmpty()){
int poppedele = myStack.pop();
System.out.println("removed element from the top:"+ poppedele);
if(!myStack.isEmpty())
{
System.out.println(myStack);
int top = myStack.peek();
System.out.println("top element of myQueue:" + top);
System.out.println("-----------------------------------------");
}
}


}

}

OUTPUT:


Related Solutions

Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4...
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4 grocery stores. Each store has 3 departments where product presentation affects sales (produce, meat, frozen). Every so often a department in a store gets a bonus for doing a really good job. You need to create a program that keeps a table of bonuses in the system for departments. Create a program that has a two dimensional array for these bonuses. The stores can...
CS 206 Visual Programming, Netbeans Problem: Write a Java program that displays all the leap years,...
CS 206 Visual Programming, Netbeans Problem: Write a Java program that displays all the leap years, 10 per line, from 1001 to 2100, separated by exactly one space. Also display the total number of leap years in this period. Hints: you need to use a loop ( for-loop is more suitable)
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
USING JAVA (netbeans) In your main, ask the user for an int. Do this by first...
USING JAVA (netbeans) In your main, ask the user for an int. Do this by first printing a request for the int, then creating an int x, and using the Scanner to read an int from the user and put it in x, like this: int x = scan.nextInt(); ☑ Next read in two doubles d1 and d2 from the user. This will look a lot like what you did for the int, but the scanner reads doubles using scan.nextDouble();...
I am coding with NetBeans LDE Java. Write a program that reads a positive integer n...
I am coding with NetBeans LDE Java. Write a program that reads a positive integer n , prints all sums from 1 to any integer m 1≤m≤n . For example, if n=100, the output of your program should be The sum of positive integers from 1 to 1 is 1;       The sum of positive integers from 1 to 2 is 3;       The sum of positive integers from 1 to 3 is 6;       The sum of positive integers...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b. Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first...
Use Java GUI to write a chessboard 8 Queens Problem Write a program that can place...
Use Java GUI to write a chessboard 8 Queens Problem Write a program that can place 8 queens in such a manner on an 8 x 8 chessboard that no queens attack each other by being in the same row, column or diagonal.
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
Write a program ( Java) to solve the 8-puzzle problem (and its natural generalizations) using the...
Write a program ( Java) to solve the 8-puzzle problem (and its natural generalizations) using the A* search algorithm. The problem. The 8-puzzle problem is played on a 3-by-3 grid with 8 square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the blocks so that they are in order. You are permitted to slide blocks horizontally or vertically into the blank square. The following shows a sequence of legal moves from an initial board...
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT