S
S
Sergey Sergey2021-05-12 11:04:58
Java
Sergey Sergey, 2021-05-12 11:04:58

Is encapsulation implemented correctly in this code and what are the errors?

class Fox {
    public static void main(String[] args) {
        FoxTest f = new FoxTest();
        f.seta(0);
        f.setb(9);
        f.setc(8);
        f.setd(5);
        int a;
        for(a=1; a<3; a++){
            int b;
        for(b=1; b<3; b++){
                int c;
            for(c=1; c<2; c++){
                    int d;
                for(d=1; d<2; d++){
                  System.out.println("   " + f.geta() + f.getb() + f.getc() + f.getd() /*+ a + b + c + d*/);
                }   
            }
        }   
    }
}
}

class FoxTest {

    private int a;
    void seta(int i) {
        a = i;
    }
    private int b;
    void setb(int i) {
        b = i;
    }
    private int c;
    void setc(int i) {
        c = i;
    }
    private int d;
    void setd(int i) {
        d = i;
    }
    int geta(){
        return a;
        }
    int getb(){
        return b;
        }
    int getc(){
        return c;
        }
    int getd(){
        return d;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-05-12
@Sergei1111

You have already been told that this code is too primitive. There is no encapsulation here. This class of yours with getters and setters can be thrown out, nothing will change.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question