Question

In: Computer Science

JAVA PROGRAMMING ASSIGNMENT - MUST USE ARRAYLIST TO SOLVE. OUTPUT MUST BE EXACT SAME FORMAT AS...

JAVA PROGRAMMING ASSIGNMENT - MUST USE ARRAYLIST TO SOLVE. OUTPUT MUST BE EXACT SAME FORMAT AS SAMPLE OUTPUT.

In this assignment, you will create a program implementing the functionalities of a standard queue in a class called Queue3503. You will test the functionalities of the Queue3503 class from the main() method of the Main class. In a queue, first inserted items are removed first and the last items are removed at the end (imagine a line to buy tickets at a ticket counter).

The Queue3503 class will contain:

a. An int[] data filed named elements to store the int values in the queue.

b. An int data field named size that stores the number of elements in the queue.

c. A no-arg constructor that creates a Queue object with default capacity 0.

d. A constructor that takes an int argument representing the capacity of the queue.

e. A method with signature enqueue(int v) that adds the int element v into the queue.

f. A method with signature dequeue() that removes and returns the first element of the queue.

g. A method with signature empty() that returns true if the queue is empty. h. A method with signature getSize() that returns the size of the queue (return type is hence int)).

The queue class you develop should be tested using the following steps: (In other words, your program named Main will consist of the following)

a. Start your queue with an initial capacity of 8.

b. When the dequeue() method is called, an element from the queue at the beginning of the queue must be removed.

c. The main() method in your Main class should consist of statements to:

i. Create a queue object;

ii. Call enqueue() to insert twenty integers (taken from the user) into the queue.

iii. After the above task is completed, include a for-loop that will print out the contents of the queue.

d. After printing the queue filled with twenty integers, call dequeue() repeatedly to remove the beginning element of the queue.

e. Print the contents of the queue after removing every fifth number.

f. For your reference, the execution of the Main program is shown below. User inputs for populating the Queue is shown in the first line. Next, your program outputs are shown.

Sample Run

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Initial content: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

After removing 5 elements: 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

After removing 5 elements: 11 12 13 14 15 16 17 18 19 20

After removing 5 elements: 16 17 18 19

Solutions

Expert Solution

/*****************************************Queue3503.java**********************************/

import java.util.ArrayList;
import java.util.Scanner;

public class Queue3503 {

   private ArrayList<Integer> queue;
   private int size;

   public Queue3503() {

       this.size = 0;
       queue = new ArrayList<Integer>(size);

   }

   public Queue3503(int capacity) {
       this.size = 0;

       queue = new ArrayList<Integer>(capacity);
   }

   public void enqueue(int v) {

       queue.add(v);
       size++;
   }

   public int dequeue() {

       int result = queue.get(0);
       queue.remove(0);
       size--;
       return result;
   }

   public boolean empty() {

       return size == 0;
   }

   public int getSize() {

       return size;

   }

   public void printQueue() {

       for (int i = 0; i < size; i++) {

           System.out.print(queue.get(i) + " ");
       }
   }

   public static void main(String[] args) {

       Queue3503 queue = new Queue3503(20);

       Scanner scan = new Scanner(System.in);

       for (int i = 0; i < 20; i++) {

           queue.enqueue(scan.nextInt());
       }

       System.out.print("Initial content: ");
       queue.printQueue();

       for (int i = 0; i < 5; i++) {

           queue.dequeue();
       }
       System.out.print("\nAfter removing 5 elements: ");
       queue.printQueue();
      
       for (int i = 0; i < 5; i++) {

           queue.dequeue();
       }
       System.out.print("\nAfter removing 5 elements: ");
       queue.printQueue();
      

       for (int i = 0; i < 5; i++) {

           queue.dequeue();
       }
       System.out.print("\nAfter removing 5 elements: ");
       queue.printQueue();
      
       scan.close();
   }
}
/**********************************output***********************/

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Initial content: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
After removing 5 elements: 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
After removing 5 elements: 11 12 13 14 15 16 17 18 19 20
After removing 5 elements: 16 17 18 19 20

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

java programming Concepts ArrayList - Collections Sorting Enhanced For Loop Collections Class Auto-boxing Programming Assignment 1....
java programming Concepts ArrayList - Collections Sorting Enhanced For Loop Collections Class Auto-boxing Programming Assignment 1. Describe auto-boxing, including why it is useful. (Google for this one) Write a few lines of code that auto-box an int into an Integer, and un-box an Integer to an int. 2. Declare an ArrayList of Strings. Add 5 names to the collection. "Bob" "Susan" ... Output the Strings onto the console using the enhanced for loop. 3. Sort the list using the method...
Java Program. Please read carefully. i need the exact same output as below. if you unable...
Java Program. Please read carefully. i need the exact same output as below. if you unable to write the code according to the question, please dont do it. thanks To the HighArray class in the highArray.java program (Listing 2.3), add the following methods: 1.      getMax() that returns the value of the highest key (value) in the array without removing it from the array, or –1 if the array is empty. 2.      removeMax() that removes the item with the highest key from the...
PLZ USE ECLIPSE JAVA and show output with explanation The object of this assignment is to...
PLZ USE ECLIPSE JAVA and show output with explanation The object of this assignment is to construct a mini-banking system that helps us manage banking data. Notice that you can copy and paste your code from previous assignments. This assignment asks you to allow read from/write to file, as well as search and sorting algorithm. The object of this assignment is to construct a mini-banking system that helps us manage banking data. This assignment asks you to allow read from/write...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c]...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c] , [d,e], [f] ] ----> [ [a,d,f], [a,e,f], [b,d,f], [b,e,f], [c,d,f], [c,e,f]] [ [a,b], [a,b,c]] ----->[[a,a],[a,b],[a,c], [b,a],[b,b],[b,c] assuming abc are integer Thanks in advance!
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
The assignment: C++ program or Java You need to use the following programming constructs/data structures on...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on this assignment. 1. A structure named student which contains:   a. int ID; student id; b. last name; // as either array of char or a string c. double GPA;    2. An input file containing the information of at least 10 students. 3. An array of struct: read the student information from the input file into the array. 4. A stack: you can use the...
The Assignment is in a format of essay writing, and it must be submitted in APA...
The Assignment is in a format of essay writing, and it must be submitted in APA style of writing, and it requires no less than one (1) full page of writing with the proper reference page and writing text referenced, minimum 300 words.   cite at least two references from the FNC Electronic Library. What influences on personality development seem most important to you? and Why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT