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
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
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 -...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
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...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have this much public class Main { int WIDTH = 0; double HEIGHT = 0; public static void drawSmileyFaces() { System.out.println(" :-):-):-):-):-):-)"); } public static void main (String[] args) { for (int WIDTH = 0; WIDTH < 3; WIDTH++ ) { for(double HEIGHT = 0; HEIGHT < 2; HEIGHT++) { drawSmileyFaces(); } } } } I am pretty sure that alot of this is wrong...
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
1. you will just write your name in bytes - once in UTF8 bytes, once in...
1. you will just write your name in bytes - once in UTF8 bytes, once in UTF16 bytes. Name :Andrew Submit: 1. Your .java file 2. A screenshot showing your code ran and gave the right output. ---- /** * In this program you will write your first name in bytes in 2 different encodings. * Then convert the byte array to a String and print it out. * * TODO in lecture: show students how I google to find...
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...