Record Oops - Exp 4
Record Oops - Exp 4
No: URK23CS1248
CREATING USER DEFINED DATATYPES USING CLASSES AND
Ex. No. 4 OBJECTS
1. Create a class called time that has separate int member data for hours, minutes and
seconds. One constructor should initialize this data to 0, and another should initialize
it to fixed values. Another member function should display it, in 11:59:59 format. The
final member function should add two objects of type time passed as arguments.
A main () program should create two initialized time objects and one that isn't
initialized. Then it should add the two initialized values together, leaving the result in
the third variable. Finally, it should display the value of this variable. Make appropriate
member functions const.
Aim:
To create a Time class in Java that encapsulates hours, minutes, and seconds, allowing
for initialization, addition, and formatted display of time values.
Description:
Objects:
In object-oriented programming (OOP), an object is an instance of a class. A class is a
blueprint or template that defines the properties (attributes) and behaviors (methods) of an object.
Objects are the fundamental building blocks of OOP and are used to represent real-world entities
or abstract concepts. Objects interact with each other by calling methods or accessing each
other's attributes.
Constructors:
A constructor is a special method in a class that is automatically called when an object is
created. The main purpose of a constructor is to initialize the object's attributes with appropriate
values. Constructors have the same name as the class and do not have a return type.
Eg: public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
20CS2033-Object Oriented Programming Lab Reg. No: URK23CS1248
Setters:
A setter method is used to modify the value of an attribute. Setters allow you to perform
validation or additional processing before assigning a value to an attribute.
Eg: public void setAttributeName(dataType value) {
this.attributeName = value;
}
Getters:
A getter method is used to retrieve the value of an attribute. Getters provide a way to
access the value of an attribute while maintaining control over how it is accessed.
Eg: public dataType getAttributeName() {
return this.attributeName;
}
Algorithm:
1. START
2. Import the Scanner class for user input and define the exp4_1 class with private attributes
for hours, minutes, and seconds.
3. Create a constructor that initializes the time attributes and calls the normalize() method to
ensure valid time representation.
4. Implement a display() method to format and print the time in "HH:MM:SS" format.
5. Implement an add() method that takes another exp4_1 object as a parameter and returns a
new exp4_1 object representing the sum of the two times.
6. Implement a private normalize() method to adjust the seconds and minutes to ensure they
are within valid ranges, wrapping around hours to fit a 24-hour format.
7. In the main() method, create a Scanner object to read user input for the first time (hours,
minutes, seconds).
8. Create the first exp4_1 object using the user-provided input and repeat the process to read
input for the second time.
9. Create a result exp4_1 object and call the add() method to sum the two time objects.
10. Display the values of the first time, second time, and the resulting time using the
display() method.
11. STOP
Program:
package EXP4;
import java.util.Scanner;
public class exp4_1 {
private int hours;
private int minutes;
private int seconds;
20CS2033-Object Oriented Programming Lab Reg. No: URK23CS1248
result = time1.add(time2);
System.out.print("Time 1: ");
time1.display();
System.out.print("Time 2: ");
time2.display();
System.out.print("Result: ");
result.display();
}
}
Output Screenshot:
Result:
2. Write a menu driven application to maintain the department details of a University using
JAVA. Use constructors, getter and setter functions. Your application must contain the
following functionalities.
a) For each department maintain the following details:
i. deptName ii. hodName iii. noOfFaculty iv. noOfStudents v. noOfPrograms
b) Get the department details from user(admin)
c) In the menu give the user options to add, edit, delete or display the department
details
AIM:
To implement a menu-driven application that allows users to manage university
department details by providing options to add, edit, delete, or display department information
using constructors, getter and setter functions.
ALGORITHM:
1. START
2. Initialize variables: Create an array department for up to 10 departments, initialize
departmentCount to 0, and create a Scanner object for user input.
3. Display menu: Print the menu options (Add, Edit, Delete, Display, Exit) and prompt the
user for their choice.
4. Process user choice: Use a switch statement to handle the user's selection:
• Add: Gather department details from the user and add the department to the array.
• Edit: Allow the user to modify an existing department's details.
• Delete: Remove a department from the array based on user input.
• Display: Show all current department details.
• Exit: Print an exit message and terminate the program.
5. Repeat menu: Continue displaying the menu and processing user choices until the user
selects the exit option.
6. Implement department operations: Use the addDepartment(), editDepartment(),
deleteDepartment(), and displayDepartments() methods to handle the corresponding
functionality.
7. End program: Exit the application gracefully when the user chooses to exit.
8. STOP.
20CS2033-Object Oriented Programming Lab Reg. No: URK23CS1248
PROGRAM:
import java.util.Scanner;
public class exp4_8 {
private static Department[] departments = new Department[10];
private static int departmentCount = 0;
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int choice;
do {
System.out.println("\n--- University Department Management ---");
System.out.println("1. Add Department");
System.out.println("2. Edit Department");
System.out.println("3. Delete Department");
System.out.println("4. Display Departments");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
sc.nextLine(); // Consume newline
switch (choice) {
case 1:
addDepartment();
break;
case 2:
editDepartment();
break;
case 3:
deleteDepartment();
break;
case 4:
displayDepartments();
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice! Please try again.");
}
} while (choice != 5);
}
private static void addDepartment() {
if (departmentCount >= departments.length) {
20CS2033-Object Oriented Programming Lab Reg. No: URK23CS1248
OUTPUT:
RESULT:
The following code has been executed and verified successfully.