

class Data {

    public int x;

    public int y = 20;

    private int z;

}



public class TestPublic {



    public static void main(String[] args) {

        Data d = new Data();

        

        System.out.println(d.y);

        d.y = 10;

        System.out.println(d.y);        

    }

}



/* The first output of d.y is 20 because the statement "public int y = =
20"

   intializes y to 20 when an instance of Data is created 

   

   The compiler won't let you compile the code if you add the line =
"System.out.println(d.z)"

   since z is a private field of class Data which means that z is only =
accessible from within

   class Data. 

 

   The following is one way to rewrite class data so that all of its =
fields are private 

   but also still accessible through so called "setter" and "getter" =
methods.

 

     class Data {

        private int x;

        private int y = 20;

        private int z;



        public int getX() {

            return x;

        }



        public int getY() {

            return y;

        }    



        public int getZ() {

            return z;

        }    



        public void setX(int x) {

            this.x = x;

        }



        public void setY(int y) {

            this.y = y;

        }



        public void setZ(int z) {

            this.z = z;

        }    

    }

 */

 

class Person {

    private int age;

    // Add fields for the new information

    private String lastname, firstname;



    // Change the constructor to initialize the new fields

    public Person(int a, String firstname, String lastname) {

        age = a; 

        this.lastname = lastname;

        this.firstname = firstname;

    }    

    public int getAge() { return age; }    

    

    // Add setter and getter methods for the firstname and lastname =
fields

    public String getLastName() { return lastname; }

    public String getFirstName() { return firstname; }

    public void setLastName(String lastname) { this.lastname = =
lastname; }

    public void setFirstName(String firstname) { this.firstname = =
firstname; }

}



class Complex {

    private double r, i;

    

    public Complex(double r, double i) {

        this.r = r;

        this.i = i;

    }

    

    public Complex add(Complex c) {

        return new Complex(r + c.r , i + c.i);

    }

    

    public String toString() {

        return r + " + " + i + "i";        

    }

}



class ComplexTest {



    public static void main(String[] args)

    {

        Complex c1 = new Complex(1, 1);

        Complex c2 = new Complex(2, 2);

        Complex result = c1.add(c2);

        

        System.out.println(result);

    }

}