• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Generic classes

 
Alexander Visser
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My home work so far now. Now I must implement a generic class Coordinate as Pair of two numeric (subtype of Number) values. The getDistanceFromOrigin method returns the distance from the coordinate to the origin.
I get stuck with the Coordinate class, what did I do wrong?

 
Stephan van Hulst
Saloon Keeper
Posts: 12878
279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Coordinate class doesn't extend Pair.

The ^ operator isn't defined for Number. You must convert your numbers to double first.
 
Mike Simmons
Rancher
Posts: 3844
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The ^ operator isn't defined for Number. You must convert your numbers to double first.


Double doesn't work here; it would have to be an integral type (byte, short, char, int, long).  Which is just a symptom of the real problem: in Java, the ^ operator doesn't do exponentiation; it's an exclusive or -- either bitwise or boolean, depending on the arguments.  To square a number x, we either use Math.sqrt(x), or just write x*x.
 
Campbell Ritchie
Marshal
Posts: 72619
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your constructor is incorrect. It doesn't use the values t and s.
Why are you creating your own exception class? Throw a NullPointerException if there are problems with nulls.
Don't use squares, nor square roots. Not if you can use this method. That gives me a problem: that method takes (double, double) as its parameters. You cannot simply use unboxing to get the value for the method call, but you can use a method of the Number class.
Why are you making your coordinate class extend Pair (even if you aren't making it extend Pair)? Have you been told to create subtypes of Pair? Maybe you would do better to find out about, “Favour composition over inheritance,” and give that class a Pair as a field.
Don't write the generic types twice (line 22,‑23). Use type inference and miss out the second pair of types: write <> on its own.
 
Piet Souris
Saloon Keeper
Posts: 4380
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you expect things like Coordinate<Float, BigInteger>?  Do you need two generic types?
 
Alexander Visser
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now Coordinate extends Pair.
Constructor now use values t and s.
Created an own exception class, because this is asked in assignment. Indeed is NullPointerException also possible.
The assignment asks to implement Coordinate as Pair, so guess inheritance.
Indeed, my fault about generic types twice.
There is nothing written in the assignment about two generic types.
Changed getDistanceFromOrigin(), but doesn't work. Method hypot() gave problems indeed. What did I do wrong?
Thanks so far.

 
Piet Souris
Saloon Keeper
Posts: 4380
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Number has the method: doubleValue()
 
Paul Clapham
Marshal
Posts: 26525
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Visser wrote:Now I must implement a generic class Coordinate as Pair of two numeric (subtype of Number) values.



This is a bit ambiguous. It doesn't say whether the two numeric values must be of the same subtype of Number, or whether they can be of different subtypes. You said "subtype" (singular) which sort of suggests only one subtype, but it doesn't definitively say that. So it might be that "public class Pair<T, T>" is what you were supposed to do.
 
Piet Souris
Saloon Keeper
Posts: 4380
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Coordinate need not be generic at all. For instance: class Coordinate extends Pair<Double, Double>
 
Campbell Ritchie
Marshal
Posts: 72619
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Visser wrote:. . . Created an own exception class, because this is asked in assignment. . . .

It is obviously testing your ability to create subclasses. I would still prefer to use the ready‑made type since there is one available. Is your exception checked or unchecked?

. . . There is nothing written in the assignment about two generic types.

Please ask your teacher for clarification. I would prefer to use one type <T>.

. . . Method hypot() gave problems indeed. What did I do wrong?

Did you read its documentation; I provided a link? Did you find the method in Number that would give you the correct type for the hypot() method?
 
Alexander Visser
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
doubleValue() and hypot() are used now.
Must be different possible generic types.
The assignment asks for a generic Coordinate.
The exception is checked.
Thanks all for your help.
 
Campbell Ritchie
Marshal
Posts: 72619
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Visser wrote:. . . Thanks all for your help.

That's a pleasure And thank you for the additional information.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic