Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. public class MathUtils {
  2. public static Answer greatestCommonDivisor(double one, double two) {
  3. double three = 0;
  4. int steps = 0;
  5. //with while we can actually assign and compare in one step which greatly simplifies things
  6. while ((three = one % two) != 0) {
  7. /* variable reassignment occurs here. While three does not equal 0 One is assigned the value of Two,
  8. * Two is assigned the value of Three then modulo occurs again to see if Three equals 0
  9. */
  10. one = two;
  11. two = three;
  12. steps++;
  13. }
  14. return new MathUtils.Answer(two, steps);
  15. }
  16.  
  17. //create an inner class so we can return both Answer and amount of steps cleanly
  18. static class Answer {
  19. private double answer=-1;
  20. private int steps=-1;
  21.  
  22. Answer(double answer, int steps) {
  23. this.answer = answer;
  24. this.steps = steps;
  25. }
  26.  
  27. public double getAnswer() {
  28. return answer;
  29. }
  30. public void setAnswer(double answer) {
  31. this.answer = answer;
  32. }
  33. public int getSteps() {
  34. return steps;
  35. }
  36. public void setSteps(int steps) {
  37. this.steps = steps;
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement