0% found this document useful (0 votes)
25 views10 pages

Record Oops - Exp 4

Uploaded by

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

Record Oops - Exp 4

Uploaded by

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

20CS2033-Object Oriented Programming Lab Reg.

No: URK23CS1248
CREATING USER DEFINED DATATYPES USING CLASSES AND
Ex. No. 4 OBJECTS

Date of Exercise 07/08/24

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

public exp4_1(int h, int m, int s) {


hours = h;
minutes = m;
seconds = s;
normalize();
}

public void display() {


System.out.printf("%02d:%02d:%02d%n", hours, minutes, seconds);
}
public exp4_1 add(exp4_1 other) {
return new exp4_1(hours + other.hours, minutes + other.minutes, seconds +
other.seconds);
}

private void normalize() {


if (seconds >= 60) {
minutes += seconds / 60;
seconds %= 60;
}
if (minutes >= 60) {
hours += minutes / 60;
minutes %= 60;
}
hours %= 24; // Wrap around to fit in a 24-hour format
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter hours, minutes, and seconds for Time 1:");


System.out.print("Hours: ");
int h1 = scanner.nextInt();
System.out.print("Minutes: ");
int m1 = scanner.nextInt();
System.out.print("Seconds: ");
int s1 = scanner.nextInt();

exp4_1 time1 = new exp4_1(h1, m1, s1);


20CS2033-Object Oriented Programming Lab Reg. No: URK23CS1248

System.out.println("Enter hours, minutes, and seconds for Time 2:");


System.out.print("Hours: ");
int h2 = scanner.nextInt();
System.out.print("Minutes: ");
int m2 = scanner.nextInt();
System.out.print("Seconds: ");
int s2 = scanner.nextInt();

exp4_1 time2 = new exp4_1(h2, m2, s2);


exp4_1 result = new exp4_1(0,0,0);

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:

The following code has been executed and verified successfully.


20CS2033-Object Oriented Programming Lab Reg. No: URK23CS1248

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

System.out.println("Cannot add more departments. Maximum limit reached.");


return;
}
System.out.print("Enter Department Name: ");
String deptName = sc.nextLine();
System.out.print("Enter HOD Name: ");
String hodName = sc.nextLine();
System.out.print("Enter Number of Faculty: ");
int noOfFaculty = sc.nextInt();
System.out.print("Enter Number of Students: ");
int noOfStudents = sc.nextInt();
System.out.print("Enter Number of Programs: ");
int noOfPrograms = sc.nextInt();
sc.nextLine(); // Consume newline
Department department = new Department(deptName, hodName, noOfFaculty,
noOfStudents, noOfPrograms);
departments[departmentCount++] = department; // Add department to array
System.out.println("Department added successfully.");
}
private static void editDepartment() {
System.out.print("Enter Department Name to edit: ");
String deptName = sc.nextLine();
for (int i = 0; i < departmentCount; i++) {
if (departments[i].getDeptName().equalsIgnoreCase(deptName)) {
System.out.print("Enter new HOD Name: ");
departments[i].setHodName(sc.nextLine());
System.out.print("Enter new Number of Faculty: ");
departments[i].setNoOfFaculty(sc.nextInt());
System.out.print("Enter new Number of Students: ");
departments[i].setNoOfStudents(sc.nextInt());
System.out.print("Enter new Number of Programs: ");
departments[i].setNoOfPrograms(sc.nextInt());
sc.nextLine(); // Consume newline
System.out.println("Department details updated successfully.");
return;
}
}
System.out.println("Department not found.");
}
private static void deleteDepartment() {
System.out.print("Enter Department Name to delete: ");
String deptName = sc.nextLine();
20CS2033-Object Oriented Programming Lab Reg. No: URK23CS1248

for (int i = 0; i < departmentCount; i++) {


if (departments[i].getDeptName().equalsIgnoreCase(deptName)) {
for (int j = i; j < departmentCount - 1; j++) {
departments[j] = departments[j + 1];
}
departments[departmentCount - 1] = null; // Clear the last element
departmentCount--; // Decrease the count
System.out.println("Department deleted successfully.");
return;
}
}
System.out.println("Department not found.");
}
private static void displayDepartments() {
if (departmentCount == 0) {
System.out.println("No departments available.");
} else {
System.out.println("\n--- Department Details ---");
for (int i = 0; i < departmentCount; i++) {
System.out.println(departments[i]);
}
}
}
}
class Department {
private String deptName;
private String hodName;
private int noOfFaculty;
private int noOfStudents;
private int noOfPrograms;
// Constructor
public Department(String deptName, String hodName, int noOfFaculty, int noOfStudents, int
noOfPrograms) {
this.deptName = deptName;
this.hodName = hodName;
this.noOfFaculty = noOfFaculty;
this.noOfStudents = noOfStudents;
this.noOfPrograms = noOfPrograms;
}
// Getters and Setters
public String getDeptName() { return deptName; }
public void setDeptName(String deptName) { this.deptName = deptName; }
20CS2033-Object Oriented Programming Lab Reg. No: URK23CS1248

public String getHodName() { return hodName; }


public void setHodName(String hodName) { this.hodName = hodName; }
public int getNoOfFaculty() { return noOfFaculty; }
public void setNoOfFaculty(int noOfFaculty) { this.noOfFaculty = noOfFaculty; }
public int getNoOfStudents() { return noOfStudents; }
public void setNoOfStudents(int noOfStudents) { this.noOfStudents = noOfStudents; }
public int getNoOfPrograms() { return noOfPrograms; }
public void setNoOfPrograms(int noOfPrograms) { this.noOfPrograms = noOfPrograms; }
public String toString() {
return "Department Name: " + deptName +
", HOD Name: " + hodName +
", Number of Faculty: " + noOfFaculty +
", Number of Students: " + noOfStudents +
", Number of Programs: " + noOfPrograms;
}
}
20CS2033-Object Oriented Programming Lab Reg. No: URK23CS1248

OUTPUT:

RESULT:
The following code has been executed and verified successfully.

You might also like