• July 13th, 2012, 08:47 PM
Proletariat
This code my look similar to another user's that posted earlier, because we are in the same class and our teacher helped us get started. I am trying to add two numbers. In this case, 1.07 and 2.07. My code currently returns 3.0. So, the 2 and 1 are adding correctly, but the .07's are not adding. Also, the accuracy should be no more than 2 cm. So 2.02 is fine, 2.04 is fine, but 2.03 isn't. What am I doing wrong?

Code :

```public class Assignment6 {   int value;   public Assignment6(int value){ this.value = value << 6;   }   public Assignment6(int integral, float decimal){ decimal += 0.01f; this.value = (integral << 6) + (int)(decimal % 1 * 50);     }   public Assignment6(float value){ value += 0.01f; this.value = ((int)value << 6) + (int)(value % 1 * 50);     }   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; } return new Assignment6(integer, fraction); }   public String toString(){ return (value >> 6) + "." + ((value & 63) * 2); }   public static void main(String[] args){ Assignment6 number1 = new Assignment6(1, .07f); Assignment6 number2 = new Assignment6(2.07f); Assignment6 number3 = Assignment6.add(number1, number2);   System.out.println(number1); System.out.println(number2); System.out.println(number3); } }```
• July 13th, 2012, 10:50 PM
aesguitar
Is there specific types you should be using? If not, then it shouldn't be to hard to use an 3 Integer variables and a String variable to get your answer.
• July 13th, 2012, 10:58 PM
Proletariat
We are only allowed to have one field, the int field that I've already created. Our professor was helping us out with the assignment right before class ended, and so I didn't get down exactly what he wrote. This line : int integer = (lhs.value >> 6) + (rhs.value >> 6); seems to be working as intended.

I believe that I need to modify this line: int fraction = (lhs.value & 63) + (rhs.value & 63);

For some reason that line is not adding the fractional part of the number, and it's confusing me. I can't really modify anything that I already have, because it's what he wants. I need to do something with the add method.
• July 14th, 2012, 10:00 AM
Zaphod_b
Quote:

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:
Code java:

``` 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
Code :

`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:
Code java:

` 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
Code java:

` 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:

Code java:

``` // 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