Java has a generic interface called Comparable. A class that implements the Comparable interface must have a
Question:
Java has a generic interface called Comparable. A class that implements the Comparable interface must have a method with this specification:
♦ compareTo
public boolean compareTo(E obj)
Compare this object to another object of the same type.
Returns:
The return value is zero if this object is equal to obj; the return value is negative if this object is smaller than obj; the return value is positive if this object is larger than obj.
Throws: ClassCastException
Indicates that obj is the wrong type of object to be compared with this object.
Write a generic class for a bag of Comparable objects, where the objects are stored in a binary search tree. The tree itself should use the BTNode class from Figure 9.10.
The first line of your new bag class should be:
public class ComparableTreeBag
>
This tells the Java compiler that the Comparable- TreeBag is a generic class, but that any instantiation of the generic type parameter E must implement the Comparable interface.
FIGURE 9.10 Specification and Implementation of the Generic Binary Tree Node Class Generic Class BTNode * public class BTNode from the package edu.colorado.nodes A BTNode provides a node for a binary tree with a reference to an E object as the data in each node. Limitations: Beyond Int. MAX_VALUE elements, treeSize is wrong. Specification Constructor for the BTNode public BTNode(E initialData, BTNode initiallLeft, BTNode initialRight) Initialize a node with specified initial data and links to children. Note that a reference to a child may be null, which indicates that there is no child. Parameters: initialData - the initial data of this new node initialleft and initialRight- references to the children of this new node Postcondition: This new node contains the specified data and links to its children. getData-getLeft-getRight public E getData( ) public BTNode getleft( ) public BTNode getRight( ) These are accessor methods to obtain this node's data or a reference to one of the children. Any of these objects may be null. A null reference to a child indicates that the child does not exist. getLeftmostData public E getleftmostData( ) Accessor method to get the data from the leftmost node of the tree below this node. Returns: The data from the deepest node that can be reached from this node following left links. getRightmostData public E getRightmostData( ) Accessor method to get the data from the rightmost node of the tree below this node. Returns: The data from the deepest node that can be reached from this node following right links.
Step by Step Answer:
Heres an implementation of the ComparableTreeBag class as described in the question public class ComparableTreeBag private BTNode root private int size public ComparableTreeBag root null size 0 public ...View the full answer
Students also viewed these Computer science questions
-
Expand the class from Project 10 or 11 so that there is an extra method that produces a Java Iterator for the bag. Data from Project 10 Write a class for a bag of strings, where the strings are...
-
If you are familiar with Javas Comparable interface (Programming Project 11), then rewrite one of the sorting methods so that it sorts an array of Comparable objects. You may choose selectionsort,...
-
In mathematics, a magma is a type of algebraic structure that has a set of elements, all of the same type, and one binary operation. Such a pairing of a set (ex. Positive integers) with this...
-
Jerome Neeson is a shareholder in Gourmet Chefs Inc., a company that owns and operates a test kitchen in a large metropolitan area. The company is involved in a number of businesses, including...
-
Weights of bears (use 11 classes with a class width of 50 and begin with a lower class boundary of 20.5). a. Construct a histogram. b. Describe the general shape of the distribution, such as...
-
WhatlevelofsocialresponsibilitydoesJ.ChristopherCapitalseemtobeoperatingat? UsingtheU.S.definitionofsexualharassment,canthecomplainant'sclaimagainstJ.ChristopherCapital includesexual harassment?...
-
OPEC crude oil imports. The data in the table below are the amounts of crude oil (millions of barrels) imported into the United States from the Organization of Petroleum Exporting Countries (OPEC)...
-
In environmental engineering (a specialty area in civil engineering), the following equation can be used to compute the oxygen level c (mg/L) in a river downstream from a sewage discharge : c = 10 ...
-
Grady received $8,800 of Social Security benefits this year. Grady also reported salary and interest income this year. What amount of the benefits must Grady include in his gross income under the...
-
The 12-ft boom AB has a fixed end A. A steel cable is stretched from the free end B of the boom to a point C located on the vertical wall. If the tension in the cable is 380 lb, determine the moment...
-
Write a class for a bag of strings, where the strings are stored in a binary search tree. In creating the binary search tree, you should use the strings compareTo method, which is described on page...
-
Using a heap, implement the priority queue ADT from Section 7.4. You can store the heap in arrays, similar to the solution to Self-Test Exercise 1. To have FIFO behavior for elements with equal...
-
Because the availability of new plots was becoming very limited, a cemetery company in a major metropolitan area began selling single plots wherein a husband and wife ultimately would be interred one...
-
Prove that Eq. (19.34) gives the simplest multi-gluon and gluon-quark states that contain an \(\mathrm{SU}(3)\) color singlet in the decomposition. Data from Eq. 19.34 (GG)1: (88)1 (Gqq) : [8 (383)8]...
-
In question 70, what is the probability that of the 100 cars test-driven, more than 35 cars get more than 45 miles per gallon? How many of the 100 cars tested would you expect to get more than 45...
-
Construct the braid group products (a) (b) using the algorithm of Fig. 29.16 . Data from Fig. 29.16
-
Worksheet The adjusted trial balance columns of a worksheet for Bond Corporation are shown below. The worksheet is prepared for the year ended December 31. Complete the worksheet by (a) entering the...
-
The Healthy Catering Service had the following transactions in July, its first month of operations: 1 Kelly Foster contributed \(\$ 18,000\) of personal funds to the business in exchange for common...
-
Simplify. 6 7 6 3 /6 9
-
The relationship described in question 7 does not always appear to hold. What factors, besides the number of firms in the market, might affect margins?
-
Calculate (1.666015625 10 0 1.9760 10 4 ) + (1.666015625 10 0 -1.9744 10 4 ) by hand, assuming each of the values are stored in the 16-bit half precision format described in Exercise 3.27 (and...
-
Based on your answers to 3.38 and 3.39, does (1.666015625 10 0 1.9760 10 4 ) + (1.666015625 10 0 -1.9744 10 4 ) = 1.666015625 10 0 (1.9760 10 4 + -1.9744 10 4 )?
-
Using the IEEE 754 floating point format, write down the bit pattern that would represent -1/4. Can you represent -1/4 exactly?
-
Summarize in your own words Sharps, Treynors, and Jensens Measures for assessing portfolio performance with respect to risk. Assess the portfolio performance of mutual fund VDIGX taking into...
-
Question 1 Slat and Company have recently set up a business which will manufacture and sell a furniture component, the F12 On the 19 August 2021, the company issued 85,000 of share capital for cash....
-
The following is Addison Corporations contribution format income statements for last month. The company has no beginning or ending inventories. A total of 10,000 units were produced and sold last...
Study smarter with the SolutionInn App