Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CO SCI 141 - Programming in Java. ( Game: nine heads and tails ) Nine coins are placed in a 3 3 matrix with some

CO SCI 141 - Programming in Java.

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

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

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

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

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

000010000 101001100 110100001 101110100 100111110

The total number of possibilities is 512. So you can use decimal numbers 0, 1, 2, 3, . . . , and 511 to represent all 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 characters H and T. Here is a sample run:

Enter a number between 0 and 511: 7

H H H

H H H

T T T

The user entered 7, which corresponds to 000000111. Since 0 stands for H and 1 for T, the output is correct.

It basically takes an input integer and converts it to an equivalent binary number. As it is suppose to fit in 9 bits, the input cannot be bigger than 511. Once the binary representation is obtained, zeros are display as H and ones are displayed as T.

Use a one dimensional matrix to store the binary representation. Write a method toBinaryRep(int number) that takes number as input and returns an array of integer (which is the binary representation of the number).

Create a method display(int arr[]) that takes an array of integer as input and prints the contents of the array in a 3X3 format (using Hs and Ts for zeroes and ones).

Include in your sample output for: 256, 32, 4.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Carlos Coronel, Steven Morris

14th Edition

978-0357673034

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago