Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1- Define a new custom exception called NonPositiveException. 2- Write a class Fact which has a constructor with an integer argument that initializes its own

1- Define a new custom exception called NonPositiveException. 2- Write a class Fact which has a constructor with an integer argument that initializes its own field. 3- Create a new method called factorial that returns the factorial of given integer using recursive only. Your method should throw NonPositiveException if the argument was negative or zero. 4- Write a test class that reads an integer xfact from the user which represents the number of Fact objects that should be created. 5- For xfact number of times, the user will insert an integer dominator that can divide xfact (i.e. should not be zero) to create an object initialized by dominator value. 6- Your program should consider all the possible exceptions and prints the appropriate outputs and errors without any error termination.

THIS is the questions and my code :

import java.util.InputMismatchException; import java.util.Scanner; class NonPositiveException extends Exception { NonPositiveException() { super("Number should be positive!!!"); } } class Fact { private int number; Fact(int number) { this.number = number; } int factorial(int n) throws NonPositiveException { if (n < 0) { throw new NonPositiveException(); } else if (n == 1) { return 1; } else { return n * factorial(n - 1); } } } public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter xfact : "); try { int xfact = sc.nextInt(); if (xfact < 0) { throw new NonPositiveException(); } int dominator; for (int i = 0; i < xfact; i++) { while (true) { System.out.print("Insert dominator for object " + (i + 1) + " : "); dominator = sc.nextInt(); int div = xfact / dominator; if (div == 0) { System.out.println("div ( xfact/dominator) cannot be zero. Try again!!!"); } else { Fact fact = new Fact(dominator); int number = fact.factorial(dominator); System.out.println("Factorial number is : " + number); break; } } } } catch (InputMismatchException e) { System.err.println(" it should be an integer!!"); e.printStackTrace(); } catch (NonPositiveException e) { e.printStackTrace(); } catch (ArithmeticException e) { System.out.println("Error , div by zero not allowed"); e.printStackTrace(); } }}

IM okay with my code but I want the program for xfact and dominator if user enter a wrong choice that cause Exception that still take another value from the user my program only show the Exception and stop I need it to keep taking from the user condition: xfact =positive number and dominator !=0 please help me

in Java programing language

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions

Question

Is this investment worthwhile? Why or why not?

Answered: 1 week ago