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
[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...
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...
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...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...