Learning to Program – Stage 3: Pseudocode—Methods


This entry is part 13 of 22 in the series Learning to Program

In today’s post I will discuss methods and classes, and describe how to use them in pseudocode.  Methods and classes are inherently the same thing, but there are differences between them.  Especially when you get to actual programming languages.

A method used to be called a subroutine or a procedure (in Pascal or Delphi, it may still be called this).  It can be thought of as a break from the main program to do a specific task.  Imagine if you will an assembly line that looks like this:

statiion 1:  Put the outside of the product together (if it has multiple parts)
station 2:  Put the inside parts into the product and anchor them.
station 3:  hooks everything up to make the product work (wires, boards, etc).
station 4: tests the product to see if it works.
station 5: packs the item.

Now, imagine that after the product is tested, you have to let a QA inspector check it.  Instead of them being in the line, you have to turn around and hand the product to them.  Then they hand it back, and you send it down to the packer in station 5.

This is the same premise as what a method does.  The main program handles the other things, and the method or class handles the specialty items.

So, what is a class?  A class is a special type of method.  In fact, it is a group of methods that all interact with the same type of data, and can be reused in more than one program without having to recode them.

In Pseudocode, you will typically start your methods after the STOP command in the main portion of your program.  If you’re creating a new class, then you will have it on a separate page—along with it’s methods.  And you will declare some type of variable with the class name for it’s type.

Methods:

START
      num miles
      num gallons
      num milesPerGallon
      print “Enter the miles”
      input miles
      print “Enter the gallons used”
      input gallons
      milesPerGallon = miles / gallons
      print “The miles Per Gallon are:  “, milesPerGallon
      TrivialFacts()
STOP

void TrivialFacts()
      print “This is a subroutine or a method.  We’ll now return you to the main program.”
return

(this pseudocode is loosely based upon an exercise in the book Programming Logic and Design, Introductory by Joyce Farrell.(pg 129). (Please note that this link is to the Amazon.com website, through my Affiliate account.)

Classes:

START
      num miles
      num gallons
      num milesPerGallon
      OtherClass differentClass
      print “Enter the miles: “
      input miles
      print “Enter the Gallons: “
      input gallons
      milesPerGallon = miles / gallons
      print “Your Miles per Gallon are:  “, milesPerGallon
      differentClass.TrivialFacts()
STOP

class OtherClass()
public TrivialFacts()
     print “You are now in a method from a different class”
return
endclass

I added the endclass as something to show the end of the class.  In a programming language such as Java, you would create the class with the name Other Class, and where I had “OtherClass differentClass”, you would put:

“OtherClass differentClass = new OtherClass” (other languages may do this differently).

With this, we are ready to move on to Stage 4 in Learning to Program.  Once again, if you have any questions or want me to go over something in more detail, please leave a comment or send me an e-mail to sales at patscomputerservices dot com (at=@ dot=.).

Have a great day:)
Patrick.

Series Navigation<< Learning to Program – Stage 3 Psuedocode (Arrays)Learning To Program—Stage 3.5 (UML Diagrams) >>

Leave a comment

Your email address will not be published. Required fields are marked *