Last active
November 26, 2015 07:04
-
-
Save qaisjp/f6335bc003203f477633 to your computer and use it in GitHub Desktop.
A2 AQA Computing | Section 2.1: Programming paradigms | Exercises in OOP programming | Exercise 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Public Class Employee | |
| ' These private variables cannot be accessed outside the class | |
| ' This means that in Module1.vb, we can't do emp.EmployeeID | |
| ' The only time we really need to do this is when validating data | |
| ' See the comments for each function to see how they *could* be validated | |
| Private EmployeeID As String | |
| Private DateOfBirth As String | |
| Private JobTitle As String | |
| ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' | |
| '' Here is the set of functions to get the individual fields '' | |
| ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' | |
| Function GetEmployeeID() As String | |
| Return EmployeeID | |
| End Function | |
| Function GetDateOfBirth() As String | |
| Return DateOfBirth | |
| End Function | |
| Function GetJobTitle() As String | |
| Return JobTitle | |
| End Function | |
| ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' | |
| '' Here is the set of subroutines to set the individual fields '' | |
| ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' | |
| Sub SetEmployeeID(ByVal id As String) | |
| ' employee ID's could be of a specific form' | |
| ' something like ISBN's from last year, if you remember? | |
| ' they could be validated using a special algorithm | |
| EmployeeID = id | |
| End Sub | |
| Sub SetDateOfBirth(ByVal dob As String) | |
| ' you could check if it's not in the future | |
| ' tbh it's better to use the DateTime class instead | |
| ' of a string, but a string is much simpler | |
| DateOfBirth = dob | |
| End Sub | |
| Sub SetJobTitle(ByVal title As String) | |
| ' you could check to make sure the title | |
| ' is a real title in a special list you make | |
| JobTitle = title | |
| End Sub | |
| End Class |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Module Module1 | |
| Sub Main() | |
| ' Create a new employee object from our class | |
| Dim emp As New Employee | |
| EnterDataForEmployee(emp) | |
| PrintDataForEmployee(emp) | |
| Console.ReadLine() | |
| End Sub | |
| ' For objects you mostly want to use references | |
| ' The reason for this because you are setting data | |
| ' If you didn't use a ByRef, the data would not change outside the function | |
| ' You can test it out by using ByVal and see what the program does | |
| ' edit: in theory it should do something else, but in vb it does the same thing | |
| ' oh well, doesn't really matter anyway | |
| Sub EnterDataForEmployee(ByRef person As Employee) | |
| Console.Write("Enter employee ID: ") | |
| Dim id As String = Console.ReadLine() | |
| person.SetEmployeeID(id) | |
| Console.Write("Enter employee's date of birth: ") | |
| Dim dob As String = Console.ReadLine() | |
| person.SetDateOfBirth(dob) | |
| Console.Write("Enter employee's job title: ") | |
| person.SetJobTitle(Console.ReadLine()) ' This does the same thing as the other lines | |
| ' The only difference is that it doesn't create a new variable, and | |
| ' that is okay, because we don't need to use that variable anywhere else! | |
| End Sub | |
| ' Here it doesn't matter if we use ByVal or ByRef (functionality wise) | |
| ' rule of thumb: if you don't need to edit it, a ByVal will work fine | |
| ' if you need to edit it, you probably need ByRef | |
| ' sometimes you do need to use a ByRef even if you're not editing it, | |
| ' but that doesn't really matter | |
| Sub PrintDataForEmployee(ByVal person As Employee) | |
| Dim id As String = person.GetEmployeeID() | |
| Console.WriteLine("ID: " & id) | |
| Console.WriteLine("Date Of Birth: " & person.GetDateOfBirth()) | |
| Console.WriteLine("Job Title: {0}", person.GetJobTitle()) | |
| End Sub | |
| End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment