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
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,...
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                               *...
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...
This program is in C++: Write a program to allow the user to: 1. Create two...
This program is in C++: Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...
***Programming language is Java. After looking at this scenario please look over the requirements at the...
***Programming language is Java. After looking at this scenario please look over the requirements at the bottom (in bold) THIS IS ALL THAT WAS PROVIDED. PLEASE SPECIFY ANY QUESTIONS IF THIS IS NOT CLEAR (don't just say more info, be specific)*** GMU in partnership with a local sports camp is offering a swimming camp for ages 10-18. GMU plans to make it a regular event, possibly once a quarter. You have been tasked to create an object-oriented solution to register,...
Create an uml Sequence Diagrams for the following scenario A student at Grambling must register for...
Create an uml Sequence Diagrams for the following scenario A student at Grambling must register for class and pay for the class each term. Show all the steps done in the process starting with getting your pin to paying your fees.   Go to GSU’S website: www.gram.edu 2. Click on current student and then Banner Web 3. Click on Enter Secure Area 4. To Login: a. Enter User ID: Student ID No. e.g., GXXXXXXXX b. Enter PIN#: Date of Birth (mmddyy)...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you start doing anything… This project involves implementing a simple university personnel management program. The program contains two different kinds of objects: students and faculty. For each object, the program stores relevant information such as university ID, name, etc. Different information is stored depending on the type of the object. For example, a student has a GPA, while a faculty has a title and department...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT