accessing class variables
Now that we have some default values, we can add a method that sets some different values for them. Add the following method to your StudentResults class:Click back to your ExamDetails class. Add the following two lines:
String sName = aStudent.fullName("Bill Gates");
System.out.println( sName );
The code for your ExamDetails class should look like the one below:System.out.println( sName );
Just like the methods we created in the previous section, though, our fullName method is on the NetBeans popup list. Notice that the constructor is not there, however:
Let's now add a method that actually does something useful. What we'll do is to allow a user to enter a two letter exam code. We'll then pass those two letters over to a method that turns the two letters into an exam name. For example, if a user enters the letter "VB", the method will return the string "Visual Basic .NET". The longer string will be stored in the Exam_Name field variable.
Add the following code to your StudentResults class, just below your fullName method:
Go back to your ExamDetails class and add the following line:
String exam = aStudent.examName("VB");
Again, we're just making a call to the method. We hand over to the two letters
"VB". The method returns the value "Visual Basic .NET",
and then stores it in the string variable we've called exam. Add a new print
line and your code should look like this:We'll continue this lesson in the next part, as the page is getting a bit too long!
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment