Java generics
Write a program that calls a method to multiply two integers and return the integer result. Modify the program to make the method generic, calling it with two generic data types and then returning the result in the same type which also has to be generic. Call the method two times, once with an integer and once with a double in your main program and display the results
//Java code
public class Multiply { public static void main(String[] args) { int z = getMultiply(5,6); System.out.println("The result: "+z); } public static int getMultiply(int x , int y) { return x*y; } }
//Output
//Generic version
//Java code
public class Multiply<T> { public static void main(String[] args) { int z = getMultiply(5,6); System.out.println("The result: "+z); // for double double z1= getMultiply(55.6, 67.8); System.out.println("The result: "+z1); } @SuppressWarnings("unchecked") public static <T extends Number> T getMultiply(T x , T y) { T t; if (x instanceof Integer && y instanceof Integer) return (T) (Integer)(x.intValue()*y.intValue()); else return (T) (Double)(x.doubleValue()*y.doubleValue()); } }
//Output
//If you need any help regarding this solution .......... please leave a comment ....... thanks
Java generics Write a program that calls a method to multiply two integers and return the...
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...
Write a method to multiply two matrices. The header of the method is: public static double[][] multiplyMatrix(double[][] a, double[][] b) To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication. Assume the column size of matrix a is n. Each element is For example, for...
Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...
Generics Program In the following problem you will illustrate how we determine the greatest of 3 arguments using a single Generic method. You'll want to restrict the kinds of types that are allowed to be passed to Comparable objects. Create a GUI application which tests a method called maximum by calling the method 3 times; first with 3 ints, then with 3 doubles, and finally with 3 string objects as parameters. Your method maximum will determine the greatest of the...
CE – Return and Overload in C++ You are going to create a rudimentary calculator. The program should call a function to display a menu of three options: 1 – Integer Math 2 – Double Math 3 – Exit Program The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once. This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and return their total. The main method then prints the digits total with proper label as shown below Method Sum )is of type integer and takes an integer value. The method recursively adds up the digits and returns their total. Document your code and use proper prompts for...
Given the incomplete program below, write two functions, one called integer and another called decimal. You can use following incomplete program. Your assignment is to supply the necessary missing code: #include #include using namespace std; int integer (….) { //REPLACE THE … WITH THE REQUIRED PARAMETER (A DOUBLE) //WRITE HERE THE CODE TO EXTRACT THE INTEGER AND RETURN IT } double decimal (….) { //REPLACE THE … WITH THE REQUIRED PARAMETER //WRITE HERE THE CODE TO EXTRACT THE DECIMAL PORTION,...
Write a C program (not C++) that calls a function to multiply a matrix (two dimensional array). The main program should ask the user to enter an integer that will be used as the matrix “adder” and then call the function. The function should perform a matrix increment (every element of the array is incremented by the same value) and assign the result to another array. The function should be flexible enough to work with any array size and “adder”,...
Java Programming Write a method named isEven that accepts an integer argument. The method should return true if the argument is even, or false if not. The program’s main method should use a loop to generate a random integer. The number of random integers is provided by a user, who must enter a number greater than 10. The loop should then invoke the isEven method to determine whether each integer is even or odd, display the random number and the...