Question

In: Computer Science

Problem 3 Prototype object doubleQ A doubleQ object has 2 instance variables: firstQ and secondQ arrays...

Problem 3

Prototype object doubleQ

A doubleQ object has 2 instance variables: firstQ and secondQ

arrays that together implement a queue

enqueue() instance method adds its argument to the end of this.secondQ

dequeue() instance method removes and returns the element at the front of this.firstQ

transfer() instance method removes the element from the front of this.secondQ, adds it to the end of this.firstQ

If this.secondQ is empty, does nothing

Test code is included in the file

Download this file (with fragments missing)

Hint: Use the push() and shift()

Listing

<html>

<head>

<title>Assignment 3, Problem 3</title>

<script type="text/Javascript">

function doubleQ( q1, q2 )

{

    this.firstQ = q1 || [];

    this.secondQ = q2 || [];

    this.enqueue = function( item )

                   {                  }; // Code missing

    this.dequeue = function()

                   {                  }; // Code missing

    this.toString = function()

                    { return "[" + this.firstQ + "] ["

                      + this.secondQ + "]"; };

}

function doubleQ_transfer()

{

        // Code missing

}

doubleQ.prototype.transfer = doubleQ_transfer;

var dQ = new doubleQ( [1, 2], [3, 4] );

document.write( "<p>dQ: " + dQ + "</p>" );

dQ.transfer()

document.write( "<p>After transfer: " + dQ + "</p>" );

document.write( "<p>Value dequeued: " + dQ.dequeue() + "</p>" );

document.write( "<p>After dequeue: " + dQ + "</p>" );

dQ.enqueue(5);

document.write( "<p>After enqueuing 5: " + dQ + "</p>" );

</script>

</head>

<body>

</body>

</html>

Output

dQ: [1,2] [3,4]

After transfer: [1,2,3] [4]

Value dequeued: 1

After dequeue: [2,3] [4]

After enqueuing 5: [2,3] [4,5]

Solutions

Expert Solution

Below is the solution with output screenshot

Code :

<html>
   <head>
      <title>Assignment 3, Problem 3</title>
      <script type="text/Javascript">
        function doubleQ(q1, q2) {
            this.firstQ = q1 || [];
            this.secondQ = q2 || [];
            this.enqueue = function(item) {
                this.secondQ.push(item);
            }; // Code missing
            this.dequeue = function() {
                return this.firstQ.shift();
            }; // Code missing
            this.toString = function() {
                return "[" + this.firstQ + "] [" + this.secondQ + "]";
            };
        }

        function doubleQ_transfer() {
            // Code missing
            this.firstQ.push(this.secondQ.shift());
        }
        doubleQ.prototype.transfer = doubleQ_transfer;
        var dQ = new doubleQ([1, 2], [3, 4]);
        document.write("<p>dQ: " + dQ + "</p>");
        dQ.transfer()
        document.write("<p>After transfer: " + dQ + "</p>");
        document.write("<p>Value dequeued: " + dQ.dequeue() + "</p>");
        document.write("<p>After dequeue: " + dQ + "</p>");
        dQ.enqueue(5);
        document.write("<p>After enqueuing 5: " + dQ + "</p>");
         
      </script>
   </head>
   <body>
   </body>
</html>

Output :


Related Solutions

Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
(a) What is a class? What is an object? What is the relationship? (b) What are the instance variables and methods of a class?
 (a) What is a class? What is an object? What is the relationship? (b) What are the instance variables and methods of a class? (c) What is the effect of declaring instance variables and methods public or private? (d) Why do we often declare the instance variables of classes private? (e) Could we declare methods private? Would we want to do so?  (f) What does the identifier this mean? Give an example of its use (g) What is a constructor? (h) What is inheritance? Why is...
You have written a JAVA program that creates Shoe object with three instance variables. You will...
You have written a JAVA program that creates Shoe object with three instance variables. You will need to use the exception you created to go along with it. So far you have been using my driver classes. Now it is time for you to create your own driver class that will implement an ArrayList of Shoe objects. This assignment uses the Shoe.java and ShoeException.java to create the driver ShoeStore.Java You will display a menu as follows: Add shoes Print all...
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables...
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables x, y, and z. b. Create a constructor that sets the variables. Also, create get and set methods for each variable. c. Create a toString() method. d. Make Point3D implement Comparable. Also, create a compareTo(Point3D other) method that compares based on the x-coordinate, then y-coordinate for tiebreakers, then z-coordinate for tiebreakers. For example, (1, 2, 5) comes before (2, 1, 4), which comes before...
PYTHON ASSIGNMENT Problem: (1) The  __init__ method should initialize the values of the instance variables. Here is...
PYTHON ASSIGNMENT Problem: (1) The  __init__ method should initialize the values of the instance variables. Here is the beginning of __init__: def __init__(self, the_hr, the_min, the_sec): self.hr = the_hr # Also initialize min and sec. (2) Include a __str__ method that returns the current state of the clock object as a string. You can use the string format method format like this: return "{0:02d}:{1:02d}:{2:02d}".format(self.hr, self.min, self.sec) (3) When the tick method is executed, add one to sec. If the resulting value...
CompSci 251: Assignment 3 Due 2/20, 2017 10:00am Topics Covered: Instance variables and methods; using a...
CompSci 251: Assignment 3 Due 2/20, 2017 10:00am Topics Covered: Instance variables and methods; using a driver class 1 Introduction This assignment will have you implement your first real Java class using instance variables(state) and meth- ods(behaviour). It will also probably be the first time that you write a program with a driver class. 2 Income tax computation Most Americans (not just Americans, really!) complain about filing their income taxes. Some people dont think they should pay at all. Others...
12. Object 1 has twice the mass of Object 2. Object 2 has the same momentum...
12. Object 1 has twice the mass of Object 2. Object 2 has the same momentum as Object 1. Which of the following is true? a. One object has 0.707 times the kinetic energy of the other. b. One object has twice the kinetic energy of the other. c. One object has 4 times the kinetic energy of the other. d. Both objects have the same kinetic energy.
Programming Problem 2 - Cycle [A] Create a class called “Cycle” which has two instance integer...
Programming Problem 2 - Cycle [A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list. [B] Edit your class...
The PARTITION INTO PATHS OF LENGTH 2 problem is as follows: INSTANCE: A graph, G =...
The PARTITION INTO PATHS OF LENGTH 2 problem is as follows: INSTANCE: A graph, G = (V, E) with IV| = 3q for a positive integer q. QUESTION: Is there a partition of V into q disjoint subsets VI, V2, ..., Vq of 3 vertices such that, for each V1 = {Vi[1], Vi[2], Vi[3), at least two of the three edges {Vi[1], Vi[2], {Vi[1], Vi[3], and {Vi[2], Vi[3]} belong to E? Prove the PARTITION INTO PATHS OF LENGTH 2 problem...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT