0% found this document useful (0 votes)
68 views5 pages

Name: M.Hamza ROLL NO: F178171 Section A Lab Ooad:: Question#01

This document contains code examples demonstrating the Factory and Singleton design patterns. For the Factory pattern, it shows code for an Employee class hierarchy with HourlyWorker and PieceWorker subclasses, and a GetEmp class that returns an object of the appropriate subclass based on a string. For the Singleton pattern, it provides code for a Singleton class to ensure only one instance is created. The document contains code snippets in Java along with descriptions and is submitted as part of a lab assignment on object-oriented analysis and design patterns.

Uploaded by

Muhammad Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views5 pages

Name: M.Hamza ROLL NO: F178171 Section A Lab Ooad:: Question#01

This document contains code examples demonstrating the Factory and Singleton design patterns. For the Factory pattern, it shows code for an Employee class hierarchy with HourlyWorker and PieceWorker subclasses, and a GetEmp class that returns an object of the appropriate subclass based on a string. For the Singleton pattern, it provides code for a Singleton class to ensure only one instance is created. The document contains code snippets in Java along with descriptions and is submitted as part of a lab assignment on object-oriented analysis and design patterns.

Uploaded by

Muhammad Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

NAME: M.

HAMZA

ROLL NO: F178171

SECTION A

LAB OOAD:

Question#01:

FACTORY Design Pattern EXAMPLE:


Employee hourly and piece worker:
Employee is the main abstract class which is hourlyworker or pieceworker.In main function I called the
function of that class which return the object of that class.and I pass the string which class has to be
called.Then I through that object I called my functions which I had to called through that object.

Code:
EMPLOYEE.JAVA:
package factory;

public abstract class Employee {


abstract void earning();
abstract void setdata();
abstract void getdata();

}

HOURWORKER.JAVA:
package factory;
import java.util.Scanner;
public class hourworker extends Employee {
String name;
float hours;
float wage;
public
void setdata() {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name ");
name=input.nextLine();
System.out.println("Enter Number of wages ");
wage=input.nextFloat();
System.out.println("Enter Number of hours worked ");
hours=input.nextFloat();
}
void getdata() {
System.out.println("Name of hourly worker ");
System.out.println(name);
System.out.println("Name of wages are ");
System.out.println(wage);
System.out.println("Working of hourly worker ");
System.out.println(hours);

}
void earning() {
if(hours<40) {
wage=wage*hours;
System.out.printf("Earning Wage of hourly worker ");
System.out.println(wage);
}
else {
wage=(float) ((wage)*(1.5)*(hours));
System.out.println(wage);

}
}

PIECEWORKER.JAVA:
package factory;

import java.util.Scanner;

public class pieceworker extends Employee {


String name;
float hours;
float wage;
public
void setdata() {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name ");
name=input.nextLine();
System.out.println("Enter Number of wages ");
wage=input.nextFloat();
System.out.println("Enter Number of hours worked ");
hours=input.nextFloat();
}
void getdata() {
System.out.println("Name of piece worker ");
System.out.println(name);
System.out.println("wages are ");
System.out.println(wage);
System.out.println("Working of hourly worker ");
System.out.println(hours);
}
void earning() {
if(hours<40) {
wage=wage*hours;
System.out.println("Earning Wage of piece worker ");
System.out.println(wage);

}
}

GETEMP.JAVA //RETURN OBJECT OF CLASS


package factory;
import java.util.Scanner;
public class getemp {
public static Employee getemp1(String emp) {
if(emp==null) {
return null;
}
if(emp.equalsIgnoreCase("hourly")) {
return new hourworker();
}
else if(emp.equalsIgnoreCase("piece")) {
return new pieceworker();

}
return null;
}

Main.java:
package factory;
import java.util.Scanner;
public class main1 {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
String employeetype;
System.out.println("Enter employee type : ");
employeetype=input.nextLine();
Employee obj=getemp.getemp1(employeetype);
obj.setdata();
obj.getdata();
obj.earning();

Screenshot:

Question#02”

Singleton pattern example:

Code:

You might also like