0

This is the question we were assigned :

Nine coins are placed in a 3x3 matrix with some face up and some face down. You can represent the state of the coins using a 3x3 matrix with values 0 (heads) and 1 (tails). Here are some examples:

0 0 0 1 0 1 1 1 0 0 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1

Each state can also be represented using a binary number. For example, the preceding matrices correspond to the numbers:

000010000 101001100 110100001

There are a total of 512 possibilities, so you can use decimal numbers 0, 1, 2, 3,...,511 to represent all the states of the matrix.

Write a program that prompts the user to enter a number between 0 and 511 and displays the corresponding matrix with the characters H and T.

I want the method toBinary() to fill the array binaryNumbers. I realized that this does not fill in 0s to the left. I have to think that through but is that the only thing that is the problem?

//https://www.geeksforgeeks.org/java-program-for-decimal-to-binary-conversion/ 
import java.util.Scanner; 
public class HeadsAndTails {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in); 
    int num = input.nextInt(); 
    int[] binaryNumbers = toBinary(num);

    for (int i = 0; i < 9; i++) {

    printArr(binaryNumbers); 
    System.out.print(binaryNumbers[1]); 
}
}

public static int[] toBinary(int inputtedNumber) { 

    int[] binaryNum = new int[9]; 
    int i = 0; 

    while (inputtedNumber > 0) { 

        binaryNum[i] = inputtedNumber % 2; 
        inputtedNumber = inputtedNumber/2; 
        inputtedNumber++; 

    } return binaryNum; 

} 
public static void printArr(int[] arr) { 

    for (int i = 0; i < 9; i++) {

        if (arr[i] == 0) { 

            System.out.print("H "); 

        } else { 

            System.out.print("T "); 

        }

        if (arr[i+1] % 3 == 0) {

            System.out.println(); 

        } System.out.print(arr[i]);

    } 

}
}
2
  • 2
    Inside the while loop of the toBinary function i is not changed. Commented Sep 16, 2018 at 19:01
  • I didn't run your code, but seems you print the identical result 9 times. for (int i = 0; i < 9; i++) { printArr(binaryNumbers); ... } Commented Sep 16, 2018 at 19:12

2 Answers 2

4

Looks like you are incrementing the wrong variable in your while loop:

while (inputtedNumber > 0) { 

    binaryNum[i] = inputtedNumber % 2; 
    inputtedNumber = inputtedNumber/2; 
    i++; // NOT inputtedNumber

} return binaryNum;

Also note, a new int[9] is probably already initialized to 0, but if not, you could just loop 9 times, rather than until the inputtedNumber is 0:

for (int i = 0; i < 9; i++) { 
    binaryNum[i] = inputtedNumber % 2; 
    inputtedNumber = inputtedNumber/2; 
} 
return binaryNum;

Finally, I think your array might be backwards when you're done, so you may need to reverse it or output it in reverse order

Sign up to request clarification or add additional context in comments.

Comments

0

I realize this is a homework assignment so you should stick with your current approach. However, sometimes it can be fun to see what can be achieved using the built in features of Java.

The Integer class has a method toBinaryString that's a good starting point:

    int n = 23;
    String s1 = Integer.toBinaryString(n);      
    System.out.println(s1);

Output: 10111

But as we can see, this omits leading 0s. We can get these back by making sure our number has a significant digit in the 10th place, using a little bit-twiddling:

    String s2 = Integer.toBinaryString(1<<9 | n);       
    System.out.println(s2);

Output: 1000010111

But now we have a leading 1 that we don't want. We'll strip this off using String.substring, and while we're at it we'll use String.replace to replace 0 with H and 1 with T:

    String s3 = Integer.toBinaryString(1<<9 | n).substring(1).replace('0','H').replace('1','T');
    System.out.println(s3);

Output: HHHHTHTTT

Now we can print this string in matrix form, again using substring to extract each line and replaceAll to insert the desired spaces:

    for(int i=0; i<9; i+=3)
        System.out.println(s3.substring(i, i+3).replaceAll("", " ").trim());

Output:

H H H
H T H
T T T

If we're up for a bit of regex wizardry (found here and here) we can do even better:

    for(String sl : s3.split("(?<=\\G.{3})"))
        System.out.println(sl.replaceAll(".(?=.)", "$0 "));

Putting it all together we get:

    int n = 23;
    String s3 = Integer.toBinaryString(1<<9 | n).substring(1).replace('0','H').replace('1','T');
    for(String s : s3.split("(?<=\\G.{3})"))
        System.out.println(s.replaceAll(".(?=.)", "$0 "));

1 Comment

Interesting... can you explain the Integer.toBinaryString(1<<9 | n); syntax? Knowing about the .replace() would have been really useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.