Question

Java Create a new Drive class. * Create a Menu class. * The menu class will...

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.

Homework Answers

Answer #1
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;
                        }
                        
                        
                }
        }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
5) Create a Java program using Scanner: Write a program where you will enter the grade...
5) Create a Java program using Scanner: Write a program where you will enter the grade score for 5 classes, then it will display total points and average and it will display the grade based on percentage. Steps: 1) Declare variable integer for English, Chemistry, Java Programming, Physics and Math 2) Declare variable total and percentage 3) Create Scanner object and prompt the user to enter grades for each class and input grades after each class 4) Calculate total of...
Write a program in Java that: 1. will prompt user with a menu that contains options...
Write a program in Java that: 1. will prompt user with a menu that contains options to: a. Add a To-Do Item to a todo list. A To-Do Item contains: i. An arbitrary reference number (4; 44,004; etc.) ii. A text description of the item ("Pick up dry cleaning", etc.) iii. A priority level (1, 2, 3, 4, or 5) b. View an item in the list (based on its reference number) c. Delete an item from the list (based...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Problem Statement: Write a Java program “HW6_lastname.java” that prints a program title and a menu with...
Problem Statement: Write a Java program “HW6_lastname.java” that prints a program title and a menu with four items. The user should then be prompted to make a selection from the menu and based on their selection the program will perform the selected procedure. The title and menu should be as the following: Student name: <your name should be printed> CIS 232 Introduction to Programming Programming Project 6 Due Date: October 23, 2020 Instructor: Dr. Lomako ******************************** 1.     Compute Angles                               *...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
Create a C# application You are to create a class object called “Employee” which included eight...
Create a C# application You are to create a class object called “Employee” which included eight private variables: firstN lastN dNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week. regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods:  constructor  properties  CalcPay(): Calculate the regular...
Modify the Date class in Fig. 8.7 by adding a new method called nextDay() that increments...
Modify the Date class in Fig. 8.7 by adding a new method called nextDay() that increments the Date by 1 when called and returns a new Date object. This method should properly increment the Date across Month boundary (i.e from the last day of the month to the first day of the next month). Write a program called DateTest that asks the user to enter 3 numbers (one at a time) corresponding to Month, Day and Year. The first two...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
Writ a program using java. Depending on the size of the room and the amount of...
Writ a program using java. Depending on the size of the room and the amount of shade that the room has, different sizes of air conditioning units must be used in order to be able to properly cool the room. The unit of measure for the amount of cooling that an air conditioner unit can provide is the BTU (British Thermal Unit) per hour. Code a program that will calculate the correct size of air conditioner for a specific room...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT