 |
BOOK: Beginning Java 2  | This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 5th, 2003, 01:36 PM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Math problem solved in Java!
- I want to solve a mathematical problem that returns some differently typed variables, so I cannot use a return array?!
Question 1 - Is a return array for a method allowed (by the way)?
Question 2 - Can a method return differently typed variables?
- I programmed this not as a method (function), but I made a Problem class that stores the result in the instance variables of a solution object.
- Then I programmed a TryProblem class that calls and displays those variables of the solution object.
Question 3 - Is making a solution object the natural way to approach such a mathematical problem in Java?
Question 4 - Is there another approach that just uses a method?
Martyn? Anyone?
|
|

December 6th, 2003, 09:55 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
1. A method can return an array.
2. A method can only have a single return type, or be declared as 'void'.
3. Witout knowing how you intend to implement the 'solution' class, it is difficult for me to comment, but in princpal it sound's like a workable solution (no pun inteaded!).
4. I'm not sure what you need to do, or how you are going to determine or manipulate the result, but if the method returned an object and you determined that the object was an instance of a particular class, then maybe that might work.
Have a look at this site, it might help:
http://www.mathtools.net/Java/Mathematics/
Cheers
Martyn
|
|

December 9th, 2003, 12:42 PM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you Martyn for your wonderful answers, and for the amazing link!!!
I still have a MATLAB program that needs to be changed over to Java, so I will read all the MATLAB stuff there!
I like this forum because its quiet, lucky there's you.
Cheering Martyn: Hurray!
So I was on the right track (just a bit confused because of the way MATLAB m-file functions work: create an output array with different variables).
I think I can live with the Java objects, I can list my solution object instance variables in an array with the Ivor Horton TryOutProblem.class main() method. OK
FOUR MORE QUESTIONS that came to me whilst programming my first Java.class was this:
{ {I {tend to} {want to} {nest {my methods}} }
- Can methods be nested?
- Is this good object oriented programming practice?
- Is it best to put them on the same level in the class, like I think was done in the standard Java classes?
- Or is there a seldom occasion when nesting methods CAN BE USEFUL?
Highest regards
Frans Lelieveld
{ the Hague, the Netherlands }
{ at chapter 06, and deep into the BigDecimal class }
|
|

December 9th, 2003, 03:34 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
No, you can't nest methods within other methods but you can call a method from within another method. Perhaps this is what you meant? It is allowable however, to nest classes: using 'standard' classes, inner classes, or annonymous classes.
Ideally, you should divide your program into methods that perform one identifiable task. Keep all of the operations in a method at the same level of abstraction. This will naturally result in programs with many small methods, each a few lines long.
Try to make the execution structure of your method visible when looked at quickly. Standardize on a few good structures so a reader will quickly be able to survey the functionality and identify where to look for interesting features.
I hope this helps.
Martyn
|
|

January 14th, 2004, 06:55 AM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by freezotic
My current project involves creating a SQUARE ROOT METHOD for the Java BigDecimal class that uses "Square root coupled Newton" ITERATION
(from the book "Pi-unleashed").
Quote:
quote:Originally posted by Martyn
No, you can't nest methods within other methods....
It is allowable however, to nest classes: using... inner classes...
Ideally, you should divide your program into methods that perform one identifiable task. Keep all of the operations in a method at the same level of abstraction. This will naturally result in programs with many small methods, each a few lines long.
Martyn
|
HOWEVER
don't you agree that is a problem with a RECURSIVE METHOD?
1. I need to declare a lot of STATIC INSTANCE VARIABLES
2. to be used recursively in the iteration of my square root METHOD
3. these VARIABLES only need to exist during execution of the method
THE SOLUTION
- I chose to create an INNER PRIVATE CLASS that declares the variables and has the recursive PRIVATE METHOD inside of it
- The inner class is addressed by an OUTER PUBLIC METHOD
- I declare only one BigDecimal variable that stores intermediate results (the recursive method from the book had several), to reduce the amount of BigDecimal variables (used in the recursive loop)
|
The program is ready!!!
And my BigDecimal method calculates the square root of a 6000 digit number in 1 second (on a Pentium II), tested and true!
Would SUN be interested?
freezotic
|
|

January 14th, 2004, 11:11 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Fantastic and well done. What was your final design?
Cheers
Martyn
|
|

January 21st, 2004, 03:16 PM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by Martyn
Fantastic and well done. What was your final design?
|
HORRIFIC!
|
|
 |