Saturday, July 8, 2023

Interview preparation series: Write a Java Program to calculate the angle formed between the hour and the minute arms in an analog clock

In this post, I am going to solve this programming puzzle following a simple approach. If you have any better approach in mind, please mention in comment. If you are liking my posts, then please share it with your friends.

Steps to solve the problem during interview or online test:

1. Patiently analyse the problem statement for 2-3 minutes, conceptualize the expected calculations. In our case, we know that analog clocks have overall 360 degree angle. The hour hand moves 30 degrees between each hour. The minute arm moves 6 degrees between each minutes. The hour hand also moves 0.5 degrees each minute. On 60th minute, the hour arm moves to a new hour, and the meeting arm gets reset to zero. When hour ==12, then there is no angle formed between hour and minute arms. 

2. Use, any of your favorite Java IDE. (Prerequisites: Java 6 or above installed, Set JAVA_HOME)

3. Create a Java class file, name it following the best practices

4. Create the main method

5. It's always a good practice to take inputs from the user, rather than using hard-coded input values. This creates a positive impact in the interviewers mind.

6. Write the logic in simplistic way possible

7. Ensure you are displaying the expected output

8. Do a proof read, before executing for the first time, if the program fails at first run, it may create a negative impact in the interviewers mind

9. Run the program against different inputs to proof the logic is correct. For online tests, upon the code submission it will auto trigger unit tests, and highlight any failures. You can fix the issues before the test gets timeout.

public class CalculateHourAndMinuteArmAnglesInAnAnalogClock {
public static void main(String[] args) {
/**
* Taking two inputs from the user at runtime
* hour: Represents the hour arm value of an analog clock
* minute: Represents the minute arm value of an analog clock
*/
int hour = new Scanner(System.in).nextInt();
int minute = new Scanner(System.in).nextInt();
//Declaring a local variable which will hold the final value of the
// computed angle
double angle;
//We need to ensure that the user has entered correct data, otherwise
// we will
// throw a RuntimeException
if (hour > 0 && hour <=12 && minute >= 0 && minute<=60) {
//if user enters minutes=60, then we will add 1 to the hour value,
// and reset minute to 0
if(minute==60 && hour<12) {
hour += 1;
minute=0;
}
//if hour=12, then we need to set hour = 0, and compute the
// angle between 12 and the minute value
if(hour==12) {
hour = 0;
angle = Math.abs(hour - (minute * 6));
}//Otherwise, we will calculate the angle as follows
else {
angle = Math.abs((60 * hour) * 0.5 - (minute * 6));
}
System.out.println("hour = " + hour);
System.out.println("minute = "+minute);
System.out.println("Angle between the hour and the " +
"minute arms of an analog clock is: " + (int) angle
+ " degrees");
}else{
throw new RuntimeException("Invalid inputs, hour should be" +
" >=1 and <=12, and minute should be >=0 and <=60");
}
}
}

No comments:

Post a Comment

Test Automation Strategy for Oracle Forms application running in Citrix servers

  Context : There are many product based applications developed using Oracle Forms and Java thick-client architecture, and most of them are ...