
Concept explainers
Implement a generic priority queue in Java and test it with an abstract Cryptid class and two concrete Cryptid classes, Yeti and Bigfoot. Create a code package called pq and put all the code for this assignment in it.
- Write the abstract class Cryptid. A Cryptid has a name (a String) and a public abstract void attack() method. Name should be protected. The class implements Comparable<Cryptid>. The compareTo method returns a negative int if the instance Cryptid's name (that is, the name of the Cryptid you called the method on) is earlier in lexicographic order than the name of the other Cryptid (the one passed in as an argument; it returns a positive int if the other Cryptid's name is earlier in lex order, and returns 0 if they are equal (that is, if they have all the same characters.) This part is easy; just have the method call String's compareTo() method on the instance Cryptid's name, with the other Cryptid's name as the argument, and return the result.
- Extend Cryptid with two concrete classes, Yeti and Bigfoot.
In addition to the inherited variable name, Yeti also has a fangLength (a double), a constructor that sets the name and fang length based on its parameters, and a reasonable toString() method. It implements the attack method by printing a message like this:
Yolanda the Yeti attacks with their 22.3 cm fangs!
Bigfoot has a shoeSize (an int), a constrcutor that sets the name and shoe size based on parameters, and a reasonable toString(). It implements attack() by printing a message like
Barbara the Bigfoot stomps with their size 30 feet!
-
Write the class PQ. Give it this exact name; in particular, do not call it PriorityQueue, because the JDK already has a class with this name.
The parameterizing type for PQ should be <E extends Comparable <? super E>>, because Yeti and Bigfoot inherit Cryptid’s implementation of Comparable, which uses a compareTo that can compare the Yeti or Bigfoot to any Cryptid. Therefore, the parameterizing class must be comparable to any subclass of its superclass (super E).
The most efficient way to represent a priority queue is usually with a min heap of E, but you should use an ArrayList of E. You do not need to implement Array List-use the ArrayList class in the JDK.
We will also simplify the priority queue by using compareTo() to determine the order in which values come out of the PQ instead of using a separate priority field.
You will need to write the insert, min, removeMin, size, and isEmpty methods.
Think about the argument and return types of insert, min, and removeMin(), then implement those methods. Insert should add the E to the list. Min should return a reference to the E in the list that is smallest according to the compareTo() calls. RemoveMin should do the same, but also remove the E form the list.
Size() and isEmpty() should be easy to implement.
- Use this driver code, and make sure the output is correct. The Cryptids should always come out of the PQ in ascending lexicographic order by name.
package pq;
public class PQDriver {
public static void main(String[] args) {
PQ<Yeti> pqy = new PQ<Yeti>();
pqy.insert(new Yeti("Yolanda", 22.3));
pqy.insert(new Yeti("Jann", 24.1));
pqy.insert(new Yeti("Yacov", 19.7));
pqy.insert(new Yeti("Yesenia", 25.8));
System.out.println("PQ of Yetis: ");
System.out.println("size: " + pqy.size());
System.out.println("min: " + pqy.min());
while(!pqy.isEmpty())
pqy.removeMin().attack();
PQ<Cryptid> pqc = new PQ<Cryptid>();
pqc.insert(new Yeti("Yolanda", 22.3));
pqc.insert(new Yeti("Jann", 24.1));
pqc.insert(new Yeti("Yacov", 19.7));
pqc.insert(new Yeti("Yesenia", 25.8));
pqc.insert(new Bigfoot("Bob", 32));
pqc.insert(new Bigfoot("Babette", 33));
pqc.insert(new Bigfoot("Brian", 29));
pqc.insert(new Bigfoot("Barbara", 30));
System.out.println("PQ of Cryptids: ");
System.out.println("size: " + pqc.size());
System.out.println("min: " + pqc.min());
while(!pqc.isEmpty())
pqc.removeMin().attack();
}
}

Trending nowThis is a popular solution!
Step by stepSolved in 5 steps with 1 images

- Implement the copy assignment operator= for the StringVar class using the options given on the right side window. Place the code into the left side using the arrows. It is possible to get the test case correct but not complete NOTE: Be careful! There are decoys! The this pointer is used extensively.. Assume that StringVar.h has the following declaration: #include <iostream> class StringVar {public:StringVar() : max_length(20) { // Default constructor size is 20value = new char[max_length+1];value[0] = '\0';}StringVar(int size); // Takes an int for sizeStringVar(const char cstr[]); // Takes a c-string and copies itStringVar(const StringVar& strObj); // Copy Constructor~StringVar(); // Destructorint size() const { return max_length; } // Access capacityconst char* c_str() const { return value; } // Access valueint length() const { return strlen(value); } // Access lengthStringVar& operator= (const StringVar& rightObj);std::istream& operator>>…arrow_forwardWrite a class MySortedArrayCollection that Inherit the SortedArrayCollection class show in screen shot and such that all the following methods are implemented in java, then write a driver program to test the methods public String toString() // Creates and returns a string that correctly represents the current collection. public T smallest() // Returns null if the collection is empty, otherwise returns the smallest element of the collection. public int greater(T element) // Returns a count of the number of elements e in the collection that are greater then element, that is such that e.compareTo(element) is > 0 public MySortedArrayCollection <T> combine (MySortedArrayCollection<T>other) // Creates and returns a new SortedArrayCollection object that is a combination of this object and the argument object. public T []toArray() // Returns an array containing all of the elements of the collection. public void clear() // Removes all elements. public boolean equals(Object o)…arrow_forwardImplement the Solver class. In doing so, you are allowed to define other classes to help you (as well as use “built-in” Java classes or the book’s classes). The point of the solver class is the solve method which takes a board/puzzle configuration represented as a 2D array of booleans and returns a char array containing a minimal sequence of moves that will lead to the solved board (all the cells around the edges being filled). The board configuration is passed in as a 5-by-5 boolean array of Booleans with exactly 16 true cells (filled) and 9 false cells (empty). The solve method then returns an array of characters representing a minimal sequence of moves that solves the puzzle. In other words, if the characters from the returned array are used in order as input to the move method on the Board object representing the initial configuration, the resulting board configuration represents the solved board. Furthermore, the solution must be minimal in the sense that there are no solutions…arrow_forward
- Write a generic class Students.java which has a constructor that takes three parameters – id, name, and type. Type will represent if the student is ‘remote’ or ‘in-person’. A toString() method in this class will display these details for any student. A generic method score() will be part of this class and it will be implemented by inherited classes. Write accessors and mutators for all data points. Write two classes RemoteStudents.java and InPersonStudents.java that inherits from Student class. Show the use of constructor from parent class (mind you, RemoteStudents have one additional parameter – discussion). Implement the abstract method score() of the parent class to calculate the weighted score for both types of students. Write a driver class JavaProgramming.java which has the main method. Create one remote student object and one in-person student object. The output should show prompts to enter individual scores – midterm, finals, ...... etc. and the program will…arrow_forwardBuiltInFunctionDefinitionNode.java has an error so make sure to fix it. BuiltInFunctionDefinitionNode.java import java.util.HashMap; import java.util.function.Function; public class BuiltInFunctionDefinitionNode extends FunctionDefinitionNode { private Function<HashMap<String, InterpreterDataType>, String> execute; private boolean isVariadic; public BuiltInFunctionDefinitionNode(Function<HashMap<String, InterpreterDataType>, String> execute, boolean isVariadic) { this.execute = execute; this.isVariadic = isVariadic; } public String execute(HashMap<String, InterpreterDataType> parameters) { return this.execute.apply(parameters); } }arrow_forwardProvide a different implementation of ChoiceQuestion. Instead of storing the choices in an array list, the addChoice method should add the choice to the question text. For this purpose, an addLine method has been added to the Question class. /** A question with multiple choices.*/public class ChoiceQuestion extends Question{ // Add any needed instance variables, but don't store the choices // The choices should be added to the text of the superclass /* code goes here */ /** Constructs a choice question with a given text and no choices. @param questionText the text of this question */ public ChoiceQuestion(String questionText) { /code goes here */ } /** Adds an answer choice to this question. @param choice the choice to add @param correct true if this is the correct choice, false otherwise */ public void addChoice(String choice, boolean correct) { /* code goes here */ } }arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





