Question

In: Computer Science

IN JAVA, For this exercise, you will create your own example of using a generic method....

IN JAVA, For this exercise, you will create your own example of using a generic method. First write a program that calls a method to multiply two integers and return the integer result. Then modify that program to make the method generic, calling it with two generic data types (T1 and T2) and then returning the result in the same type (also needs to be generic). Demonstrate your program by calling the method two times, once with an integer and once with a double. In your main program, display the results of the method call showing both an integer output and a decimal output. Submit code for both your original method (non-generic) and your revised method (generic), along with execution screenshots.

Solutions

Expert Solution

// do comment if any problem arises

//code

This is for original/non-generic method:

class Multiplication {

    // Method to multiply 2 integers

    static int multiply(int a,int b)

    {

        return a*b;

    }

    public static void main(String args[]) {

        // print 3 x 4

        System.out.println("3 x 4 = "+multiply(3, 4));

    }

}

Output:

For generic method:

class Multiplication {

    // Method to multiply 2 integers

    static <T extends Number> T multiply(T a, T b) {

        if (Integer.class.isInstance(a))

            return (T) Integer.valueOf(a.intValue() * b.intValue());

        else

            return (T) Double.valueOf(a.doubleValue() * b.doubleValue());

    }

    public static void main(String args[]) {

        // print 3 x 4

        System.out.println("3 x 4 = " + multiply(3, 4));

        // print 3.1 x 4.6

        System.out.println("3.1 x 4.6 = " + multiply(3.1, 4.6));

    }

}

Output:


Related Solutions

Java - In this lab, you will be writing your own method using the brute force...
Java - In this lab, you will be writing your own method using the brute force algorithm, to solve a second degree polynomial. This method will take in four parameters, three coefficients, and one constant, and it will have the following signature: public static String bruteForceEquationSolver(int one, int two, int three, int constant){} The equation you are trying to solve is of the following format, given the parameters stated above: (constant) = (one) * x + (two) * y +...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create a generic class called GenLinkedList. GenLinkedList will use nodes that store a value of the generic type to store its contents. It should have the following methods. The methods should all operate on the object making the call (none are static). Perform checking of the parameters and throw exceptions where appropriate. The linked list should be singly-linked. It should not use sentinel nodes (empty...
Create a generic method to print objects in java. Include a tester class.
Create a generic method to print objects in java. Include a tester class.
(JAVA) ExceptionSum The objective of this exercise is to create a method that adds all the...
(JAVA) ExceptionSum The objective of this exercise is to create a method that adds all the numbers within an array with an exception you cant sum the 6, and the 7, and the numbers between them cannot be added too, taking into account that whenever a 6 appears will be followed by a 7. If we have an array such that [1,2,3,6,10,9,8,7,5] when we pass it through the method it will give us an int such that the sum will...
Create your own example of a misleading statistic.
Create your own example of a misleading statistic. Explain the context of the data, the source of the data, the sampling method that you used (or would use) to collect the data, and the (misleading) conclusions that would be drawn from your example. Be specific in explaining how the statistic is misleading.
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create an application with 5 classes: 1.) Vehicle 2.) Car 3.) Bus 4.) Truck 5.) Tester Following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats, maxWeightLoad, numberAxels **Class Vehicle: should have a constructor that initializes all of it's data **Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data...
TrackMinMax i want the generic version based on java For this lab, you will create a...
TrackMinMax i want the generic version based on java For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, called TrackMinMax. The API is: Function Signature Description constructor TrackMinMax() constructor check void check(T i) compares i to the current minimum and maximum values and updates them accordingly getMin T getMin() returns the minimum value provided to check() so far getMax T getMax() returns the maximum value provided to check() so far...
In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of...
In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of type Comparable and returns the maximum element in the array. (i.e. "public static Comparable getMax(Comparable [] anArray)"). 5. Using the generic techniques to specify super-class relationships, implement a type safe version of the method in 4 named getMaxGen().
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder....
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder. 3. Import Scanner and Stacks from the java.util package. 4. Create a Stack object named basket. 5. The output shall: 5.1.Ask the user to input the number of fruits s/he would like to catch. 5.2.Ask the user to choose a fruit to catch by pressing A for apple, O for orange, M for mango, or G for guava. 5.3.Display all the fruits that the...
Use JAVA language. Using the switch method concept create a program in which you have an...
Use JAVA language. Using the switch method concept create a program in which you have an artist (singer) and the list of his or her songs. Add 18 songs. Then alter the script to achieve the following in each test run: a) Print all the songs. b) Print only song 15. c) Print only song 19.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT