Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
3rd Edition
ISBN: 9780134038179
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 18.2, Problem 18.7CP

Explanation of Solution

Instance creation without providing type argument:

Yes”, the instances of generic class can be created without providing type argument.

Explanation:

  • Instance creation of a generic class is possible without providing the type argument.

Example:

// Create generic class exam with type parameter T

public class exam<T>

{   

    //Declare the variable with type parameter T

    private T to;

    private T ra;

    //Define the generic class constructor

    public exam(T tot, T rank)

    {   

        //Assigning values for variable

        to= tot;

        ra=rank;

    }

    //Define the main method

    public static void main (String[] args)

    {

        //Create the Integer object

        Integer rank= new Integer(2);

            //Create Double object

        Double tot= new Double(466...

Explanation of Solution

Recommendation:

No”, it is not recommended, even the instance of a generic class can be created without providing the type argument.

Explanation:

  • Instance creation of a generic class is possible without providing the type argument.

Example:

// Create generic class exam with type parameter T

public class exam<T>

{   

    //Declare the variable with type parameter T

    private T to;

    private T ra;

    //Define the generic class constructor

    public exam(T tot, T rank)

    {   

        //Assigning values for variable

        to= tot;

        ra=rank;

    }

    //Define the main method

    public static void main (String[] args)

    {

        //Create the Integer object

        Integer rank= new Integer(2);

        //Create Double object

        Double tot= new Double(466...

Blurred answer
Students have asked these similar questions
1.     Why do you need to implement all the methods of an interface in class which implements an interface?
Describe how you can use an interface as an alternative to a class to specify the type for a parameter.
Design a generic class to hold the following information about a bank account. - Balance, number of deposits this month, number of withdrawals, annual interest rate, monthly service charges. The class should have the following member functions constructor: accepts arguments for the balance and annual interest rate deposit: A virtual function that accepts an argument for the amount of the deposit. The function should add the argument to the account balance. It should also increment the variable holding the number of deposits. withdraw: A virtual function that accepts an argument for the amount of the deposit. The function should subtract the argument to the account balance. It should also increment the variable holding the number of deposits. calcInt: A virtual function that updates the balance by calculating the monthly interest earned by the account, and adding this interest to the balance. This is performed by the following formulas: Monthly Interest Rate = (Annual Interest Rate /12)…

Chapter 18 Solutions

Starting Out with Java: From Control Structures through Data Structures (3rd Edition)

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
Background pattern image
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education