Concept explainers
(Modify BST using Comparator) Revise BST in Listing 25.4 using a Comparator for comparing objects. Define the new class as BST<E> with two constructors:
BST(); // Compare elements using their natural order
BST(Comparator<? super E> comparator)
Hint: You need to add a data field for Comparator in the BST class as follows:
protected Comparator<E> c = (e1, e2) ->
((Comparable<E>)e1).compareTo(e2);
The lambda expression gives the default comparator using a natural order. You need to use comparator c to replace e.compareTo(anotherElement) with c.compar(e, anotherElement) in Listing 25.4.
Want to see the full answer?
Check out a sample textbook solutionChapter 25 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Software Engineering (10th Edition)
Java How To Program (Early Objects)
Digital Fundamentals (11th Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Starting out with Visual C# (4th Edition)
C How to Program (8th Edition)
- (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class. Draw the UML diagram for the classes Triangle and GeometricObject and then implement the Triangle class. Write a test program that prompts the user to enter three sides of the triangle, a colour, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the colour and filled properties using the input. The program should display the area, perimeter, colour, and true or false to indicate whether it is filled or not.arrow_forwardTape for a Turing Machine (Doubly-linked List)”.(I USE ECLIPSE & RUN CHECK ONLINE CONSOLE)A class named Tape to represent Turing machine tapes. The class should have an instance variable of type Cell that points to the current cell with the method: public Cell getCurrentCell() -- returns the pointer that points to the current cell. public char getContent() -- returns the char from the current cell. public void setContent(char ch) -- changes the char in the current cell to the specified value. Don't forget the Javadoc comments. IT WOULD HAVE A PUBLIC CLASS public void moveLeft() -- moves the current cell one position to the left along the tape. Note that if the current cell is the leftmost cell that exists, then a new cell must be created and added to the tape at the left of the current cell, and then the current cell pointer can be moved to point to the new cell. The content of the new cell should be a blank space. (Remember that the Turing machine's tape is conceptually…arrow_forward(The Account class) Account -id: int -balance: double -annualInterestRate : double -dateCreated : Date +Account( ) +Account(someId : int, someBalance : double) +getId() : int +setId(newId : int) : void +getBalance() : double +setBalance(newBalance : double) : void +getAnnualInterestRate( ) : double +setAnnualInterestRate(newRate : double) : void +getDateCreated( ) : Date +getMonthlyInterestRate( ) : double +getMonthlyInterest( ) : double +withdraw(amt : double) : void +deposit(amt : double) : void Design a class named Account that contains: ■ A private int data field named id for the account (default 0). ■ A private double data field named balance for the account (default 0). ■ A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. Make it static. ■ A private Date data field named dateCreated that stores the date when the account was created. (private Date dateCreated)…arrow_forward
- Question 2: (TicTacToe Class) Create a class TicTacToe that will enable you to write a complete program to play the game of tic-tac-toe. The class contains as private data a 3-by-3 two-dimensional array of integers. The constructor should initialize the empty board to all zeros. Allow two human players. Wherever the first player moves, place a 1 in the specified square. Place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has been won or is a draw. If you feel ambitious, modify your program so that the computer makes the moves for one of the players. Also, allow the player to specify whether he or she wants to go first or second.arrow_forwardCreate a Matrix class and implement the following member functions: The constructors and the destructor getSize() which returns the size of the matrix; setValue( int position, int value) which sets the value in the matrix at given position; getValue (int position) which returns the current value at given position; an add method which adds two matrices together; A= ● ● a subtract method which subtract two matrices together; a multiply methods which multiplies two matrices together, if possible; an overlap function which overlaps two matrices together. The augment function takes two matrices A and B of size mlxnl and m2xn2, respectively, and produce a new matrix C of size mLxnL, where mL (resp. nL) is the largest between m1 and m2 (resp. nl and n2). The values of C will either come from the original matrices by overlapping matrix B over matrix A. Wherever a value is not available it initialized to default value 0. For example: If you overlap two matrices A of size 4x2 and B of size 3x5,…arrow_forwardInstructions-Java Assignment is to define a class named Address. The Address class will have three private instance variables: an int named street_number a String named street_name and a String named state. Write three constructors for the Address class: an empty constructor (no input parameters) that initializes the three instance variables with default values of your choice, a constructor that takes the street values as input but defaults the state to "Arizona", and a constructor that takes all three pieces of information as input Next create a driver class named Main.java. Put public static void main here and test out your class by creating three instances of Address, one using each of the constructors. You can choose the particular address values that are used. I recommend you make them up and do not use actual addresses. Run your code to make sure it works. Next add the following public methods to the Address class and test them from main as you go: Write getters and…arrow_forward
- Exercise: Exercises Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume all eight sides of the octagon are of equal length. The area can be computed using the following formula: The Octagon class has a private double data field named side with its getter and setter methods. The class has a no-arg constructor that creates an Octagon with side 0, and a constructor to create an Octagon with a specified side. Write a test program that creates an Octagon object with side value 5 and displays its area and perimeter. Create a new object using the clone method, and compare the two objects using the compareTo method. As far as classes go, the abstract GeometricObject class contains the abstract doubles getArea() and getPerimeter(). The programming language used in this exercise is Java, and it covers the Interfaces topic. I did get how most of the program should look like, but I'm facing trouble understanding a few…arrow_forwardv clailg pi % Consider the below UML diagram and do the following: > Comparable Computer modelName: String price: double yearOfManufacture: int +Computer(String, double, int) //include getters and setters //override the toString() method Implement the Comparable interface for the class Computer and do the following: 1) Create 5 objects of this class. 2) Store these objects using an Array or ArrayList. 3) Display the object of the Array/ArrayList before sorting. 4) Display the objects after sorting based on the field - "yearOfManufacture". arch O a 1) ENarrow_forwardDiscuss: The difference between non-member operator overloading and member operator overloading. Discuss how we can have do both of the following operations for a Car object. myCar + 5 and 5 + myCar Note: We know there is a default assignment operator fo objects. Is there any need to overload the assignment operator ever?arrow_forward
- Please read the main function and define a class CStudent. (object array, static member, string) Please do not change the main function and complete the program given. 【Description】 Please read the main function and define a class CStudent. In the main funciton, two arrays of the ojbect of CStudent are define. Please analyze the call of constructor and destructor in the process of creating and releasing objects. 1. CStudent has two private member variables:stirng name, int age. 2. CStudent has a static member count. It will be incremented by 1 when the object is created and decremented by 1 when the object is destroyed. 3. The constructor function should output "*** is contructing". The destructor function should output “*** is destructing”. "***" indicates the name of the current CStudent object. 【Input】 No input 【Output sample】 There are 0 students. noname is contructing noname is contructing noname is contructing noname is contructing noname is contructing name:noname…arrow_forwardDevelop class Polynomial . The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent—e.g., the term 2x4has the coefficient 2 and the exponent 4. Develop a complete class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities: "Overload the subtraction operator ( - ) to subtract two Polynomial s."arrow_forward(Inheriting Interface vs. Inheriting Implementation) A derived class can inherit “interface” or “implementation” from a base class. How do inheritance hierarchies designed for inheriting interface differ from those designed for inheriting implementation?arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning