package colors;

public class Color {

    private int red, green, blue;

    private String name;

    public Color(int red, int green, int blue, String name) {

        assert(red < 256 && red >= 0);

        assert(green < 256 && green >= 0);

        assert(blue < 256 && blue >= 0);

        this.red = red;

        this.green = green;

        this.blue = blue;

        this.name = name;

    }

    String getName() {

        return name;

    }

    public String getHexValue() {

        return "#"+getHexValue(red)+getHexValue(green)+

             getHexValue(blue).toUpperCase();

    }

    private String getHexValue(int intValue) {

        String s = Integer.toHexString(intValue);

        if (s.length() == 1) {

            return "0"+s;

        } else {

            return s;

        }

    }

}

 

 

 

 

package constructors;

public class Person {

    private String firstName;

    private String lastName;

     

    public Person() {}

     

    public Person(String firstName, String lastName) {

        this.firstName = firstName;

        this.lastName = lastName;

    }

}

 

 

 

 

 

package constructors;

import java.util.Calendar;

import java.util.GregorianCalendar;

 

public class Person {

    private String firstName;

    private String lastName;

    private final int yearRegistered;

     

    public Person() {

        Calendar c = new GregorianCalendar();

        yearRegistered = c.get(Calendar.YEAR);

    }

     

    public Person(String firstName, String lastName) {

        this();

        this.firstName = firstName;

        this.lastName = lastName;        

    }

}

 

 

 

 

 

package constructors;

public class A {

    public A() {

        System.out.println("This is the A constructor");

    }

     

    public A(int num) {

        System.out.println("This is the A constructor with an int parameter");

    }

}

 

 

 

 

 

package constructors;

public class B extends A {

    public B() {

        System.out.println("This is constructor B");

    }

     

    public B(int num) {

        System.out.println("This is constructor B with an int parameter");

    }

 

    public static void main(String[] args) {

        B b = new B();

        B b2 = new B(1);

    }

}

 

 

 

 

 

package constructors;

public class B extends A {

    public B() {

        super(1);

        System.out.println("This is constructor B");

    }

     

    public B(int num) {

        super(num);

        System.out.println("This is constructor B with an int parameter");

    }

 

    public static void main(String[] args) {

        B b = new B();

        B b2 = new B(1);

    }

}

 

 

 

 

 

package constructors;

public class A {

    public A(int num) {

        System.out.println("This is the A constructor with an int parameter");

    }

}

 

 

 

 

 

package constructors;

public class B extends A {

    public static void main(String[] args) {

        B b = new B();

    }

}

 

 

 

 

 

package staticinit;

public class A {

    public static final int STATIC_FIELD;

    static {

        STATIC_FIELD = 22;

    }

}

 

 

 

 

 

package factorymethod;

public class A {

     

    private A() {      

    }

     

    public static A getInstance() {

        return new A();

    }

}

 

 

 

 

 

package factorymethod;

public class Single {

    private static Single single;

     

    private Single() {       

    }

     

    public static Single getInstance() {

        if (single == null) {

            single = new Single();

        }

        return single;

    }

}

 

 

 

 

 

package factorymethod;

public class Client {

    public static void main(String[] args) {

        Single s = Single.getInstance();

        Single s2 = Single.getInstance();

        assert(s == s2);

    }

}

 

 

 

 

 

 

package cloning;

public class A implements Cloneable {

    private StringBuffer sb = new StringBuffer("hello");

    private int num = 10;

     

    public A() {}

 

    public StringBuffer getSb() {

        return sb;

    }

    public void setSb(StringBuffer sb) {

        this.sb = sb;

    }

    public int getNum() {

        return num;

    }

    public void setNum(int num) {

        this.num = num;

    }

 

    @Override

    public Object clone() throws CloneNotSupportedException {

        return super.clone();

    }

}

 

 

 

 

 

 

package cloning;

public class Main {

    public static void main(String[] args) {

        try {

            A a1 = new A();

            A a2 = (A) a1.clone();

            a2.setNum(100);

            System.out.println(a1.getNum());

            System.out.println(a2.getNum());

            a2.getSb().append(" World");

            System.out.println(a1.getSb());

            System.out.println(a2.getSb());                

        } catch (CloneNotSupportedException e) {

            e.printStackTrace();

        }

    }

}

 

 

 

 

 

@Override

public Object clone() throws CloneNotSupportedException {

    try {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ObjectOutputStream oos = new ObjectOutputStream(baos);

        oos.writeObject(this);

        ByteArrayInputStream bais =

             new ByteArrayInputStream(baos.toByteArray());

        ObjectInputStream ois = new ObjectInputStream(bais);

        return ois.readObject();

    } catch (IOException e) {

       return null;

    } catch (ClassNotFoundException e) {

        return null;

    }

}