Question: This problem contains two parts. If you can only help with part 1, that's fine. But if you can also help with part 2, that
This problem contains two parts. If you can only help with part 1, that's fine. But if you can also help with part 2, that will be greatly appreciated. I will give a thumbs up if your answer is helpful regardless of you completing both parts or just part 1. Thanks!
1. Please make changes on top of the following GCD.java and Factorial.java code, providing error(exception) handling of incorrect input(such as inputs that're not numbers and decimal numbers), using Javas try-catch mechanism. The Factorial class must throw an exception if the user enters a negative number.
2. These code will then be put into Netbeans with a GUI JFrame. You will develop a GUI to wrap around the GCD and Factorial classes. The GUI menubar should have, as a minimum, items File, and Compute. The File pulldown just needs to contain Exit. The Compute pulldown should contain GCD and Factorial. When GCD or Factorial is selected, the appropriate dialog box should pop up, allowing you to enter data and compute results. (You will need to install Swing in Netbeans (Tools->Plugins), if not already done.) Hints: You will need to add code to the top-level Frame which activates your new dialog. This will go inside the handler you created for the GCD menu item. To bring up a GCD dialog, simply declare one (see its constructor, and set modal true) and then call setVisible(true) to bring it up
Factorial.java
public class factorial {
public static void main (String args[]) {
if (args.length < 1) {
//check for too less arguments
System.out.println("Not enough arguments. Please enter a integer.");
System.exit(0);
}
else if (args.length > 1) {
//check for too much arguments
System.out.println("Too much arguments. Please only enter one integer.");
System.exit(0);
}
int n = Integer.parseInt(args[0]);
double result = fact(n);
if (n != -1) {
//print output
System.out.println("The factorial of " + n + " is: " + result);
}
}
public static double fact (int n) {
if (n < 0){
//print negative error
System.out.println("Negative Error!");
System.exit(0);
return -1;
}
double f = 1.0;
for (int i = n; i >1; i--) {
f = f * i;
}
return f;
}
}
GCD.java
public class GCD {
public static void main (String[] args){
if (args.length < 2){
//check for too less arguments
System.out.println("Not enough arguments. Please enter two integers.");
System.exit(0);
}
else if (args.length >= 3){
//check for too much arguments
System.out.println("Too much arguments. Please enter two integers.");
System.exit(0);
}
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);
int GCD = GCDofTwo(num1, num2);
//print output
System.out.println("The GCD of " + num1 + " and " + num2 + " is " + GCD);
}
//find GCD
public static int GCDofTwo(int num1, int num2){
if (num2 == 0){
if (num1 < 0){
return num1*-1;
}
return num1;
}
return GCDofTwo(num2, num1 % num2);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
