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.studentNum = studentNum;
}
public int getSemester() // semester
{
return semester;
}
public void setSemester(int semester)
{
this.semester = semester;
}
void Display() // Method Overriding
{
}
}
public class Course extends Student
{
String course;
Course(String name, int age, String address, int studentNum, int semester, String course)
{
super(name, age, address, studentNum, semester);
this.course = course;
}
public void display()
{
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Test implements StudentFees
{
public static void main(String args[ ]) throws IOException
{
System.out.println("============" + "=================");
System.out.println("Students " + "Personal Details");
System.out.println("============" + "=================");
String name, address, course;
int age, studentNum, semester;
List<Student> studentsList = new ArrayList<Student>(); // array list to store user input
for (int i = 0; i < 2; i++)
{
int studentNumber = (i + 1);
//System.out.println("");
//System.out.println("Please enter " + "data for student " + studentNumber);
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
System.out.println("Enter Student "+ studentNumber + " Name:");
name = in.readLine();
System.out.println("Enter Student " + studentNumber + " Age (Integer):");
age = Integer.valueOf(in.readLine());
System.out.println("Enter Student " + studentNumber + " Address:");
address = in.readLine();
System.out.println("Enter Student " + studentNumber + " Number:");
studentNum = Integer.valueOf(in.readLine());
System.out.println("Enter Student " + studentNumber + " Semester:");
semester = Integer.valueOf(in.readLine());
System.out.println("Enter Student " + studentNumber + " Course:");
course = in.readLine();
Student student = new Student(name, age, address, studentNum, studentNum);
studentsList.add(student); // add student
}
for (int j = 0; j < studentsList.size(); j++)
{
Student st = studentsList.get(j);
System.out.println("Information of Student : " + (j + 1));
System.out.println("");
System.out.println("Name: " + st.getName() + " - Age: "+st.getAge() + " - Address: " + st.getAddress() + " - Student Number: " + st.getStudentNum() + " - Semester: " + st.getSemester() + " - Course: " + st.getCourse()); // print out results entered by user
System.out.println("");
}
}
String course;
public String getCourse()
{
return course;
}
public void setCourse(String course)
{
this.course = course;
}
public void payFees(float fees)
{
}
}
Course
definitely should not extendStudent
. Inheritance represents an is-a relationship, and a Course is not a Student. \$\endgroup\$EveningCourse
, it should extendCourse
. Don't make them until you need them. \$\endgroup\$