Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 9 Coins: Nine coins are placed to form a 3-by-3 matrix as in the example below. HTT Some coins are represented using H's (heads)
1 9 Coins: Nine coins are placed to form a 3-by-3 matrix as in the example below. HTT Some coins are represented using H's (heads) THH HTH while the others are represented using T's (tails). The state of these 9 coins can be represented using a binary number with a 0 for each T and a 1 for each H. Example: 100011101 is the binary number that represents the example matrix given above. There are 512 different binary numbers with 9-digits which means that there are 512 different states when placing 9 coins to form a 3-by-3 matrix. Write a program that prompts the user to enter a number between 0 and 511, then creates and displays the corresponding matrix to the user-entered number with the characters Hand T as in the example console input and output below. Note: Write a method that converts a decimal number given as an input argument to a binary string with the given number of digits as an input argument and returns the resulting string. Use this method in your program to convert the user-entered number to a binary string and use the binary string for creating the matrix with H's and T's. The header of the method should be public static String decimaltoBinary(int decimalNumber, int numDigits). Decimal to Binary Conversion Binary string Example Console Input and Output Initially Decimal number: 12 and Binary string: (empty string) "000001100" 12 %2 = 0 Binary string: "0" and 12 /2 = 6 Enter a number between 0 and 511: 12 Coins matrix TTI 6%2 = 0 Binary string: "00" and 6 / 2 = 3 3%2 = 1 Binary string: "100" and 3/2 = 1 (integer division) ITH TTT 1% 2 = 1 Binary string: "1100" and 1/2 = 0 (loop ending condition) HII TTH Completion to 9 digits Binary string: "000001100" HTT Example
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started