Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using generics in Java and numeric types

Tags:

java

generics

Hey there I have just started Java for a course I will be taking soon (yeah I'm a nerd, I study before I have start the class). I wanted to cram a few concepts together to learn how everything works together in Java.

Now, what I wanted to try to do is a simple Shape (Forme in french) class which has a least 1-2 classes inheriting from it and make a few instances inside a collections object. I also wanted to make those classes generics to try that out as well. That is the part I'm having trouble with.

I'm very used to C++ so that may be where the confusion lies, but it seems that I can't use generics so that, for example, I can specify that dimensions will be in, say, Integer or Double or Float or whatever. Is it possible ?

The GetArea() method below can't compile because of base and hauteur.

package testconcepts;

import java.util.*;
import java.lang.*;

abstract class Forme <T extends Number> {
    protected String nom_forme;

    String NomForme() { return nom_forme; }

    abstract Double GetArea();
}

class Triangle <T extends Number> extends Forme {
    protected T base, hauteur;

    Triangle() { 
        this.nom_forme = "TRIANGLE"; 
    }

    @Override
    Double GetArea() { return base * hauteur / 2; }

    T GetBase() { return base; }
    T GetHauteur() { return hauteur; }
    void SetBase(T base) { this.base = base; }
    void SetHauteur(T hauteur) { this.hauteur = hauteur; }
}

public class TestConceptsJava {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(new Triangle<Double>());
    }
}

Notice that I also cannot make assignments to 0 in the constructor because of type issues.

Also, I know there are very similar topics, but I haven't found a solution to my problem yet in those other questions.

TLDR How can I make work generics with primitive type wrappers (Double, Integer..) ?

like image 786
ApplePie Avatar asked Dec 15 '22 18:12

ApplePie


2 Answers

So your problem is this line I guess:

Double GetArea() { return base * hauteur / 2; }

The reason you receive an error is because the * operator is undefined for objects of type Number. Since you know that you will be dealing with ints and doubles (I assume), you can try this:

Double GetArea() { return base.doubleValue() * hauteur.doubleValue() / 2; }

As a note, usually we start method names with a lower case letter, i.e. getArea().

like image 122
arshajii Avatar answered Dec 31 '22 09:12

arshajii


In GetArea(), you're calling the * operator on two classes. How much sense would it make to say something like:

Object o1 = //whatever;
Object o2 = //whatever;

return o1 * o2;

Not much right? You can't multiply two classes. What you actually want to do is multiply the underlying value of those wrapper classes (which is not done implicitly for you in Java). Rewriting GetArea() like this should solve your problem:

Double GetArea() { return base.doubleValue() * hauteur.doubleValue() / 2; }

Note that you'll also have to use the underlying value everywhere else that you care about it in that class.

like image 34
Jordan Kaye Avatar answered Dec 31 '22 10:12

Jordan Kaye