Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Good morning, everyone! I am having a bit of trouble with this Java programming

ID: 3572638 • Letter: G

Question

Good morning, everyone! I am having a bit of trouble with this Java programming assignment due to absences. Is there any way someone could help me out? The assignment is as follows:

First write a method to calculate the greatest common divisor (GCD) of two positive integers using Euclid’s algorithm (also known as the Euclidean algorithm). Then write a main method that requests two positive integers from the user, validates the input, calls your method to compute the GCD, and outputs the return value of the method (all user input and output should be done in main). Check Wikipedia to find more information about GCDs1 and Euclid’s algorithm2.

In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:

function gcd(a, b)

while b 0

t := b

b := a mod b

a := t

return a

Here is an example run of the program:

Enter a: 34

Enter b: 289

The GCD of 34 and 289 is 17

ALSO, the following format and prepared code is provided below:

package edu.wit.cs.comp1000;

public class LA4a {

  

   /**

   * Error to output if either input is not positive

   */

   static final String E_POSITIVE = "Inputs must both be positive.";

   public static void main(String[] args) {

       // TODO: write your code here

      

      

   }

  

   /**

   * Returns the greatest common denominator (gcd) of two positive integers

   *

   * @param num1 integer 1

   * @param num2 integer 2

   * @return gcd of integers 1 and 2

   */

   public static int gcd(int num1, int num2) {

       for (int i=0;i<num1;i++){

          

       }

       return 0; // TODO: replace with your code

   }

}

Thank you everyone so much in advance! :)

Explanation / Answer

package edu.wit.cs.comp1000;

import java.util.Scanner;

public class LA4a {

   /**
   * Error to output if either input is not positive
   */
   static final String E_POSITIVE = "Inputs must both be positive.";

   public static void main(String[] args) {
       // TODO: write your code here
       Scanner scanner = null;
       try {
           scanner = new Scanner(System.in);
           System.out.print("Enter a: ");
           int a = scanner.nextInt();

           System.out.print("Enter b: ");
           int b = scanner.nextInt();

           if (a > 0 && b > 0)
               System.out.println("The GCD of " + a + " and " + b + " is "
                       + gcd(a, b));
           else
               System.out.println(E_POSITIVE);

       } catch (Exception e) {
           // TODO: handle exception
       }

   }

   /**
   * Returns the greatest common denominator (gcd) of two positive integers
   *
   * @param num1
   * integer 1
   * @param num2
   * integer 2
   * @return gcd of integers 1 and 2
   */
   public static int gcd(int num1, int num2) {

       while (num2 != 0) {

           int temp = num2;
           num2 = num1 % num2;
           num1 = temp;
       }

       return num1; // TODO: replace with your code
   }

}

OUTPUT:

Enter a: 34
Enter b: 289
The GCD of 34 and 289 is 17

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote