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

Student details project

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

Problem

I have made a student details project. The user is asked to enter the info for two students, which is then displayed on the screen.

Questions:

  • Have I used inheritance correctly?



  • What would be a better way of outputting the results?



```
public class Person extends Test
{
// Variables

String name;
int age;
String address;

// Default Constructor

Person()
{
name = "";
age = 0;
address = "";
}

Person(String name, int age, String address)
{
this.name = name;
this.age = age;
this.address = address;

}

String getName()
{
return name;
}

public void display()
{

System.out.println("Name = "+ name);
System.out.println("Age = "+ age);
System.out.println("address = "+ address);

}

}

public class Student extends Person
{

int studentNum, semester;

Student(String name, int age, String address, int studentNum, int semester)
{
super(name, age, address); // calls parent class’s constructor
this.studentNum = studentNum;
this.semester = semester;
//this.course = course;

}

public String getName() // name
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public int getAge() // age
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

public String getAddress() // address
{
return address;
}

public void setAddress(String address)
{
this.address = address;
}

public int getStudentNum() // studentNum
{
return studentNum;
}

public void setStudentNum(int studentNum)
{
this.st

Solution

Alright, let's go through everything that is wrong with this code...

public class Person extends Test


^ A person is not a test. ^

// Variables

            String name;


^ Too much vertical white space. ^

String name;
        int age;
        String address;

        // Default Constructor

        Person()
        {
            name = "";
            age = 0;
            address = "";
        }


^ Very verbose. You can set the default values at the site of variable declaration and leave the default constructor empty. ^

String name;
        int age;
        String address;


^ You didn't specify that these member variables are private. They are public by default (which is unacceptable except for final values). ^

public void display()
        {

            System.out.println("Name = "+ name);
            System.out.println("Age = "+ age);
            System.out.println("address = "+ address);

        }


^ Waaaaay too much vertical whitespace. ^

int studentNum, semester;


^ Avoid declaring multiple variables on one line in Java. It hampers readability and promotes using the same modifiers for all your variables when in reality they should be different (ex. final, volatile, etc). ^

public void setAge(int age)
{
    this.age = age;
}


^ The whole point of not accessing variables directly is to be able to control how they are modified. In this case, it doesn't make sense to change your age to any integer. You probably want to get rid of the setter and instead use a method that increments your age (assuming that your age can't ever go down). At the very least check to make sure that your age isn't negative.

void Display() // Method Overriding
{

}


^ Don't leave a comment saying "// Method Overriding"". Instead, on the above line, write "@Override" (without the quotation marks). ^

public class Course extends Student


^ BIG HUGE TERRIBLE MISTAKE. NEVER EVER EVER EVER USE INHERITANCE THIS WAY. A course is not a specific type of student. Student has an instance of course. Student has an array of courses and student should be able to add and drop courses from the array.^

public class Test  implements StudentFees


You're using inheritance wrong. Every time you use inheritance, ask yourself "is type X a subclassification of type Y".

Example:

public class Dog extends Animal


Is type dog a sub-classification of type animal? Are all dogs animals? Yes. Good. We are using inheritance the right way.

Now try this:

public class Fish extends Bird


Is type fish a sub-classification of type bird? Are all fishes birds? No. Bad. We are using inheritance the wrong way.

Code Snippets

public class Person extends Test
// Variables

            String name;
String name;
        int age;
        String address;

        // Default Constructor

        Person()
        {
            name = "";
            age = 0;
            address = "";
        }
String name;
        int age;
        String address;
public void display()
        {

            System.out.println("Name = "+ name);
            System.out.println("Age = "+ age);
            System.out.println("address = "+ address);



        }

Context

StackExchange Code Review Q#19408, answer score: 8

Revisions (0)

No revisions yet.