Question

1. Paste the code below into your Java IDE and make sure it runs correctly. 2....

1. Paste the code below into your Java IDE and make sure it runs correctly.

2. Create a program that receives numeric data from the user and then adds it to a queue.

3. Create a program that receives text based data and then adds it to a queue.

4. Create a program that automatically populates a queue on start up with 5 pieces of data and then displays each entry in the queue after receiving a command from the user.

Make sure you submit your coding answers to this assessment before you leave class. Make sure everyone's name is on the submission.

Prof. Nizich

package q_sample;

import java.util.LinkedList;

import java.util.NoSuchElementException;

import java.util.Queue;

public class q_sample {

    public static void main(String[] args) {

        Queue myQueue = new LinkedList();

        myQueue.offer("Monday");

        myQueue.offer("Thusday");

        boolean flag = myQueue.offer("Wednesday");

        System.out.println("Wednesday inserted successfully? "+flag);

        try {

            myQueue.add("Thursday");

            myQueue.add("Friday");

            myQueue.add("Weekend");

        } catch (IllegalStateException e) {

            e.printStackTrace();

        }

        System.out.println("Pick the head of the queue: " + myQueue.peek());

        String head = null;

        try {

            head = (String) myQueue.remove();

            System.out.print("1) Push out " + head + " from the queue ");

            System.out.println("and the new head is now: "+myQueue.element());

        } catch (NoSuchElementException e) {

            e.printStackTrace();

        }

        head = (String) myQueue.poll();

        System.out.print("2) Push out " + head + " from the queue");

        System.out.println("and the new head is now: "+myQueue.peek());

        System.out.println("Does the queue contain 'Weekend'? " + myQueue.contains("Weekend"));

        System.out.println("Does the queue contain 'Monday'? " + myQueue.contains("Monday"));

    }

}

Homework Answers

Answer #1

//This below program is the rectifyed one and it work successfully
package com.acadgild;

import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;

public class q_sample {
  
   public static void main(String[] args) {
      
       //changes made in the below code by adding String on both the sides of the interface as queue is an interfave class.
   Queue<String> myQueue = new LinkedList<String>();
   myQueue.offer("Monday");
   myQueue.offer("Thusday");
     
   boolean flag = myQueue.offer("Wednesday");
     
   System.out.println("Wednesday inserted successfully? "+flag);
     
   try {
   myQueue.add("Thursday");
   myQueue.add("Friday");
   myQueue.add("Weekend");
     
   } catch (IllegalStateException e) {
     
   e.printStackTrace();
   }
     
   System.out.println("Pick the head of the queue: " + myQueue.peek());
   String head = null;
     
   try {
     
   head = (String) myQueue.remove();
   System.out.print("1) Push out " + head + " from the queue ");
   System.out.println("and the new head is now: "+myQueue.element());
     
   } catch (NoSuchElementException e) {
     
   e.printStackTrace();
     
   }
     
   head = (String) myQueue.poll();
   System.out.print("2) Push out " + head + " from the queue");
   System.out.println("and the new head is now: "+myQueue.peek());
     
   System.out.println("Does the queue contain 'Weekend'? " + myQueue.contains("Weekend"));
     
   System.out.println("Does the queue contain 'Monday'? " + myQueue.contains("Monday"));
   }

}

#program -02
package com.acadgild;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Second {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
      
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the number as theinput!");
       int x= scan.nextInt();          
      
       Queue queue = new LinkedList();
       queue.add(x);
      
       for (Object object : queue) {
             
           System.out.println("Successfully added the input taken from the user"
                  + "Added to the queue"+object.toString());
          
       }
              

   }

}

#Program - 03
package com.acadgild;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Third {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
      
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the number as theinput!");
       int x= scan.nextLine();//Change the int to the Line      
      
       Queue queue = new LinkedList();
       queue.add(x);
      
       for (Object object : queue) {
             
           System.out.println("Successfully added the input taken from the user"
                  + "Added to the queue"+object.toString());
          
       }
              

   }

}

#program -04
package com.acadgild;
import java.util.*;
public class QueueDemoQueue {
static String newLine = System.getProperty("line.separator");
public static void main(String[] args) {
  
System.out.println(newLine + "Queue in Java" + newLine);
System.out.println("-----------------------" + newLine);
System.out.println("Adding items to the Queue" + newLine);
//Creating queue would require you to create instannce of LinkedList and assign
//it to Queue
//Object. You cannot create an instance of Queue as it is abstract
Queue queue = new LinkedList();
  
//you add elements to queue using add method
queue.add("A");
queue.add("B");

queue.add("C");
queue.add("D");
queue.add("E");
  
System.out.println(newLine + "Items in the queue..." + queue + newLine);

//You remove element from the queue using .remove method
//This would remove the first element added to the queue, here Java
System.out.println("remove element: " + queue.remove() + newLine);
  
//.element() returns the current element in the queue, here when "java" is removed
//the next most top element is .NET, so .NET would be printed.
System.out.println("retrieve element: " + queue.element() + newLine);
  
//.poll() method retrieves and removes the head of this queue
//or return null if this queue is empty. Here .NET would be printed and then would
//be removed
//from the queue
System.out.println("remove and retrieve element, null if empty: " + queue.poll() +
newLine);
  
//.peek() just returns the current element in the queue, null if empty
//Here it will print Javascript as .NET is removed above
System.out.println("retrieve element, null is empty " + queue.peek() + newLine);

}}

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
I need the java code for a 4-function calculator app on android studio. Please make sure...
I need the java code for a 4-function calculator app on android studio. Please make sure all the requirements shown below are followed (such as the error portion and etc). The topic of this app is to simply create a 4 function calculator which calculates math expressions (add, subtract, multiply, and divide numbers). The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
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 -...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT