The Artima Developer Community
Sponsored Link

Java Answers Forum
adding

4 replies on 1 page. Most recent reply: May 11, 2006 8:44 AM by James Watson

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 4 replies on 1 page
Alex Tanil

Posts: 10
Nickname: alextanil
Registered: May, 2006

adding Posted: May 8, 2006 5:49 AM
Reply to this message Reply
Advertisement
as you understand i have many problems with the project i am trying to wtite. i want to add two coins. lets say 3.33 and 9.789. i have already store the int parts in integers and the decimals as well. i am trying to add the two coins. but theres the case that the decimal parts give me one more unit that should be added to the int part...
i have though this: if the result of the decimal has more factors than the longest of the two decimals.... and goes on....

the question is this. is there a method that calculates the length of an int?????


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: adding Posted: May 8, 2006 7:01 AM
Reply to this message Reply
You could use the logarithm on the base of ten.
int n = 3;
double numDigits = Math.log10(intValue);
if (numDigits > n) {
}


Then again, this is not necessary. The following way is somewhat more elegant. It could be implemented more compact, but this way you should understand it better.

Let's say you want to have 3 digits.

final int n = 3;
final int modifier = 10 ^ n; // = 1000;
int num1 = 525;
int num2 = 639;
int temp = num1 + num2; // = 1164;
int intResult = temp / modifier; // = 1
int fracResult = temp % modifier; // = 164;

Alex Tanil

Posts: 10
Nickname: alextanil
Registered: May, 2006

adding Posted: May 9, 2006 8:26 AM
Reply to this message Reply
take a look at this
i know its to much...
my problem is this now!!!
as you have understand i have two coins 9.12 an 7.789
with their integer parts and the decimal parts separate.
I have solved the problem (not vey nice though) with the carry but i have now this problem. If the decimal parts are of diferent length like above i want to do this adding 789+120 and not 789+12. But they dont have a fixed length. I have try to solve this but it does work. its the method as comments. what have i done wrong!!!!!!!

private int mhkosMegaluterouDecimalPart(Nomisma a, Nomisma b) {
String wString = Integer.toString(a.getDecimal());
int w = wString.length();
String lString = Integer.toString(b.getDecimal());
int l = lString.length();
int proswrinos = 3;

if (w >= l)
proswrinos = w;
else
proswrinos = l;
//System.out.println(proswrinos);
return proswrinos;
}
/** private void au3isiMikousDecimal(Nomisma a,Nomisma b){
int r=0;
if(compareDecimalParts(a,b)){
r = mhkosMegaluterouDecimalPart(a, b);
//System.out.println(r);
int i;
int v = 1;
for (i = 1; i < (r-1); i++)
v = v * 10;
String wString = Integer.toString(a.getDecimal());
int w = wString.length();
String lString = Integer.toString(b.getDecimal());
int l = lString.length();
if (l > w)
a.decimal = a.decimal * v;
// System.out.println(a.getDecimal());
if (w > l)
b.decimal = b.decimal * v;
//System.out.println(b.getDecimal());
}
}
*/

private int addDecimals(Nomisma a, Nomisma b) {

mhkosMegaluterouDecimalPart(a,b);
//au3isiMikousDecimal(a,b);
int apotelesma = a.getDecimal() + b.getDecimal();
if(elegxosCratoumenou(apotelesma,a,b))
apotelesma=apotelesma-pra3iCratoumenou(apotelesma);

//System.out.println("apotelesma prosthesis decimals: "+apotelesma);
return apotelesma;

}
private int addInetegers(Nomisma a,Nomisma b){
int apotelesma = a.decimal + b.decimal;

//System.out.println(elegxosCratoumenou(apotelesma,a,b));
if(elegxosCratoumenou(apotelesma,a,b))
au3isiIntegerPart(b);

int apoteleInt=a.integer+b.getInteger();
//System.out.println(apoteleInt);
return apoteleInt;
}
/**
* @return boolean
*/

private boolean elegxosCratoumenou(int apot,Nomisma a,Nomisma b) {
String cratoumeno =Integer.toString(apot);
int megethosApot = cratoumeno.length();
//System.out.println("to megethos tou apotelesmatos einai: "+megethosApot);
//System.out.println("To mikos tou megaluterou decimal einai: "+mhkosMegaluterouDecimalPart(a,b));

return megethosApot>mhkosMegaluterouDecimalPart(a,b);

}
private int pra3iCratoumenou(int apot){
String cratoumeno =Integer.toString(apot);
int megethosApot = cratoumeno.length();
//System.out.println("megethos apotelesmatos: "+megethosApot);

int i;
int t =1;
for(i=1;i<megethosApot;i++){
t=t*10;
}
//System.out.println("megethos dunamis: "+t);
return t;
}
private int au3isiIntegerPart(Nomisma b){
b.integer=b.integer+1;
//System.out.println("au3imeno integer: "+b.integer);

return b.integer;
}
public String pra3iProsthesiNomismatwn(Nomisma a,Nomisma b){

if(a.getSymbol().endsWith(b.getSymbol()) || a.getSign()==b.getSign())
return "to apotelesma tis prosthesis einai: "+a.getSymbol()+a.getSign()+addInetegers(a,b)+"."+addDecimals(a,b);

else
return "den einai dunati h prosthesi";
}
}

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: adding Posted: May 9, 2006 10:27 PM
Reply to this message Reply
Please reformat your code using the java tags as shown on the right side of the input field.
It just plain sucks reading unformatted code.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: adding Posted: May 11, 2006 8:44 AM
Reply to this message Reply
I would back up a few steps and define a Money class (or find one). The problem you have can go away easily:
class Money
{
    private final int places;
    private final int value; // or long
 
    public Money(int value, places)
    {
        this.places = places;
        this.value = value;
    }
 
    public Money(String text)
    {
       pos = value.indexOf('.');
 
       if (pos < 0) {
           places = 0;
           value = Integer.parseInt(value);
       } else {
           places = (value.length() - pos) - 1;
           value = Integer.parseInt(value.substring(0, pos) + value.subString(pos + 1));
    }
 
    Money add(Money other)
    {
        if (this.places == other.places) {
            return new Money(this.value + other.value, this.places);
        } else {
            // throw error or add places to one value
        }
    }
}

Flat View: This topic has 4 replies on 1 page
Topic: ending a loop with input? Previous Topic   Next Topic Topic: Switch Statement

Sponsored Links



Google
  Web Artima.com   

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