Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package domein;
- public class Berekening
- {
- private int x, y;
- // setters, in UML met - dus private
- private void setX(int x)
- {
- this.x = x;
- }
- private void setY(int y)
- {
- this.y = y;
- }
- // constructor
- public Berekening(int g1, int g2)
- {
- int gr, kl;
- gr = Math.max(g1, g2);
- kl = Math.min(g1, g2);
- this.setX(gr);
- this.setY(kl);
- }
- // getters
- public int getX()
- {
- return this.x;
- }
- public int getY()
- {
- return this.y;
- }
- // andere methoden
- public String geefSom()
- {
- String s;
- s = String.format("De som van %d en %d is %d", this.x, this.y, this.x+this.y);
- return s;
- }
- public String geefVerschil()
- {
- String s;
- s = String.format("Het verschil tussen %d en %d is %d", this.x, this.y, this.x-this.y);
- return s;
- }
- public String geefProduct()
- {
- String s;
- s = String.format("Het product van %d en %d is %d", this.x, this.y, this.x*this.y);
- return s;
- }
- public String geefQuotient()
- {
- String s;
- if(this.y == 0)
- s = "Fout: deling door nul";
- else
- s = String.format("Het quotiënt van %d en %d is %d", this.x, this.y, this.x/this.y);
- return s;
- }
- }
- -------------
- package ui;
- import java.util.Scanner;
- import domein.Berekening;
- public class BerekeningApplicatie
- {
- public static void main(String[] args)
- {
- int g1, g2, code;
- String s = "";
- Scanner input = new Scanner(System.in);
- do // input controleren: do while
- {
- System.out.println("code = 1: x + y");
- System.out.println("code = 2: grootste getal - kleinste getal ");
- System.out.println("code = 3: x * y ");
- System.out.println("code = 4: grootste getal / kleinste getal (gehele deling!)");
- System.out.println("-1 om te stoppen");
- System.out.print("Geef een code in: ");
- code = input.nextInt();
- } while(!(code == 1 || code == 2 || code == 3 || code == 4 || code == -1));
- while(code != -1) // schildwacht: while
- {
- System.out.print("Geef een geheel getal: ");
- g1 = input.nextInt();
- System.out.print("Geef nog een geheel getal: ");
- g2 = input.nextInt();
- Berekening berekening = new Berekening(g1, g2);
- switch(code)
- {
- case 1: s = berekening.geefSom();break;
- case 2: s = berekening.geefVerschil();break;
- case 3: s = berekening.geefProduct();break;
- case 4: s = berekening.geefQuotient();break;
- }
- System.out.printf("%s%n%n", s);
- do
- {
- System.out.println("code = 1: x + y");
- System.out.println("code = 2: grootste getal - kleinste getal ");
- System.out.println("code = 3: x * y ");
- System.out.println("code = 4: grootste getal / kleinste getal (gehele deling!)");
- System.out.println("-1 om te stoppen");
- System.out.print("Geef een code in: ");
- code = input.nextInt();
- } while(!(code == 1 || code == 2 || code == 3 || code == 4 || code == -1));
- }
- System.out.println("Tot ziens!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement