package overloading;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

public class OverLoaded {

 

    public void printDate(Date date) {

        DateFormat sd = new SimpleDateFormat("yyyy-MM-dd");

        System.out.printf("The date is %s", sd.format(date));

    }

     

    public void printDate() {

        printDate(new Date());

    }

}

 

 

 

 

 

package overloading;

 

public class Shape {

}

 

class Circle extends Shape {

}

 

class Square extends Shape {

}

 

 

 

 

 

package overloading;

public class ShapePrinter {

    public void acceptShape(Shape s) {

        System.out.printf("Method called with shape of type %s\n",

           s.getClass().getSimpleName());

    }

   

    public void acceptShape(Circle s) {

        System.out.printf("Method called with circle of type %s\n",

             s.getClass().getSimpleName());

    }

   

    public void acceptShape(Square s) {

        System.out.printf("Method called with square of type %s\n",

            s.getClass().getSimpleName());

    }

}

 

 

 

 

 

package overloading;

public class Main {

    public static void main(String[] args) {

        ShapePrinter sp = new ShapePrinter();

        Circle c = new Circle();

        sp.acceptShape(c);

        Shape sq = new Square();

        sp.acceptShape(sq);

    }

}

 

 

 

 

 

package overloading;

public class Shape {

    public void printShape() {

        System.out.printf("Method called with shape of type %s\n",

             this.getClass().getSimpleName());

    }

}

 

class Circle extends Shape {

    public void printShape() {

        System.out.printf("Method called with circle of type %s\n",

            this.getClass().getSimpleName());

    }

}

 

class Square extends Shape {

    public void printShape() {

        System.out.printf("Method called with square of type %s\n",

            this.getClass().getSimpleName());

    }

}

 

package overloading;

public class Main2 {

    public static void main(String[] args) {

        Circle c = new Circle();

        c.printShape();

        Shape sq = new Square();

        sq.printShape();

    }

}

 

 

 

 

 

 

package visitor;

public interface Shape {

    public void visit(WordDocument wd);

    public void visit(PDFDocument pdf);

}

 

class Circle implements Shape {

    @Override

    public void visit(WordDocument wd) {

        System.out.println("Printing a circle on a Word Document");

    }

 

    @Override

    public void visit(PDFDocument pdf) {

        System.out.println("Printing a circle on a PDF Document");

    }

}

 

class Square implements Shape {

    @Override

    public void visit(WordDocument wd) {

        System.out.println("Printing a square on a Word Document");

    }

 

    @Override

    public void visit(PDFDocument pdf) {

        System.out.println("Printing a square on a PDF Document");

    }

}

 

 

 

 

 

 

package visitor;

 

public interface DocumentType {

    void accept(Shape s);

}

 

class PDFDocument implements DocumentType {

    @Override

    public void accept(Shape s) {

        s.visit(this);

    }

}

 

class WordDocument implements DocumentType {

    @Override

    public void accept(Shape s) {

        s.visit(this);

    }

}

 

 

 

 

 

 

package visitor;

public class Main {

    public static void main(String[] args) {

        DocumentType dt = new WordDocument();

        Shape s = new Square();

        dt.accept(s);

        dt = new PDFDocument();

        s = new Circle();

        dt.accept(s);

    }

}

 

 

 

 

 

 

package recusion;

public class Factorial {

    public int compute(int value) {

        if (value == 1) {

            return 1;

        }

        return value * compute(value-1);

    }

}

 

 

 

 

 

 

package recusion;

public class Factorial {

    public int compute(int value) {

        System.out.println("compute called with "+value);

        if (value == 1) {

            System.out.println("returning 1");

            return 1;

        }      

        int returnVal =  value * compute(value-1);    

        System.out.println("returning "+returnVal);

        return returnVal;

    }

}

 

 

 

 

 

 

package recusion;

public class Main {

    public static void main(String[] args) {

        Factorial f = new Factorial();

        int result = f.compute(6);

        System.out.println("The result is "+ result);

    }

}

 

 

 

 

 

 

package arguments;

public class Adder {

 

    public static void main(String[] args) {

        int[] vals = {4,7,5,1,8};

        System.out.print(add(vals));

    }

     

    private static int add(int[] vals) {

        int result = 0;

        for (int i = 0; i < vals.length; i++) {

            result+= vals[i];

        }

        return result;

   }

}

 

 

 

 

 

 

package arguments;

public class Adder {

 

    public static void main(String[] args) {

        System.out.print(add(4,7,5,1,8));

    }

     

    private static int add(int...vals) {

        int result = 0;

        for (int i = 0; i < vals.length; i++) {

            result+= vals[i];

        }

        return result;

    }

}