View previous topic :: View next topic |
Author |
Message |
superfirepig Master Cheater
Reputation: 0
Joined: 07 Oct 2006 Posts: 313 Location: city of hackers
|
Posted: Tue Dec 01, 2009 11:11 am Post subject: [Java]Help Needed |
|
|
Code: | public class BCD {
private int[] digits;
BCD(int num){
digits = new int[1];
digits[0] = num % 10;
num = num / 10;
while ( num > 0 ){
addADigit( num % 10 );
num = num / 10;
}
}
//-------------------------------------------------
public BCD addBCDs(BCD other){
int num = 0;
for(int i = 0;i < this.numberOfDigits()-1 && i
< other.numberOfDigits()-1 ; i++){
num = 0;
num = (this.nthDigit(i)+other.nthDigit(i))/10 + num;
digits[i+1] = (this.nthDigit(i+1)+other.nthDigit(i+1))%10 + num;
}
digits[0] = (this.nthDigit(0)+other.nthDigit(0))%10;
if(num!=0)
addADigit(num);
for(int i = 0;i < this.numberOfDigits() - 1;i++){
other = other + nthDigit(i);
}
return other;
}
//-------------------------------------------------
public int numberOfDigits(){
int number = digits.length;
return number;
}
//-------------------------------------------------
public int nthDigit(int n) {
int zero = 0;
if (n >= numberOfDigits()){
return zero;
}else{
n = digits[n];
return n;
}
}
//-------------------------------------------------
public String toString(){
String bcd = "";
for(int i = digits.length - 1 ; i >= 0 ; i--){
bcd = bcd + digits[i];
if (i%3 == 0 && i != 0){
bcd = bcd + ",";
}
}
return bcd;
}
//-------------------------------------------------
public void addADigit(int newdigit){
int[] added = new int[digits.length+1];
for(int i = 0; i < digits.length; i++){
added[i] = digits[i];
}
added[added.length - 1] = newdigit;
digits = added;
}
//-------------------------------------------------
public static void main(String[] args){
BCD test1 = new BCD(0);
BCD test2 = new BCD(0);
System.out.println(test2.addBCDs(test1));
BCD test3 = new BCD(0);
BCD test4 = new BCD(1);
System.out.println(test3.addBCDs(test4));
BCD test5 = new BCD(0);
BCD test6 = new BCD(1);
System.out.println(test6.addBCDs(test5));
BCD test7 = new BCD(0);
BCD test8 = new BCD(11);
System.out.println(test7.addBCDs(test8));
BCD test9 = new BCD(0);
BCD test10 = new BCD(11);
System.out.println(test10.addBCDs(test9));
BCD test11 = new BCD(999);
BCD test12 = new BCD(8888);
System.out.println(test11.addBCDs(test12));
BCD test13 = new BCD(999);
BCD test14 = new BCD(8888);
System.out.println(test14.addBCDs(test13));
BCD test15 = new BCD(777777);
BCD test16 = new BCD(888888);
System.out.println(test15.addBCDs(test16));
}
}
|
Here is what i have so far... basically, i am trying to add 2 numbers. And I have to use the line below to program.
My questions are these:
Code: | public BCD addBCDs(BCD other){ |
1.Is the return type for this line a reference, object, or something else?
2.How do i set up a return statement that will work with my code? I am trying to have the addBCDs to return the result from the two added value.
Thanks
_________________
You don't have enough MP to f#@% me, please try again later |
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Tue Dec 01, 2009 12:10 pm Post subject: |
|
|
Code: | class BCD {
private int[] digits;
BCD( int num ) {
digits = new int[ ( int )Math.ceil ( Math.log10( num + 1 ) ) ];
for( int i = 0; i < digits.length; i++ )
digits[ i ] = ( int )( num / ( 1 * Math.pow( 10, i ) ) ) % 10;
}
public void display() {
for( int i = digits.length; i > 0; i-- )
System.out.print( digits[ i - 1 ] );
}
private int convert() {
int converted = 0;
for( int i = 0; i < digits.length; i++ )
converted += digits[ i ] * Math.pow( 10, i );
return converted;
}
public BCD addBCD( BCD num ) {
return new BCD( this.convert() + num.convert() );
}
}
public class Assignment {
public static void main( String[] args ) {
BCD x = new BCD( 1273 );
BCD y = new BCD( 83749 );
BCD newNum = x.addBCD( y );
newNum.display();
}
} |
i'm incredibly bored..
|
|
Back to top |
|
 |
Fendaril Cheater
Reputation: 0
Joined: 08 Nov 2009 Posts: 27
|
Posted: Wed Dec 02, 2009 5:12 am Post subject: Re: [Java]Help Needed |
|
|
DELETED*
Last edited by Fendaril on Wed Dec 02, 2009 3:19 pm; edited 1 time in total |
|
Back to top |
|
 |
superfirepig Master Cheater
Reputation: 0
Joined: 07 Oct 2006 Posts: 313 Location: city of hackers
|
Posted: Wed Dec 02, 2009 7:51 am Post subject: Re: [Java]Help Needed |
|
|
I wrote the code... for my computer science class, I was absent on the day when my teacher present the instance method. I had to learn everything myself...
It is kind of rude for you to coming in and say the code is not mine...
_________________
You don't have enough MP to f#@% me, please try again later |
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Dec 02, 2009 8:57 am Post subject: Re: [Java]Help Needed |
|
|
Fendaril wrote: | Where did you steal this code from. Who wrote it for you? | I gotta agree with superfirepig that he's probably written his code by himself. I'd say that, because it's overcomplicated and quite hard to read -> obliviously written by someone who is learning how to code.
Fendaril wrote: | Anyone who does not know what public BCD addBCDs returns clearly did not write the code. | I bet you don't know what's the difference between references, pointers and objects(values). His question is totally valid. Being a C++ coder, it's confusing me too.
superfirepig wrote: | 1.Is the return type for this line a reference, object, or something else? | I'm not 100 sure about Java, but I'd guess they are references. Easy way to test out: Store the returned reference and compare it to the original, eg. Code: | BCD a = new BCD(111);
BCD b = new BCD(222);
BCD c = a.addBCDs(b);
System.out.println("They are the same");
if( b != c )
System.out.println(" ...not"); | This works as long as the comparison operator actually compares the memory addresses, not eg. content. If the comparison operator is overloaded and it compares the contents, you could modify the other and see if it affects the other one: Code: | BCD a = new BCD(111);
BCD b = new BCD(222);
BCD c = a.addBCDs(b);
c.setBCD(123);
System.out.println("They are the same");
if( b != c )
System.out.println(" ...not"); |
superfirepig wrote: | 2.How do i set up a return statement that will work with my code? I am trying to have the addBCDs to return the result from the two added value. | Write a proper addition method and just return the resulting object. Your code is a bit confusing and I'm not as bored as Slugsnack, so I'm not going to put any effort into reading it :P
|
|
Back to top |
|
 |
superfirepig Master Cheater
Reputation: 0
Joined: 07 Oct 2006 Posts: 313 Location: city of hackers
|
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Dec 02, 2009 8:03 pm Post subject: |
|
|
Hehe.. :D
Jani wrote: | Being a C++ coder, it's confusing me too. | Eh.. That is pointerless Java. Not pointers, etc in C++ :)
superfirepig wrote: | what gets printed out when i do | Err, why don't you try yourself?
|
|
Back to top |
|
 |
|