Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1.  
  2. public class IntCaculator {
  3. protected int x;
  4. protected int y;
  5.  
  6. IntCaculator(){
  7. x = 1;
  8. y = 1;
  9. }
  10.  
  11. IntCaculator(int x, int y){
  12. setX(x);
  13. setY(y);
  14. }
  15. public void setX(int x){
  16. if(x == 0) this.x = 1;
  17. else this.x = x;
  18. }
  19. public void setY(int y){
  20. if(y == 0) this.y = 1;
  21. else this.y = y;
  22. }
  23. public int getX(){
  24. return this.x;
  25. }
  26. public int getY(){
  27. return this.y;
  28. }
  29. public int add(){
  30. return x+y;
  31. }
  32. public int substract(){
  33. return x-y;
  34. }
  35. public int multiply(){
  36. return x * y;
  37. }
  38. public int divide(){
  39. return x / y;
  40. }
  41.  
  42.  
  43.  
  44. public static void main(String[] args){
  45.  
  46. NewIntCaculator n = new NewIntCaculator(2, 4);
  47.  
  48. System.out.println("X: "+n.getX());
  49. System.out.println("Y: "+n.getY());
  50. System.out.println("Divide: "+ n.divide());
  51. System.out.println("Mod: "+ n.mod());
  52. System.out.println("Min: "+ n.min());
  53. System.out.println("Max: "+ n.max());
  54. System.out.println("Pow x^y: "+n.pow());
  55.  
  56. }
  57. }
  58.  
  59. class NewIntCaculator extends IntCaculator{
  60.  
  61. NewIntCaculator(int x, int y){
  62. super(x, y);
  63. }
  64.  
  65. public int mod(){
  66. return x % y;
  67. }
  68.  
  69. public int min(){
  70. return (x < y) ? x : (x > y) ? y : 0;
  71. }
  72. public int max(){
  73. return (x > y) ? x : (x < y) ? y : 0;
  74. }
  75. public int pow(){
  76. int rs = x;
  77. for(int i=1; i<y; i++)
  78. rs *= x;
  79. return rs;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement