Java
Create a new Drive class.
* Create a Menu class.
* The menu class will use the previous 4 classes you created in GCD, LCM, FACTORIAL, DIGITS
The Menu will display a menu that give you the option of selecting:
1) Greatest Common Denominator
2) Lowest Common Multiple
3) Factorial
4) Number of Digits in an Integer
Enter 1,2,3 or 4:
When the User enter the choice,
then the correct function/method is called for the class and
asks the user for input(s) to use.
The system will then display the results.
Run your code and Test with the following values:
1) 144
2) 42
3) 5
4) 987654321
Create UML diagrams for your classes.
import java.util.Scanner;
import java.util.Vector;
public class Drive {
static void count(int num) {
int count = 0;
while(num != 0)
{
num /= 10;
++count;
}
System.out.println("Number of digits: " + count);
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static void findGCD(int arr[], int n)
{
int result = arr[0];
for (int i = 1; i < n; i++)
result = gcd(arr[i], result);
System.out.printf("GCD is: %d", result);
}
public static void factorial(int num){
long factorial = 1;
for(int i = 1; i <= num; ++i)
{
// factorial = factorial * i;
factorial *= i;
}
System.out.printf("Factorial of %d = %d", num, factorial);
}
static long LCM(int arr[], int n) {
int max_num = 0;
for (int i = 0; i < n; i++) {
if (max_num < arr[i]) {
max_num = arr[i];
}
}
// Initialize result
long res = 1;
// Find all factors that are present in
// two or more array elements.
int x = 2; // Current factor.
while (x <= max_num) {
// To store indexes of all array
// elements that are divisible by x.
Vector<Integer> indexes = new Vector<>();
for (int j = 0; j < n; j++) {
if (arr[j] % x == 0) {
indexes.add(indexes.size(), j);
}
}
// If there are 2 or more array elements
// that are divisible by x.
if (indexes.size() >= 2) {
// Reduce all array elements divisible
// by x.
for (int j = 0; j < indexes.size(); j++) {
arr[indexes.get(j)] = arr[indexes.get(j)] / x;
}
res = res * x;
} else {
x++;
}
}
// Then multiply all reduced array elements
for (int i = 0; i < n; i++) {
res = res * arr[i];
}
return res;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int ch, n, i;
while(true) {
System.out.println("Menu: ");
System.out.println("1) Greatest Common Denominator");
System.out.println("2) Lowest Common Multiple");
System.out.println("3) Factorial");
System.out.println("4) Number of Digits in an Integer");
System.out.println("Enter your choice: ");
ch = sc.nextInt();
switch(ch) {
case 1:
System.out.println("How many numbers do you want to input: ");
n = sc.nextInt();
int[] arr = new int[20];
System.out.println("Enter the numbers: ");
for(i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
findGCD(arr, n);
break;
case 2:
System.out.println("How many numbers you want to input: ");
n = sc.nextInt();
int[] a = new int[20];
System.out.println("Enter the numbers: ");
for(i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.println(LCM(a, n));
break;
case 3:
System.out.println("Enter the number you want to find the factorial of: ");
n = sc.nextInt();
factorial(n);
break;
case 4:
System.out.println("Enter integer: ");
n = sc.nextInt();
count(n);
break;
default: break;
}
}
}
}
Get Answers For Free
Most questions answered within 1 hours.