0% found this document useful (0 votes)
6 views16 pages

LabAssign02 (SP24-BSE-047)

The document contains a series of Java programming assignments focused on Object Oriented Programming concepts. It includes classes for managing dates, employees, addresses, books, and pizzas, along with methods for displaying information and calculating costs. Each section demonstrates the implementation of constructors, getters, setters, and logic for determining experience and other attributes.

Uploaded by

Shaheer Qureshi
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)
6 views16 pages

LabAssign02 (SP24-BSE-047)

The document contains a series of Java programming assignments focused on Object Oriented Programming concepts. It includes classes for managing dates, employees, addresses, books, and pizzas, along with methods for displaying information and calculating costs. Each section demonstrates the implementation of constructors, getters, setters, and logic for determining experience and other attributes.

Uploaded by

Shaheer Qureshi
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/ 16

Object Oriented Programming

LAB ASSIGNMENT 02

Submitted to: - Ma’am Haseena Kainat

Submitted by: - Shaheer Qureshi

Reg-no: - SP24-BSE-047
Question 01
import java.time.LocalDate;

class Date{
private int date;
private int month;
private int year;

public Date(){

public Date(int date,int month,int year){


this.date = date;
this.month = month;
this.year = year;
}

public int getDate() {


return date;
}

public void setDate(int date) {


this.date = date;
}

public int getMonth() {


return month;
}

public void setMonth(int month) {


this.month = month;
}

public int getYear() {


return year;
}

public void setYear(int year) {


this.year = year;
}
}
class Employee{
private String firstName,lastName;
Date birthDate,hireDate;

public Employee(){

}
public Employee(String firstName,String lastName,Date birthDate,Date
hireDate){
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.hireDate = hireDate;
}

public String getFirstName() {


return firstName;
}

public void setFirstName(String firstName) {


this.firstName = firstName;
}

public String getLastName() {


return lastName;
}

public void setLastName(String lastName) {


this.lastName = lastName;
}

public Date getBirthDate(){


return birthDate;
}

public void setBirthDate(Date birthDate){


this.birthDate = birthDate;
}

public Date getHireDate(){


return hireDate;
}

public void setHireDate(Date hiredate){


this.hireDate = hiredate;
}

public void display(){


System.out.println("Employee first name: "+firstName);
System.out.println("Employee last name: "+lastName);
System.out.println("Birthdate: "+birthDate.getDate() +":"
+birthDate.getMonth()+":"+birthDate.getYear());
System.out.println("Hiredate: "+hireDate.getDate() +":"
+hireDate.getMonth()+":"+hireDate.getYear());
}

public boolean isExperienced(){


LocalDate currentDate = LocalDate.now();
int currYear = currentDate.getYear();
int currMonth = currentDate.getMonthValue();
int currDate = currentDate.getDayOfMonth();

int yearDiff = currYear - hireDate.getYear();


if(yearDiff > 5){
return true;
}else if(yearDiff == 5){
if(currMonth > hireDate.getMonth()){
return true;
}else if(currMonth == hireDate.getMonth() && currDate >
hireDate.getDate()){
return true;
}
}
return false;
}
}

public class LA2T1{


public static void main(String args []){
Date birthDate = new Date(13,4,2005);
Date hireDate = new Date(14,5,2019);

Employee e1 = new
Employee("Shaheer","Qureshi",birthDate,hireDate);
System.out.println("Experienced: "+ e1.isExperienced());
e1.display();
}
}
Question 2
import java.time.LocalDate;

class Date{
private int date;
private int month;
private int year;

public Date(){

public Date(int date,int month,int year){


this.date = date;
this.month = month;
this.year = year;
}

public int getDate() {


return date;
}

public void setDate(int date) {


this.date = date;
}

public int getMonth() {


return month;
}

public void setMonth(int month) {


this.month = month;
}

public int getYear() {


return year;
}

public void setYear(int year) {


this.year = year;
}
}

class Employee{
private String firstName,lastName;
Date birthDate,hireDate;
Job job;

public Employee(){

}
public Employee(String firstName,String lastName,Date birthDate,Date
hireDate,Job job){
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.hireDate = hireDate;
this.job = job;
}

public String getFirstName() {


return firstName;
}

public void setFirstName(String firstName) {


this.firstName = firstName;
}

public String getLastName() {


return lastName;
}

public void setLastName(String lastName) {


this.lastName = lastName;
}

public Date getBirthDate(){


return birthDate;
}

public void setBirthDate(Date birthDate){


this.birthDate = birthDate;
}

public Date getHireDate(){


return hireDate;
}

public void setHireDate(Date hiredate){


this.hireDate = hiredate;
}

public Job getJob() {


return job;
}

public void setJob(Job job) {


this.job = job;
}

public void display(){


System.out.println("Employee first name: "+firstName);
System.out.println("Employee last name: "+lastName);
System.out.println("Birthdate: "+birthDate.getDate() +":"
+birthDate.getMonth()+":"+birthDate.getYear());
System.out.println("Hiredate: "+hireDate.getDate() +":"
+hireDate.getMonth()+":"+hireDate.getYear());
System.out.println("-----------------------");
System.out.println("Job Details: ");
System.out.println("Designation: "+job.getDesignation());
System.out.println("Salary: "+job.getSalary());
System.out.println("ID: "+job.getId());
}

public boolean isExperienced(){


LocalDate currentDate = LocalDate.now();
int currYear = currentDate.getYear();
int currMonth = currentDate.getMonthValue();
int currDate = currentDate.getDayOfMonth();

int yearDiff = currYear - hireDate.getYear();


if(yearDiff > 5){
return true;
}else if(yearDiff == 5){
if(currMonth > hireDate.getMonth()){
return true;
}else if(currMonth == hireDate.getMonth() && currDate >
hireDate.getDate()){
return true;
}
}
return false;
}
public boolean checkSalary(){
double Salary = job.getSalary();
if(Salary>50000)
return true;

return false;
}

public class LA2T1{


public static void main(String args []){
Date birthDate = new Date(13,4,2005);
Date hireDate = new Date(14,5,2019);
Job job1 = new Job("Software Engineer",55000,001);

Employee e1 = new
Employee("Shaheer","Qureshi",birthDate,hireDate,job1);
e1.display();
System.out.println("-------------------");
System.out.println("Experienced: "+ e1.isExperienced());

System.out.println("Salary greater than 50,000:


"+e1.checkSalary());
}}

class Job{
private String designation;
private double salary;
private int id;

public Job(){

public Job(String designation,double salary,int id){


this.designation = designation;
this.salary = salary;
this.id = id;
}

public String getDesignation(){


return designation;
}

public void setDesignation(String designation){


this.designation = designation;
}

public double getSalary() {


return salary;
}

public void setSalary(double salary) {


this.salary = salary;
}

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public class LA2T2 {


public static void main(String[] args) {

}
}

Question 3
class Address{
private int home,street;
private String city;

public Address(){

public Address(int home,int street,String city){


this.home = home;
this.street = street;
this.city = city;
}

public int getHome() {


return home;
}

public void setHome(int home) {


this.home = home;
}

public int getStreet() {


return street;
}

public void setStreet(int street) {


this.street = street;
}

public String getCity() {


return city;
}

public void setCity(String city) {


this.city = city;
}
}

class Person{
private String firstName,lastName;
private Address address;

public Person(){

public Person(String firstName, String lastName, Address address){


this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}

public String getFirstName() {


return firstName;
}

public void setFirstName(String firstName) {


this.firstName = firstName;
}

public String getLastName() {


return lastName;
}

public void setLastName(String lastName) {


this.lastName = lastName;
}

public Address getAddress() {


return address;
}

public void setAddress(Address address) {


this.address = address;
}

public void display(){


System.out.println("First Name: "+firstName);
System.out.println("Last Name: "+lastName);
System.out.println("House No: "+address.getHome());
System.out.println("Street no: "+address.getStreet());
System.out.println("City: "+address.getCity());
}

public boolean inIslamabad(){


String currCity = address.getCity();
if(currCity.equals("Islamabad")){
return true;
}
return false;
}

public class LA2T3 {


public static void main(String[] args) {
Address a1 = new Address(164,6,"Islamabad");
Person p1 = new Person("Shaheer","Qureshi",a1);

p1.display();
System.out.println("--------------");
System.out.println("Lives in Islamabad: "+p1.inIslamabad());
}
}

Question 4
class Book{
private String bookName,publisher;
Person author;

public Book(){
}

public Book(String bookName, String publisher, Person author){


this.bookName = bookName;
this.publisher = publisher;
this.author = author;
}

public void display(){


System.out.println("Book name: "+bookName);
System.out.println("Publisher: "+publisher);
System.out.print("Author Name: "+author.getFirstName()+"
"+author.getLastName());
}

public boolean lessThanTen(){


return author.getAddress().getStreet()<10;
}
}
public class LA2T4 {
public static void main(String[] args) {
Address a1 = new Address(164,6,"Islamabad");
Person p1 = new Person("Shaheer","Qureshi",a1);
Book b1 = new Book("Cats","NewYork times",p1);

System.out.println(b1.lessThanTen());
System.out.println("Before modifiying address");
p1.display();
System.out.println("-----------------------");
System.out.println();

Address newAddress = new Address(170,9,"Rawalpindi");


p1.setAddress(newAddress);
System.out.println("After modifiying address");
p1.display();

}
}

Question 5
class Pizza{
private String size;
private int cheesetoppings,pepptoppings,hamtoppings;

public Pizza(){}
public Pizza(String size, int cheesetoppings,int pepptoppings,int
hamtoppings){
this.size = size;
this.cheesetoppings = cheesetoppings;
this.pepptoppings = pepptoppings;
this.hamtoppings = hamtoppings;
}

public String getSize() {


return size;
}

public void setSize(String size) {


this.size = size;
}

public int getCheesetoppings() {


return cheesetoppings;
}

public void setCheesetoppings(int cheesetoppings) {


this.cheesetoppings = cheesetoppings;
}

public int getPepptoppings() {


return pepptoppings;
}
public void setPepptoppings(int pepptoppings) {
this.pepptoppings = pepptoppings;
}

public int getHamtoppings() {


return hamtoppings;
}

public void setHamtoppings(int hamtoppings) {


this.hamtoppings = hamtoppings;
}

public double calcCost(){


double cost;
if(size.equalsIgnoreCase("small")){
cost = 10 + (2*(pepptoppings+cheesetoppings+hamtoppings));
}else if(size.equalsIgnoreCase("Medium")){
cost = 12 + (2*(pepptoppings+cheesetoppings+hamtoppings));
}else{
cost = 14 + (2*(pepptoppings+cheesetoppings+hamtoppings));
}
return cost;
}

public String getDescription(){


return "Size: " + size + " || Cheese toppings: " +cheesetoppings
+" || Pepperoni Toppings: "+pepptoppings +" || Ham Toppings: "
+hamtoppings;
}
}

class PizzaOrder{
Pizza [] pizza = new Pizza[3];
int numOfOrder=0;

public PizzaOrder(){
this.pizza = new Pizza[3];
}

public void addPizza(Pizza p){


if(numOfOrder < 3){
this.pizza[numOfOrder] = p;
numOfOrder ++;
}else{
System.out.println("Order limit exceeded");
}
}
public void setPizza1(Pizza p1){
this.pizza[0] = p1;
}

public void setPizza2(Pizza p2){


this.pizza[1] = p2;
}

public void setPizza3(Pizza p3){


this.pizza[2] = p3;
}

public double calcTotal(){


double totalCost=0;

for(int i=0;i<pizza.length;i++){
totalCost += pizza[i].calcCost();
}

return totalCost;
}

public void getOrderDetails(){


for(int i=0;i<pizza.length;i++){
System.out.println("Pizza: " + (i+1) +" " +
pizza[i].getDescription() );
System.out.println("--------------------------------------");
}
System.out.println("Total order cost: "+ calcTotal());
}
}

public class LA2T5 {


public static void main(String[] args) {
Pizza p1 = new Pizza("Large", 2,3,4);
Pizza p2 = new Pizza("Small",4,5,1);
Pizza p3 = new Pizza("Medium",6,7,3);

PizzaOrder order1 = new PizzaOrder();


order1.addPizza(p2);
order1.addPizza(p3);
order1.addPizza(p1);

System.out.println("Order 1 Details: ");


order1.getOrderDetails();
}
}

You might also like