Note that there are some explanatory texts on larger screens.

plurals
  1. POThrowing exceptions of different classes within a sane method
    primarykey
    data
    text
    <pre><code>abstract class CustomException extends Exception { abstract public String toString(); abstract public String getMessage(); } interface SimpleInterestCalculator { public void setPrincipalAmount(int principalAmount) throws CustomException; public int getPrincipalAmount(); public void setRateOfInterest(int rateOfInterest) throws CustomException; public int getRateOfInterest(); public void setTime(int Time) throws CustomException; public int getTime(); public int getSimpleInterest(); } interface CompoundInterestCalculator { public void setPrincipalAmount(int principalAmount) throws CustomException; public int getPrincipalAmount(); public void setRateOfInterest(int rateOfInterest) throws CustomException; public int getRateOfInterest(); public void setTime(int Time) throws CustomException; public int getTime(); public int getCompoundInterest(); } class SimpleInterestCalculationException extends CustomException { String message; SimpleInterestCalculationException(String message) { this.message = message; } SimpleInterestCalculationException() { this.message = null; } public String toString() { if (this.message == null) { return "Simple Interest Calculation Exception"; } else { return "Simple Interest Calculation Exception : " + this.message; } } public String getMessage() { return this.message; } } class CompoundInterestCalculationException extends CustomException { String message; CompoundInterestCalculationException(String message) { this.message = message; } CompoundInterestCalculationException() { this.message = null; } public String toString() { if (this.message == null) { return "Compound Interest Calculation Exception"; } else { return "Compound Interest Calculation Exception : " + this.message; } } public String getMessage() { return this.message; } } class InterestCalculator implements SimpleInterestCalculator, CompoundInterestCalculator { private int principalAmount, rateOfInterest, time; InterestCalculator() { this.principalAmount = 0; this.rateOfInterest = 0; this.time = 0; } InterestCalculator(int principalAmount, int rateOfInterest, int time) { this.principalAmount = principalAmount; this.rateOfInterest = rateOfInterest; this.time = time; } public void setPrincipalAmount(int principalAmount) throws SimpleInterestCalculationException { if (principalAmount &lt; 0) { throw new SimpleInterestCalculationException("Principal Amount Cannot be Negative"); } if (principalAmount == 0) { throw new SimpleInterestCalculationException("Principal Amount Cannot be Zero"); } this.principalAmount = principalAmount; } public int getPrincipalAmount() { return this.principalAmount; } public void setRateOfInterest(int rateOfInterest) throws SimpleInterestCalculationException { if (rateOfInterest &lt; 0) { throw new SimpleInterestCalculationException("Rate Of Interest Cannot be Negative"); } if (rateOfInterest == 0) { throw new SimpleInterestCalculationException("Rate Of Interest Cannot be Zero"); } this.rateOfInterest = rateOfInterest; } public int getRateOfInterest() { return this.rateOfInterest; } public void setTime(int time) throws SimpleInterestCalculationException { if (time &lt; 0) { throw new SimpleInterestCalculationException("Time Cannot be Negative"); } if (time == 0) { throw new SimpleInterestCalculationException("Time Cannot be Zero"); } this.time = time; } public int getTime() { return this.time; } public int getSimpleInterest() { return (this.principalAmount * this.rateOfInterest * this.time) / 100; } public int getCompoundInterest() { int x, y, z; x = (this.rateOfInterest / 100) + 1; y = this.time; z = 1; while (y &gt; 0) { z = z * x; y--; } z = z * this.principalAmount; return z; } } class calculatepsp { public static void main(String gg[]) { InterestCalculator simpleInterest = new InterestCalculator(); InterestCalculator compoundInterest = new InterestCalculator(); try { simpleInterest.setPrincipalAmount(-100); simpleInterest.setRateOfInterest(5); simpleInterest.setTime(2); int simpleinterest = interestCalculator.getSimpleInterest(); System.out.println("Simple Interest : " + simpleinterest); compoundInterest.setPrincipalAmount(1000); compoundInterest.setRateOfInterest(-8); compoundInterest.setTime(4); int compoundinterest = interestCalculator.getCompoundInterest(); System.out.println("Compound Interest : " + compoundinterest); } catch (SimpleInterestCalculationException sice) { System.out.println(sice); } catch (CompoundInterestCalculationException cice) { System.out.println(cice); } } } </code></pre> <p>I want to throw the exception of the class, if i am calculating simple interest then it should be SimpleInterestCalculationException and if i am calculating compound interest then it should be CompundInterestCalculationException.<br> Please Help Regarding this. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload