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

given GCFAlgorithm.java public class GCFAlgorithm{ public static int gcf1(int a,

ID: 3876355 • Letter: G

Question

given

GCFAlgorithm.java

public class GCFAlgorithm{

public static int gcf1(int a, int b){
if(Math.abs(a)==Math.abs(b)) return Math.abs(a);
if(a*b==0) return Math.abs(a+b);
return gcf1(a %b, b%a);
}
public static int gcf2(int a, int b){
a=Math.abs(a);
b=Math.abs(b);
int tmp=a;
if(a==b) return a;
if(a * b==0) return a+b;
while(a*b !=0){
tmp=a;
a =a %b;
b = b % tmp;
}
return a+b;
}
public static int gcf3(int a, int b){
a=Math.abs(a);
b=Math.abs(b);
int tmp=a;
if(a==b) return a;
if(a * b==0) return a+b;
while(a*b !=0){
if(a>b) a=a-b;
else b=b-a;
}
return a+b;
}
}

Explanation / Answer

Keep the file name as GCFAlgorithm.java and run the code.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class GCFAlgorithm{
   public static int gcf1(int a, int b){
       if(Math.abs(a)==Math.abs(b)) return Math.abs(a);
       if(a*b==0) return Math.abs(a+b);
       return gcf1(a %b, b%a);
   }
   public static int gcf2(int a, int b){
       a=Math.abs(a);
       b=Math.abs(b);
       int tmp=a;
       if(a==b) return a;
       if(a * b==0) return a+b;
       while(a*b !=0){
           tmp=a;
           a =a %b;
           b = b % tmp;
       }
       return a+b;
   }
   public static int gcf3(int a, int b){
       a=Math.abs(a);
       b=Math.abs(b);
       int tmp=a;
       if(a==b) return a;
       if(a * b==0) return a+b;
       while(a*b !=0){
           if(a>b) a=a-b;
           else b=b-a;
       }
       return a+b;
   }


   public static void main(String agr[]) throws IOException{
       int x,y,z;
       char c;
       int a=2,b=6;//change the value to pass as argument in gcf1/gcf2/gcf3
       File fout = new File("d:/out.txt");
       BufferedWriter bw=null;
       Scanner sc = new Scanner(System.in);
       /*Open file*/
       FileOutputStream fos = new FileOutputStream(fout);
       bw = new BufferedWriter(new OutputStreamWriter(fos));
       bw.write(" -------------------------------------------");
       bw.newLine();
       bw.write("| a, b | gcf1 | gcf2 | gcf3 |");
       bw.newLine();
       bw.write("--------------------------------------------");
       bw.newLine();
      
       do{
          
              
           System.out.println("Enter the value of a");
           a = sc.nextInt();
           System.out.println("Enter the value of b");
           b = sc.nextInt();
      
          
      
          
           /*The performance is calculated by adding the time taken to
           ren the program and the memory space occupied*/
           long startTime=0,elapsedTime=0,stopTime=0,memory=0;//temp variable set to 0
           Runtime runtime = Runtime.getRuntime();//To get the runtime environment
           runtime.gc();//garbage collector
           startTime = System.currentTimeMillis();
           x=gcf1(a,b);
           stopTime = System.currentTimeMillis();
           elapsedTime = stopTime - startTime;
           System.out.println(elapsedTime);
           memory = runtime.totalMemory() - runtime.freeMemory();
           System.out.println("Used memory is bytes: " + memory);
           System.out.println("***********************************");
           long gcf1=memory+elapsedTime;// total performance
          
           startTime=0;elapsedTime=0;stopTime=0;memory=0;
           runtime = Runtime.getRuntime();
           runtime.gc();
           startTime = System.currentTimeMillis();
           y=gcf2(a,b);
           stopTime = System.currentTimeMillis();
           elapsedTime = stopTime - startTime;
           System.out.println(elapsedTime);
           memory = runtime.totalMemory() - runtime.freeMemory();
           System.out.println("Used memory is bytes: " + memory);
           System.out.println("***********************************");
           long gcf2=memory+elapsedTime;
          
           startTime=0;elapsedTime=0;stopTime=0;memory=0;
           runtime = Runtime.getRuntime();
           runtime.gc();//garbage collector
           startTime = System.currentTimeMillis();
           y=gcf3(a,b);
           stopTime = System.currentTimeMillis();
           elapsedTime = stopTime - startTime;
           System.out.println(elapsedTime);
           memory = runtime.totalMemory() - runtime.freeMemory();
           System.out.println("Used memory is bytes: " + memory);
           System.out.println("***********************************");
           long gcf3=memory+elapsedTime;
           bw.write("| "+a+" ,"+ 6 +" | "+gcf1+" | "+gcf2+" | " +gcf3+" |");
           bw.newLine();
           System.out.println("Do you want to continue?");
           c = (char) System.in.read();
           if(c=='n'||c=='N')
               break;
           }while(c=='y'||c=='Y');
      
       bw.close();

      
       //System.out.println(x+" "+y+" "+z);
  
      
   }
}

This program is self explanatory .Provide the path correctly and check the output file

Console output:

Enter the value of a
2
Enter the value of b
5
0
Used memory is bytes: 1180800
***********************************
0
Used memory is bytes: 1179888
***********************************
0
Used memory is bytes: 1179888
***********************************
Do you want to continue?
y
Enter the value of a
5
Enter the value of b
7
0
Used memory is bytes: 1180456
***********************************
0
Used memory is bytes: 1180456
***********************************
0
Used memory is bytes: 1180456
***********************************
Do you want to continue?
y
Enter the value of a
8
Enter the value of b
2
0
Used memory is bytes: 1180456
***********************************
0
Used memory is bytes: 1180456
***********************************
0
Used memory is bytes: 1180456
***********************************
Do you want to continue?

N

Note: Feel free to drop a comment if you have any queires.