Student Management System (Basic Project)
Project Description
We will create a simple Student Management System. This project will help you understand the core concepts of Java, including:
1. Classes and Objects
2. Variables and Data Types
3. Methods (Functions)
4. Input/Output (Scanner Class)
5. Loops and Conditionals
Step-by-Step Explanation
Step 1: Plan the Project
The system should do the following:
1. Add student details (name, age, marks).
2. Display student details.
3. Exit the program.
Step 2: Create the Blueprint (Class)
We’ll create a Student class to represent each student.
What is a class?
- A class is like a blueprint or template.
In our case, the blueprint is "Student," and each real student will be an object.
Code Example:
class Student {
String name; // Variable to store the name
int age; // Variable to store the age
double marks; // Variable to store the marks
}
Explanation:
1. String name: Stores the name of the student (text).
2. int age: Stores the age (whole number).
3. double marks: Stores the marks (decimal numbers).
Step 3: Write the Main Logic
The main method controls how the program runs.
What is the main method?
- It is the starting point of any Java program.
We will:
1. Use the Scanner class to take input from the user.
2. Use a loop to repeatedly show options (add student, display student, exit).
Full Code for Day 1 Project
import java.util.Scanner; // Import Scanner for user input
class Student {
String name; // Student's name
int age; // Student's age
double marks; // Student's marks
}
public class StudentManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Create Scanner object
Student student = new Student(); // Create a Student object
while (true) {
// Display menu
System.out.println("\n--- Student Management System ---");
System.out.println("1. Add Student Details");
System.out.println("2. Display Student Details");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt(); // Take user input
// Process user's choice
switch (choice) {
case 1: // Add Student Details
System.out.print("Enter Student Name: ");
scanner.nextLine(); // Consume leftover newline
student.name = scanner.nextLine(); // Read name
System.out.print("Enter Student Age: ");
student.age = scanner.nextInt(); // Read age
System.out.print("Enter Student Marks: ");
student.marks = scanner.nextDouble(); // Read marks
System.out.println("Student details added successfully!");
break;
case 2: // Display Student Details
System.out.println("\n--- Student Details ---");
System.out.println("Name: " + student.name);
System.out.println("Age: " + student.age);
System.out.println("Marks: " + student.marks);
break;
case 3: // Exit
System.out.println("Exiting... Goodbye!");
System.exit(0); // End program
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
Detailed Explanation
1. Import the Scanner Class:
import java.util.Scanner;
This allows us to take input from the user (e.g., name, age, marks).
2. Create a Class:
class Student:
Blueprint to define what a "Student" is.
We define the student’s name, age, and marks inside this class.
3. Main Class and Method:
public class StudentManagementSystem: Main program class.
public static void main(String[] args): Entry point where the program starts.
4. Using Loops and Switch:
The while (true) loop keeps showing the menu until the user chooses to exit.
switch (choice) processes user choices:
Option 1: Add student details.
Option 2: Display stored details.
Option 3: Exit the program.
5. Taking User Input:
scanner.nextLine(), scanner.nextInt(), and scanner.nextDouble() are used to take different types of input.
6. Exit Command:
System.exit(0) stops the program when the user chooses to exit.
Tasks for You:
1. Run the Program:
Copy this code into any Java IDE (like IntelliJ IDEA, Eclipse, or VS Code) or online compiler and run it.
2. Experiment:
Try adding more students (you'll notice the code only works for one student; we’ll fix this in a future project).
Add new fields like "Class" or "Roll Number."
3. Think:
What if you want to store multiple students? Tomorrow, we’ll enhance this project with arrays or ArrayLists to handle multiple students!
Interview Questions Based on This Project
1. What is a Class in Java?
A class is a blueprint for creating objects. It defines the data and behavior of the object.
2. What is an Object in Java?
An object is an instance of a class. It is created using the new keyword.
3. What is the Scanner class used for?
It is used to take input from the user in Java.
4. What is the difference between int, double, and String in Java?
int: Stores whole numbers (e.g., 10).
double: Stores decimal numbers (e.g., 10.5).
String: Stores text (e.g., "Hello").
Comments
Post a Comment