• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another refactoring excersise!

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Terror has stricken on Roundville! Well not really. I just wanted to get your attention. Anyway, see how you guys can make this ugly mess into a beautiful class. The rounding starts out as a method, but I need to see some OOP getting involved in the code, so make the rounding into a complete class of its own! Also, this should be a rounding method right? WELL WHERE'S THE ROUNDING? I forgot to round the number, another task for you guys to fix. Here's the code:

There are probably some errors lurking because I copied the code from vi (in UNIX). I wish you all luck!

[This message has been edited by Conrad Kirby (edited June 18, 2001).]
[This message has been edited by Conrad Kirby (edited June 18, 2001).]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yikes! When you said a million lines, you weren't kidding.
OK, (rubbing hands and kicking off shoes) let's start with some tests. You do have tests, don't you?
Well, if you don't we need to make some up. How do want to be able to use this rounding class of yours? What is the motivation for writing it?

[This message has been edited by JUNILU LACAR (edited June 18, 2001).]
 
Conrad Kirby
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main purpose for this rounding function is to round a certain number so that the result is however many characters you wanted. For instance: fround (1.12345678, 5) would return a string value of "1.123". I don't know what you mean by tests...Ummm AAAACK my hairs on fire!
HAHAHA
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, here are some of my initial comments:
1. Isn't there a way we can use classes in the standard Java packages to do the same thing? Have you looked at java.text? Would the classes in this package be able to do what you want?
2. If the standard packages do not provide the functionality you want, since you are basically writing a library function and dressing it up as a class (much like what java.lang.Math does) then you might as well be consistent with java.lang.Math and the rest of the standard classes.
I think you should keep the result of round as a double. Then just override the toString method to return a String in the desired format.
 
Conrad Kirby
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ummm...there are many problems with that.
First, Java simplifies all doubles to the format it wishes. For instance, I want to keep this value as a double: 1.123e2. Nope, says Java, I'm gonna have to change you to this: 112.3! Different amount of charaters completely!
Second, Java's math library is stupid in this case. The only rounding methods it has are toInt() and toDouble().
Yes, I pretty much am creating a library, and I'll use the library a lot. So there !
[This message has been edited by Conrad Kirby (edited June 18, 2001).]
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Conrad,
Have you tried java.math.BigDecimal for your problem.If it doesn't meet your requirement I would suggest you extend this class am implement the functionality of doubleValue() method.
Hope you have seen the static ints in the BigDecimal class : ROUND_??? and also the BigDecimal(BigInteger unscaledVal,int scale).
Does this help?
Sandeep
[This message has been edited by Desai Sandeep (edited June 20, 2001).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I see this is fairly old, but I just came across it, and maybe someone will still care.
My first two reactions to the code were (1) break some of those nested blocks out into separate methods, for readability if nothing else, and (2) I've never seen so many unnecessary String constructors in one method. In fact I'm pretty sure every single use of "new String" is unnecessary, since the argument to the constructor is already a String. I'm not sure why the Java language even includes a String(String) constructor, as its only possible use seems to be to construct teaching examples on garbage collection and the difference between == and .equals(). But I digress...
On further reading though, I decided there would be little point to refining the code in its current form. Read on for reasons...
> First, Java simplifies all doubles to the format it wishes.
> For instance, I want to keep this value as a double: 1.123e2.
> Nope, says Java, I'm gonna have to change you to this: 112.3!
> Different amount of charaters completely!
Since the two numbers you give are in fact equal, they are stored in the double datatype exactly the same way. What you want is to control the format of the string representation of the number. This is probably best handled with the DecimalFormat class, hidden away in the java.text package like Junilu suggested. Here's a quick demo, which also shows how you can test your method to verify its behavior:
<code><pre>
import java.text.*;

class Formatter {
static String format(double value, int integerDigits, int fractionalDigits) {
DecimalFormat df = new DecimalFormat();
df.setMaximumIntegerDigits(integerDigits);
df.setMinimumIntegerDigits(integerDigits);
df.setMaximumFractionDigits(fractionalDigits);
df.setMinimumFractionDigits(fractionalDigits);
return df.format(value);
}
}

class Test {
public static void main(String[] s) throws Throwable {
test(1.23, 0, 0, "");
test(1.23, 1, 0, "1");
test(1.23, 1, 1, "1.2");
test(1.27, 1, 1, "1.3");
test(1.23, 1, 2, "1.23");
test(1.23, 2, 5, "01.23000");
test(-51.3, 2, 5, "-51.30000");
test(123, 2, 5, "??.???");
System.out.println("Test completed");
}

public static void test(double value, int integerDigits, int fractionalDigits,
String expectedResult) {
String actualResult = Formatter.format(value, integerDigits, fractionalDigits);
if (!expectedResult.equals(actualResult)) {
System.out.println("Failed for");
System.out.println(" value = " + value);
System.out.println(" integerDigits = " + integerDigits);
System.out.println(" fractionalDigits = " + fractionalDigits);
System.out.println(" actualResult = " + actualResult);
}
}
}
</pre></code>
I included two tests which the method currently fails, since this was intended as a simple demo, and I'm not even really sure what expected behavior you want to have. Many more possibilities are present - read the docs on DecimalFormat carefully. It takes some study, but is pretty powerful.
> Second, Java's math library is stupid in this case. The only
> rounding methods it has are toInt() and toDouble().
You probably shouldn't call a library stupid if you're unfamiliar with it. For real rounding, in the sense of methods that actually modify the value of a number rather than merely control the String representation of the number, look at Math.ceil(), Math.floor(), and Math.round(), as well as the methods in BigDecimal.
 
Lookout! Runaway whale! Hide behind this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic