To start with OOP, you should think of the Student class as an Object, a tangible thing like a ball. Say you were given a student. What properties might it have? For example, it may have a name, a gender, a major, a list of courses, etc etc. What could you ask it to do? You could ask it to tell you its name, to tell you whether it is enrolled in a given course or not, whether it shares a course with a given other student or not, etc etc.
In your example, Students appear to have a name, an age, a course, a year, and a section. These are the values that define a student. We can begin a more OOP approach by defining a Student class that adequately stores this information about a student, and allows construction and access to these fields:
public class Student{
public String name;
public int age;
public String course;
public String year;
public String section;
/** Constructs a Student object with the given values */
public Student(String name, int age, String course, String year, String section){
this.name = name;
this.age = age;
this.course = course;
this.year = year;
this.section = section;
}
}
Now, you may notice that everything in the above example is public. This is (almost certainly) wrong. For example, making the age field public allows anyone to walk up to a student and say "you are now -12 years old". Certainly a bug. Let's revise and only allow get access to be public.
public class Student{
private String name;
private int age;
private String course;
private String year;
private String section;
/** Constructs a Student object with the given values */
public Student(String name, int age, String course, String year, String section){
this.name = name;
this.age = age;
this.course = course;
this.year = year;
this.section = section;
}
/** Returns the name of this Student */
public String getName(){
return name;
}
/** Returns the age of this Student */
public int getAge(){
return age;
}
/** Returns the course of this Student */
public String getCourse(){
return course;
}
/** Returns the year of this Student */
public String getYear(){
return year;
}
/** Returns the section of this Student */
public String getSection(){
return section;
}
}
Now, while a student does have an intrinsic value for their age, you can't access it directly. You can, however, ask the student how old they are with getAge()
, which will tell you what you wanted to know.
Finally, let's tackle the main method you were doing in the first place. Now that we have the Student class, we can represent a (name, age, course, year, section) as a single Student object. Instead of having 5 separate arrays, one for each field, we have a single array of Students. Just add this main method to the above Student class:
public static void main(String[] args) {
int x = 0;
int menuChoice = -1;
Student[] students = new Student[30]; //As a note, hard-coding this '30' is a bad idea.
//Probably should be static, final const in class.
Scanner input = new Scanner (System.in);
do{
System.out.println("\t\t\tStudent Record Menu");
System.out.println("\t\t1. Add Student\t2. View Students\t3. Search Student\t4. Exit");
System.out.println("Enter a choice: ");
menuChoice = input.nextInt();
if (menuChoice==1){
if(x < 30) { //Able to add new student.
System.out.println("Full name:");
String name = input.next(); //This was your error - should be next like the others,
//Not nextLine()
System.out.println("Age:");
int age = input.nextInt();
System.out.println("Course:");
String course = input.next();
System.out.println("Year:");
String year = input.next();
System.out.println("Section:");
String section = input.next();
//Create the new student using the given inputs
Student s = new Student(name, age, course, year, section);
//Place in array
students[x] = s;
//Increment x for next student placement
x++;
} else { //Not able to add new student
System.out.println("Can't add new student, students full");
}
}
else if (menuChoice==2) {
for (int i=0; i<x; i++) {
Student s = students[i];
System.out.println(s.getName() + s.getAge() + s.getCourse()
+ s.getYear() + s.getSection());
}
}
else if(menuChoice < 1 || menuChoice > 4){
System.out.println("Unrecognized menu choice; please re-enter");
}
} while (menuChoice != 4);
//Do close your scanners when you're done with them to avoid a resource leak.
//This is closing System.in (which is bad), but you're code is terminating anyway
//so its ok
input.close();
}
Some places you could go from here are using a List (ArrayList
will do) instead of an array for student storage in the main method, because arbitrarily limiting the number of students to 30 seems silly. You could also give students more complex behavior (a list of friends, for example) to do some really cool stuff.
Welcome to Java and OOP! If there's any way I can clarify my answer let me know.