Last active
January 5, 2017 08:32
-
-
Save dharmakshetri/b3feb881f1e52804e7d4d93dc02e78ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
@BindView(R.id.editTextHour) | |
EditText editTextHour; | |
@BindView(R.id.editTextMinute) | |
EditText editTextMinute; | |
@BindView(R.id.textViewOutput) | |
TextView textViewOutput; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ButterKnife.bind(this); | |
} | |
@OnClick(R.id.buttonFindAngle) | |
public void findAngle(View view){ | |
String strHour=editTextHour.getText().toString().trim(); | |
String strMinute=editTextMinute.getText().toString().trim(); | |
double hour; | |
double min; | |
if(TextUtils.isEmpty(strHour)){ | |
editTextHour.setError("Please enter hour"); | |
return; | |
}else{ | |
hour=Double.parseDouble(editTextHour.getText().toString().trim()); | |
if(hour <0 || hour >12 ){ | |
editTextHour.setError("Hour must be between 1 to 12"); | |
return; | |
} | |
} | |
if(TextUtils.isEmpty(strMinute)){ | |
editTextMinute.setError("Please enter Minutes"); | |
return; | |
}else{ | |
min=Double.parseDouble(editTextMinute.getText().toString().trim()); | |
if(min <0 || min >60 ){ | |
editTextMinute.setError("Minute must be between 1 to 60"); | |
return; | |
} | |
} | |
int angle=calculateAngle(hour, min); | |
textViewOutput.setText("Angle between hour and minute: "+String.valueOf(angle)); | |
} | |
// method for calculating the angle between hour and minute | |
private int calculateAngle(double hour, double minute) { | |
//base condition | |
if(hour==12) hour=0; | |
if(minute==60) minute=0; | |
//Based on 12 hour refrence, then calculate the angle | |
double hourAngle=0.5*(60*hour+minute); | |
double minuteAngle=6*minute; | |
//find the angle between two angle | |
int diffAngle=(int)(Math.abs(hourAngle-minuteAngle)); | |
// return the smallest angle between two possible angles | |
return minimumPossibleAngle(360-diffAngle, diffAngle); | |
} | |
// find the minimum angle | |
private int minimumPossibleAngle(int maxAngle, int minAngle) { | |
return (maxAngle<minAngle) ? maxAngle:minAngle; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment