Click to See Complete Forum and Search --> : Generic methods and parameter types
fozny
April 6th, 2010, 10:47 PM
I'm writing a math class. It has a bunch of methods in it, some are quite long. My constructor and methods only accept an int as a parameter right now. I want to extend this to include long's and BigInteger's and in some cases double's. But I don't want to have to rewrite every method for each data type. I tried a bunch of things, including generics, but I kept running into incomparable type problems when i started trying to operate on the variables even after I cast them as a specific datatype. My prof said it would be possible using generics but I cant seem to get it to work. Has anyone ran across this problem before?
keang
April 7th, 2010, 04:33 AM
For a start you can't use generics with primitive data types so you can't do this for int. long etc. However, you can use the primitive wrapper classes ie Integer, Long etc.
If you are performing operations on the objects (which i assume you are) then they all need to subclass a common type which has methods to allow you access the object attributes you need. You may also need to be able to change the value(s) or create a new instance of the object if they are immutable (which the primitive wrapper classes are). For your Math class to be able to create a new instance of the generic type you will need to pass in its class type.
dlorde
April 7th, 2010, 07:53 AM
Has anyone ran across this problem before?
I can't really tell, unless you explain exactly what the problem is. I expect most people who've used generics have had problems with them at some point.
If you ask a specific question about it, post up the relevant code, and the full text of any error messages you need help with, maybe we can give you a specific answer.
Learning results from what the student does and thinks, and only from what the student does and thinks. The teacher can advance learning only by influencing the student to learn...
H. Simon
fozny
April 7th, 2010, 11:42 PM
For a start you can't use generics with primitive data types so you can't do this for int.
I set zero as a global variable so that I could operate with it using a wrapper class, which seemed to work.
If you are performing operations on the objects (which i assume you are) then they all need to subclass a common type which has methods to allow you access the object attributes you need
I set the classes of the variables to Number. Is this what you meant by subclass to a common type? This is where my current problem is. I cant seem to access any of the methods of the primitive wrapper classes from Number. Such as compareTo(). I need that method if I cant use logical operators. When I tried using generics I ran across the same problem. My variables would always be recognized as a generic class no matter what I did.
I thought about writing my own compareTo() method. But it seems like there must be a different way.
public class RationalNumber3
{
private Number n; // the numerator
private Number d; // the denominator
private Number zero;
private Number num;
private Number denom;
private Number divisor;
/**
* Constructor.
* @param n The numerator.
* @param d The denominator.
*/
public RationalNumber3(Integer n, Integer d) {
zero = new Integer(0);
this.n = new Integer(n);
this.d = new Integer(d);
setClass(n, d);
simplify(n, d);
}
/**
* Set the class of the given object.
* @param n The numerator.
* @param d The denominator.
*/
public void setClass(Number n, Number d) {
Object obj = n.getClass();
if (obj instanceof Integer) {
num = new Integer(n.intValue());
denom = new Integer(d.intValue());
divisor = new Integer(1);
}
}
/**
* Simplify the rational number using euclid's greatest common divisor
* algorithm.
* @param n The numerator.
* @param d The denominator.
*/
public void simplify(Number n, Number d)
throws NumberFormatException {
if (denom.equals(zero))
throw new NumberFormatException("Denominator cannot be zero");
// when the denominator is negative move the sign to the numerator
if (denom < zero)) {
num = -num;
denom = -denom;
}
// when the numerator is zero set the denominator to 1
if (num == 0)
denom = 1;
else {
// simplify the fraction using Euclid's gcd algorithm
divisor = gcd(Math.abs(num), Math.abs(denom));
num = num / divisor;
denom = denom / divisor;
}
this.n = num;
this.d = denom;
}
ajhampson
April 9th, 2010, 12:50 PM
Try this: java-generics-and-numbers (http://stackoverflow.com/questions/877897/java-generics-and-numbers) for pointers. It's the first result from Google for java generic math class (http://www.google.com/search?q=java+generic+math+class&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&safe=active)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.