Question

In: Computer Science

Write a small Java class called StoreAddStuff that uses generics to store two variables of type...

Write a small Java class called StoreAddStuff that uses generics to store two variables of type T passed in during construction, then returns their sum through a non-static method.

Solutions

Expert Solution

class StoreAddStuff<T extends Number>{
        T x;
        T y;
        
        public StoreAddStuff(T aX, T aY) {
                super();
                x = aX;
                y = aY;
        }

        public <T extends Number> double sum() {
                return x.doubleValue()+y.doubleValue();
        }
}
public class TestStoreAddStuff {
        public static void main(String[] args) {
                StoreAddStuff<Integer>a =new StoreAddStuff<Integer>(10,3);
                System.out.println(a.sum());
        }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level,...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level, cottage, etc.) and size. Provide the following methods: A no-arg/default constructor. A constructor that accepts parameters. A constructor that accepts the type of the house and sets the size to 100. All other required methods. An abstract method for calculating heating cost. Come up with another abstract method of your own. Then write 2 subclasses, one for mobile house and one for cottage. Add...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
JAVA How to make a graph class that uses a linked list class to store nodes...
JAVA How to make a graph class that uses a linked list class to store nodes and linked list within each node to store adjacency list The linked list class has been made already.   import java.util.*; public class LinkedList implements Iterable { private int size = 0; private Node head; private Node tail; private class Node { private T data; private Node prev; private Node next; public Node(T data) { this.data = data; } }    public Iterator iterator() {...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
. Write a function in JAVA called shorten that is defined in the class P8 and...
. Write a function in JAVA called shorten that is defined in the class P8 and shortens each element of an array of strings. Every string with more than two characters is cut down to its first two characters. For example, a program that uses the function shorten follows. public class P8 { public static void main(String args[]) { String x[] = {"CSCI", "1", "11", "Queens", "College", "CUNY"}; shorten(x); for (int i = 0; i < 6; i++) System.out.print(x[i] +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT