Advertisement
BOT_Yokel

ICS3U1 Unit 2 Activity 4: Question 4

Jul 21st, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Date: July 21, 2017
  2. // Purpose: To reduce a given fraction by finding the greatest common denominator (GCD or GCF)
  3.  
  4. // This line makes the button, btnCalculate wait for a mouse click
  5. // When the button is clicked, the calculateReduced function is called
  6. btnCalculate.addEventListener(MouseEvent.CLICK, calculateReduced);
  7.  
  8. // These lines make the TextInput wait for a mouse click
  9. // When any of these components are clicked, the clearLabels function is called
  10. txtinNumerator.addEventListener(MouseEvent.CLICK, clearLabels);
  11. txtinDenominator.addEventListener(MouseEvent.CLICK, clearLabels);
  12.  
  13. // Declare Global Variables
  14. var Numerator:Number;     // The numerator of the fraction
  15. var Denominator:Number;  // The denominator of the fraction
  16. var Factor:Number; // The greatest common denominator of the numerator and the denominator
  17.    
  18. // This is the reduce function
  19. // e:MouseEvent is the click event experienced by the button
  20. // void indicates that the function does not return a value
  21. function calculateReduced(e:MouseEvent):void
  22. {  
  23.     getData();
  24.     lblResult.text = Numerator + "/" + Denominator + " can be reduced to " + (Numerator / gcd(Numerator,Denominator)) + "/" + (Denominator / gcd(Numerator,Denominator))
  25. }
  26.  
  27. // This is function gcd
  28. // It uses the Eculidian Algorithm for calculating the GCD of two numbers
  29. function gcd(a:Number,b:Number):Number
  30. {
  31.     var tmp:Number;
  32.     //Swap the numbers so a >= b
  33.     if(a < b)
  34.     {
  35.         tmp = a;
  36.         a = b;
  37.         b = tmp;
  38.     }
  39.     //Find the gcd
  40.     while(b != 0)
  41.     {
  42.         tmp = a % b;
  43.         a = b;
  44.         b = tmp;
  45.     }
  46.     return a;
  47. }
  48.  
  49. // This is the getData function
  50. // It retrieves the numerator and the denominator from their respective TextInput boxes
  51. function getData()
  52. {
  53.     Numerator = Number(txtinNumerator.text);
  54.     Denominator = Number(txtinDenominator.text);
  55. }
  56.  
  57. // This is the clearLabels function
  58. // e:MouseEvent is the click event experienced by the textInput
  59. // void indicates that the function does not return a value
  60. function clearLabels(e:MouseEvent):void
  61. {
  62.      lblResult.text = "";
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement