Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在sane方法中引发不同类的异常 抽象类CustomException扩展异常 { 抽象公共字符串toString(); 抽象公共字符串getMessage(); } 接口SimpleInterestCalculator { public void setPrincipalAmount(int principalAmount)抛出CustomException; public int getPrincipalAmount(); public void setRateOfInterest(int-rateOfInterest)抛出CustomException; public int getRateOfInterest(); 公共void setTime(int Time)抛出CustomException; 公共int getTime(); public int getSimpleInterest(); } 接口组件兴趣计算器 { public void setPrincipalAmount(int principalAmount)抛出CustomException; public int getPrincipalAmount(); public void setRateOfInterest(int-rateOfInterest)抛出CustomException; public int getRateOfInterest(); 公共void setTime(int Time)抛出CustomException; 公共int getTime(); public int getCompoundInterest(); } 类SimpleInterestCalculationException扩展了CustomException { 字符串消息; SimpleInterestCalculationException(字符串消息) { this.message=消息; } SimpleInterestCalculationException() { this.message=null; } 公共字符串toString() { if(this.message==null) { 返回“单利计算例外”; } 其他的 { 返回“单利计算异常:”+this.message; } } 公共字符串getMessage() { 返回此消息; } } 类CompoundInterestCalculationException扩展了CustomException { 字符串消息; CompoundInterestCalculationException(字符串消息) { this.message=消息; } CompoundInterestCalculationException() { this.message=null; } 公共字符串toString() { if(this.message==null) { 返回“复利计算例外”; } 其他的 { 返回“复利计算异常:”+this.message; } } 公共字符串getMessage() { 返回此消息; } } 类InterestCalculator实现SimpleInterestCalculator、CompoundInterestCalculator { private int principalAmount、利率、时间; 兴趣计算器() { 此.principalAmount=0; 此.rateOfInterest=0; 这个时间=0; } 兴趣计算器(int principalAmount、int rateOfInterest、int time) { this.principalAmount=principalAmount; this.rateOfInterest=利率; 这个时间=时间; } public void setPrincipalAmount(int principalAmount)抛出SimpleInterestCalculationException { if(principalAmount_Java - Fatal编程技术网

Java 在sane方法中引发不同类的异常 抽象类CustomException扩展异常 { 抽象公共字符串toString(); 抽象公共字符串getMessage(); } 接口SimpleInterestCalculator { public void setPrincipalAmount(int principalAmount)抛出CustomException; public int getPrincipalAmount(); public void setRateOfInterest(int-rateOfInterest)抛出CustomException; public int getRateOfInterest(); 公共void setTime(int Time)抛出CustomException; 公共int getTime(); public int getSimpleInterest(); } 接口组件兴趣计算器 { public void setPrincipalAmount(int principalAmount)抛出CustomException; public int getPrincipalAmount(); public void setRateOfInterest(int-rateOfInterest)抛出CustomException; public int getRateOfInterest(); 公共void setTime(int Time)抛出CustomException; 公共int getTime(); public int getCompoundInterest(); } 类SimpleInterestCalculationException扩展了CustomException { 字符串消息; SimpleInterestCalculationException(字符串消息) { this.message=消息; } SimpleInterestCalculationException() { this.message=null; } 公共字符串toString() { if(this.message==null) { 返回“单利计算例外”; } 其他的 { 返回“单利计算异常:”+this.message; } } 公共字符串getMessage() { 返回此消息; } } 类CompoundInterestCalculationException扩展了CustomException { 字符串消息; CompoundInterestCalculationException(字符串消息) { this.message=消息; } CompoundInterestCalculationException() { this.message=null; } 公共字符串toString() { if(this.message==null) { 返回“复利计算例外”; } 其他的 { 返回“复利计算异常:”+this.message; } } 公共字符串getMessage() { 返回此消息; } } 类InterestCalculator实现SimpleInterestCalculator、CompoundInterestCalculator { private int principalAmount、利率、时间; 兴趣计算器() { 此.principalAmount=0; 此.rateOfInterest=0; 这个时间=0; } 兴趣计算器(int principalAmount、int rateOfInterest、int time) { this.principalAmount=principalAmount; this.rateOfInterest=利率; 这个时间=时间; } public void setPrincipalAmount(int principalAmount)抛出SimpleInterestCalculationException { if(principalAmount

Java 在sane方法中引发不同类的异常 抽象类CustomException扩展异常 { 抽象公共字符串toString(); 抽象公共字符串getMessage(); } 接口SimpleInterestCalculator { public void setPrincipalAmount(int principalAmount)抛出CustomException; public int getPrincipalAmount(); public void setRateOfInterest(int-rateOfInterest)抛出CustomException; public int getRateOfInterest(); 公共void setTime(int Time)抛出CustomException; 公共int getTime(); public int getSimpleInterest(); } 接口组件兴趣计算器 { public void setPrincipalAmount(int principalAmount)抛出CustomException; public int getPrincipalAmount(); public void setRateOfInterest(int-rateOfInterest)抛出CustomException; public int getRateOfInterest(); 公共void setTime(int Time)抛出CustomException; 公共int getTime(); public int getCompoundInterest(); } 类SimpleInterestCalculationException扩展了CustomException { 字符串消息; SimpleInterestCalculationException(字符串消息) { this.message=消息; } SimpleInterestCalculationException() { this.message=null; } 公共字符串toString() { if(this.message==null) { 返回“单利计算例外”; } 其他的 { 返回“单利计算异常:”+this.message; } } 公共字符串getMessage() { 返回此消息; } } 类CompoundInterestCalculationException扩展了CustomException { 字符串消息; CompoundInterestCalculationException(字符串消息) { this.message=消息; } CompoundInterestCalculationException() { this.message=null; } 公共字符串toString() { if(this.message==null) { 返回“复利计算例外”; } 其他的 { 返回“复利计算异常:”+this.message; } } 公共字符串getMessage() { 返回此消息; } } 类InterestCalculator实现SimpleInterestCalculator、CompoundInterestCalculator { private int principalAmount、利率、时间; 兴趣计算器() { 此.principalAmount=0; 此.rateOfInterest=0; 这个时间=0; } 兴趣计算器(int principalAmount、int rateOfInterest、int time) { this.principalAmount=principalAmount; this.rateOfInterest=利率; 这个时间=时间; } public void setPrincipalAmount(int principalAmount)抛出SimpleInterestCalculationException { if(principalAmount,java,Java,我想抛出类的异常,如果我计算单利,那么它应该是SimpleInterestCalculationException,如果我计算复利,那么它应该是ComputeInterestCalculationException。 请帮忙 三个主要选项: 打基础 abstract class CustomException extends Exception { abstract public String toString(); abstract public String getMes

我想抛出类的异常,如果我计算单利,那么它应该是SimpleInterestCalculationException,如果我计算复利,那么它应该是ComputeInterestCalculationException。
请帮忙

三个主要选项:

  • 打基础
    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 < 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 < 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 < 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 > 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);
            }
        }
    }