Advertisement
Guest User

H5oef1

a guest
Nov 14th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. package domein;
  2.  
  3. public class Berekening
  4. {
  5. private int x, y;
  6.  
  7. // setters, in UML met - dus private
  8. private void setX(int x)
  9. {
  10. this.x = x;
  11. }
  12.  
  13. private void setY(int y)
  14. {
  15. this.y = y;
  16. }
  17.  
  18. // constructor
  19. public Berekening(int g1, int g2)
  20. {
  21. int gr, kl;
  22. gr = Math.max(g1, g2);
  23. kl = Math.min(g1, g2);
  24. this.setX(gr);
  25. this.setY(kl);
  26. }
  27.  
  28. // getters
  29. public int getX()
  30. {
  31. return this.x;
  32. }
  33.  
  34. public int getY()
  35. {
  36. return this.y;
  37. }
  38.  
  39. // andere methoden
  40. public String geefSom()
  41. {
  42. String s;
  43. s = String.format("De som van %d en %d is %d", this.x, this.y, this.x+this.y);
  44. return s;
  45. }
  46.  
  47. public String geefVerschil()
  48. {
  49. String s;
  50. s = String.format("Het verschil tussen %d en %d is %d", this.x, this.y, this.x-this.y);
  51. return s;
  52. }
  53.  
  54. public String geefProduct()
  55. {
  56. String s;
  57. s = String.format("Het product van %d en %d is %d", this.x, this.y, this.x*this.y);
  58. return s;
  59. }
  60.  
  61. public String geefQuotient()
  62. {
  63. String s;
  64. if(this.y == 0)
  65. s = "Fout: deling door nul";
  66. else
  67. s = String.format("Het quotiënt van %d en %d is %d", this.x, this.y, this.x/this.y);
  68. return s;
  69. }
  70.  
  71. }
  72.  
  73. -------------
  74.  
  75. package ui;
  76.  
  77. import java.util.Scanner;
  78.  
  79. import domein.Berekening;
  80.  
  81. public class BerekeningApplicatie
  82. {
  83.  
  84. public static void main(String[] args)
  85. {
  86. int g1, g2, code;
  87. String s = "";
  88. Scanner input = new Scanner(System.in);
  89.  
  90. do // input controleren: do while
  91. {
  92. System.out.println("code = 1: x + y");
  93. System.out.println("code = 2: grootste getal - kleinste getal ");
  94. System.out.println("code = 3: x * y ");
  95. System.out.println("code = 4: grootste getal / kleinste getal (gehele deling!)");
  96. System.out.println("-1 om te stoppen");
  97. System.out.print("Geef een code in: ");
  98. code = input.nextInt();
  99. } while(!(code == 1 || code == 2 || code == 3 || code == 4 || code == -1));
  100.  
  101. while(code != -1) // schildwacht: while
  102. {
  103. System.out.print("Geef een geheel getal: ");
  104. g1 = input.nextInt();
  105. System.out.print("Geef nog een geheel getal: ");
  106. g2 = input.nextInt();
  107.  
  108. Berekening berekening = new Berekening(g1, g2);
  109.  
  110. switch(code)
  111. {
  112. case 1: s = berekening.geefSom();break;
  113. case 2: s = berekening.geefVerschil();break;
  114. case 3: s = berekening.geefProduct();break;
  115. case 4: s = berekening.geefQuotient();break;
  116. }
  117.  
  118. System.out.printf("%s%n%n", s);
  119.  
  120. do
  121. {
  122. System.out.println("code = 1: x + y");
  123. System.out.println("code = 2: grootste getal - kleinste getal ");
  124. System.out.println("code = 3: x * y ");
  125. System.out.println("code = 4: grootste getal / kleinste getal (gehele deling!)");
  126. System.out.println("-1 om te stoppen");
  127. System.out.print("Geef een code in: ");
  128. code = input.nextInt();
  129. } while(!(code == 1 || code == 2 || code == 3 || code == 4 || code == -1));
  130. }
  131. System.out.println("Tot ziens!");
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement