お世話になります… 相も変わらず今日が期限の課題にてこずっています… 説明を読んでも、Exceptionと、try〜catchについて全く理解できずでもう頭が痛いです… もしよろしかったら、この問題の解き方を教えていただけないでしょうか… よろしくお願いします!! 1.If your program crashes, 50 points will be deducted, as the goal of this assignment is to learn how to use try-catch blocks.
2.Write a Java program that implements this algorithm: a.Display a calculator menu
b.Ask the user to enter the integer choice, followed by two (2) integers
c.Get the integer input by using class Scanner method nextInt() & store the 3 integers in 3 variables
d.Use else-if statements or a switch statement to calculate the result of these four integer functions (division, modulus, gcd, lcm) using 4 static methods. Note that division and modulus will automatically throw an ArithmeticException exception, if the user enters 0 as the third integer. The methods for GCD & LCM need to be designed so that both throw a NegativeIntegerException exception, if the user enters a negative number for the second or third integer.
e.Each method must have two integer parameters and return an integer result. For example, you should use this method for the division operator: public static int division(int x, int y){ int result = x / y; return result; }
f.For the greatest common divisor (GCD), use this algorithm: while(y != 0){ temporary = y y = x % temporary x = temporary } gcd = x
g.For the lowest common multiple (LCM), use this formula: (x*y)/(GCD of x & y)
h.Display the result of integer (whole number) operators division, modulus, gcd, lcm
i.If the user enters any incorrect input, the program should give an appropriate error message, and then display the menu again. Use try-catch blocks to do this. (To check the integer range for selecting a choice from the menu, use an if statement, not a try-catch block.)
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 1 18 12 18 / 12 = 1
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 2 18 12 18 % 12 = 6
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 3 18 12 GCD of 18 & 12 = 6
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 4 18 12 LCM of 18 & 12 = 36
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 1 1 0 java.lang.ArithmeticException: / by zero
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 2 2 0 java.lang.ArithmeticException: / by zero
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 4 0 0 java.lang.ArithmeticException: / by zero
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 3 -3 3 NegativeIntegerException: in greatest common divisor method
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 3 3 -3 NegativeIntegerException: in greatest common divisor method
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 3 -3 -3 NegativeIntegerException: in greatest common divisor method
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 4 -4 4 NegativeIntegerException: in least common multiple method
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 4 4 -4 NegativeIntegerException: in least common multiple method
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 4 -4 -4 NegativeIntegerException: in least common multiple method
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 1 2 spam java.util.InputMismatchException: Please enter 3 integers
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: a b c java.util.InputMismatchException: Please enter 3 integers
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: this is a pen java.util.InputMismatchException: Please enter 3 integers
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program
Enter your choice followed by two integers: 1 w 3 java.util.InputMismatchException: Please enter 3 integers
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: -1 2 3 INCORRECT INPUT: Please answer a choice from 1 to 5
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 999 999 999 INCORRECT INPUT: Please answer a choice from 1 to 5
***** CALCULATOR MENU ***** 1. Division 2. Modulus 3. Greatest Common Divisor 4. Lowest Common Multiple 5. Exit the program Enter your choice followed by two integers: 5
public static void display(){
System.out.println();
System.out.println("***** CALCULATOR MENU *****");
System.out.println("1.Division");
System.out.println("2.Modulus");
System.out.println("3.Greatest Common Divisor");
System.out.println("4.Lowest Common Multiple");
System.out.println("5.Exit the program");
System.out.print("Enter your choice followed by two integers: ");
}
public static void check(String code) throws InputMismatchException{
if(!code.matches(" *-?[0-9]+ +-?[0-9]+ +-?[0-9]+ *")){
throw new InputMismatchException("Please enter 3 integers.");
}
if(!code.matches(" *[1-5]+ +-?[0-9]+ +-?[0-9]+ *")){
throw new InputMismatchException("Select operator from 1 to 5.");
}
}
public static void division(int a , int b){
System.out.println(a+" / "+b+" = "+a/b);
}
public static void modulus(int a , int b){
System.out.println(a+" % "+b+" = "+a%b);
}
public static void gcd(int a , int b) throws NegativeIntegerException{
if(a < 0 || b < 0){
throw new NegativeIntegerException("in greatest common divisor method.");
}
System.out.println("GCD of "+a+" & "+b+" = "+gcdCal(a,b));
}
public static int gcdCal(int x , int y){
while(y!=0){
int temp = y;
y = x%temp;
x = temp;
}
return x;
}
public static void lcm(int a , int b) throws NegativeIntegerException{
if(a < 0 || b < 0){
throw new NegativeIntegerException("in lowest common multiple method.");
}
System.out.println("LCM of "+a+" & "+b+" = "+a*b/gcdCal(a,b));
}
}
class NegativeIntegerException extends Exception{
NegativeIntegerException(String str){
super(str);
}
}