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

Please use java in eclipse for the solution if possible. Thank you for any help!

ID: 3727275 • Letter: P

Question

Please use java in eclipse for the solution if possible.   Thank you for any help!

1. Write a code that calculates the Greatest Common Divisor (GCD) of two positive integers (user-defined inputs). Include an exception such that if their greatest common divisor equals to 1, it prints out the message, saying its GCD is 1 2. implement a cueue ciss(for characters) You can use an arryor a lnke list (a) Build a test program that stores the alphabet using your queue. (b) You can use enqueue() or dequeue() methods. In dequeue() method, you need to be able throw an exception where there is nothing to dequeue (e.g., throw new RuntimeException("Queue underflow"):). (c) Limit the size of the queue so that you can only store up to 10 characters. If you enter more than 10 characters, your test program should show an error message.

Explanation / Answer

1) GCD Program:-

public class GCDProgram {

public static void main(String[] args) {

int num1 = 6;

int num2 = 9;

int gcd = 1;

for(int i = 1; i <= num1 && i <= num2; ++i)

{

if(num1 % i==0 && num2 % i==0)

gcd = i;

}

if(gcd == 1) {

System.out.println(" ERROR Message: GCD is 1");

}

  

System.out.printf("GCD of %d and %d is %d", num1, num2, gcd);

}

}

SAMPLE OUTPUT:-  

GCD of 6 and 9 is 3

------------------------------------------------------------------------------------------------------------------------------------

2) Queue Program using Array: -

public class ArrayQ {

private char Queue[];

private int front;

private int rear;

private int size;

/**

* Constructor

*/

public ArrayQ() {

size = 10;

front = -1;

rear = -1;

Queue = new char[size];

}

/**

* inqueue char to queue

*

* @param c

*/

public void enqueue(char c) {

if (rear == -1) {

front = 0;

rear = 0;

Queue[rear] = c;

} else if (rear + 1 >= size)

System.out.println("Right Now Queue is full");

else if (rear + 1 < size)

Queue[++rear] = c;

}

/**

* Dequeue from queue

*

* @return

*/

public char dequeue() {

if (isEmpty())

throw new RuntimeException("Queue Underflow");

else {

char c = Queue[front];

if (front == rear) {

front = -1;

rear = -1;

} else {

front++;

}

return c;

}

}

/**

* Function to check if queue is empty

**/

public boolean isEmpty() {

return front == -1;

}

}

Program to test Queue:-

public class Test {

public static void main(String str[]) {

ArrayQ queue = new ArrayQ();

System.out.println("Adding char : a");

queue.enqueue('a');

System.out.println("Adding char : b");

queue.enqueue('b');

System.out.println("Adding char : c");

queue.enqueue('c');

System.out.println("Adding char : d");

queue.enqueue('d');

System.out.println("Removed char : " + queue.dequeue());

System.out.println("Removed char : " + queue.dequeue());

System.out.println("Removed char : " + queue.dequeue());

}

SAMPLE output:-

Adding char : a

Adding char : b

Adding char : c

Adding char : d

Removed char : a

Removed char : b

Removed char : c