Question

In: Computer Science

Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...

Complete the java program.

/*

Note: Do not add any additional methods, attributes.

Do not modify the given part of the program.

Run your program against the provided Homework2Driver.java for

requirements.

*/

/*

Hint: This Queue implementation will always dequeue from the first element of

the array i.e, elements[0]. Therefore, remember to shift all elements

toward front of the queue after each dequeue.

*/

public class QueueArray<T> {

public static int CAPACITY = 100;

private final T[] elements;

private int rearIndex = -1;

public QueueArray() {

}

public QueueArray(int size) {

}

public T dequeue() {

}

public void enqueue(T info) {

}

public boolean isEmpty() {

}

public boolean isFull() {

}

public int size() {

}

}

Run this program ( Homework2Driver.java ) to test.

Comment out sections that you have not finished, so it does not

interfere your troubleshooting.

For example, commenting out parts 2 and 3 while testing part 1.

public class Homework2Driver {

public static void main(String [] args) {

int score = 0;

// Part 1: Array based Queue - QueueArray.java

QueueArray myQueueArray = new QueueArray(2);

myQueueArray.dequeue();

if (myQueueArray.isEmpty() && !myQueueArray.isFull() && myQueueArray.size()

== 0)

score += 6;

myQueueArray.enqueue("Orange");

myQueueArray.enqueue("Mango");

myQueueArray.enqueue("Guava"); // Note: with Queue size 2, this won't get

into the queue.

if (myQueueArray.isFull())

score += 6;

if (myQueueArray.dequeue().equals("Orange") && myQueueArray.size() == 1

&& !myQueueArray.isEmpty())

score += 6;

if (myQueueArray.dequeue().equals("Mango") && myQueueArray.size() == 0 &&

myQueueArray.isEmpty())

score += 6;

// Part 2: Linked List based Queue - QueueLinkedList.java

QueueLinkedList myQueueList = new QueueLinkedList();

myQueueList.dequeue();

if (myQueueList.isEmpty() && myQueueList.size() == 0)

score += 6;

myQueueList.enqueue("Apple");

myQueueList.dequeue();

myQueueList.enqueue("Orange");

myQueueList.enqueue("Lemon");

if (myQueueList.dequeue().equals("Orange") && myQueueList.size() == 1 && !

myQueueList.isEmpty())

score += 6;

if (myQueueList.dequeue().equals("Lemon") && myQueueList.size() == 0 &&

myQueueList.isEmpty())

score += 6;

// Part 3: Linked List based Stack - StackLinkedList.java

StackLinkedList myStack = new StackLinkedList();

myStack.pop();

if (myStack.isEmpty() && myStack.size() == 0)

score += 6;

myStack.push("Peach");

if (!myStack.isEmpty() && myStack.size() == 1)

score += 6;

myStack.pop();

myStack.push("Pineapple");

if (myStack.pop().equals("Pineapple") && myStack.isEmpty() &&

myStack.size() == 0)

score += 6;

System.out.printf("your score is %d/60 \n", score);

}

}

Solutions

Expert Solution

If you have any doubts, please give me comment...

/*

Note:   Do not add any additional methods, attributes.

        Do not modify the given part of the program.

        Run your program against the provided Homework2Driver.java for requirements.

*/

/*

Hint:   This Queue implementation will always dequeue from the first element of

        the array i.e, elements[0]. Therefore, remember to shift all elements

        toward front of the queue after each dequeue.

*/

public class QueueArray<T> {

    public static int CAPACITY = 100;

    private final T[] elements;

    private int rearIndex = -1;

    @SuppressWarnings("unchecked")

    public QueueArray() {

        elements = (T[]) new Object[CAPACITY];

    }

    @SuppressWarnings("unchecked")

    public QueueArray(int size) {

        CAPACITY = size;

        elements = (T[]) new Object[size];

    }

    public T dequeue() {

        if(!isEmpty()){

            T temp = elements[0];

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

                elements[i] = elements[i + 1];

            }

            rearIndex--;

            return temp;

        }

        return null;

    }

    public void enqueue(T info) {

        if(!isFull()){

            rearIndex++;

            elements[rearIndex] = info;

        }

    }

    public boolean isEmpty() {

        return rearIndex == -1;

    }

    public boolean isFull() {

        return rearIndex+1 == CAPACITY;

    }

    public int size() {

        return rearIndex+1;

    }

}

/*

Tips:Comment out sections that you have not finished,so it does not

interfere your troubleshooting.

For example,commenting out parts 2 and 3 while testing part 1.

*/

public class Homework2Driver {

    public static void main(String[] args) {

        int score = 0;

        // Part 1: Array based Queue - QueueArray.java

        QueueArray<String> myQueueArray = new QueueArray(2);

        myQueueArray.dequeue();

        if (myQueueArray.isEmpty() && !myQueueArray.isFull() && myQueueArray.size() == 0){

            System.out.println("points-1");

            score += 6;

        }

        myQueueArray.enqueue("Orange");

        myQueueArray.enqueue("Mango");

        myQueueArray.enqueue("Guava"); // Note: with Queue size 2, this won't get into the queue.

        if (myQueueArray.isFull()){

            System.out.println("points-2");

            score += 6;

        }

        if (myQueueArray.dequeue().equals("Orange") && myQueueArray.size() == 1 && !myQueueArray.isEmpty()){

            System.out.println("points-3");

            score += 6;

        }

        if (myQueueArray.dequeue().equals("Mango") && myQueueArray.size() == 0 && myQueueArray.isEmpty()){

            System.out.println("points-4");

            score += 6;

        }

        // // Part 2: Linked List based Queue - QueueLinkedList.java

        // QueueLinkedList myQueueList = new QueueLinkedList();

        // myQueueList.dequeue();

        // if (myQueueList.isEmpty() && myQueueList.size() == 0)

        // score += 6;

        // myQueueList.enqueue("Apple");

        // myQueueList.dequeue();

        // myQueueList.enqueue("Orange");

        // myQueueList.enqueue("Lemon");

        // if (myQueueList.dequeue().equals("Orange") && myQueueList.size() == 1 && !

        // myQueueList.isEmpty())

        // score += 6;

        // if (myQueueList.dequeue().equals("Lemon") && myQueueList.size() == 0 &&

        // myQueueList.isEmpty())

        // score += 6;

        // // Part 3: Linked List based Stack - StackLinkedList.java

        // StackLinkedList myStack = new StackLinkedList();

        // myStack.pop();

        // if (myStack.isEmpty() && myStack.size() == 0)

        // score += 6;

        // myStack.push("Peach");

        // if (!myStack.isEmpty() && myStack.size() == 1)

        // score += 6;

        // myStack.pop();

        // myStack.push("Pineapple");

        // if (myStack.pop().equals("Pineapple") && myStack.isEmpty() &&

        // myStack.size() == 0)

        // score += 6;

        System.out.printf("your score is %d/24 \n", score);

    }

}


Related Solutions

Complete the attached program by adding the following: a) add the Java codes to complete the...
Complete the attached program by adding the following: a) add the Java codes to complete the constructors for Student class b) add the Java code to complete the findClassification() method c) create an object of Student and print out its info in main() method of StudentInfo class. * * @author * @CS206 HM#2 * @Description: * */ public class StudentInfo { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application...
Complete all parts of this java program. Use Homework2Driver.java below for testing. /* Note: Do not...
Complete all parts of this java program. Use Homework2Driver.java below for testing. /* Note: Do not add any additional methods, attributes. Do not modify the given part of the program. Run your program against the provided Homework2Driver.java for requirements. */ public class Node<T> { private T info; private Node nextLink; public Node(T info) { } public void setInfo(T info) { } public void setNextLink(Node nextNode) { } public T getInfo() { } public Node getNextLink() { } } /* Note:...
How to write a Java program that has a base class with methods and attributes, subclasses...
How to write a Java program that has a base class with methods and attributes, subclasses with methods and attributes. Please use any example you'd like.
Java programming: Save the program as DeadlockExample.java   Run the program below.  Note whether deadlock occurs.  Then modify the...
Java programming: Save the program as DeadlockExample.java   Run the program below.  Note whether deadlock occurs.  Then modify the program to add two more threads: add a class C and a class D, and call them from main.  Does deadlock occur? import java.util.concurrent.locks.*; class A implements Runnable {             private Lock first, second;             public A(Lock first, Lock second) {                         this.first = first;                         this.second = second;             }             public void run() {                         try {                                     first.lock();                                     System.out.println("Thread A got first lock.");                                     // do something                                     try {                                                 Thread.sleep( ((int)(3*Math.random()))*1000);...
Modify Java program to accept a telephone number with any numberof the letters, using the...
Modify Java program to accept a telephone number with any number of the letters, using the if else loop. The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits. Also, modify the program to process as many telephone numbers as the user wants.
Modify the attached files to do the following in java: 1) Add all necessary Getters and...
Modify the attached files to do the following in java: 1) Add all necessary Getters and Setters to the Class file. 2) Add code to compare two instances of the class to see which one comes before the other one based on the zipcode. //Address1.java public class Address {    // attributes    private String street, aptNum, city, state;    private int zip;    // constructors    public Address(String street, String aptNum, String city, String state, int zip)    {...
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT