The Artima Developer Community
Sponsored Link

Java Answers Forum
please help me on this simple program...

5 replies on 1 page. Most recent reply: Sep 7, 2005 10:54 PM by marjorjie

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
marjorjie

Posts: 6
Nickname: jorj
Registered: Sep, 2005

please help me on this simple program... Posted: Sep 6, 2005 7:44 PM
Reply to this message Reply
Advertisement
1. Write a program that mimics the function of a check writer. The primary function of a program is to convert the amount in figure it its equivalent in words. Example, 110.25 should be written as One Hundred Ten Pesos and Twenty Five centavos. The program asks for an amount in figure and the name of the payee (and other information in a check). The output should closely resemble a real check. Make sure that you should use methods during conversion.

HOPE SOMEBODY COULD HELP ME...

IM STILL A STUDENT AND THIS IS OUR EXCERCISES...

THANKS A LOT!!!

I WILL REALLY APPRECIATE YOUR EFFORT...

GOD BLESS!!!


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: please help me on this simple program... Posted: Sep 6, 2005 11:13 PM
Reply to this message Reply
Here is a kick start to one of the methods, you
may fill in the rest of the logic your self.
final char ONE = '1';
final char TWO = '2';
final char THREE = '3';
public String convertMoney(double money){
   String str = ""+money;
   StringTokenizer tokenizer = new StringTokenizer(str, ".");
   
   String first_digits = tokenizer.nextToken();
   int num_digits = first_digits.length();
   
   String largest_pos = "";
   if(num_digits == 4)
      largest_pos = "thousand";
   else if(num_digits == 3)
      largest_pos = "hundred";
   else
      largest_pos = "";
   String leading_digit = "";
   String ret_value = "";
   for(int i = 0; i < num_digits; i++){
      leading_digit = first_digit.charAt(i);
      //  pseudo -
      if(leading_digit == ONE)
         ret_value = ret_value+leading_digit+" "+largest_pos+...;
   }
   return ret_value;
}

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: please help me on this simple program... Posted: Sep 7, 2005 12:02 AM
Reply to this message Reply
Of course you are a student. I wrote programs like this during my school days and never after. It helps understanding String operations.

1. separate the numbers before and after the separator, build Strings displaying those numbers.

3. converting the numbers after the separator is eays. Just check every single char and print it's "name" (one, two, three) out.

4. the part before the separator is a bit more complicated because you need the words "twenty", "thirty" ...
Split the String representing the number like this:
0,00,0,00,0,00,0,00
The Strings with just one character are to treat like the digits after the separator.
For those with two characters:
You need a special threatment for the numbers from 0 to 19, for the others you must build the name of the first digit (twenty, thirty), then the name for the second digit (one, two)

5. "hundred": Just insert it after every SINGLE char of the patten shown above.

6. "thousand", "million". Best thing would be an aray with the names, then check on wich position of the splittedf Strin you are. This word allways comes after the parts with TWO chars.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: please help me on this simple program... Posted: Sep 7, 2005 12:13 AM
Reply to this message Reply
Seems I didn't reload the page before answering.

Anyway, allthough I don't usually do other peoples homework here's the method to split the numbers before the separator:

    private static String[] splitMe(long l) {
        String s = "" + l;
        int div = 2;
        java.util.ArrayList<String> result = new java.util.ArrayList<String>();
        while (s.length() > 0) {
            String sright = s.substring(Math.max(s.length()-div, 0));
            s = s.substring(0, Math.max(0, s.length()-div));
            div = div == 2 ? 1 : 2;
            result.add(sright);
        }
        return result.toArray(new String[result.size()]);
    }


The evaluation is not much of a problem (just make sure to start at the last element and end with the first element), Kondwani has some interesting ideas in his code, allthough I prefer the use of arrays and looping search methods.

One more thing: Here is an array with the hundred/thousand/million/billion names corresponding to the index in the array generated by my method.
String[] names = new String[]{"", "hundred", "thousand", "hundred", "million", "hundred", "million", "hundred", "billion", "hundred"};

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: please help me on this simple program... Posted: Sep 7, 2005 1:50 AM
Reply to this message Reply
I agree, indexing via an array would seem like a
*cleaner* solution.

marjorjie

Posts: 6
Nickname: jorj
Registered: Sep, 2005

Re: please help me on this simple program... Posted: Sep 7, 2005 10:54 PM
Reply to this message Reply
hi spike!!!

i really appreciate what you have done...

you really exert effort just to teach a beginner like me...

thanks a lot!!!

GOD BLESS YOU !!!

AND MORE POWER!!!

Flat View: This topic has 5 replies on 1 page
Topic: C-Linda Compiler Previous Topic   Next Topic Topic: C-Linda Coordination Laguage Programming

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use