Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
3rd Edition
ISBN: 9780134038179
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 18.8, Problem 18.22CP
Program Plan Intro
Generic Method:
- Methods that introduce their own type parameters are called as Generic method.
- List of type parameters included inside the angle bracket which appears before the method’s return type.
- In generic method, type parameters are allowed to express the dependencies among the types of one or more arguments and its return type.
- The programmers are enabled to specify a single method declaration with a set of related methods.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Can we have static methods in an Interface?
java
Declare a generic interface called Pair that takes two type parameters K and V. This interface has two methods getKey that returns a value of type K, and getValue that returns a value of type V.
Write a class OrderedPair implementing the Pair interface. The class has two instance variables which are key (of type K) and value (of type V). Define the constructor of the class OrderedPair that initializes the instance variables.
In a class named Util, define a generic method, compare, which compares two Pair objects. Based on equals method inherited from the Object class, returns true if the keys and values of the two pairs are equal.
Test your compare method.
Write and test a generic method countGreaterThan that receives an array from any type that implements the Comparable interface and a parameter e of the same type. The method counts and returns the array elements that are greater than e.
Part 1: Without using generics
1. Define the interface NumFilter. The interface has a single method
named predicate that takes one Integer argument and returns a boolean
value.
interface NumFilter {
boolean predicate (Integer n);
}
A typical implementation of the predicate method would be as follows:
- returns true if the integer n satisfy the condition of the filter
- else returns false.
Chapter 18 Solutions
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Ch. 18.1 - Prob. 18.1CPCh. 18.1 - When ArrayList is used as a non-generic class, why...Ch. 18.1 - Suppose we use the following statement to...Ch. 18.1 - Assume we have used the statement shown in...Ch. 18.2 - Prob. 18.5CPCh. 18.2 - Prob. 18.6CPCh. 18.2 - Prob. 18.7CPCh. 18.2 - Prob. 18.8CPCh. 18.3 - Prob. 18.9CPCh. 18.3 - Prob. 18.10CP
Ch. 18.3 - Prob. 18.11CPCh. 18.3 - Prob. 18.12CPCh. 18.3 - Prob. 18.13CPCh. 18.3 - Prob. 18.14CPCh. 18.4 - Prob. 18.15CPCh. 18.5 - Prob. 18.16CPCh. 18.5 - Prob. 18.17CPCh. 18.6 - Prob. 18.18CPCh. 18.6 - Prob. 18.19CPCh. 18.6 - Prob. 18.20CPCh. 18.8 - Prob. 18.21CPCh. 18.8 - Prob. 18.22CPCh. 18.9 - Prob. 18.23CPCh. 18.9 - During the process of erasure, when the compiler...Ch. 18.9 - Prob. 18.25CPCh. 18 - Prob. 1MCCh. 18 - Prob. 2MCCh. 18 - Look at the following method header: void...Ch. 18 - Look at the following method header: void...Ch. 18 - Look at the following method header: void...Ch. 18 - Look at the following method header: void...Ch. 18 - Prob. 7MCCh. 18 - Prob. 8MCCh. 18 - Prob. 9MCCh. 18 - The process used by the Java compiler to remove...Ch. 18 - True or False: It is better to discover an error...Ch. 18 - Prob. 12TFCh. 18 - True or False: Type parameters must be single...Ch. 18 - Prob. 14TFCh. 18 - Prob. 15TFCh. 18 - True or False: You cannot create an array of...Ch. 18 - Prob. 17TFCh. 18 - Prob. 18TFCh. 18 - Prob. 1FTECh. 18 - Assume the following is a method header in a...Ch. 18 - public class MyClassT { public static void...Ch. 18 - public class PointT extends Number super Integer {...Ch. 18 - Assume there is a class named Customer. Write a...Ch. 18 - Assume names references an object of the...Ch. 18 - Prob. 3AWCh. 18 - Prob. 4AWCh. 18 - Prob. 5AWCh. 18 - Prob. 6AWCh. 18 - Prob. 7AWCh. 18 - Prob. 1SACh. 18 - Look at the following method header: public T...Ch. 18 - Prob. 3SACh. 18 - Do generic types exist at the bytecode level?Ch. 18 - Prob. 5SACh. 18 - When the compiler encounters a class, interface,...Ch. 18 - Prob. 1PCCh. 18 - Prob. 2PCCh. 18 - Prob. 3PCCh. 18 - Prob. 4PCCh. 18 - Prob. 5PCCh. 18 - Prob. 6PCCh. 18 - Prob. 7PC
Knowledge Booster
Similar questions
- please help me understand this code in java (using generic method). More specifically why was item1,item2, item3 parameter names never used again and what is the purpose of this line of code: TheType tripleMin(TheType item1, TheType item2, TheType item3) {arrow_forwardWrite a java program that Create a generic class AnimalList that has a type parameter T. It should have at least two properties declared as the passed type, constructors), getters, setters, and toString methods. Create a superclass Animal that has two properties (String, int, etc.), and constructor, getters, setters, and toString methods. Create two subclasses of Animal called Cat and Dog. Each class should have a unique property and method. Create a class Lizard with a method of your choice. Create a main class AnimalTest that creates two instances of Animal: Dog type and Cat type. Create a method that takes Animal type as parameter constraining the types to be subclasses of the Animal class only; check the type of object and call the unique method in the Dog and Cat. Create an instance of the Lizard class and test it. Display all objects.arrow_forward7. To declare an interface named A with two generic types, use a. public interface A(E, F) { ... } b. public interface A { } ... c. public interface A { } .. d. public interface A (E) { ... }arrow_forward
- Design an interface Polynomial that defines the following operations. This is your polynomial abstract data type. Specifically this interface should have the following method signatures: A method addTerm that takes a coefficient and a power (both integer numbers) and adds the resulting term to the polynomial. (This will enable you to build a polynomial term-by-term.) It should throw an IllegalArgumentException if a negative power is passed to it. A method removeTerm that takes a power and removes any and all terms in the polynomial with that power. A method getDegree that returns the degree of this polynomial. A method getCoefficient that takes a power and returns the coefficient for the term with that power. A method evaluate that takes a double-precision decimal number and returns a double-precision result. A method add that takes another Polynomial object and returns the polynomial obtained by adding the two polynomials. Any implementation should ensure that this method does not…arrow_forwardPlease request you to solve..arrow_forwardwhat is the generic term for a method that creates an instance of a class (constructors?) and how would you call it for a class named MyClass that does not take any parameters in PYTHON?arrow_forward
- Create the first Apex class or program-- In this apex class, we have to create 2 methods-- In the first method, we will take the argument of (account id), and the return type will be (the list of contact).-- In the second method, we will take the argument of (list of account) and the return type will be ( map<key-{account id}, value-{list of contact}>)arrow_forwardPlease use java 8 and JavaDoc: - Create a generic interface. Create an abstract class that implements the generic interface. Show strong type checking and code reuse.arrow_forwardQuestion 1 A generic class is defined below. Complete the program as told by the comments. public class Example { private T1 number; private T2 description3B I| ----- define the method setExampleData() to set both variables--- public void setExampleData (T1 n, T2 d){ public static void main( String[]args ){ // arguments // --- create an object obj1 by parameters Integer and String--- call the setExampleData() method- }arrow_forward
- Programming in Java. What would the difference be in the node classes for a singly linked list, doubly linked list, and a circular linked list? I attached the node classes I have for single and double, but I feel like I do not change enough? Also, I use identical classes for singular and circular node which does not feel right. Any help would be appreciated.arrow_forwardA generic class is defined below. Complete the program as told by the comments. public class Example { private T1 number; private T2 description; I -- define the method setExampleData() to set both variables- public void setExampleData (T1 n, T2 d){ [1] [2] } public static void main( [3] ) { // arguments create an object obj1 by parameters Integer and String- --- [4] -call the setExampleData() method-- [5] } }arrow_forwardWhat is the difference between static and dynamic binding? (i.e., between virtual and nonvirtual methods).arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- 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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education