0% found this document useful (0 votes)
4 views4 pages

Java Practical Sheet

The document outlines a lab practical for a Java Programming course, focusing on creating a Student Record Management System using Object-Oriented Programming principles. It details the objectives, theory of encapsulation, an algorithm for managing student records, and provides source code for the Student and StudentManagementSystem classes. The conclusion confirms the successful execution of the program and the implementation of key programming concepts.

Uploaded by

zyackgrg111
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)
4 views4 pages

Java Practical Sheet

The document outlines a lab practical for a Java Programming course, focusing on creating a Student Record Management System using Object-Oriented Programming principles. It details the objectives, theory of encapsulation, an algorithm for managing student records, and provides source code for the Student and StudentManagementSystem classes. The conclusion confirms the successful execution of the program and the implementation of key programming concepts.

Uploaded by

zyackgrg111
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

LAB PRACTICAL SHEET

Subject: Java Programming | Program: BICTE | Semester: 5th

Name: Aamos Practical No: 1 Date: _______________ Roll No: _______

Title
Student Record Management System (Console-based Java Application)

Objective
To develop a menu-driven Java console application that demonstrates the core Object-Oriented
Programming concepts of classes, objects, and encapsulation, while using the ArrayList collection to add,
view, search, update, and delete student records.

Theory
Encapsulation is an OOP principle where the internal state (fields) of an object is hidden from outside access
and can only be modified through public methods (getters and setters). In this program, the Student class
keeps its fields private and exposes controlled access through methods.
An ArrayList is a resizable array implementation from the Java Collections Framework, allowing dynamic
addition and removal of elements, unlike a fixed-size array. It is used here to store an unknown number of
Student objects at runtime.

Algorithm
1. Start the program and display the main menu.
2. Read the user's choice.
3. If choice is Add Student, read student details and add a new Student object to the list.
4. If choice is View All, display every student's details.
5. If choice is Search, read an ID and display the matching record, if found.
6. If choice is Update Marks, read an ID and update the marks field of the matching student.
7. If choice is Delete, read an ID and remove the matching student from the list.
8. Repeat from step 1 until the user chooses Exit.
9. Stop.

Source Code
[Link]
// [Link]
// Represents a single student record.
// Demonstrates ENCAPSULATION: fields are private, accessed only via getters/setters.

public class Student {


private int id;
private String name;
private String faculty;
private double marks;

public Student(int id, String name, String faculty, double marks) {


[Link] = id;
[Link] = name;
[Link] = faculty;
[Link] = marks;
}

// Getters
public int getId() { return id; }
public String getName() { return name; }
public String getFaculty() { return faculty; }
public double getMarks() { return marks; }

// Setters
public void setName(String name) { [Link] = name; }
public void setFaculty(String faculty) { [Link] = faculty; }
public void setMarks(double marks) { [Link] = marks; }

// Returns a grade based on marks — simple example of a method acting on


// the object's own state.
public String getGrade() {
if (marks >= 90) return "A+";
if (marks >= 80) return "A";
if (marks >= 70) return "B+";
if (marks >= 60) return "B";
if (marks >= 50) return "C";
return "F";
}

@Override
public String toString() {
return "ID: " + id +
" | Name: " + name +
" | Faculty: " + faculty +
" | Marks: " + marks +
" | Grade: " + getGrade();
}
}

[Link]
// [Link]
// A simple console-based Student Record Management System.
// Demonstrates: classes & objects, encapsulation, ArrayList (collections),
// and basic exception handling.

import [Link];
import [Link];

public class StudentManagementSystem {

private static ArrayList<Student> students = new ArrayList<>();


private static Scanner sc = new Scanner([Link]);

public static void main(String[] args) {


int choice;

do {
printMenu();
choice = readInt("Enter your choice: ");

switch (choice) {
case 1 -> addStudent();
case 2 -> viewAllStudents();
case 3 -> searchStudent();
case 4 -> updateMarks();
case 5 -> deleteStudent();
case 6 -> [Link]("Exiting program. Goodbye!");
default -> [Link]("Invalid choice. Try again.");
}
} while (choice != 6);

[Link]();
}
private static void printMenu() {
[Link]("\n===== STUDENT MANAGEMENT SYSTEM =====");
[Link]("1. Add Student");
[Link]("2. View All Students");
[Link]("3. Search Student by ID");
[Link]("4. Update Marks");
[Link]("5. Delete Student");
[Link]("6. Exit");
}

private static void addStudent() {


int id = readInt("Enter ID: ");
if (findStudent(id) != null) {
[Link]("A student with this ID already exists.");
return;
}
[Link]("Enter Name: ");
String name = [Link]();
[Link]("Enter Faculty: ");
String faculty = [Link]();
double marks = readDouble("Enter Marks: ");

[Link](new Student(id, name, faculty, marks));


[Link]("Student added successfully.");
}

private static void viewAllStudents() {


if ([Link]()) {
[Link]("No student records found.");
return;
}
[Link]("\n--- All Students ---");
for (Student s : students) {
[Link](s);
}
}

private static void searchStudent() {


int id = readInt("Enter ID to search: ");
Student s = findStudent(id);
if (s != null) {
[Link]("Student found:");
[Link](s);
} else {
[Link]("No student found with ID " + id);
}
}

private static void updateMarks() {


int id = readInt("Enter ID to update: ");
Student s = findStudent(id);
if (s != null) {
double newMarks = readDouble("Enter new marks: ");
[Link](newMarks);
[Link]("Marks updated successfully.");
} else {
[Link]("No student found with ID " + id);
}
}

private static void deleteStudent() {


int id = readInt("Enter ID to delete: ");
Student s = findStudent(id);
if (s != null) {
[Link](s);
[Link]("Student deleted successfully.");
} else {
[Link]("No student found with ID " + id);
}
}

// Helper: find a student by ID, or return null


private static Student findStudent(int id) {
for (Student s : students) {
if ([Link]() == id) return s;
}
return null;
}

// Helper methods for safe input reading


private static int readInt(String prompt) {
[Link](prompt);
while (![Link]()) {
[Link]("Please enter a valid number: ");
[Link]();
}
int value = [Link]();
[Link](); // consume leftover newline
return value;
}

private static double readDouble(String prompt) {


[Link](prompt);
while (![Link]()) {
[Link]("Please enter a valid number: ");
[Link]();
}
double value = [Link]();
[Link]();
return value;
}
}

Output

(Paste your program's console output / screenshot here)

Conclusion
The program was successfully executed, and the concepts of classes, objects, encapsulation, and the
ArrayList collection were implemented and verified.

You might also like