subject

File Factorials contains a program that calls the factorial method of the MathUtils class to compute the factorials of integers entered by the user. Save these files to your directory and study the code in both, then compile and run Factorials to see how it works. Try several positive integers, then try a negative number. You should find that it works for small positive integers (values < 17), but that it returns a large negative value for larger integers and that it always returns 1 for negative integers. Create a custom exception class called myException. This class should take in one String parameter that can be passed to the super class inside the constructor
Returning 1 as the factorial of any negative integer is not correct—mathematically, the factorial function is not defined for negative integers. To correct this, you could modify your factorial method to check if the argument is negative, but then what? The method must return a value, and even if it prints an error message, whatever value is returned could be misconstrued. Instead it should throw an exception indicating that something went wrong so it could not complete its calculation. Use your myException class to handle the situation. Modify your program as follows:
Modify the header of the factorial method to indicate that factorial can throw a myException.
Modify the body of factorial to check the value of the argument and, if it is negative, throw a myException. Note that what you pass to throw is actually an instance of the myException class, and that the constructor takes a String parameter. Use this parameter to be specific about what the problem is.
Compile and run your Factorials program after making these changes. Now when you enter a negative number an exception will be thrown, terminating the program. The program ends because the exception is not caught, so it is thrown by the main method, causing a runtime error.
Modify the main method in your Factorials class to catch the exception thrown by factorial and print an appropriate message, but then continue with the loop. Think carefully about where you will need to put the try and catch.
Returning a negative number for values over 16 also is not correct. The problem is arithmetic overflow—the factorial is bigger than can be represented by an int. This can also be thought of as a myException — this factorial method is only defined for arguments up to 16. Modify your code in factorial to check for an argument over 16 as well as for a negative argument. You should throw a myException in either case, but pass different messages to the constructor so that the problem is clear.
Submit the following:
Modified MathUtils. java
Modified Factorials. java
myException. java
//
// Factorials. java
//
// Reads integers from the user and prints the factorial of each.
//
//
import java. util. Scanner;
public class Factorials
{
public static void main(String[] args)
{
String keepGoing = "y";
Scanner scan = new Scanner(System. in);
while (keepGoing. equals("y") || keepGoing. equals("Y"))
{
System. out. print("Enter an integer: ");
int val = scan. nextInt();
System. out. println("Factorial(" + val + ") = "
+ MathUtils. factorial(val));
System. out. print("Another factorial? (y/n) ");
keepGoing = scan. next();
}
}
}

//
// MathUtils. java
//
// Provides static mathematical utility functions.
//
//
public class MathUtils
{
//
// Returns the factorial of the argument given
//
public static int factorial(int n)
{
int fac = 1;
for (int i=n; i>0; i--)
fac *= i;
return fac;
}
}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 18:20
Write a javascript program that reads three integers named start, end, and divisor from three text fields. your program must output to a div all the integers between start and end, inclusive, that are evenly divisible by divisor. the output integers must be separated by spaces. for example, if a user entered 17, 30, and 5, your program would output "20 25 30" (without the quotes) because those are the only integers between 17 and 30 (including 17 and 30) that are evenly divisible by 5.
Answers: 2
question
Computers and Technology, 22.06.2019 16:50
Consider a slotted aloha system, where the time slot equals the fixed duration of each packet. assume that there are 4 stations a,b,c,d sharing the medium. (a) stations a,b,c,d receive one packet each from higher layers at times 1.3, 1.5, 2.6,5.7 respectively. show which transmissions take place when, according to the slottedaloha protocol; describe all transmissions until all four packets have been successful.when needed, each station has access to the following sequence of random number, provided by a random number generator and drawn uniformly between 0 and 1: (1) station a draws numbers: 0.31, 0.27, 0.78, 0.9, 0.9, 0.11, 0. (2) station b draws numbers: 0.45, 0.28, 0.11, 0.83, 0.37, 0.22, 0. (3)station c draws numbers: 0.1, 0.2, 0.3, 0.4, 0. (4) station d draws numbers: 0.36, 0.77, 0.9, 0.1, 0.1, 0.1, 0.1, 0. (b) in slotted aloha, a station transmits in each time slot with a given probability. what probabilities would you assign to each of the four stations so as to: (i) maximize the efficiency of the protocol? (ii) maximize fairness among the four stations? (c) will the efficiency increase or decrease if we modify slotted aloha as follows: (i) get rid of slots and allow stations to transmit immediately? (ii) implement carrier sensing? (iii) implement collision detection? (iv) implement collision avoidance?
Answers: 3
question
Computers and Technology, 22.06.2019 20:10
Assume the existence of a bankaccount class. define a derived class, savingsaccount that contains two instance variables: the first a double, named interestrate, and the second an integer named interesttype. the value of the interesttype variable can be 1 for simple interest and 2 for compound interest. there is also a constructor that accepts two parameters: a double that is used to initialize the interestrate variable, and a string that you may assume will contain either "simple", or "compound", and which should be used to initialize the interesttype variable appropriately. there should also be a pair of functions getinterestrate and getinteresttype that return the values of the corresponding data members (as double and int respectively).
Answers: 2
question
Computers and Technology, 25.06.2019 02:30
What are the charges for invasion of privacy on computers
Answers: 1
You know the right answer?
File Factorials contains a program that calls the factorial method of the MathUtils class to compute...
Questions
question
Mathematics, 18.05.2021 01:00
question
Mathematics, 18.05.2021 01:00
question
Mathematics, 18.05.2021 01:00
question
English, 18.05.2021 01:00