HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaMinor

Class design for a student class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
studentdesignclassfor

Problem

Problem Statement:


Design a STUDENT class to store roll, name, course, admission date and
marks in 5 subjects taken from user. Create an array of STUDENT
objects. Provide methods corresponding to admission date and receiving
marks, preparing mark sheet. Support must be there to show the number
of students who have taken admission.

What should I do to improve the error handling features?

I am looking for code review, best practices and better approaches.

Student class:

```
package student;

import java.util.Date;
import java.util.Scanner;

class Student {
public String name, course;
public int marks[] = new int[5];
public int roll;
public String adm_date;
public static int student_count = 0;;

public static void student_number()
{
System.out.println("Number of students admitted : " + student_count);
}

public int ret_roll() {
return roll;
}

public void admission() {
Scanner sc = new Scanner(System.in);
Date date = new Date();
System.out.println("STUDENT DETAILS\n");
System.out.println("\nEnter name: ");
name = sc.nextLine();
System.out.println("\nEnter course name: ");
course = sc.nextLine();
adm_date = date.toString();
System.out.println("Admission Date:" + adm_date);
student_count++;
roll = student_count;
System.out.println("Roll: "+ roll);

}

void get_marks() {
Scanner sc = new Scanner(System.in);
boolean flag;
for (int i = 0; i 100) || (marks[i] < 0) );

i++;

}

}

void marksheet() {
int j;
System.out.println("STUDENT DETAILS");
System.out.println("NAME : " + name);
System.out.println("ROLL NUMBER : " + roll);
System.out.println("COURSE : " + course);
System.out.println("ADMISSION DATE : " + adm_date);
for (j = 0; j < 5; j++){

Solution


  1. Best practices



The details of the student i.e the instance variables: Name, Course , Marks etc should be private and you should provide public methods to operate on them. Instance variables are made private to force the users of those class to use methods to access them. In most cases there are plain getters and setters but other methods might be used as well.

Using methods would allow you, for instance, to restrict access to read only, i.e. a field might be read but not written, if there's no setter. That would not be possible if the field was public.

Say for example, your roll number field is public.To use it one can do something like this -> student.roll=123123

Making your roll number private and providing getter and setters can help you a lot in the long run

private int roll;

public void setRoll(int roll){
   this.roll=roll
}


suppose in the future need arises to perform validation while setting roll no , your decision to make the roll no field private and provide setters will help you a lot.
You could just do ->

public void setRoll(int roll){
    performvalidation();
    this.roll=roll;
}


This would not have been possible if you had public fields since clients would have set the roll number using object.roll=roll number. This would break their code too.

2.Exception Handling Scenarios

you are using
size = sc.nextInt();

what if user inputs a character by mistake? How would you handle that?
use something like

String tempSize=sc.next
validateIfEnteredTextIsInteger
int size=Integer.parseInt(tempSize);


Another thing , in the 3rd case of your switch statement in the StudentList class, you loop over every student till you find the one with the matching roll no.Since the roll numbers are unique , a Map can come in handy.

Map students = new HashMap<>();
Student student=new Student();

student.setRoll(rollnumber);
Map.put(rollNumber,student);


Doing this , in your 3rd case , you could simply do

Student student=students.get(rollNumber);
student.get_Marks();

Code Snippets

private int roll;

public void setRoll(int roll){
   this.roll=roll
}
public void setRoll(int roll){
    performvalidation();
    this.roll=roll;
}
String tempSize=sc.next
validateIfEnteredTextIsInteger
int size=Integer.parseInt(tempSize);
Map<Integer,Student> students = new HashMap<>();
Student student=new Student();

student.setRoll(rollnumber);
Map.put(rollNumber,student);
Student student=students.get(rollNumber);
student.get_Marks();

Context

StackExchange Code Review Q#68843, answer score: 5

Revisions (0)

No revisions yet.