Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 19.7, Problem 19.7.4CP
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
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.
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…
Two 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.
Chapter 19 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th 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
- Practical:1. Write a Java code to illustrate the concept of Set, List and deque. Generate the correct output.2. Give a suitable example of Generic Classes and generate correct output.3. Write a Java code to implement the concept of vector and stack. Generate the correct output.arrow_forwardpresentation: Show how to use the diamond operator when creating instances of generic classes in javaarrow_forwardProblem 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_forward
- Write 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_forward1. Write a generic static method that takes a Stack of any type element as a parameter, pops each element from the stack, and prints it. It should have a type parameter that represents the Stack's element type.arrow_forwardWrite C++ Class for Doubly linked list DList (Class for Node and a Class for DList) where class DList should had methods - Constructor and Destructor [Starting with head null] with their definitions - Insert Method [insert a new node at a particular position| with its definition Other methods are neglected.arrow_forward
- 3. 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_forwardDiscuss what is meant by a generic classarrow_forwardIn java code (a) In one line: write the definition of a generic class called MyList (only first line). (b) In one line: write the definition of a generic method called myMethod with two parameters (write only the first line). (c) In 3 lines: write the definition of a generic class called MyList with 2 attributes (size, startValue). (provide only the first three linesarrow_forward
- a. Write a class called hyprid that uses linked lists to represent a queue and stack. As you can recall that the only difference in between the stack and the queue is that the stack uses the addHead method to push items and the queue uses the addTail method to insert items. The class should be implemented using LinkedLists. The class should contain the following methods: a. boolean empty() to check if the underlying list is empty b. void push(int item) to add the item using addFirst c. void insert (int item) to add the item using addLast d. int remove() to remove an item from the beginning of the list e. int pop() the same as above but with name changedarrow_forwardWhich of the following terms best describes the case where a Stack class is implemented using an internal instance of List? Choose only one option.(a) Association(b) Specialization(c) Genericity(d) Composition(e) Singularityarrow_forwardIn C++,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