6.2 Student Profile
Hi and welcome back to the course. I hope you guys are doing well. And in this video lecture or lab, we're gonna be creating a student profile program. Now, to go over the learning objectives of this lab, we're gonna be creating this student profile program to use encapsulation and demonstrate how encapsulation works and that we're gonna achieve by displaying that specific students University in four profile, which will consist of their name, age, school name, student number and many other things. All right, so at this moment, I'd like you to go ahead and switch over to intelligence now in and tell Ajay I have two classes. As you can see, one is called student and one is called Tester. No, the tester class here has ah bunch of errors in it. And the reason it has errors in it is because the testing for it has been implemented in the tester class. But I haven't completed the student class yet.
I'm gonna leave the code inside the tester class so that instead of writing the test code from scratch, we can just go off from what we have. So having an error right now is completely normal that we can avoid for now. So in our student clause, we want to be able to set a student's name, get a student's name and do the setting and getting for age, student number, school name and status. And their status is basically if they're full time or if they're a part time. Now. Since we're trying to use encapsulation as we discussed in the previous lecture, in order to fully implement this idea of encapsulation, we first need to make our variables private. So that's the first thing that we're gonna start by doing so for that, let's start by first creating a private string may. So I created a private string called Name, which is the first parameter here that we want to be able to create. And then I'm gonna move on to age on. This is also a story. This is a H, okay. And then we're gonna create a student number. So a student number would also be represented by an integer, and I'm gonna need my variable student number.
And then then we're gonna have a school name and school name would be a string, so I'm gonna make it a private string school name. Okay, And then finally, we're gonna have a Boolean which will determine the students status. So as we know, billions can either be true or false, So I'm gonna call my bullion status. Perfect. So now that we have these private variables, how do we actually modify these private variables outside of our class? Now, that is what I mentioned a few seconds ago. In order to achieve that, we need to be able to create gather and center methods to get this information in another class, for example, in our tester class, and to set this information in our tester class.
And that is the only way we can access these private fields or attributes. Now, I will show you an example of a getter method. So let's say we want to make a getter method for our private string name. I would say public t to tell other classes that this is a public method, and then let's call it get me And then that, uh, is a string. So it's gonna return a string. And in order to do that, we're just gonna type a return, um, name. Now, what that's gonna do is whenever we asked to get the name of a student once we set the name that it's going to return the name of that student and we need to do, um, something similar for the rest of We need to essentially do the same getting offer method creation for the rest of these parameters.
So I'm gonna go ahead and stop for a few seconds and complete that, and then I will be right back. Okay? Perfect. So now, as you can see, we have or get her methods. Now I'll explain them step by step. So since we have a name, a tribute or variable, we need to obviously get the name of the students. So that's why I have get name, as I mentioned a few seconds ago. And then I have get age gets to numb, get school name and get status. No, just because we have all our get her methods. It doesn't really do anything on its own because we need to be able to set values for these variables and to do that, meaning to have set her methods. Now I will make each center method under each corresponding getter method.
So let's say So obviously, I have get name as my first method getter method here. And I want to be able to set the name of a student. Says you can see I can auto complete this operation over here. If I click enter, what it's gonna say is public white set name, string name, and then this dot name equals need. Now, that is how I want to be able to set a student's name. I'm gonna slightly modify this so that it makes a bit more sense. I'm gonna call my parameter new name. Um, sorry. New name. And then I removed that this and then we're going to set name to new name. Your name. Okay, Perfect. So what that will do is it will set the name of a student and which we will do in our tester class. Now, I'm gonna go ahead and do the same that I did with the getter methods and complete the center methods. And then we will be right back and discuss how we exactly did that.
All right, so, going back to our center methods, what I have done here is I've made a corresponding set method for each Get method. So as you can see, we have get name, set, name get age, set age Get student numb set student, get school name set school name and then get status said status. Now the reason all these methods are yellow is because there are in use right now. And where would you think they're being used there being used in the tester class. Now, once we have all of our getter and center methods completed, we will go into our tester class And actually I can create a test case to see the result of our student class. So I've made a new student and I've called It s one. And then I've used my set of center methods first to set the name of the student, the age, the no stew number, the school name and then the status.
And then what I've done here is I have made a simple print statement with using the get her methods for the student s one, and I've printed their name, age, student number, school name as well as their status. Now, that is a simple test case for our program and simple test code that you can write up to see how your student class works and to see how your getters insiders work. Now, if we were to run this class, then we should be seeing the open that you were seeing a few seconds ago. And I will go ahead and expanded. It says name Zelman, age 19. Student number 45 62 1 79 School named harder Harvard University and then status. Full time equals truth or colon troop.
So that is essentially how encapsulation works. You make getters and centers to make these private after attributes publicly available outside of your student class. And then we made a quick test her to see how this works. Now, going back to our video lecture in this lab, we covered the implementation of encapsulation. And I hope you guys understood how we accomplish that. Now, coming up next, we're gonna be starting Marshall seven, which is all about abstraction. As always. If you have any questions, feel free to contact me via Ellington, and I would be more than happy to answer any of your questions. Thank you very much for watching. And I look forward to seeing you in the next module.
I am pursuing my undergraduate degree in computer science but with a specialization in information security. I have been a cyber security teaching assistant at my university and am also preparing for a few security certifications. Additionally, my Co-op/Internship is in cyber security through which I have been working on a variety of projects.