Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* -------------Plik Main -------- */
- package zadanie1a;
- public class Main {
- public static void main(String[] args) {
- try {
- Nwd n = new Nwd(1, 20);
- n.setB(0);
- System.out.println("Dla a = " + n.getA() + " oraz B = " + n.getB() + " NWD wynosi: " + n.getNwd() + " lub: -" + n.getNwd());
- } catch (Exception e) {
- System.out.println("\u001B[31m"+ "NWD nie obsługuje 0" + "\u001B[0m");
- e.printStackTrace(System.out);
- }
- }
- }
- /* -------------Plik classa NWD -------- */
- package zadanie1a;
- public class Nwd {
- private int a;
- private int b;
- private int nwd;
- public Nwd(int a, int b) throws Exception{
- if (a == 0 || b == 0) throw new Exception("NWD nie obsługuje 0");
- this.a = a;
- this.b = b;
- nwd();
- }
- private void nwd(){
- int a = this.a;
- int b = this.b;
- /* Fixing problems */
- if (a < 0) a = Math.abs(a);
- if (b < 0) b = Math.abs(b);
- while (a != b) {
- if (a > b) {
- a = a - b;
- } else {
- b = b - a;
- }
- }
- this.nwd = a;
- }
- public int getA() {
- return a;
- }
- public void setA(int a) throws Exception{
- if(a == 0) throw new Exception("A nie moze przyjąć wartości 0");
- this.a = a;
- nwd();
- }
- public int getB() {
- return b;
- }
- public void setB(int b) throws Exception{
- if(b == 0) throw new Exception("B nie moze przyjąć wartości 0");
- this.b = b;
- nwd();
- }
- public int getNwd() {
- return nwd;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement