Advertisement
ZivkicaI

PocketCalculatorExceptios

Nov 27th, 2019
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.27 KB | None | 0 0
  1. package exercises.part2oop
  2.  
  3. import java.nio.BufferOverflowException
  4.  
  5. object PocketCalcExcep extends App {
  6.  
  7.   class OverflowException extends RuntimeException
  8.   class UnderflowException extends RuntimeException
  9.   class MathCalculationException extends RuntimeException("Divide by 0:/")
  10.  
  11.   object PoceketCalc{
  12.     def add(x:Int, y:Int) = {
  13.       val result= x+y
  14.       if(x>0 && y>0 && result<0) throw new OverflowException
  15.       else if(x<0 && y<0 && result>0) throw new UnderflowException
  16.       else result
  17.     }
  18.  
  19.     def substract(x:Int, y:Int)={
  20.       val result=x-y
  21.  
  22.       if(x>0 && y<0 && result <0) throw new OverflowException
  23.       else if(x<0 && y>0 && result>0) throw new UnderflowException
  24.       else result
  25.  
  26.     }
  27.  
  28.     def multiply(x:Int, y:Int)={
  29.       val result=x*y
  30.       if(x>0 && y>0 && result <0) throw new OverflowException
  31.       else if(x<0 && y<0 && result<0) throw new OverflowException
  32.       else if(x>0 && y<0 && result>0) throw new UnderflowException
  33.       else if(x<0 && y<0 && result>0) throw new UnderflowException
  34.       else result
  35.     }
  36.  
  37.     def divide(x:Int, y:Int)={
  38.       if(y==0) throw new MathCalculationException
  39.       else x/y
  40.     }
  41.  
  42.   }
  43.  
  44.   println(PoceketCalc.divide(2,0)) //kje frli greshka radi delenje so 0 i poraka
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement