# extending Math classes

Edwin Davidson

Greenhorn

Posts: 27

posted 11 years ago

- 0

Dear all:

I am working through the "Just Java 2" book by Peter van der Linden. I am on page 173 question 6. He asks me to create a class with Math's methods in it and to create a wrapper for each Math method.

What he is asking exactly is:

"Create a class that provides all the same methods as java.lang.Math but which operates on degrees not radians. Do this by creating a wrapper for each method in Math, in your class".

You cannot extend Math's methods as they are all static. I am writing to

ask if you guys can give me a hint as to the solution. I've read the abstract class section in that chapter a few times and I've been trying various things but I cannot get this to work.

In my example below I wish to overwrite math.random method to make it

multiply by 2, just to get going. Here is my code - file is mathh.java:

This doesn't work but you can see my logic, so to speak. Can someone tell me where I am going wrong ?

Thank you very much.

Edwin Davidson

Halifax, Nova Scotia

Canada

[ edited to surround code block with the UBB [code] and [/code] tags, in order to help preserve formatting -ds ]

[ February 17, 2004: Message edited by: Dirk Schreckmann ]

I am working through the "Just Java 2" book by Peter van der Linden. I am on page 173 question 6. He asks me to create a class with Math's methods in it and to create a wrapper for each Math method.

What he is asking exactly is:

"Create a class that provides all the same methods as java.lang.Math but which operates on degrees not radians. Do this by creating a wrapper for each method in Math, in your class".

You cannot extend Math's methods as they are all static. I am writing to

ask if you guys can give me a hint as to the solution. I've read the abstract class section in that chapter a few times and I've been trying various things but I cannot get this to work.

In my example below I wish to overwrite math.random method to make it

multiply by 2, just to get going. Here is my code - file is mathh.java:

This doesn't work but you can see my logic, so to speak. Can someone tell me where I am going wrong ?

Thank you very much.

Edwin Davidson

Halifax, Nova Scotia

Canada

[ edited to surround code block with the UBB [code] and [/code] tags, in order to help preserve formatting -ds ]

[ February 17, 2004: Message edited by: Dirk Schreckmann ]

Eric Fletcher

Ranch Hand

Posts: 188

posted 11 years ago

- 0

Hi Edwin,

You can't extend java.leng.Math, Math is a

So, you would take all the methods that take an angle in radians as an arg in the Math class, (like, say the cos(double a) method), create a new method in your class(let's call it MathWrapper) with the same method signature, but in your wrapper class the argument value will be in degrees. You will then convert the arg value to radians inside your method, then simply have your method call the cos method on Math. So you have

Does that make sense?

E

[ February 12, 2004: Message edited by: Eric Fletcher ]

You can't extend java.leng.Math, Math is a

*final*class which means it cannot be extended. What he means by a*wrapper*class is to build a class that*wraps*Math. This class will have methods that mirror all the methods in Math, then you will call the mirrored method in the Math class.So, you would take all the methods that take an angle in radians as an arg in the Math class, (like, say the cos(double a) method), create a new method in your class(let's call it MathWrapper) with the same method signature, but in your wrapper class the argument value will be in degrees. You will then convert the arg value to radians inside your method, then simply have your method call the cos method on Math. So you have

*wrapped*your method around the functionality of the cos method on the Math class.Does that make sense?

E

[ February 12, 2004: Message edited by: Eric Fletcher ]

My theory of evolution is that Darwin was adopted. - Steven Wright

Edwin Davidson

Greenhorn

Posts: 27

posted 11 years ago

nope. your assignement IS to write that code!!!

Eric's answer above tells you what you need to do. you need to create a new class, call it MathWrapper. you will then have to write a bunch of functions... all the ones you find in the Math api (see here.) make sure your signiatures match the others EXACTLY.

then, for each of your functions, you will need to convert the degress passed to you to radians (you may want to write another method in your class that does this... or you may notice that the Math class HAS a toRadians() method you may be able to use...). pass that CONVETED value to the Math method, which will send you back a result. return THAT value from your method.

This is called a wrapper because you effectively "wrap" your code around every Math method. does that help??

[ February 16, 2004: Message edited by: fred rosenberger ]

- 0

how do I wrap the math class in a wrapper ? Is there code for that somewhere ?

nope. your assignement IS to write that code!!!

Eric's answer above tells you what you need to do. you need to create a new class, call it MathWrapper. you will then have to write a bunch of functions... all the ones you find in the Math api (see here.) make sure your signiatures match the others EXACTLY.

then, for each of your functions, you will need to convert the degress passed to you to radians (you may want to write another method in your class that does this... or you may notice that the Math class HAS a toRadians() method you may be able to use...). pass that CONVETED value to the Math method, which will send you back a result. return THAT value from your method.

This is called a wrapper because you effectively "wrap" your code around every Math method. does that help??

[ February 16, 2004: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Edwin Davidson

Greenhorn

Posts: 27

posted 11 years ago

- 0

I think I got it now. It's a simply assigment for a complicated person !

Since I am not up on radians, I just used the Math.random() method to make my point.

The static method Math.random() will generate a simple random number by itself. In my little example below I am taking that number (generated by Math.random()) and multiplying it by two (what Math.random() does not do but I want it to).

So my version of random() in my MathWrapper does just that. So the morale of the story (assignment) was to create a class of your own, use Math.random() in it then take that result and rejig it as you like.

Thank you very much - you guys are really, really helpful.

Edwin

************************************************************

[ edited to surround code block with the UBB [code] and [/code] tags, in order to help preserve formatting -ds ]

[ February 17, 2004: Message edited by: Dirk Schreckmann ]

Since I am not up on radians, I just used the Math.random() method to make my point.

The static method Math.random() will generate a simple random number by itself. In my little example below I am taking that number (generated by Math.random()) and multiplying it by two (what Math.random() does not do but I want it to).

So my version of random() in my MathWrapper does just that. So the morale of the story (assignment) was to create a class of your own, use Math.random() in it then take that result and rejig it as you like.

Thank you very much - you guys are really, really helpful.

Edwin

************************************************************

[ edited to surround code block with the UBB [code] and [/code] tags, in order to help preserve formatting -ds ]

[ February 17, 2004: Message edited by: Dirk Schreckmann ]

posted 11 years ago
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

- 0

If you want, when you get some done, post your code here (make sure you use the code tags!!!). I'll be happy to look at it and make sure you're on the right track (it sounds like it from your post above), or if you have any other questions about why/how it's working/not working (as probably would many others here).

good luck!!!

good luck!!!