Originally Posted by

**Proletariat**
...

For some reason that line is not adding the fractional part of the number...

Well, yes it is. The fractional part is an integer that represents 50ths of a unit in the given number system, and that's what gets added here

Why not put in a print statement to show what is happening in the add() method:

public static Assignment6 add(Assignment6 lhs, Assignment6 rhs){
int integer = (lhs.value >> 6) + (rhs.value >> 6);
int fraction = (lhs.value & 63) + (rhs.value & 63);
if(fraction > 49){
integer++;
fraction -= 50;
}
System.out.printf("in add: integer = %d, fraction = %d\n",
integer, fraction);
.
.
.

With your values of number1 and number2 you should see something like

in add: integer = 3, fraction = 7

In Human Language (English version), the result is "Three and seven fiftieths."

When you create an object in your number system, the integer part will be shifted to the left six bits, and the fraction part (representing 50ths of a unit) will be in the lower six bits.

There are two possible constructors that you are given that will allow you to create an object with that value.

Here is one:

public Assignment6(int integral, float decimal)

So, you can convert the "fraction" value in the add() method to a floating point decimal fraction (divide by 50.0f) and use that result as the second argument in the constructor shown above.

Or...

You could use the constructor

public Assignment6(float value)

How would you do this? Well you could create a floating point value equal to the "integer" value plus a decimal floating point fraction (divide "fraction" by 50.0f) and use that sum in this constructor. I think I like the first one better. (But: Chacun à son goût!)

By the way: Didn't you notice that the values printed by your toString method were incorrect?

What did it print for your number1? What should it have printed? How about number2?

Your method does not print leading zeros in the fractional part. So, an object that represents 1.08 gets printed as 1.8. An object that represents 2.06 gets printed as 2.6.

I mean, if I can't print the numbers correctly

*before* adding, how the heck can I know if the

*add()* method works, since I won't have any confidence in being able to print its result?

Here's a way: In the

*toString()* method, use

*String.format()* to get leading zeros (if there are any) to show up in the fractional part of the result:

// The decimal part of the number is obtained by shifting
// the number six bits to the right.
//
// How can we get the fractional part of the number into the string?
//
// Well...
//
// "value & 63" represents 50ths of a unit, so multiplying
// "value & 63" by 2 gives the (integer) decimal value in 100ths of a unit.
// To make sure that leading zeros are part of the string, use
// %02d as the format specifier:
//return (value >> 6) + "." + String.format("%02d",((value & 63) * 2));
// Heck, as long as we are using the String.format() method, why
// not use it on the whole enchilada:
return String.format("%d.%02d", (value >> 6),((value & 63) * 2));

Cheers!

Z