The goal for this exercise is to understand how to create your own, simple class, with a method(s) on that class.
What you need to do to prepare for this exercise:
Defining a new method in a Console app is slightly complicated, in the sense that we have a couple of options open to us. Further, we want to prepare for Object Oriented Programming (OOP), which we’ll cover in more detail later in the term. Towards that end, we will see one way of creating a class with method, and we’ll try to use only that one method (even though there are other options)
In OOP, our overall program is divided up into classes, where each class contains data and functions that we can use. Some classes we will create ourselves, other classes are provided for us to use. Each class defines a category (or type) of software that we will make use of - a software ‘building block’, if you will. You are encouraged to look online for a more detailed explanation, but for the purposes of this course right now, we will restrict ourselves to this simple overview. Especially because this concept should be familiar from prior courses / experience, even if you’re not 100% solid on the topic.
We want to create a method (aka function, or subroutine, or routine, etc) that accomplishes some work for us. Ultimately, we want to call the method, which will result in the program jumping to the new method, executing all the statements (commands) in that method, and returning to where it was.
The basic pattern that we’ll use is to create a class, and within that class, we will create a new method (or methods). Here’s an example:
using System; namespace PCE_StarterProject { class Program { static void Main(string[] args) {
} }
} |
Example 1 (above)
First, you’ll notice that this is almost identical to code from a previous exercise – you’ve already worked with some OO code! Secondly, the class is everything inside the yellow box (including all the stuff in the orange box, inside the yellow box). The class starts with the lines
class Basic_Console_IO {
And continues until the matching close-curly brace ( }
). Everything between the open and closing curly
braces are “inside the class”, and are considered to be a part of the class definition. In this example, we have
a single method (in the orange box), but we could add multiple methods if we wanted to. When we define a method,
we will (for now) start with ‘public void
’, give the method a name, follow the name with open and close parentheses,
and then the opening curly brace (which indicates where the method begins. There is a matching close curly brace
at the bottom of the orange box that tells C# where the method ends. Inside are the familiar Console.WriteLine
commands that you’ve seen previously:
public void RunExercise() { Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World, again!"); }
Having written out all the code in the yellow (and orange) box, we have defined our class, and our method. The next question is “How do we call our method?” – this is demonstrated by the code in the green box:
Basic_Console_IO bcio = new Basic_Console_IO(); bcio.RunExercise();As you can see, we first need to create a new object that is an instance of the
Basic_Console_IO
class. Once we’ve done that, we can call public void
methods on the Basic_Console_IO
class,
using that new object. Here, we’ll name the object bcio
(just to give it a name), and then invoke the
RunExercise
method. Note the syntax for calling the method – name of the object, a dot/period, name
of the method (this is case sensitive!), the open and close parentheses, then the semi-colon at the end.
Just to be clear, the flow of execution for this would look like the following.
In Main
, the program will create the new object, then call the RunExercise
method, which will cause it to jump down
to the method (following the blue arrow). The first line of code (which prints "Hello, World!"
) gets executed,
followed by the second line of code in that method. The third thing that happens in the method is that the program
jumps back to main (red arrow), ready to do the next thing in the program.
Once you’ve created an object, and called a method, you may call other methods in that same class, like so (note that each method is being put into a separate, orange box:
using System; namespace PCE_StarterProject { class Program { static void Main(string[] args) {
} }
} |
Example 2 (above)
Based on what you’ve seen above, the pattern of having Main
create an object (aka “an instance of a class”), then
call the RunExercise
method on it should make more sense. In this course, you will typically be given the above
code (a starter class, with a RunExercise
method, that gets created and called from Main
), but it’s good to understand
why things work the way they do.
We can also create a class, with a new method on it, in the RunExercise
method, if we want. For example:
using System; namespace PCE_StarterProject { class Program { static void Main(string[] args) {
} }
} |
Example 3 (above)
(Classes are in yellow boxes, methods are in orange boxes)
The only difference between this example, and the prior example is that there is an extra step: the RunExercise
method
creates a second object (of the ConsolePrinter
class), and then calls PrintMessage
on that other object.
Within the provided starter project, there is already a class named
Define_An_Instance_Method
. Add a method to it called
RunExercise
, which will print out the phrase "Hello, World, from the Instance Method
Exercise!". Uncomment the lines of code in
Main
that will cause this code to be run (if they haven’t been already).
Your code should strongly resemble the code provided in Example 1, above.
Create a new class, named HelloPrinter
, with a method named printHello. This method will print out a friendly message to the user, and do nothing else. Make sure to
create an instance of the class, and call the method, from
RunExecise
, as is demonstrated above.
Your code should resemble the code provided in Example 3, above (the main
difference is that you will have only ONE method in your HelloPrinter
class,
and Example 3 has two methods)
Create a new class, named NumberPrinter
, with a method named printTwoNumbers
. This method will ask the user for two integer numbers, and then print out both numbers to the
console. Make sure to create an instance of the class, and call the method, from RunExecise
,
similar to what’s demonstrated above.
Make sure that you’re comfortable with the practice of creating a new class, and adding a simple
method to it (one that has no parameters, no return values, and is called from the RunExercise
method. You should accomplish this by practicing this – repeatedly create a new class, add a
method to the class, then call that method from RunExercise
by creating an instance
of the class and calling the method.
This last item has no concrete requirements that will be graded – you’re not actually required to repeat this any particular number of times, and so you’re not required to hand anything in for item #4.