Lab 4: Classes and Objects in Java for Object Oriented Programming, Exercises of Object Oriented Programming

A lab exercise for cs212: object oriented programming, focusing on the creation and manipulation of classes and objects in java. The lab involves defining three classes: employee, student, and product, and a fourth class, inventory, to manage an inventory of products. The exercise includes creating individual objects, updating attributes, and displaying information for each class. The document also covers the creation of a hospital class to manage patient data, including adding new patients, updating medical conditions, and searching for patients by id.

Typology: Exercises

2023/2024

Uploaded on 03/21/2024

ushba-fatima
ushba-fatima 🇵🇰

2 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS212: Object Oriented Programming Page 1
Faculty of Computing
CS212: Object Oriented
Programming Class: BESE-13B
Lab 4: Classes and Objects
Date: 6th March, 2024
Time: 10:00 am 1:00
pm
2:00pm 5:00 pm
Name: Ushba Fatima
CMS: 467212
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Lab 4: Classes and Objects in Java for Object Oriented Programming and more Exercises Object Oriented Programming in PDF only on Docsity!

Faculty of Computing

CS212: Object Oriented

Programming Class: BESE- 1 3B

Lab 4 : Classes and Objects

Date: 6

th

March, 2024

Time: 10:00 am – 1:

pm

2:00pm – 5:00 pm

Name: Ushba Fatima

CMS: 467212

Lab 04 : Introduction to Classes and Objects in Java Task 1: Employee Class Definition

Define a class named Employee with the following attributes:

  • name : representing the name of the employee (type: String)
  • id : representing the employee's ID (type: int)
  • position : representing the employee's position (type: String)
  • salary : representing the employee's salary (type: double)

The class should also include a constructor to initialize these attributes.

Employee Information Display

  • Create a method within the Employee class named displayInfo() to display the details of

an employee.

  • The displayInfo() method prints out the name, ID, position, and salary of the employee. Creating Employee Objects
  • In the main method of the EmployeeManagementSystem class, create individual

Employee objects with specific details using the constructor.

Displaying Employee Information

  • Call the displayInfo() method for each employee object to display their details. Updating Employee Details

Update the details of an employee directly by accessing and modifying the instance variables of the

Employee object.

E1.displayInfo(); } } Output:

Task 2: Student Grade Management System:

Develop a Java program to manage student grades. Define a class Student with attributes such as name,

roll number, and grades in different subjects. Implement methods to add new students, update grades,

calculate average grades, and display student information. Demonstrate the program by creating student

objects, adding grades for each student, calculating their average grades, and displaying student details.

Code: Student Class: class Student { String name; int rollNumber; int numSubjects= 0 ; int maxSubjects= 5 ; String[] subjects= new String[maxSubjects]; String[] grades= new String[maxSubjects]; Student(String name, int rollNumber) { this.name = name; this.rollNumber = rollNumber; } public void addGrade(String subject, String grade) { subjects[numSubjects] = subject; grades[numSubjects] = grade; numSubjects++; } public void updateGrade(String subject, String grade) { for (int i = 0 ; i < numSubjects; i++) { if (subjects[i]==subject) { grades[i] = grade; return; } } System. out .println("Subject not found for this student."); } public String averageGrade() { int total = 0 ; int gradeNumber; String avgGrade; int avgGradeNumber; for (int i = 0 ; i < numSubjects; i++) {

Main Program: public class StudentManagementSystem { public static void main(String[] args) { Student student1 = new Student("Alice", 101 ); Student student2 = new Student("Bob", 102 ); System. out .println("\n\t\tSTUDENT MANAGEMNET SYSTEM\n\t\t--------------- ----------\n"); student1.addGrade("Math", "A+"); student1.addGrade("Science", "A"); student1.addGrade("English", "F"); student1.addGrade("Computer", "B"); student1.addGrade("Urdu", "F"); student2.addGrade("Math", "B"); student2.addGrade("Science", "C"); student2.addGrade("English", "A"); student2.addGrade("Computer", "D"); student2.addGrade("Urdu", "A+"); student1.updateGrade("Math", "B"); student1.displayInfo(); student2.displayInfo(); } }

Task 3: Product Inventory System:

Create a Java application to manage product inventory for a store. Define a class Product with attributes

like name, ID, quantity, and price. Implement methods to add new products, update quantities, calculate

the total value of the inventory, and display product information. Demonstrate the program by creating

product objects, adding them to the inventory, updating quantities, calculating the total value of the

inventory, and displaying product details.

Code: Product Class: class Product { String name; int ID; int quantity; double price; public Product(String name, int ID, int quantity, double price) { this.name = name; this.ID = ID; this.quantity = quantity; this.price = price; } public void updateQuantity(int newQuantity) { quantity = newQuantity; } public double calculateProductValue() { return quantity * price; } public void displayProductInfo() { System. out .println("Product Name: " + name); System. out .println("Product ID: " + ID); System. out .println("Quantity: " + quantity); System. out .println("Price: $" + price); System. out .println("Total Value: $" + calculateProductValue()); } } Inventory Management:

System. out .println("\t\tBAKERY INVENTORY MANAGEMNENT\n\t\t----------- -----------------\n"); Product product1 = new Product("Biscuits", 108 , 5 , 40 ); Product product2 = new Product("Cake", 92 , 20 , 534.50); Product product3 = new Product("Donut", 231 , 16 , 110 ); bakery.addProduct(product1); bakery.addProduct(product2); bakery.addProduct(product3); bakery.updateQuantity("Donut", 26 ); bakery.displayInventoryProducts(); bakery.displayInventoryDetails(); System. out .println("Total Inventory Value: $" + bakery.calculateInventoryValue()); } } Output:

Task 4: Hospital Patient Management System:

Design a Java program to manage patient information in a hospital. Define a class Patient with attributes

such as name, ID, age, and medical condition. Implement methods to add new patients, update medical

conditions, search for patients by ID, and display patient information. Demonstrate the program by

creating patient objects, adding them to the hospital database, updating medical conditions for some

patients, searching for patients by their ID, and displaying patient details.

Code: Patient Class: public class Patient { String name; int ID; int age; String medicalCondition; public Patient(String name, int ID, int age, String medicalCondition) { this.name = name; this.ID = ID; this.age = age; this.medicalCondition = medicalCondition; } public void updateMedicalCondition(String newCondition) { medicalCondition = newCondition; } public void displayPatientInfo() { System. out .println("Patient Name: " + name); System. out .println("Patient ID: " + ID); System. out .println("Age: " + age); System. out .println("Medical Condition: " + medicalCondition); } }

public static void main(String[] args) { Scanner input= new Scanner(System. in ); Hospital hospital = new Hospital( 10 ); System. out .println("\t\tHOSPITAL PATIENT MANAGEMENT\n\t\t------------ --------------\n"); Patient patient1 = new Patient("John", 101 , 35 , "Fever"); Patient patient2 = new Patient("Smith", 102 , 45 , "Broken Arm"); Patient patient3 = new Patient("Alice", 103 , 28 , "Headache"); hospital.addPatient(patient1); hospital.addPatient(patient2); hospital.addPatient(patient3); hospital.updateMedicalCondition( 102 , "Flu"); hospital.displayPatients(); int patientID; System. out .println("Enter Patient ID to Search: "); patientID=input.nextInt(); hospital.searchPatientByID(patientID); } }

Output: