








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 14
This page cannot be seen from the preview
Don't miss anything!









Lab 04 : Introduction to Classes and Objects in Java Task 1: Employee Class Definition
Employee Information Display
Displaying Employee Information
E1.displayInfo(); } } Output:
Task 2: Student Grade Management System:
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:
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:
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: