- Learning to Program—The stages of programming
- Learning to Program – Some Terms You Should Understand
- Learning to Program – IPO Charts (Stage 1)
- Learn to Program- Flowcharting (Stage 2)
- Learning to Program – Flowcharting (Stage 2). — Decisions and Loops
- Learning to Program—Flowcharting (Stage 2) – Case Statements
- Learning to Program (Stage 2)—Flowcharting – Methods and Classes
- Learning to Program – Structure and Spaghetti Code
- Learning to Program – Pseudocode (Stage 3) an overview
- Learning to Program – Stage 3 Pseudocode commands and reserved words
- Learning to Program—Stage 3 Pseudocode examples Part 1
- Learning to Program – Stage 3 Psuedocode (Arrays)
- Learning to Program – Stage 3: Pseudocode—Methods
- Learning To Program—Stage 3.5 (UML Diagrams)
- Learning to Program – (Stage 4) Coding
- Learning To Program—Stage 5 Testing
- Learning to Program—Stage 6 (Documentation)
- Learning to Program – Stage 7 (Maintenance)
- Learning to Program—Random Thoughts with a Theme
- Learning to Program- Two Main Types of Errors
- Learning to Program – Integrated Development Environments (IDE’s)
- 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.
print “Enter the miles”
print “Enter the gallons used”
milesPerGallon = miles / gallons
print “The miles Per Gallon are: “, milesPerGallon
print “This is a subroutine or a method. We’ll now return you to the main program.”
(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.)
print “Enter the miles: “
print “Enter the Gallons: “
milesPerGallon = miles / gallons
print “Your Miles per Gallon are: “, milesPerGallon
print “You are now in a method from a different class”
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:)