Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8782207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:30:53+00:00 2026-06-13T20:30:53+00:00

I’m quite stuck from a problem in the liang introduction to Java book –

  • 0

I’m quite stuck from a problem in the liang introduction to Java book – Here’s the problem:

“Sixteen coins are placed in a 4-by-4 matrix with some face up and some face down. You can represent the state of the coins using a 4-by-4 matrix with values 0 (head) and 1 (tail). Here are some examples:

0000
0100
0000
0010

each state can also be repesented using a binary number (a concatenation of the above). Write a program that prompts the user to enter a number between 0 and 65,536 and displays the corresponding matrix – a balue of 7 would give 00000111.

My question is how would you verify that this was the case for 7 ? I’m not even that sure how to begin prototyping such a method – perhaps int x = (int)Math.rand(1) and assign to each of the rows/cols?

Perhaps someone could give me some hints on where to start?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:30:54+00:00Added an answer on June 13, 2026 at 8:30 pm

    The problem is essentially converting an integer in the range [0, 65536] to its binary representation. This can be done with Integer.toBinaryString. For instance,

    Integer.toBinaryString(7);
    

    would result in the string 111. Now, we want the total length of the string to be 16, so we need to pad with 0s on the left to obtain 0000000000000111. We can do something like:

    String.format("%16s", Integer.toBinaryString(7)).replace(' ', '0');
    

    which results in the string 0000000000000111.

    Once you have this, you can simply copy it into a 4×4 int (or boolean) array. Or, you can just print the data right off the bat,

    String bin = String.format("%16s", Integer.toBinaryString(7)).replace(' ', '0');
    
    for (int i = 0 ; i < 4 ; i++)
        System.out.println(bin.substring(4 * i, 4 * (i + 1)));
    

    Output:

    0000   
    0000  
    0000  
    0111
    

    Of course, you can replace that 7 with whatever integer you read form the user, but the idea is the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.