Question

Assignment This Programming Project deals two well-known arithmetic calculations that can be implemented recursively. The project...

Assignment
This Programming Project deals two well-known arithmetic calculations that can be
implemented recursively.
The project will have 3 class files:
FactorialClass.java
FibinnaciClass.java
RecursionMainClass.java
The FactorialClass.java will calculate a factorial for a long type argument to a FactorialClass
class method.
The FibinnaciClass.java will calculate the Fibonacci sum for an int type argument to a
FibinnaciClass class method.
Both classes should display the results in a well formatted manner. See the sample output below
for required formatting details, use commas and right justification in the number results for both
calculations.
Each of the calculation classes should detect overflow using the Exception class try-catch
technique described in the book and slides (result <= 0). The overflow exception must cause the
parameter that caused the overflow and the overflow value to be displayed.
In a main program, write a loop for each calculation that will use inputs from 1 to a number that
will cause overflow. The loop for each calculation should display each result up until the
overflow happens.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot of the code:

//FactorialClass.java

//FibinnaciClass.java

//RecursionMainClass.java

Sample Output:


Code to copy:

//FactorialClass.java

package arithmetic;

//Define the class FactorialClass .

public class FactorialClass

{

//Define the method factorial().

public static long factorial(long n) throws Exception

{

//Declare the variable fact of long type.

long fact;

  

//If number of elements are zero than factorial is 1.

if (n == 0)

{

fact = 1;

}

  

//Number of elements are not 0.

else

{

//Find the factorial using recursion.

fact = n * factorial (n-1);

  

//Throw the exception if factorial is less

//than zero.

if (fact < 0)

{

throw new Exception(String.format("\n%d "

+ "Factorial = %,d"

+ " - This is Overflow\n",

n, fact));

}

}

  

//Return factorial.

return fact;

}

}

//FibinnaciClass.java

package arithmetic;

//Define the class FibinnaciClass

public class FibinnaciClass

{

// Define the fibonnaci() method.

public static int fibonnaci(int n) throws Exception

{

// Declare the variable.

int fib;

// if the number of elements are less than 1

if (n <= 1)

{

fib = n;

}

// Number of elements are more than 1.

else

{

// Use recursion to find the fibonnaci numbers.

fib = fibonnaci(n - 1) + fibonnaci(n - 2);

// Throw exception, if the fibonnaci is

//less than 0.

if (fib < 0)

{

throw new Exception(String.format(

"\n%d Fibonnaci = %,d "

+ "- This is Overflow\n",

n, fib));

}

}

// Return fibonnaci numbers.

return fib;

}

}

//RecursionMainClass.java

package arithmetic;

//Create and define the class RecursionMainClass.

public class RecursionMainClass

{

// Start the main() method.

public static void main(String[] args)

{

// Start the for loop.

for (int i = 0; i < 25; i++)

{

// Start the try block for the factorial.

try

{

// Print the factorial of the ith number.

System.out.println(String.format

("%d Factorial = %,d", i,

FactorialClass.factorial(i)));

}

// Start the catch block.

catch (Exception a)

{

// Print the exception.

System.out.println(a);

// Break.

break;

}

}

// Start the for loop

for (int i = 0; i < 25; i++)

{

// Start the try block for fibonacci numbers.

try

{

// Print the fibonacci numbers.

System.out.println(String.format

("%d Fibonnaci = %,d", i,

FibinnaciClass.fibonnaci(i)));

}

// Start the catch block

catch (Exception a)

{

// Catch the exception.

System.out.println(a);

// Break.

break;

}

}

}

}

Add a comment
Know the answer?
Add Answer to:
Assignment This Programming Project deals two well-known arithmetic calculations that can be implemented recursively. The project...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CS 2050 – Programming Project # 1 Due Date: Session 3 (that is, in one week!)....

    CS 2050 – Programming Project # 1 Due Date: Session 3 (that is, in one week!). Can be resubmitted up to two times until Session 6 if your first version is submitted by session 3. In this project, you create two classes for tracking students. One class is the Student class that holds student data; the other is the GradeItem class that holds data about grades that students earned in various courses. Note: the next three projects build on this...

  • CMSC 256 – Project 5 Programming Assignment 5 Note: When you turn in an assignment to...

    CMSC 256 – Project 5 Programming Assignment 5 Note: When you turn in an assignment to be graded in this class, you are making the claim that you neither gave nor received assistance on the work you turned in (except, of course, assistance from the instructor or teaching assistants). Program: Ticketing System Points: 100 A set of classes is used to handle the different ticket types for a theater event. All tickets have a unique serial number that is assigned...

  • Java Project In Brief... For this Java project, you will create a Java program for a...

    Java Project In Brief... For this Java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain: I need a code that works, runs and the packages are working as well The room number of the classroom. The teacher and the subject assigned to the classroom. A list of students assigned to the classroom including their student id and final grade....

  • CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The...

    CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The Assignment ? Specifications General Structure your file name and class name on the following pattern: The first three letters of your last name (begin with upper case.). Then the first two letters of your first name (begin with upper case.). Follow this with the name of the program: TryCatch. For a student called ’John Doe,’ the class name and file name would be: DoeJoTryCatch...

  • Amusement Park Programming Project Project Outcomes Use the Java selection constructs (if and if else). Use...

    Amusement Park Programming Project Project Outcomes Use the Java selection constructs (if and if else). Use the Java iteration constructs (while, do, for). Use Boolean variables and expressions to control iterations. Use arrays or ArrayList for storing objects. Proper design techniques. Project Requirements Your job is to implement a simple amusement park information system that keeps track of admission tickets and merchandise in the gift shop. The information system consists of three classes including a class to model tickets, a...

  • Project overview: Create a java graphics program that displays an order menu and bill from a...

    Project overview: Create a java graphics program that displays an order menu and bill from a Sandwich shop, or any other establishment you prefer. In this program the design is left up to the programmer however good object oriented design is required. Below are two images that should be used to assist in development of your program. Items are selected on the Order Calculator and the Message window that displays the Subtotal, Tax and Total is displayed when the Calculate...

  • Programming Project #5 Project Outcomes: Develop a Java program that Uses selection constructs (if, and if...

    Programming Project #5 Project Outcomes: Develop a Java program that Uses selection constructs (if, and if else). Uses the Java iteration constructs (while, do, for). Uses static variables. Ensure integer variables input are within a range that will not cause integer overflow. Uses proper design techniques including reading UML Class Diagrams Background Information: The Unified Modeling Language (UML) provides a useful notation for designing and developing object-oriented software systems. One of the basic components of the UML is a class...

  • I really need help with this, please. This is a java programming assignment. Project 1 The first programming project inv...

    I really need help with this, please. This is a java programming assignment. Project 1 The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of four classes. 1. The first class is the Employee class, which contains the employee's name and monthly salary, which is specified in whole dollars. It should have three methods: a. A constructor that allows the name and monthly salary to be...

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT