0% found this document useful (0 votes)
52 views12 pages

JAVA MICRO Project Sem 4, IT, GTU

The document discusses object-oriented programming concepts like class, object, inheritance, polymorphism, encapsulation and abstraction. It also discusses general programming concepts like serialization, file I/O, collections, generics, exception handling and input handling. It provides an implementation of a college management system demonstrating these concepts.

Uploaded by

aammkrun
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)
52 views12 pages

JAVA MICRO Project Sem 4, IT, GTU

The document discusses object-oriented programming concepts like class, object, inheritance, polymorphism, encapsulation and abstraction. It also discusses general programming concepts like serialization, file I/O, collections, generics, exception handling and input handling. It provides an implementation of a college management system demonstrating these concepts.

Uploaded by

aammkrun
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/ 12

Object-Oriented Programming Concepts:

❑ Class and Object: Class and ObjectDescription: A class is a


blueprint for creating objects, providing initial values for state
(member variables or fields), and implementations of behavior
(member functions or methods). In the provided project,
College, Department, and Student are classes, and instances of
these classes are objects.

❑ Inheritance: InheritanceDescription: Inheritance is a


mechanism wherein a new class inherits the properties and
behaviors of another class. The Department class inherits from
the College class, and the Student class inherits from the
Department class.

❑ Polymorphism: Polymorphism allows methods to do different


things based on the object it is acting upon, even when using a
single interface. In this project, method overriding
demonstrates polymorphism. The toString method in the
Student class overrides the toString method from the Object
class

.
❑ Encapsulation: Encapsulation is the technique of making
the fields in a class private and providing access via public
methods. This is used to protect the data within an object
from unintended modification. While the project could
further encapsulate fields by making them private and
providing getter and setter methods, it already encapsulates
the functionality within class methods.

❑ Abstraction: Abstraction is the concept of hiding the


complex implementation details and showing only the
necessary features of an object. The classes in this project
abstract the idea of College, Department, and Student
without showing the internal complexities

General Programming Concepts:

.❑ Serialization: Serialization is the process of converting an


object into a byte stream, which can then be reverted back
into a copy of the object. The project uses serialization to
save and load Student objects to and from a file using
ObjectOutputStream and ObjectInputStream.
❑ File I/O: File I/O refers to reading from and writing to files.
This project demonstrates file I/O by saving and loading
student data to and from files.

❑ Collections : Collections are used to store and manipulate


groups of objects. The project uses an ArrayList to manage
the collection of Student objects

❑ Generics: Generics enable types (classes and interfaces) to


be parameters when defining classes, interfaces, and
methods. This allows for type-safe collections. In the project,
the ArrayList<Student> utilizes generics.

❑ Comparator and Comparable:These interfaces are used


for sorting collections. The project uses Comparator to sort
Student objects based on their marks in descending order.

❑ Exception Handling: Exception HandlingDescription:


Exception handling is a mechanism to handle runtime
errors, ensuring the normal flow of the application. The
.
project uses try-catch blocks to handle IOException and
ClassNotFoundException during file operations.

❑ Input Handling: Input HandlingDescription: Input handling


refers to capturing and processing user input. The project
uses Scanner to read input from the console for various
operations.
-: Implementation :-
1. import java.io.*;
2. import java.util.ArrayList;
3. import java.util.Collections;
4. import java.util.Comparator;
5. import java.util.Scanner;
6.
7. // Base class College
8. class College implements Serializable {
9. String name;
10. String location;
11.
12. College(String name, String location) {
13. this.name = name;
14. this.location = location;
15. }
16. }
17.
18. // Derived class Department inheriting from College
19. class Department extends College implements Serializable {
20. String departmentName;
21.
22. Department(String name, String location, String departmentName) {
23. super(name, location);
24. this.departmentName = departmentName;
25. }
26. }
27.
28. // Derived class Student inheriting from Department
29. class Student extends Department implements Serializable {
30. String studentName;
31. double marks;
32.
33. Student(String name, String location, String departmentName, String studentName, double marks) {
34. super(name, location, departmentName);
35. this.studentName = studentName;
36. this.marks = marks;
37. }
38.
39. @Override
40. public String toString() {
41. return "Student Name: " + studentName + ", Department: " + departmentName + ", Marks: " + marks;
42. }
43. }
44.
45. // CollegeManagement class to handle all functionalities
46. public class CollegeManagement {
47. ArrayList<Student> students = new ArrayList<>();
48.
49. // Method to add student details
50. public void addStudent(Student student) {
51. students.add(student);
52. }
53.
54. // Method to display individual student result
55. public void displayStudentResult(String studentName) {
56. for (Student student : students) {
57. if (student.studentName.equals(studentName)) {
58. System.out.println(student);
59. return;
60. }
61. }
62. System.out.println("Student not found!");
63. }
64.
65. // Method to display all student data
66. public void displayAllStudents() {
67. for (Student student : students) {
68. System.out.println(student);
69. }
70. }
71.
72. // Method to calculate and display pass percentage
73. public void displayPassPercentage() {
74. int passCount = 0;
75. for (Student student : students) {
76. if (student.marks >= 40) {
77. passCount++;
78. }
79. }
80. double passPercentage = (double) passCount / students.size() * 100;
81. System.out.println("Pass Percentage: " + passPercentage + "%");
82. }
83.
84. // Method to display top 3 students
85. public void displayTopThreeStudents() {
86. Collections.sort(students, Comparator.comparingDouble((Student s) -> s.marks).reversed());
87. System.out.println("Top 3 Students:");
88. for (int i = 0; i < Math.min(3, students.size()); i++) {
89. System.out.println((i + 1) + ". " + students.get(i).studentName + " - " + students.get(i).marks);
90. }
91. }
92.
93. // Method to serialize student data to a file
94. public void saveToFile(String fileName) {
95. try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName))) {
96. oos.writeObject(students);
97. System.out.println("Data saved to file: " + fileName);
98. } catch (IOException e) {
99. System.out.println("Error saving data to file: " + e.getMessage());
100. }
101. }
102.
103. // Method to deserialize student data from a file
104. public void loadFromFile(String fileName) {
105. try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName))) {
106. ArrayList<Student> loadedStudents = (ArrayList<Student>) ois.readObject();
107. for (Student loadedStudent : loadedStudents) {
108. if (!students.contains(loadedStudent)) {
109. students.add(loadedStudent);
110. }
111. }
112. System.out.println("Data loaded from file: " + fileName);
113. } catch (IOException | ClassNotFoundException e) {
114. System.out.println("Error loading data from file: " + e.getMessage());
115. }
116. }
117.
118. // Main method for testing
119. public static void main(String[] args) {
120. CollegeManagement collegeManagement = new CollegeManagement();
121. Scanner scanner = new Scanner(System.in);
122. collegeManagement.loadFromFile("studentData");
123.
124. // Menu-driven interface
125. while (true) {
126. System.out.println("1. Add Student");
127. System.out.println("2. Display Individual Student Result");
128. System.out.println("3. Display All Students Data");
129. System.out.println("4. Display Pass Percentage");
130. System.out.println("5. Display Top 3 Students");
131. System.out.println("6. Save Student Data to File");
132. System.out.println("7. Load Student Data from File");
133. System.out.println("8. Exit");
134. System.out.print("Enter your choice: ");
135. int choice = scanner.nextInt();
136. scanner.nextLine(); // Consume newline character
137.
138. switch (choice) {
139. case 1:
140. System.out.print("Enter college name: ");
141. String collegeName = scanner.nextLine();
142. System.out.print("Enter college location: ");
143. String collegeLocation = scanner.nextLine();
144. System.out.print("Enter department name: ");
145. String departmentName = scanner.nextLine();
146. System.out.print("Enter student name: ");
147. String studentName = scanner.nextLine();
148. System.out.print("Enter marks: ");
149. double marks = scanner.nextDouble();
150. collegeManagement.addStudent(new Student(collegeName,
collegeLocation, departmentName,
studentName, marks));
151. break;
152. case 2:
153. System.out.print("Enter student name: ");
154. String searchStudentName = scanner.nextLine();
155.
collegeManagement.displayStudentResult(searchStudentName);
156. break;
157. case 3:
158. collegeManagement.displayAllStudents();
159. break;

160. case 4:
161. collegeManagement.displayPassPercentage();
162. break;
163. case 5:
164. collegeManagement.displayTopThreeStudents();
165. break;.
166. case 6:
167. System.out.print("Enter file name to save: ");
168. String saveFileName = scanner.nextLine();
169. collegeManagement.saveToFile(saveFileName);
170. break;
171. case 7:
172. System.out.print("Enter file name to load: ");
173. String loadFileName = scanner.nextLine();
174. collegeManagement.loadFromFile(loadFileName);
175. break;
176. case 8:
177. System.out.println("Exiting...");
178. System.exit(0);
179. break;
180. default:
181. System.out.println("Invalid choice!");
182. }
183. }
184. }
185. }
-: Output :-

.
.

You might also like