Programming Challenge. Write a program using Java, that solves the following pro
ID: 3760690 • Letter: P
Question
Programming Challenge.
Write a program using Java, that solves the following problem:
A binary clock is a clock which displays sexagesimal time (military format) in a
binary format. There are also two kinds of representations for a binary clock;
vertical or horizontal.
Below is the horizontal representation of the time 10:37:49
173
(H) 0 0 1 0 1 0 (10)
(M) 1 0 0 1 0 1 (37)
(S) 1 1 0 0 0 1 (49)
so when it is grouped together as a binary string, it looks like this:
001010 100101 110001
Find the total number of 1s in the representations of military time for a full 24 hour
period which elapses from 00:00:00 to 23:59:59. How many total 1s appear for the
entire year (a regular 365 day year)?
Explanation / Answer
Answer :
class Binaryclock
{
public static void main(String args[])
{
int s_hour=0, s_min=0, s_sec=0;
for(int i=0;i<24;i++)
s_hour+=binary_counting(i); //sum for 24h er a day
for(int i=0;i<60;i++)
{
s_min+=binary_counting(i); //sum for 60 min per hour
s_sec+=binary_counting(i); //sum for 60 sec per min
}
int day = s_hour + 24 * s_min + 1440 * s_sec; // make the sum for a day
System.out.println("The total number of 1s in the representations of military
time for a full 24 hour period is "+day);
System.out.println("The total number of 1s appear for the entire year is "+(day*365)); //
year
}
static int binary_counting(int x)
{
int c=0, count = 0;
int a[] = new int[100];
while(x!=0)
{
a[c]=x%2;
c++;
x=x/2;
}
for(int i=0;i<=c-1;i++)
if(a[c-1-i] == 1) count++;
return count;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.