Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / October 2006

Tip: Looking for answers? Try searching our database.

java package - money

Thread view: 
java_nut - 27 Oct 2006 00:39 GMT
good evening guys,
i am finding a piece of work extremely tough going at the moment and
wondered if i could get any hints/help on where im going wrong. i am
aiming to produce a package (money.java) which could be used in order
to produce arithmetic calculations on sums of money.

The code i have at the moment is:

MONEY.JAVA:

package java;

public class money
{
    private int data1, data2;

    public money()
    {
       data1=0;
       data2=0;
    }

    public money(int value1, int value 2)
    {
        data1 = value1;
        data2 = value2;
   }

   public void addMoney(int value1, int value2)
   {
        return value1 + value2;
    }

    public void subtractMoney(int value1, int value2)
    {
        return value1 - value2;
    }

    public void divideMoney(int value1, int value2)
    {
        return value1 / value2;
    }

    public void multiplyMoney(int value1, int value2)
    {
        return value1 * value2;
    }

}

MONEYTEST.JAVA

import java.money;

public class MoneyTest
{
   public static void main(String [] args)
    {
        money p = new money(10,20);
        System.out.println("The total is " + p.addMoney());
    }
}

I think i may need a 'toString' method? and also, is there any way of
covering errors? im sorry if these problems are trivial. I have various
books, but can't seem to use them in this case.

Thank you for any feedback.

Jess
Lew - 27 Oct 2006 04:11 GMT
You should fix your newsgroup client so that you only post the same message
once, not eight times.

> good evening guys,
> i am finding a piece of work extremely tough going at the moment and
[quoted text clipped - 5 lines]
>
> MONEY.JAVA:

Upper- and lower-case matter.  The class should be called "Money", not
"money", so the file should be "Money.java", not "MONEY.JAVA".

> package java;

You are not permitted to define classes in the 'java' or 'javax' packages.  It
is against the license that lets you use the Java(tm) language.  Also, the
package name will correspond to a subdirectory name of the source code.

> public class money

By convention, class names should begin with an uppercase letter: "Money"
rather than "money".

> {
>     private int data1, data2;

Why two data values?  How do you intend to use these?

Pick more meaningful names.

>     public money()
>     {
>        data1=0;
>        data2=0;

Java initializes instance variables to zero or the equivalent anyway, so this
initialization is redundant.

>     }
>
[quoted text clipped - 7 lines]
>     {
>         return value1 + value2;

It looks like you never use data1 or data2.  Why are they in the class?

You don't really need to repeat the word "Money" in the method names, since
the methods are specific to the "Money" clss anyway.

>     }
>
[quoted text clipped - 7 lines]
>         return value1 / value2;
>     }

What would you like the result to be if you divide "66 /100"?

>     public void multiplyMoney(int value1, int value2)
>     {
[quoted text clipped - 6 lines]
>
> import java.money;

By convention, packages are based on your domain name.  You are not permitted
to use "java" or "javax" as a package part in any published Java(tm) code,
unless you are Sun.  Read the license that lets you use the language.

You are advised not to use "com.lewscanon" unless you are me.

If you had a domain "foo.com", your package would be something like
"com.foo.money".  If you don't have a domain, or don't want to use one, just
use "money":

   import money;

> public class MoneyTest
> {
>     public static void main(String [] args)
>      {
>          money p = new money(10,20);
>          System.out.println("The total is " + p.addMoney());

Did you actually try compiling this class?  You didn't define an "addMoney()"
method, only an "addMoney( int, int )" method.

What if someone wanted to add three values?  Divide? Negate?

You should test all your methods, not just one.  Test all significant cases,
e.g., division of a large amount by a small one and vice versa. Division of
zero or by zero.  Think about so-called "corner cases": those boundary
scenarios that can make life unpleasantly interesting if not handled properly.

>      }
> }
>
> I think i may need a 'toString' method?

Why?

Would you want to format it?

Take a look at
http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html

What about equals() and hashCode()?  These are practically the first methods
you should consider overriding.

> and also, is there any way of covering errors?

First, eliminate the compilation errors. One of them was already pointed out
by another respondent.

Your methods provide no advantage or difference over the conventional int
operations.

int is probably too small a type for money anyway.  A better choice might be
java.math.BigInteger, which already has the necessary methods defined to
handle many calculations.  java.math.BigDecimal is also a good choice.  Of
course, these might not be so good if you are calculating compound interest.

Note: both these classes are immutable.  Immutable classes are very often a
Good Thing.  Your Money class most likely should be immutable, assuming you
don't decide that BigInteger has everything in it already that you want.

How much money we talking 'bout, anyway?

- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.