Program No. 1 One-to-one association import java.util.*; class Company { BoardOfDirectors bd; String name; Scanner scn = new Scanner(System.in); public Company() { System.out.println("Enter name of the company"); this.name = scn.next(); } void setBoardOfDirectors (BoardOfDirectors b) { bd = b; } void display() { System.out.println("Name of the company is : " + name); System.out.println("This company is managed by : "); bd.display(); } } class BoardOfDirectors { Company c; String[] s; Scanner scn = new Scanner(System.in); public BoardOfDirectors() { System.out.println("Enter number of members in board of directors"); int n = scn.nextInt(); s = new String [n]; for(int i=1;i<=n;i++) {
System.out.println("Enter name of director " + i + " : "); s[i-1] = scn.next(); } } void setCompany(Company com) { c = com; } void display() { for(int i=0;i<s.length;i++) System.out.println((i+1) + " : "s[i]); } } class Test { public static void main(String[] args) { Company c = new Company(); BoardOfDirectors b = new BoardOfDirectors(); c.setBoardOfDirectors(b); b.setCompany(c); c.display(); } } Program No. 2 One-to-many association import java.io.*; import java.util.*; class Manager { Set emp; String name; int id; Scanner scn = new Scanner(System.in); Manager() { System.out.println("Enter manager name");
this.name = scn.next(); System.out.println("Enter id"); this.id = scn.nextInt(); } void initEmp() { emp = new HashSet(); } void addEmployee(Employee e) { emp.add(e); } void displayEmp() { Iterator i = emp.iterator(); while(i.hasNext()) { Employee e = (Employee)i.next(); System.out.println(e); } } public String toString() { return "Name is : "+ name + "\nid is : "+ id+ "\n"; } }
class Employee { String name; int id ; Manager m; Scanner scn = new Scanner(System.in); Employee() {
System.out.println("Enter Employee name"); this.name = scn.next(); System.out.println("Enter id"); this.id = scn.nextInt(); } void setManager(Manager m1) { m = m1; } public String toString() { return "Name is : "+ name + "\nid is : "+ id+ "\n"; } } class Test { public static void main(String []args) { Manager m1 = new Manager(); Employee e1 = new Employee(); Employee e2 = new Employee(); Employee e3 = new Employee(); m1.initEmp(); e1.setManager(m1); e2.setManager(m1); e3.setManager(m1); m1.addEmployee(e1); m1.addEmployee(e2); m1.addEmployee(e3); m1.displayEmp(); } } Program No. 3 Many-to-many association import java.util.*; class Tournament { Scanner scn = new Scanner(System.in); String name;
Set Players; Tournament() { System.out.println("Enter name of tournament"); this.name = scn.next(); } void setPlayer(Set x) { Players = x; } void addPlayer(Player p1) { Players.add(p1); } void dispPlayer() { Iterator i = Players.iterator(); while(i.hasNext()) { Player p = (Player)i.next(); System.out.println(p); } } void removePlayer(Player p1) { Players.remove(p1); } public String toString() { return "Tournament is :" + name; } } class Player { String name; Set Tournaments; Scanner scn = new Scanner (System.in); Player() { System.out.println("Enter name of player :"); this.name = scn.next(); } void setTournament(Set x)
{ Tournaments = x; } void addTournament(Tournament t1) { Tournaments.add(t1); } void dispTournament() { Iterator i = Tournaments.iterator(); while(i.hasNext()) { Tournament t = (Tournament)i.next(); System.out.println(t); } } void removeTournament(Tournament t1) { Tournaments.remove(t1); } public String toString() { return "Player is :" + name; } } class Test { public static void main(String args[]) { Player p1 = new Player(); Player p2 = new Player(); Player p3 = new Player(); Tournament t1 = new Tournament(); Tournament t2 = new Tournament(); Tournament t3 = new Tournament(); t1.setPlayer(new HashSet()); t2.setPlayer(new HashSet()); t3.setPlayer(new HashSet()); p1.setTournament(new HashSet()); p1.addTournament(t1); p1.addTournament(t2); t1.addPlayer(p1); t2.addPlayer(p1); t3.addPlayer(p1); t1.addPlayer(p2); t2.addPlayer(p2); t3.addPlayer(p2);
t1.addPlayer(p3); t2.addPlayer(p3); t3.addPlayer(p3); System.out.println("Player of tournament t1"); t1.dispPlayer(); System.out.println("After removing a player from tournament t1 players are :"); t1.removePlayer(p1); t1.dispPlayer(); System.out.println("Tournament of p1"); p1.dispTournament(); } } Program No. 4 Adapter Pattern import java.util.*; //Adapter Pattern class A { void volume() {} } class B extends A { D a=new D(); void volume() { System.out.println("this is adapter class and control is delegated to adaptee class"); a.calcvolume(); } } class C { } class D extends C { Scanner scn= new Scanner(System.in); void calcvolume() { System.out.println("Calculating volume from adaptee class"); System.out.println("Enter side"); int n=scn.nextInt(); System.out.println("Volume is"+n*n*n); } } class adapter {
public static void main(String args[]) { A m=new A(); m.volume(); m=new B(); m.volume(); } } or class A { void volume() { System.out.println("Volume method of Class A"); } } class Adapter extends A { Adaptive a1; Adapter() { a1 = new Adaptive(); } void volume() { a1.calcVolume(); } } class B { void calcVolume() { System.out.println("Volume method of Class B"); } } class Adaptive extends B { void calcVolume() { System.out.println("Volume method of Class Adaptive"); } }
class Test { public static void main(String[] args) { A a1 = new A(); a1.volume(); a1 = new Adapter(); a1.volume(); } } Program No. 5 Singleton Pattern import java.util.*; //Singleton Pattern class Test { private static Test theInstance; private Test() { theInstance = null; } public static Test createInstance() { if(theInstance==null) { theInstance= new Test(); return theInstance; } else return theInstance; } public static void main(String []args) { Test x=createInstance(); Test y=createInstance(); if(x.equals(y)) System.out.println("both the reference variables are referring to same object"); else System.out.println("both the reference variables are not referring to same object"); } } or class Test
{ private static Test theInstance; private Test() { } public static Test get() { if(theInstance == null) return (theInstance = new Test()); else return theInstance; } public static void main (String []args) { Test e1 = get(); Test e2 = get(); if(e1.equals(e2)) System.out.println("Single instance created"); else System.out.println("Duplicate instance created"); } } Program No. 6 Observer Pattern import java.util.*; class MyClass extends Observable { int mc = 0; public void change() { this.mc = 1; setChanged(); notifyObservers(); } } class MyClass1 implements Observer { public void update(Observable o, Object s) { System.out.println("MyClass1 received change update"); } }
class MyClass2 implements Observer { public void update(Observable o, Object s) { System.out.println("MyClass2 received change update"); } } class MyClass3 implements Observer { public void update(Observable o, Object s) { System.out.println("MyClass3 received change update"); } } class Test { public static void main(String[] args) { MyClass c = new MyClass(); MyClass1 c1 = new MyClass1(); MyClass2 c2 = new MyClass2(); MyClass3 c3 = new MyClass3(); c.addObserver(c1); c.addObserver(c2); c.addObserver(c3); c.change(); } } or import java.util.*; class a extends Observable { int x=10; void inc() { x=x+1; setChanged(); } void xyz() { System.out.println("in xyz"); if(hasChanged())
{ notifyObservers(); } } } class b implements Observer { int b=5; void dis() { System.out.println("b="+b); } public void update(Observable a,Object o) { b++; } } class c implements Observer { int c=6; void dis() { System.out.println("c="+c); } public void update(Observable a,Object o) { c=c+1; //System.out.println("c="+c); } } class d implements Observer { int d=7; void dis() { System.out.println("d="+d); } public void update(Observable a,Object o) { System.out.println("in update");
d=d+1; //System.out.println("d="+d); } }
class obs { public static void main(String args[]) { a A=new a(); b B=new b(); c C=new c(); d D=new d(); A.addObserver(B); A.addObserver(C); A.addObserver(D); A.inc(); A.xyz(); B.dis(); C.dis(); D.dis(); } }