Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
11th Edition
ISBN: 9780134671710
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 19.7, Problem 19.7.3CP
Program Plan Intro
Given generic method in “Listing 19.9”:
The given generic method “add()” which has generic type “<T>” as arguments “stack1” and “stack2” for “GenericStack” class.
//Given Generic method
public static <T> void add(GenericStack<T> stack1, GenericStack<? super T> stack2)
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
This one
in c++.
Programming Exercise 8 asks you to redefine the class to implement the nodes of a linked list so that the instance variables are private. Therefore, the class linkedListType and its derived classes unorderedLinkedList and orderedLinkedList can no longer directly access the instance variables of the class nodeType. Rewrite the definitions of these classes so that these classes use the member functions of the class nodeType to access the info and link fields of a node. Also write programs to test various operations of the classes unorderedLinkedList and orderedLinkedList.
template <class Type>class nodeType{public:const nodeType<Type>& operator=(const nodeType<Type>&);//Overload the assignment operator.void setInfo(const Type& elem);//Function to set the info of the node.//Postcondition: info = elem;Type getInfo() const;//Function to return the info of the node.//Postcondition: The value of info is returned.void setLink(nodeType<Type>…
Java:
Code a generic class called Location that describes a point in the xy plane. The Location class requires instances variables to represent the x and y coordinates, a parameterized constructor, one method to change both coordinates of the point (both x and y), getX and getY methods and a toString. These are the only methods in this class. The class uses a generic type so that any numeric value can be used for the coordinates (2 integers, 2 floats, 2 doubles, etc) or 2 Strings (for example three and ten).
Write a code segment in main( ) to thoroughly test the class Location. Your code segment should create two locations, one represented by a pair of real numbers and one represented by a pair of Strings.
Chapter 19 Solutions
Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
Ch. 19.2 - Are there any compile errors in (a) and (b)?Ch. 19.2 - Prob. 19.2.2CPCh. 19.2 - Prob. 19.2.3CPCh. 19.3 - Prob. 19.3.1CPCh. 19.3 - Prob. 19.3.2CPCh. 19.3 - Prob. 19.3.3CPCh. 19.3 - Prob. 19.3.4CPCh. 19.4 - Prob. 19.4.1CPCh. 19.4 - Prob. 19.4.2CPCh. 19.5 - Prob. 19.5.1CP
Ch. 19.5 - Prob. 19.5.2CPCh. 19.6 - What is a raw type? Why is a raw type unsafe? Why...Ch. 19.6 - Prob. 19.6.2CPCh. 19.7 - Prob. 19.7.1CPCh. 19.7 - Prob. 19.7.2CPCh. 19.7 - Prob. 19.7.3CPCh. 19.7 - Prob. 19.7.4CPCh. 19.8 - Prob. 19.8.1CPCh. 19.8 - Prob. 19.8.2CPCh. 19.8 - Prob. 19.8.3CPCh. 19.8 - Prob. 19.8.4CPCh. 19.8 - Prob. 19.8.5CPCh. 19.9 - Prob. 19.9.1CPCh. 19.9 - How are the add, multiple, and zero methods...Ch. 19.9 - How are the add, multiple, and zero methods...Ch. 19.9 - What would be wrong if the printResult method is...Ch. 19 - (Revising Listing 19.1) Revise the GenericStack...Ch. 19 - Prob. 19.2PECh. 19 - (Distinct elements in ArrayList) Write the...Ch. 19 - Prob. 19.4PECh. 19 - (Maximum element in an array) Implement the...Ch. 19 - (Maximum element in a two-dimensional array) Write...Ch. 19 - Prob. 19.7PECh. 19 - (Shuffle ArrayList) Write the following method...Ch. 19 - (Sort ArrayList) Write the following method that...Ch. 19 - (Largest element in an ArrayList) Write the...Ch. 19 - Prob. 19.11PE
Knowledge Booster
Similar questions
- A) Write a generic Java queue class (a plain queue, not a priority queue). Then, call it GenericQueue, because the JDK already has an interface called Queue. This class must be able to create a queue of objects of any reference type. Consider the GenericStack class shown below for some hints. Like the Stack class below, the GenericQueue should use an underlying ArrayList<E>. Write these methods and any others you find useful: enqueue() adds an E to the queue peek() returns a reference to the object that has been in the queue the longest, without removing it from the queue dequeue() returns the E that has been in the queue the longest, and removes it from the queue contains(T t) returns true if the queue contains at least one object that is equal to t *in the sense that calling .equals() on the object with t the parameter returns true.* Otherwise contains returns false. size() and isEmpty() are obvious.arrow_forwardjava quetion Write a generic AddressBook<E extends Person> class that manages a collection ofPerson objects. The AddressBook class is a generic class limited to person objects. Forexample, once you instantiate an AdressBook of Student objects, you cannot add anyother type of person objects into that instance of address book.The AddressBook should have methods to add, delete, or search for a Person objects inthe address book.• The add method should add a person object to the address book. Make sure thatthe add method does not add duplicate person objects to the address book.• The delete method should remove the specified person object from the addressbook.• The search method that searches the address book for a specified person andreturns the list of persons matching the specified criteria. The search can be doneeither by first name, last name, or person id.Write an AddressBookTester class to test your class.arrow_forwardTwo stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of class stackType.arrow_forward
- Problem 3: In classroom, we implemented MyStack by including an ArrayList as private data field of the class (using composition). In this problem, we will use another way to implement the stack class. Define a new MyStack class that extends ArrayList. Draw the UML diagram for the classes and then implement MyStack. Write a test program that prompts the user to enter five strings and displays them in reverse order. (1) Your UML diagram: (3)arrow_forwardUsing C++ Without Using linked lists: Create a class AccessPoint with the following: x - a double representing the x coordinate y - a double representing the y coordinate range - an integer representing the coverage radius status - On or Off Add constructors. The default constructor should create an access point object at position (0.0, 0.0), coverage radius 0, and Off. Add accessor and mutator functions: getX, getY, getRange, getStatus, setX, setY, setRange and setStatus. Add a set function that sets the location coordinates and the range. Add the following member functions: move and coverageArea. Add a function overLap that checks if two access points overlap their coverage and returns true if they do. Add a function signalStrength that returns the wireless signal strength as a percentage. The signal strength decreases as one moves away from the access point location. Represent this with bars like, IIIII. Each bar can represent 20% Test your class by writing a main function that…arrow_forwarda. What does the function begin of the class linkedListType do?b. What does the function end of the class linkedListType do?arrow_forward
- 1. Show the ListStackADT<T> interface 2. Create a ListStackDataStrucClass<T> with the following methods: defaultconstructor, overloaded constructor, copy constructor, getTop, setTop,isEmpty, ifEmpty (if empty throw the exception), push, peek, pop, toString. 3. Create a private inner class of ListStack<T> called StackNode<T> with thefollowing methods: default constructor, overloaded constructor, copyconstructor, getValue, getNext, setValue, setNext 4. Create a BaseConverter class (non-generic) with the following methods: defaultconstructor, inputPrompt, convert [converts a BaseNumber to a convertedString], convertAll [instantiate a String object] , toString, processAndPrint 5. Create a private inner class BaseNumber. The inner class has the following methods: default constructor, overloaded constructor, getNumber, getBase,setNumber, setBase. [Make your private instance variables in the inner classLong type]. 6. Create a BaseConverterDemo class that only…arrow_forwardWrite a generic class that stores two elements of the same type and can tell the user which one is the larger between the two. The type of the two elements should be parameterized and the class should have a function called “maxElement” that returns the larger element. write this java codearrow_forwardTwo stacks of the same kind are the same if they contain the same number of elements and have the same items at the same places. Overload the relational operator == for the class stackType, which returns true if two stacks of the same type are identical; otherwise, it returns false. Also, write the function template definition to overload this operator. Create a programme to test the overloaded operators and functions of the stackType class.arrow_forward
- In C++, Write the implementation for the methods of the Node class including the constructor. The constructor should set value to the passed in parameter, and set the value of the next pointer to NULL. getNext() should return the value of the next pointer. setNext() should set the value of the next pointer to the parameter that is passed in. In the main() function below (where it says // Step 2 code here), write the code to make a list of 10 Node objects. To do this, make a root pointer which is a pointer to a Node object, and then use a loop to create new Nodes, hooking up the list using setNext() and getNext() for each node. Set the value of each new Node to be the even numbers 2 through 18 (in addition to the root node which has value 0). Make sure that your nodes are linked together! Write code to step through your linked list (where it says // Step 3 code here), printing the value of each node. Write code to clean up the list (where it says // Step 4 code here). Print a…arrow_forward3. Define a Pair class using Java Generics framework a. Properties: first, second b. Constructors: Create 2 different constructor with different initialization parameters c. Methods: setFirst(), setSecond(), getFirst(), getSecond(); d. Test Pair class print out the value of Pair instance: i. Define a Pair instance and assign (10,10.1) to the pair; ii. Define a Pair instance and assign (8.2, "ABC") to the pair; iii. Define an array holding[] of Pair class. 1. holding has 100 elements; 2. Assign (int, double) to array by using loop -> (0, 100.0), (1, 99.0), (2, 98.0), ..., (99, 1.0).arrow_forwardin C++ Write a method ‘void addBack(double x)’ that adds value x to the back of a linked list. Assume you have access to a ‘head’ pointer pointing to the first node in the list. Please note that you do NOT have a tail pointer. Be sure to check for any special cases.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning