Java Streams Challenge

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
@Jaylim123 has posted a challenge awhile back which can be found at
https://forums.hardwarezone.com.sg/...y-to-solve-this-programming-question.6858059/
I would call that part 1.
I shall help continue the challenge with Parts 2 and 3.
Feel free to try it out if you are keen.

PART 2 Challenge
Following the constraints listed in the original challenge in Post #1 at
https://forums.hardwarezone.com.sg/...y-to-solve-this-programming-question.6858059/lets change the addition to base-2(binary) working.

Using the same inputs 2048, and 56, produce the binary working for 2048 + 56.

Below is the sample output
Code:
$ java AdditionTutor 2048 56
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
1 + 0     = 1

$ java AdditionTutor 9999 1
1 + 1     = 0 (carry 1)
1 + 0 + 1 = 0 (carry 1)
1 + 0 + 1 = 0 (carry 1)
1 + 0 + 1 = 0 (carry 1)
0 + 0 + 1 = 1
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
1 + 0     = 1
1 + 0     = 1
1 + 0     = 1
0 + 0     = 0
0 + 0     = 0
1 + 0     = 1


PART 3 Challenge

As a successor challenge to PART 2 and maintaining on the constraint on PART 1, lets handle negative inputs too.
We will have the following changes from PART 2 binary challenge.
  • Instead of just printing only the non-zero bits, we will stick with always printing out 16bits width of data. That means the operands can only be of range [-32768,32767]. This is for easy demonstration in the challenge, but if you like to print all the way to 64bits, it shouldn't an issue at all.
  • The inputs should handle both positive and negative operands. Meaning
    If we want A + B, both operands A and B will be positive values.
    If we want A - B, operand A will be positive value and operand B will be a negative value.
    If we want -A + B, operand A will be negative value and operand B will be a positive value.
    If we want -A - B, both operands A and B will be negative values.
  • All negative values will be printed in its 2's complements binary form. (eg: -1 in 8bits -> 11111111, -123 in 8bits -> 10000101)
    Better go flip your textbooks(assuming you still have them), if you don't understand 2's complements binary form (^.^)
Below are the sample output
Code:
$ java AdditionTutor 0 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0

$ java AdditionTutor 1 1
1 + 1     = 0 (carry 1)
0 + 0 + 1 = 1
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0

$ java AdditionTutor 2048 56
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
1 + 0     = 1
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0
0 + 0     = 0

$ java AdditionTutor 0 -1 #ans: -1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1

$ java AdditionTutor 1000 -1123 #ans: -123
0 + 1     = 1
0 + 0     = 0
0 + 1     = 1
1 + 1     = 0 (carry 1)
0 + 1 + 1 = 0 (carry 1)
1 + 0 + 1 = 0 (carry 1)
1 + 0 + 1 = 0 (carry 1)
1 + 1 + 1 = 1 (carry 1)
1 + 1 + 1 = 1 (carry 1)
1 + 1 + 1 = 1 (carry 1)
0 + 0 + 1 = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
0 + 1     = 1
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Strangely no Java developers in this forum?
Is this CS101 even consider a hard question?
:)
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top