0% found this document useful (0 votes)
100 views2 pages

Java Program for Student Details

The program defines a Student class with name, roll number, class, and marks in 3 subjects as properties. It accepts student details from the user, calculates the total marks, and displays the student's name, class, roll number, individual subject marks, and total marks.

Uploaded by

Manvi Gour
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)
100 views2 pages

Java Program for Student Details

The program defines a Student class with name, roll number, class, and marks in 3 subjects as properties. It accepts student details from the user, calculates the total marks, and displays the student's name, class, roll number, individual subject marks, and total marks.

Uploaded by

Manvi Gour
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

Name: Manvi Gour

Program 2.a Write a program for class Student, accept name, roll no,marks of three
subjects, Calculate total and display the complete information. use Constructor.

import [Link];
class Student1{
String Name;
int Roll_No;
String Class;
int m1;
int m2;
int m3;
int t;
public Student1() {
}
public Student1(String initName, int initRoll_No, String initClass, int initm1, int initm2, int initm3, int
initt) {

Name = initName;
Roll_No = initRoll_No;
Class = initClass;
m1 = initm1;
m2 = initm2;
m3 = initm3;
t = initt;
}
}

public class Student {

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Enter name of student: ");


String Name = [Link]();
[Link]("Enter class of student: ");
String Class = [Link]();
[Link]("Enter roll no of student: ");
int Roll_No = [Link]();
[Link]("Enter marks in 1st subject: ");
int m1 = [Link]();
[Link]("Enter marks in 2nd subject: ");
int m2 = [Link]();
[Link]("Enter marks in 3rd subject: ");
int m3 = [Link]();
int t = m1 + m2 + m3;

[Link]("The Details of Student are: ");


[Link]("Name = " + Name);
[Link]("Class = " + Class );
[Link]("Roll No = " + Roll_No);
[Link]("m1 = " + m1);
[Link]("m2 = " + m2);
[Link]("m3 = " + m3);
[Link]("Total Marks = " + t);
}
}

OUTPUT:-

Common questions

Powered by AI

The constructor in the Student1 class allows for the initialization of student object attributes such as name, roll number, and marks at the time the object is created. This ensures that all necessary information is provided and set up for a Student1 object to function correctly. It also reduces errors and makes the code cleaner by encapsulating object-specific initialization logic within the class itself .

The Student1 class can be improved by declaring its fields as private and providing public getter and setter methods to access and modify these fields. This would ensure that data integrity is maintained while using object-oriented principles for better data management. Additionally, input validation could be included in setter methods to prevent invalid data entries .

The program lacks robust error handling mechanisms. There is no validation to ensure that numeric inputs for roll numbers and marks are valid integers, nor is there handling for input mismatch exceptions. This leaves the program vulnerable to runtime errors when invalid inputs are entered, such as entering text instead of numbers .

The Scanner class is used to gather input from the user. In this program, it reads data such as the student's name, class, roll number, and marks for three subjects from the console, allowing dynamic input of student information at runtime .

Using a parameterized constructor enables the passing of initial values for all class properties at the moment an object is created, which guarantees a complete setup for the object and mitigates the risks of having uninitialized or default values. This leads to more maintainable and less error-prone code, as compared to a default constructor which requires a separate initialization process .

Encapsulation is not effectively implemented in the provided student program because the class fields are public and accessible directly from outside the class. Proper encapsulation would require these fields to be private, and access to them managed through getter and setter methods. This would protect the data from unauthorized access and modification .

Calculating the total marks directly within the main method simplifies the program and makes it straightforward for small-scale applications. However, it does not align with separation of concerns or scalability design principles. By doing it within the main method, the logic is tightly coupled with input/output processes, making future modifications difficult. It would be better practice to have a dedicated method for calculating total marks within the student class to improve code modularity and reusability .

To improve clarity, the input prompts could include more detailed instructions, such as specifying the format or range of acceptable inputs. For example, prompts could specify that marks should be integers between 0 and 100. Adding prompts for invalid entries or confirming correct entry can also aid in providing a smooth user input experience .

To achieve better 'Separation of Concerns,' input handling, business logic, and data representation should be split into separate functions or classes. The input handling can be managed by a separate utility class, while the Student class could handle all logic related to the student data, such as marks calculation. This would minimize dependencies and make each part of the program responsible for a single aspect, enhancing maintainability and reusability .

Displaying student details after computing total marks provides users with a complete overview of the input data along with the calculated result. This improves user experience by verifying the input accuracy and increasing transparency in how the total was derived. It helps in building trust and assures users that the system is working as intended .

You might also like