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 withvalues 0 (heads) and 1 (tails). Here are some examples:00 0 1 0 1 1 100 10 0 0 1 10000010000 1Each state can also be represented using a binary number. For example, the preceding matrices correspond to the numbers:000010000 101001100 110100001There 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.SAMPLE RUN #1: java HeadsAndTailsStandard OutputHide InvisiblesEnter.an integer representing.the.state.of.the.coins:H.T.TT T.T

Question
Asked Mar 11, 2019
546 views

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. 

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:
00 0 1 0 1 1 10
0 10 0 0 1 100
00010000 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.
SAMPLE RUN #1: java HeadsAndTails
Standard Output
Hide Invisibles
Enter.an integer representing.the.state.of.the.coins:H.T.T
T T.T
help_outline

Image Transcriptionclose

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: 00 0 1 0 1 1 10 0 10 0 0 1 100 00010000 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. SAMPLE RUN #1: java HeadsAndTails Standard Output Hide Invisibles Enter.an integer representing.the.state.of.the.coins:H.T.T T T.T

fullscreen
check_circle

Expert Answer

Step 1

As there is no programming language mentioned, we are writing this program in JAVA. The program is given below:

Step 2

import java.util.Scanner;

public class Headtail

{

public static void main(String[] args)

{

    Scanner ht = new Scanner(System.in);

    System.out.print("Enter number between 0 and 511: ");

    int input = ht.nextInt();

String binary = Integer.toBinaryString(input); // Turning the input to binary string

binary = binary.format("%09d", Integer.parseInt(binary)); // Adding zeros in front so that the string has 9 characters

    for (int i = 1; i < 10; i++) // Iterate one char at a time through binary string

    {

        if ('0' == binary.charAt(i - 1))

        {

            System.out.print("H ");

        }

        else

        {

            System.out.print("T ");

        }

        if (i % 3 == 0) // New line after 3 letters

        {

            System.out.println();

        }

    }

}

}

Step 3

In the above java program, a class is created with the name Headtail and the number is inputted through the user. Then the given input is converted into the binary string using the function Integer.toBinaryStrin...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: Develop pseudocode to read in the radius of a sphere, calculate the volume of the sphere (using the ...

A: Pseudocode is an informal approach of programming description that does not need any exact programmi...

question_answer

Q: Consider the following procedure: procedure mystery (x, y, z); integer x, y, z;beginz = 2; y= z+x;en...

A: Pass by value copies the value of argument and passes it to the parameter. Hence, the final value of...

question_answer

Q: Function Name: leapYear Parameters: year – an nonnegative integer representing the year Test Cases: ...

A: Click to see the answer

question_answer

Q: Create a list of 3 technologies that have gone from analog to digital. What was the reasoning for e...

A: Back in 1970s and earlier main technology in use was analog technology. Analog technology used vario...

question_answer

Q: Explain the following error message. $ lsabc abd abe abf abg abh$ rm abc ab*rm: cannot remove ‘abc’ ...

A: “rm” command: This command is used to removes each specified file on the command line.For example, i...

question_answer

Q: What is notepad ++ or notebad ? How to use it and what for ?It's required for my class The Web Stand...

A: We are explaining here the concept of notepad and notepad ++ and how and for what purpose these are ...

question_answer

Q: Use Draw.IO https://www.draw.io/ to create an ERD based on the following information:• The RDBMS wou...

A: Below is the design of the Employees entity:- 

question_answer

Q: A common problem in health informatics is to read a message from another healthcare organization and...

A: In order to solve the problem we would follow these steps:Break the entire string into tokens separa...

question_answer

Q: Visual Basic. Write the definition of a function named quadratic that receives three double paramete...

A: Formula for quadratic equation: