Table of Contents
- Thinking In OOP
- OOP As A Foundation
- Expressions, Math, Exceptions, If-Else, and Case
- Adding Complexity + Looping
- OOP - Getting Real
- Appendix
- Notes
Who is this book for?
To-Do:
- Talk about the intended audience (first-term programming students)
- What are LOGs
- Instructor’s Guide (a future book)
The ABCs of Programming with Objects
An “Objects-First” Introduction to Programming
Learning how to program is a journey into thinking about things and how to manipulate them. For a long time, most instructors and teachers who’ve taken up the challenge of teaching programming have done so with a focus on the manipulations and less concern with the things the program represents. This procedural mindset has been the way many of us (myself included) learned programming in the first place. The problem with this mindset, however, is that “real” programs haven’t been written that way for decades now.
Object-Oriented Programming (OOP) has been around for a long time, but it can be hard to adjust one’s approach from a procedural mindset to become comfortable with OOP. This book aims at offering a way to teach programming starting with OOP, rather than tacking it on at the end. Through simple examples, and a focus on the things first, this book will guide the student (and the teacher) through numerous examples that introduce all the concepts needed to get started in programming with Objects.
The essence of this OOP-First approach are seen in what I call the “ABCs of Classes and Objects”. From these first three topics I move to the “traditional” concerns of programming, all the while placing them within their most natural context: Objects.
Topic A
This topic is the first of a three-part introduction to object-oriented programming. The focus of this first part is to introduce classes and methods. This topic does not go beyond the use of static methods in a class.
Introduction
The purpose of this topic is to introduce the class as the central piece of modern computer programs. A computer program is “a set of instructions for manipulating information.” Notice the two key elements of a computer program: Instructions and Information. A class is a kind of template that groups together data (information) and instructions for manipulating that data.
Manipulating information is done through something called “methods”. A method is basically a set of instructions that has an overall name (method name). Methods cannot exist on their own; methods must exist in a “context”, and the context for all methods is the class. Methods are where programmers tell a computer “what to do.”
Every computer program has one important method that is the entry point or starting point for running the program: main. The main method must be publicly available in order for the operating system to be able to run the program.
Topic B
The focus of this second part is to show how classes act as templates for creating objects. This topic limits itself to showing a) how to create (instantiate) objects using classes and b) how classes can be used to describe what an object “looks like”.
Introduction
The purpose of this topic is to show how classes don’t just “contain” methods: Classes act as templates for defining complex types of information (objects). In fact, a class can almost be thought of as a “mini-program”, because a class brings together the two key elements of every program: Instructions and Information. With classes, we can create objects that do what we want them to.
The class is the basic building block for creating objects. As a template, a class describes
a) What an object “looks like” (that is, what information is inside the object), and
b) How an object “works” (that is, what instructions an object can perform on data).
This topic focuses on the first part – describing what an object “looks like” – by introducing fields and exploring the idea of object state.
Topic C
The focus of this third part is to show the concepts of encapsulation and information hiding as well as good object design by creating private fields and introducing public getters/setters and constructors.
Introduction
The purpose of this topic is to highlight how a major responsibility of every class is to “protect” the data within the class. This is done by designing classes that only use private fields and that “expose” access to the values of those fields through getter and setter methods. Getters and setters provide a way for the programmer to control access to fields in an object and to ensure that the fields are “used correctly”.
(Note: Later topics will address how to write code that tests the values coming through the setters’ parameters to ensure that only “acceptable” values are allowed into an object’s fields.)
This topic also covers the important role of constructors in ensuring that objects have a “stable state” at the moment that they are created. In other words, constructors are responsible to ensure that all of the fields in an object are filled with “meaningful” values. A constructor also forces other code to supply required information when creating (instantiating) an object based on that class. Constructors are the “first line of defense” for ensuring that objects are created properly.
Thinking In OOP
What is a Computer Program
Introduction
What is a computer program? A computer program is basically a set of instructions for manipulating information. To be of any value, the information used by a computer program must have a way to get into the program (input) and get out of the program (output). That may sound too simplistic, but ultimately it is entirely accurate; even the most sophisticated computer program can do nothing more than process information (albeit, in a very fast and efficient way), and no matter how cool the program looks, if there is no way to put information into it and get information out of it then it’s not of much use to anyone.
The most familiar kind of input and output (or I/O) in a computer program comes from the user. Textual information is entered (input) from the user via the keyboard and displayed (output) to the user via the monitor. Other means of user I/O can include computer mice, microphones, speakers, printers, fax machines, Braille readers, touch screens, stylus, etc.
Besides handling user I/O, computer programs can also do file or database I/O. Computer files and databases allow information to be persistent. Information is said to be persistent when it can be stored outside of the computer program and later retrieved for further processing. The combination of a) user I/O, b) fast and accurate processing, and c) persistent information storage is what makes computer programs so useful.
Variables, Values and Data Types
”… manipulating information.”
At the heart of all computer programs is the idea of information or data. This is especially true in Object-Oriented Programming languages. With information being such a critical component of computer programs, it’s important to understand what we mean by information.
It’s All About Information
Since computer programs are all about handling information, it is important to understand something about the different kinds of information programs deal with. Essentially, most information falls into one of the following categories:
- Numeric information (e.g.: a person’s age or the price of a vehicle)
- Textual information (e.g.: the title of a book)
- Conceptual information (e.g.: truth - whether a statement is considered true or false)
These different kinds of information can be said to be primitive. They represent the most basic kind of data that today’s computer programs have to deal with. These three kinds of information can be further subdivided into fundamental data types:
Category of Information | Basic Data Types |
---|---|
Numeric | Integer - whole numbers |
Real - numbers with fractional components | |
Currency - numbers representing monetary values | |
Textual | Character - a single letter or symbol |
String - a set of characters (with or without spaces) | |
Conceptual | Boolean - true/false, yes/no, on/off |
Any meaningful handling of information requires the understanding of three related (but distinct) concepts: variables, values, and data types.
In order for a computer program to manipulate information, it needs a way to hold or refer to information. This is the concept of a variable. A variable is a name or identifier used to refer to a single piece of information. It acts like a container that allows a program to refer to a piece of information indirectly, without being dependent on the exact content of that information. One analogy is that a variable is like a cup: a cup can be used to hold different kinds of liquids (water, juice, or even motor oil). The cup is independent of the liquid (the cup can even be empty), but it provides an effective way of working with a liquid (for example, measuring the amount of water used in a recipe).
Besides the idea of a variable that can hold information, there is the concept of a value. Unlike a variable (which is only used to refer to a piece of information), a value is the specific information contained in a variable. Returning to the previous analogy of a cup, a value is like the specific liquid inside the cup (variable). What does a particular cup hold? Is it water or motor oil? The cup is not what’s really important; what’s important is what is in the cup (especially if you are making a recipe!).
The final concept is one that has already been alluded to: the idea of a data type. A data type refers to the kind of information that a variable can hold. For example, the kind of material that a cup is designed to hold is a liquid. The description “liquid” is a description of the type of content that the cup can hold. A plate, on the other hand, is designed to hold a different kind of material - something that is solid (such as a steak or chicken cordon bleu). More specifically, the “data type” that a plate is intended to hold is “food” and steak would qualify as a type of food while motor oil would not. Notice that the concept of data type (e.g.: “food”) can be applied to both the variable (plate) and the value (steak).
This brings up an important consideration: The data type of a variable should be appropriate for the kind of information the variable is intended to hold. There is a close relationship between variables, values and data types, even though those three ideas refer to different things. Cups are used to hold liquids; plates are used to hold food; and a balloon is used to hold a gas. It would be entirely inappropriate (if not impossible) to use a balloon to hold a piece of cake! Using these analogies, the concepts of variable, value and data type can be clearly distinguished:
Variable | Value | Data Type |
---|---|---|
Cup | Orange Juice | Liquid |
Plate | Black Forest Cake | Food |
Balloon | Helium | Gas |
Returning to the way these concepts are used in a computer program, the following table gives some examples of variables and values for the data types fundamental to most high-level programming languages.
Variable | Value | Data Type |
---|---|---|
Age | 25 | Integer |
PI | 3.14159 | Real |
Cost | $4.95 | Currency |
Gender | ‘M’ | Character |
Title | “Computer Basics 101” | String |
Success | True | Boolean |
Rules About Variables, Values and Data Types
It has been stated that while variables, values and data types are distinct concepts, they are closely related. This close association between these concepts allows us to work with and manipulate information. Before we can begin working with these concepts in a simple program, however, we need to establish some basic rules about how to work with variables, values and data types.
The first rule has already been alluded to in the previous section: The data type of a value must match the data type of the variable that stores the value. This rule is closely related to a second rule: A variable and its data type must be declared before the variable can be used. Whenever a variable name is first identified in a computer program, the data type of the variable must be declared. Stating, or declaring, what the data type of a variable is will allow a computer program to know two things:
- How much computer memory must be set aside for the variable’s value.
- How to interpret a value stored in a variable.
A variable cannot be used until it is declared, and once a variable’s data type has been identified it can only hold values that match that data type. For example, if a variable called Status has been declared as a character, then it can only hold a character value (such as ‘T’ or ‘5’). Any attempt to store a value of a different type (such as “Temporary”) in Status would produce an error in the computer program.
A third rule that is related to the second is that once a variable’s data type has been declared, its data type cannot be changed.1 This is because a variable’s data type tells the computer how to interpret the variable’s value (e.g.: is it a character or a number?), and any attempt to change the data type would result in a misinterpretation of the previously stored value.
Another rule is that each primitive variable in a computer program can only hold a single value at any given moment in time. The reason for this rule is so that the computer knows what value to supply when a variable is used in the program. In fact, when a primitive variable’s data type is declared, the computer sets aside enough memory to store only one value - no more! If a new value is stored in the variable, then any previously stored value is lost. This also gives an advantage to the programmer - it is easier to plan for and test a computer program if you can predict what value will be stored in the variable. Since a variable can only hold one value at a time, it is not unusual for a computer program to use many variables.
Notice that this last rule applies to variables with primitive data types. Often, computer programs allow the creation of more complex data types, such as classes and arrays. The rule of a variable storing only one value changes slightly for these complex types. In the case of classes, one variable whose type is based on a class can only store one object (even though the object might be complex and hold many values). Likewise, a variable that refers to an array can only refer to one array (even though arrays can hold many values). These complex data types will be discussed in a later lesson.
Lastly, in order for a computer program to distinguish one variable from another, each variable (in a given scope) must have a distinct name. If two variables were given identical names, then the computer would not be able to tell which variable to use in a given calculation. By giving each variable a different name, it is easier for both the computer and the programmer to keep track of which ones to use.
Notice, however, the qualification to this rule: in a given scope. In general, scope refers to the context in which a variable is used. If two variables are used in two different contexts, then it is permissible to use the same variable name because the computer uses the different contexts to tell them apart (similar to how two people can have the same first name but be distinguished from each other if they have different last names). An obvious (and trivial) example of this is if a variable called Age is used in two different computer programs, then the computer can tell which variable to use because it knows which program it is currently running. Usually, the term scope is used to refer to distinctly different contexts within the same computer program. The idea of scope will be discussed in more detail later. To begin with, most of the programs will be fairly simple and will only have one scope, so it is fair to simplify this rule as each variable must have a distinct name.
In summary, the basic rules for variables, values and data types are:
- Any value stored in a variable must be of the same data type as the variable.
- Variables must be declared before they can be used.
- A variable’s data type can only be declared once.
- Each primitive variable can only hold one value at a given time.
- Each variable must have a distinct name.
Programming Languages and Coding Instructions
”… a set of instructions …”
What Programmers Write
Programmers write code in plain-text files known as source files. The language that a programmer use to write code is called a programming language. Just as with human languages, there are many different programming languages. Some examples of modern programming languages are C# (the language used in this book), Java, Visual Basic .Net, SQL, and JavaScript. In addition, many other programming languages have been developed over the years such as Python, Lisp, Fortran, Cobol, Haskell, C, C++, and many more.
Each programming language defines their own words (called keywords), punctuation and grammar rules (or syntax). And like human languages, programming languages typically promote certain “world-views” or perspectives on how code should be written. Some languages emphasize the steps involved in manipulating information (a Procedural perspective). Others put the emphasis on the information itself (as in most Object-Oriented programming languages).
In all cases, programming languages allow us to write instructions that manipulate *data. Through variables, a computer program has the ability to manipulate the values stored in those variables. This manipulation of stored information is accomplished by giving the computer specific instructions on what manipulations to perform.
What Computers “See”
Programming languages allow programmers to give instructions to a computer in a form that is more familiar to and easier to use by the programmer. Today’s programming languages are known as high-level languages because they incorporate simple words with familiar meanings in English, such as if, else, while, do, and return. Low-level languages, such as Assembler, use more cryptic commands and are much closer to machine language, thereby making them harder for programmers to work with.
The exact form that the instructions will take depends on the programming language that is used. These instructions are then examined by a special program (a compiler, an assembler, or an interpreter) and translated into machine language. Machine language consists of a series of ones (1) and zeros (0) which act as on and off switches in the computer’s memory. These on and off switches change other information that is stored in memory, resulting in the manipulation of information.
The on and off switches in computer memory represent the flow of electricity through the computer. Millions and billions of these switches (also known as “gates” - no relation to Bill) are arranged on tiny computer chips in highly sophisticated patterns to allow for the organized flow of electricity. It is this organized flow of ones and zeros that allow us to use these machines for fast and efficient information processing.
Doing the Math
High-level programming languages (such as C++, C#, and Visual Basic) make it relatively easy to manipulate the values of variables. Assignment statements tell the computer to store specific values into specific variables. Assignment statements are often used with expressions, which are combinations of variables, values, and/or operators (such as Arithmetic operators). It is through expressions and assignment statements that the bulk of a computer’s work is done.
Expressions allow new values to be computed based on existing values. For example, if a variable called Cost has the value $5.95 and another variable called Markup has a value of 2.0, then a mathematical expression such as Cost * Markup can be used to create a new value - $11.90 (the Cost multiplied by the Markup value). Using this expression in an assignment statement, this resulting value of the expression can then be stored in another variable:
Price = Cost * Markup
All numeric information can be used with the common arithmetic operators of addition, subtraction, multiplication, and division. When combined to create complex expressions, the commonly accepted rules of mathematics are used to determine which operations should be used first (multiplication and division before addition and subtraction). The order of operations can be modified by using parentheses, as in regular mathematics.
Some arithmetic operations, such as calculating the remainder of a division operation, are only applicable to certain types of numeric information. Some languages supply a special symbol to represent these operations while other languages require the use of special library functions to perform the math. Complex arithmetic operations, such as calculating the sine of an angle, are typically done using library functions. Library functions are simply specialized routines or sets of instructions that can be called upon to do the required calculations.
The following chart summarizes key arithmetic operations and their corresponding symbols as used in the more common high-level programming languages. The chart also shows some of the more complex operations and the names of the library functions used to calculate them (for specific details on using functions, see the documentation for the corresponding language).
Arithmetic Operation | Symbols / Function Calls | ||||
C++ | C# | JavaScript | Visual Basic | SQL | |
Addition | + | + | + | + | + |
Subtraction | - | - | - | - | - |
Multiplication | * | * | * | * | * |
Division | / | / | / | / | / |
Remainder of Division | % | % | % | Mod | MOD(Division Expression) |
Square Root | sqrt( ) | Math.Sqrt( ) | Math.sqrt( ) | Sqr( ) | SQR( ) |
Exponential Notation (Power) | pow( ) | Math.Pow( ) | Math.pow( ) | ^ | POWER( ) |
Sine | sin( ) | Math.Sin( ) | Math.sin( ) | Sin( ) | SIN( ) |
Cosine | cos( ) | Math.Cos( ) | Math.cos( ) | Cos( ) | COS( ) |
Tangent | tan( ) | Math.Tan( ) | Math.tan( ) | Tan( ) | TAN( ) |
Parenthesis (affects order of operation) | ( ) | ( ) | ( ) | ( ) | ( ) |
The only “arithmetic” operation that is typically applied to string data types is addition. The addition of two strings is usually interpreted as the concatenation (joining together) of the two string values into a single string value.
Besides performing assignment statements and expressions, programming languages have instructions that allow the program to control which instructions to perform and which ones to skip over. These control statements allow for performing mutually exclusive instructions (this or that) and for performing certain instructions repeatedly. High level languages also incorporate mechanisms to identify sets of instructions as particular groups or functions as well as ways to call these program-defined functions whenever they are needed.
Summary
Information comes in various forms and programmers can create both simple and complex sets of instructions to manipulate that information. Successful computer programs combine the best techniques for user I/O, information processing, and persistent storage in order to meet the needs of individuals and businesses. Grasping the basic concepts of computer programs - variables, values, data types, I/O, assignments and expressions, and control statements - is fundamental to the efficient and effective development of computer software.
The C# Language
C# (pronounced ‘See-sharp’) is a programming language developed by Microsoft and rooted in the earlier languages of C and C++. It is an Object-Oriented programming language that has evolved quite a bit over time, with each released version adding new features and capabilities to the language. At present, C# is in version 6.0 and version 7.0 is under development. Like every programming language, however, it must allow us to hold information and provide sets of instructions to manipulate that information.
Data (information) is held inside of variables. The following keywords represent the commonly used built-in data types for the general categories of primitive information.
Category | C# Data Type |
---|---|
Whole numbers | int long short byte |
Real numbers | double single float |
Text | char string |
Conceptual | boolean |
Instructions are always placed inside of methods. In fact, a method can be defined as a named set of instructions. Methods can never exist all on their own; they have to be placed inside of some kind of context (or scope), and they are typically placed inside of classes. Classes, themselves, are one of the main means by which we can declare complex data types (which, in turn, can be used to declare objects designed to hold and process sets of data).
Where are variables placed in our program? Variables can appear inside of classes (where they are sometimes called member variables) and inside of methods (where they are called local variables). As we will learn later, the location of the variable defines its scope, and that scope will determine when and where the variable can be used.
If you are beginning to imagine a lego-like structure to the C# programming language, then you are on the right track! We can code classes that hold variables and methods. The classes themselves are new data types which can be used for for creating complex variables (objects). This characteristic of providing keywords for primitive data types along with a means to combine them into more complex data types is called type extensibility. You can think of it in this way: primitive data types are like chemical elements in the periodic table, while complex data types (which we design ourselves) are like molecules made by combining elements. While this analogy has its limits in describing the nature of C#, it does provide a good starting point in understanding how the language is designed.
The Main Method
Speaking of starting points, it’s important to note that every computer program needs a place to start. When we run a computer program, we are telling the operating system (OS) to start running the instructions in the program. That’s why every program needs a starting place - a place (method) where the computer can begin executing the program’s instructions. For C# (like many other languages), that entry point is traditionally called the Main()
method.
Let’s look at an example. The following code is a complete program that performs a simple task - displaying the text “Hello World” in a console window.
The name of the class is MyFirstProgram
and it contains only one method - Main
- which is the starting point of the program. That method contains a single instruction telling the computer to display the text “Hello World” to the user.
Notice that besides the name of the class and the Main method, there are various keywords (in blue) and punctuation (such as curly braces { }
, parentheses ( )
, square brackets [ ]
, semicolons ;
and “dot operators” .
). All of these pieces have to be arranged according to the specific rules of the programming language. We call these rules the grammar and the arrangement of our code using the rules the syntax. You can learn more about some of the subtle distinctions between these two terms by searching online, but for now you can think of these words as being almost the same thing.
Syntax is the study of sentences and their structure, and the constructions within sentences. Syntax tells us what goes where in a sentence. Grammar is the general term referring to the set of rules in a given language including syntax , morphology, while syntax studies sentence structures. Apr 3, 2013 - by Googlebot
Order is important
The order in which we place our instructions within a method is important. All of the instructions are processed sequentially - one after the other. Look at this simple example:
In the main method are three lines of code (lines 5-7) which run sequentially. If I change the order of these instructions, then the program won’t run correctly. In fact, changing the order might even violate part of the C# grammer, creating what’s called a syntax error, and the computer won’t be able to run the program at all!
Understanding the Grammar of a programming language is fundamental to knowing how that language works. Without that knowledge, our attempts to write code will quickly be filled with frustration. The next chapter examines the grammar of C#.
Summary
We’ve covered a lot of ground so far, and all of it is fundamental to writing computer programs in C#. To recap, you should now know that
- Instructions are always placed inside methods (in fact, a method can be defined as a “set of instructions”).
- Methods always appear inside of classes or similar structures.
- C# provides keywords for primitive data types. We use these keywords to declare variables which hold values.
- C# is an extensible language - allowing one to define new complex data types with their own information (variables) and instructions (methods) for processing that information.
A Simplified C# Grammar
A language grammar is a set of small rules that can be combined to produce a syntax.
The many small rules in the C# programming language is its grammar. These rules specify the accepted way to write C# code. Individually, each rule in itself is not enough to produce complete working code. Instead, the rules are like building blocks, offering various ways to assemble or construct your code.
Take, for example, some of the grammar around variable declarations and program statements. A simplified grammar for declaring a variable in C# would look like this:
* dataType variableName*
We would have to supply both a data type and a variable name to declare a variable. Thus, if we wanted to store a whole number to represent a count, we could use the int
keyword and write the following:
int count
By itself, this variable declaration is not enough to produce a complete instruction in C#. We have to combine the variable declaration grammar with the grammar for a program statement in order to make a single instruction telling the computer to create the variable. In general, a program statement must be a statement that ends with a semicolon (;
). In other words, the program statement grammar requires the semicolon as “punctuation”, much in the same way that English sentences must end in a period. Thus, our completed syntax for creating a variable would be
int count;
The formal grammar for C# is actually quite large and complex. In fact, the grammars for most programming languages are so complex that they require another “language” to describe the grammar. An early and fairly standard language or means to express grammars is the Bakus-Naur Form, or BNF.
For our purposes, we will present a much more simplified grammar. The following sections express the bulk of the C# grammar used in this introduction to programming fundamentals in C#.
Common Grammar Elements
Most of the grammar rules in C# are quite short, defining the order of keywords, identifiers and symbols. In the following grammars, the use of square brackets ([]
) indicate an optional part of the syntax for the grammar rule; those square brackets are not actual symbols in the final syntax of the grammar. With each of the rules is a very brief explanation. Later chapters will go into more depth and provide examples of how to use these grammars.
Program Statements and Statement blocks
Individual instructions are known as Program Statements. The instructions can be short and simple, or they can be long an complex. In either case, the program statement must end in a semicolon (;). These individual instructions run sequentially, one after the other, so the order of individual instructions is important.
Besides individual instructions, we can group individual statements into Statement Blocks. A statement block is a set of zero or more program statements that are enclosed in a set of curly braces ({ }
). Statement blocks are frequently used with Flow Control Statements (such as the if
and for
statements).
Variable Declarations
Before a variable can be used, it must be declared. Declaring a variable tells the compiler to
- set aside room in memory to store information,
- treat that data as a specific data type, and
- refer to that information by the variable name
The grammar for declaring variables is as follows.
- A Variable Declaration defines a new variable where
-
dataType
is any built-in or programmer-defined data type. -
variableName
is - an optional initial value may be assigned, as denoted by
[= expression]
, whereexpression
is any valid C# expression whose final data type matches the variable’s data type. When a variable is declared and initialized at the same time, it is called a Variable Initialization. - additional variable names (with or without initial values) can be declared using a comma-separated list. All variables are of the same data type as the first variable in the list.
Assignment Operation
- Assignment Operations are operations where a value is assigned or stored in a variable.
-
variableName
is the name of the variable that will receive/store the value. -
assignmentOperator
is one of the following:=
Equals+=
Plus-Equals-=
Minus-Equals*=
Multiply-Equals/=
Divide-Equals%=
Modulus-Equals -
expression
is any valid C# expression whose final data type matches the variable’s data type. - An assignment operation is made into an assignment statement by adding a semicolon to the end of the operation. For example,
total = price * quantity;
Expressions
An Expression is any combination of literal values, variable names, operators (such as the arithmetic operators), and/or method calls (where the method returns a value). When an expression is processed by the computer, a single value is produced. This value can then be used in whatever operation the expressions occurs. For example, the value might be passed into a method as part of a method call, or it might be placed in a variable as part of an assignment statement.
Namespace Declaration and Using Statements
- A Namespace Declaration groups programmer-defined data types where
- The
Name
of the namespace can be one or more dot-separated names. For example, the following are all valid names for namespaces.System
System.Collections
MyGame
MyGame.GameRules
Namespaces are used to group classes and other programmer-defined data types into a single named group. The reason for grouping programmer-defined data types into namespaces is to prevent what are called “naming collisions”. A naming collision is where two or more classes or other programmer-defined data types are given the same name. In other words, you cannot have two classes named Circle
in the same namespace. However, if you place those two classes in different namespaces, then that is acceptable because the compiler will then be able to distinguish between the two classes based on the namespace they belong to.
Whenever a class or other data type is placed in a namespace, that namespace becomes part of the fully-qualified name of the data type. For example, if a class named Circle
is placed in a namespace called Geometry.Shapes
, then the fully qualified name of the class is Geometry.Shapes.Circle
.
Namespaces allow us to isolate our classes and other data types into groups. All of the classes/data types in a given namespace can automatically reference each other. To reference or use data types in other namespaces, we must either use their fully-qualified names or include them through the Using Statement.
The using statement allows access to all the data types in referenced namespace. Using statements are typically placed at the beginning of a file. The NamespaceName
is simply the complete name of the namespace that contains the classes or data types that we want to access.
Classes and Class Members
As an object-oriented language, classes play a very prominent part of the code we write in C#. It is within classes, for example, that we place variables (also called fields) and methods (which are “named sets of instructions”). One of the first things that classes give us developers is a context or scope for the code that we write. Classes are also building blocks, acting as blueprints for new and complex data types that we as programmers can create as we develop richer and more complex computer programs. Classes permeate all the code that we write in C# and are so fundamental that you can’t even write a “Hello World” program without them.
Class Definition
- A Class Definition describes a new data type where
-
[accessModifier]
is eitherpublic
{format: csharp} orinternal
{format:csharp}. If no access modifier is provided, then the default modifier isinternal
{format:csharp}. -
ClassName
is the programmer-supplied name for the class (in TitleCase format) -
FieldDeclarations
,PropertyDeclarations
,Constructors
andMethodDeclarations
are all optional and can appear in any order.
See the related grammars below to see how they are defined.
Field Declarations
- A Field Declaration identifies a static or instance member variable of the class where
-
[accessModifier]
is eitherpublic
{format: csharp},private
{format: csharp},protected
{format: csharp}, orinternal
{format:csharp}. If no access modifier is provided, then the default modifier isprivate
{format:csharp}. -
[static]
{format:csharp} is an optional keyword. If present, the field is shared among all instances of the class. If absent (which is the common case) then the field is an instance member and one is created every time an object based on the class is created. -
dataType
is any built-in data type or the name of a programmer-defined data type. -
_FieldName
is a the name you give to the member variable. By convention, private fields are given an underscore as a prefix to the name. -
constantExpression
is an optional expression that generates data whose value can be determined at compile time. Being a constant expression does not mean that the field is a constant, only that the initial value stored in the field is a constant and can be known at compile time before the program runs.
Property Declarations
Properties are a kind of cross between methods and fields. On one hand, they are used in the same way that fields are. When you want to assign (or set) a value to a property, you place the property on the left side of the assignment operator. When you want to use (or get) the properties value, you simply reference the property name just as you would a field name.
Internally, however, the get and set operations are like the bodies of a method, where you can place instructions to retrieve or change the data in the class or object.
Explicitly Implemented Property
Explicitly implemented properties are properties where the programmer supplies the getter and setter implementations. The bodies of the getter and setter may reference a field (known as a backing store) that holds the actual information. In these cases, the property is working to provide a controlled access to the underlying field’s data.
In other situations, a property may merely have a getter where the body of the getter derives or calculates a value to return from some other source, such as a calculation.
- A Property Declaration identifies a static or instance member of the class where
-
[accessModifier]
is eitherpublic
{format: csharp},private
{format: csharp},protected
{format: csharp}, orinternal
{format:csharp}. If no access modifier is provided, then the default modifier isprivate
{format:csharp}. -
[static]
{format:csharp} is an optional keyword. If present, the Property is shared among all instances of the class. If absent (which is the common case) then the Property is an instance member. -
dataType
is any built-in data type or the name of a programmer-defined data type. -
PropertyName
is a the name you give to the member property. -
Body of getter
is a set of instructions that must ultimately return a value of the same data type as the property. -
Body of the setter
is a set of instructions that can process incoming data that is in thevalue
{format:csharp} keyword. A typical implementation will store that data into the property’s backing store.
Autoimplemented Property
Autoimplemented properties are properties where the compiler takes care of the getter and setter implementations and also supplies a hidden field as the backing store for the property. The default get implementation is to retrieve the value from the backing store while the default set implementation is to place a value into the backing store.
- A Property Declaration identifies a static or instance member of the class where
-
[accessModifier]
is eitherpublic
{format: csharp},private
{format: csharp},protected
{format: csharp}, orinternal
{format:csharp}. If no access modifier is provided, then the default modifier isprivate
{format:csharp}. -
[static]
{format:csharp} is an optional keyword. If present, the Property is shared among all instances of the class. If absent (which is the common case) then the Property is an instance member. -
dataType
is any built-in data type or the name of a programmer-defined data type. -
PropertyName
is a the name you give to the member property.
Method Declarations
- A Method Declaration defines a named set of instructions.
-
[accessModifier]
is eitherpublic
{format: csharp},private
{format: csharp},protected
{format: csharp}, orinternal
{format:csharp}. If no access modifier is provided, then the default modifier isprivate
{format:csharp}. -
[static]
{format:csharp} is an optional keyword. If present, the method is shared among all instances of the class. If absent (which is the common case) then the method is an instance member. -
returnType
is any built-in data type or the name of a programmer-defined data type. The return type signifies the kind of information that the method will return. If the method does not return any information, then the keywordvoid
is used as the return type. -
MethodName
is a the name you give to the method. -
ParameterList
is a comma-separated list of individual variable declarations.
Constructors
- A Constructor is a set of instructions used when instantiating (creating) an object.
-
[accessModifier]
is eitherpublic
{format: csharp},private
{format: csharp},protected
{format: csharp}, orinternal
{format:csharp}. If no access modifier is provided, then the default modifier isprivate
{format:csharp}. -
ClassName
- All constructors use the name of the class to which they belong as the name of the constructor. -
ParameterList
is a comma-separated list of individual variable declarations. - Classes never return any information - they are simply blocks of instructions used to set up the initial state of the object.
Flow-Control Statements
- If-Else
- Case
- For and Foreach
- While and Do-While
Interlude 1 - Console Input/Output
TODO:
- Output
- Input
- Converting user input
The ABCs of Classes and Objects
A Tutorial and Walkthrough of coding Classes and Objects
The purpose of this tutorial is to give the student an opportunity to write code and to explore the fundamentals of Classes and Objects. Specifically, this tutorial demonstrates fields, properties, constructors, and methods in class declarations along with object instantiation and the use of the member access operator.
About Classes and Objects
A computer program is a set of instructions for manipulating information.
Information is central to the whole purpose of computer programs, and is the “thing” that has value in the eyes of the end-user. Information is often complex, being made up of many smaller pieces of related information. For example, the information could be the details of a bank account, the marks earned by a student, or information about a company or an employee. A key aspect of all of these is that the details of the information (First and Last name, for example) are bundled together and treated as a single unit - a composition of related information. In programming, those bundled sets of detailed information are known as objects.
Objects embody more than just information, however. A person can do things with objects. For example, if you imagine a Bank Account as an object, it can be made up of a bunch of information, such as Account Number, Balance and Overdraft Limit. Now imagine the things you can do with a bank account: you can Deposit or Withdraw money. These actions are tightly related to bank accounts, largely because they directly affect the information in the bank account. These actions are carried out as a set of instructions - known as methods - which are carried out in a prescribed way through a set of one or more program statements.
Two objects can have the same set of characteristics, but different information. For example, consider two Bank Account objects where one account has a balance of $4,500.00 and another has a balance of $125.00. The actual information is different, but the characteristics (what Bank Accounts “look like”) remain the same. The things you can do with the two bank accounts also remain the same: you can Deposit and Withdraw money. In programs, the characteristics of an object are defined in something called a Class. A class defines
- What an object “looks like” (information)
- How an object “behaves” (instructions to manipulate information)
For a computer program to be able to work with objects, it must first know the class definition of that object. Coding a class definition is like telling the computer about a new data type that you are defining. The name of the class is the name of the new data type. Once the data type is defined, then it can be used to create objects. A class definition (or “class” for short) acts as a blueprint for creating objects; from the blueprint, the computer is told what the object “looks like” and how it “behaves”. Every object is said to be an “instance” of a class, in the same way that a physical car would be an instance of the car’s blueprint. The term instantiation basically means “to instantiate”, and it refers to the act of creating objects in code. An object is said to be “based on” a class when that class is the data type from which an object has been instantiated (just as a physical car would be “based on” the car’s blueprint).
So, objects and classes are closely related, but not exactly the same. The relationship between objects and classes is pretty much the same as the relationship between ordinary values and data types.
Coding the Class Definition
A class definition can contain four parts: Fields, Properties, Constructors, and Methods. Fields and properties work together to describe what an object based on the class “looks like” (the object’s information). Constructors and Methods describe how the object “behaves” (what instructions an object performs to manipulate information).
- Fields
- Fields are variables that are shared by/accessible to all the other members of a class (properties, constructors, and methods).
- Fields are almost always declared as private (meaning that they are not directly accessible from outside the class).
- Although not commonly done, a field can be assigned an initial default value when it is declared.
- Properties
- Properties are used like ordinary fields or variables, but work internally as methods.
- Properties can have a Get method and/or a Set method.
- The Get method is used when attempting to retrieve a value from the property.
- The Set method is used when attempting to store a value in the property.
- Properties often (but not always) have a corresponding field that is used to hold or store the property’s value in the “background”; the field associated with a property is often referred to as the “backing store”.
- Properties that do not have a corresponding backing store will only have a Get method that calculates a value based on other values; there is no Set method because there is no single field in which to store a value for the property.
- Properties are usually declared as public (meaning that they can be accessed directly from outside the class).
- Constructors
- Constructors are called automatically whenever an object is created. Objects are created by the new keyword, whose general syntax is as follows:
new ClassName(ArgList)
- ArgList (or Argument List) is a comma-separated list of values that are sent to the constructor. The decision of which constructor can be called depends on the arguments (values) sent in when the new keyword is used; the compiler looks for a constructor with a parameter list that matches the argument list.
- Constructors have one purpose: to make sure that the fields in the object have meaningful values. This usually means that a constructor will need some parameters to get those values from the code that creates the object.
- Usually, constructors are made public.
- A class can define more than one constructor. If no constructor is defined for a class, then a “default constructor” is used, which simply sets all the fields to their default values.
- Constructors are called automatically whenever an object is created. Objects are created by the new keyword, whose general syntax is as follows:
- Methods
- A method is a set of instructions that are typically focused on manipulating or working with the fields and properties of an object.
- Methods are identified by a name (called the Method Name) and a parameter list; together the method name and parameter list make up what is called the Method Signature.
- If a method has a return type of void, then it is known as a Subroutine; subroutines do not return any information to the code that called it.
- If a method has any return type except void, then it is called a Function; functions can only return one item, and must do so using a return statement.
A Tutorial - OOP First in Three Parts
Practice makes better!
This three-part tutorial establishes the foundations of the key grammars around classes and objects. The first two work on a BankAccount and Bank class while the last part demonstrates the same concepts around the idea of rental cars and rental companies.
Part 1
This tutorial will walk you through the steps to create a class and an object based on the following design. This first program will demonstrate a BankAccount class and the transfer of money from one bank account to another.
- Open up Visual Studio and create a new C# Console Application project. In that project, create a folder called “Tutorial”.
- Right-click on the Tutorial folder, and create a class called “BankAccount”. Edit the BankAccount class so that it has the following code.
- Create another class inside the Tutorial folder, and name it “Bank”. Edit the class so that it has the following code.
- Create a driver class named “DemoDriver” and place it in the Tutorial folder. Edit the driver so that it has the following code.
- Open the Program.cs file and edit the Main() method to call the demo driver class.
When you run the program, you should see the result as shown in the following screen-shot.
Part 2
A big aspect of developing computer programs is the work of modifying existing code that you or others have written. In this part of the tutorial, we’ll revisit the BankAccount and DemoDriver classes to add and take advantage of a new method on the BankAccount class: ToString().
- Open the BankAccount class and modify it by adding the following ToString() method in the body of the class.
- Open the DemoDriver class and modify the DisplayAccount() method to look like the following code.
Part 3
Many times, properties act to provide transparent access to fields. (All of the examples so far have used Properties in this simplest way.) In C# version 3.0, a new syntax was introduced called auto-implemented properties which reduces the amount of code required to get the same effect as these transparent properties.
Auto-implemented properties do not have a field as a “backing store” and do not have bodies for the get and set methods. For example, the following code demonstrates an auto-implemented property.
This property has the exact same effect as the following, longer approach of using a property with a field in the background.
In this part of the tutorial, you will create classes that use auto-implemented properties, as well as regular properties.
- Open up Visual Studio and create a new C# Console Application project. In that project, create a folder called “Tutorial”.
- Create a new class in the Tutorial folder called RentalCar. Edit the class so that it matches the following code.
- Create another class named RentalCompany. Edit the code to match the following.
- Create a final class named DemoRentals. This will be the driver to use RentalCompany and RentalCar objects. Edit DemoRentals to match the following.
- Lastly, modify the Main method in Program.cs to call the DemoRentals.Start() method.
- Run the program. You should get results similar to the following screenshot.
OOP As A Foundation
Topic A - Starting Classes
Overview
This topic introduces some of the basic syntax and grammar surrounding Object-Oriented Programming in C#. The following C# keywords are introduced.
- public
- class
- static
- string
- void
- return
- namespace
This topic will also introduce the following grammars, syntax and language constructs:
- Simple class declaration syntax (no members)
- Simple class declaration syntax (with static methods)
- Method overloading
- Method declaration syntax (class methods, with the static keyword)
- Parameter declaration (variable declaration) syntax
- Class (static) method call syntax (with and without arguments)
- main function (entry point of program)
- Console output
- Simple string concatenation
- C# as a case-sensitive language
- Single-line, multi-line and XML comments
LOGs
At the end of this topic, you should be able to …
OOP Basics
- Define the term “class” as used in OOP
- Explain the purpose of classes in computer programs
- Describe the primary keywords used in declaring a class
- Define the term “method” and give an example
- Create simple classes with methods only
- Explain what is meant by “overloading”
- Describe the syntax of a method declaration (aka, method implementation)
- Explain how classes with identical names can be distinguished from each other
General Programming Concepts and Terms
- Define the term “keyword” as it applies to programming languages
- Define the term “identifier” as it applies to programming languages
- Define the term “program statement” and identify how the computer recognizes a program statement
- Define the term “syntax” as it applies to programming languages
- Identify the entry point for every computer program
- Perform simple output to the console using System.Console
- Identify the basic structure of a simple C# program
- Explain what is meant by a “driver”
- Explain what is meant by a “case-sensitive” programming language
- Explain what is meant by a “strongly-typed” programming language
- Explain what “string concatenation” is and how it works
- Define and distinguish the terms “argument” and “parameter”
- Use single-line, multi-line and XML comments as a means of documenting code
- List the four pieces of information to include in comments at the top of every source file
Intro to IDEs
- Define the term “IDE” as it relates to software development tools
- Define the terms “project” and “solution”
- Identify the various parts of the IDE (Integrated Development Environment) used in this course
- Create a new project in the IDE for this course
- Create new source files in the IDE for this course
- Add existing files to a project
Code Samples
The following examples are used to illustrate this topic.
- Nothingness - This class represents the absolute minimum code required to code a class. Even though it’s not a very “useful” class, it does provide and introduction to the ideas of classes, keywords and identifiers.
- HelloWorld - This adaptation of the classic “Hello World” program illustrates static methods. This example includes and introduces the concept of a “driver”.
- Comments - This class is not really “functional”, but is intended to illustrate the basic types of comments allowed in the programming language.
Nothingness
This class represents the absolute minimum code required to code a class. Even though it’s not a very “useful” class, it does provide and introduction to the ideas of classes, keywords and identifiers.
HelloWorld
This adaptation of the classic “Hello World” program illustrates static methods. This example includes and introduces the concept of a “driver”.
Comments
This class is not really “functional”, but is intended to illustrate the basic types of comments allowed in the programming language.
Practice Exercises
- OuterSpace – This class is the simplest possible class (and, coincidentally, one of the most useless).
- AnsweringMachine – The AnsweringMachine class provides a method to give an answer to an incoming phone call.
Driver – This class is the driver for the AnsweringMachine class. - Salutation – This is the Salutation class that was previously demonstrated.
HelloWorld_Driver – This class is the driver for the Salutation class.
OuterSpace
This class is the simplest possible class (and, coincidentally, one of the most useless).
Problem Statement:
Create a class called OuterSpace. This class has no members at all (it is empty, like outer space). Add a multi-line comment at the beginning that should include your full name as the author of the class. Also make an XML comment for the class as a whole.
AnsweringMachine
The AnsweringMachine class provides a method to give an answer to an incoming phone call.
Problem Statement:
Create the AnsweringMachine class so that it can provide a default answer for an incoming phone call as well as a customizable answer. The methods should be named answer and they should return a String. There should be two methods in total, and both of them should be declared as static. The actual content of the default message can be whatever you choose, while the customizable method will receive a single String argument that has the name of the person receiving the message. Also create a driver that demonstrates the AnsweringMachine in a Console environment.
Use the following details to guide your solution.
Method Detail
-
public static string Answer()
Provides a standard default message for the answering machine.Returns:
A String that instructs the caller to leave a message. -
public static string Answer(string name)
Provides a customizable message for the answering machine.To use this method, supply a person’s name, and this name will be incorporated into the message that is returned. For example,
System.Console.WriteLine(AnsweringMachine.Answer("Bob");
Parameters:
name
- A String that is the name of the person being called.
Returns:
A String that instructs the caller to leave a message for the person with the supplied name.
Salutation and HelloWorld_Driver
In each of the single-line comments above a line of code, enter a phrase or sentence in English that describes what is done in that line of code.
Salutation
HelloWorld_Driver
Topic B – Starting Classes
Overview
This topic introduces some of the basic syntax and grammar surrounding Object-Oriented Programming in Java. The following keywords are introduced. (Note that additional keywords from previous topics may also be present.)
- new
- char
- int
- double
This topic will also introduce the following grammars, syntax and language constructs. (Note that additional concepts from previous topics may also be present.)
- Method declaration syntax (instance methods)
- Object instantiation
- Instance method call syntax (with and without arguments)
- Class Field declaration syntax (member variables)
- Assignment statement syntax
- Accessing public fields in objects (storing & retrieving values)
Note that this package introduces the distinction between static and non-static methods. Static methods, also known as “class methods”, are identified by the keyword static and must be qualified by the class name; for example, in the following method call Salutation is the name of the class and Greeting is a static method of that class.Salutation.Greeting()
Non-static methods, also known as “instance methods”, are identified by the absence of the static keyword and must be called by using an instance of the class (that is, an object). An example of calling an instance method would beapp.Greeting()
where the app is an instance of the Salutation class.
LOGs
At the end of this topic, you should be able to …
OOP Basics
- Define the term “object” as used in OOP
- Describe the syntax of static and non-static method declarations
- Compare and contrast the syntax for method declarations (implementations) vs. method calls (for both static and non-static methods)
- Define the term “field” as used in OOP and give an example
- Compare and contrast instance and class members, for both methods and fields
- Identify the “member access operator” and explain it’s purpose
- Create (instantiate) objects
- Distinguish between classes and objects in OOP
- Explain what is meant by the phrase “type extensibility” and how classes represent “complex” data types in OOP
General Programming Concepts and Terms
- List the three categories of simple information
- Define the term “intrinsic data type”
- Explain what an assignment statement is and how it works
- Compare and contrast the three categories of simple information with the complex types of information which classes represent
- Explain the advantages of using classes and objects instead of only using intrinsic data types
Code Samples
The following examples are used to illustrate this topic.
- Salutation - This adaptation of the classic “Hello World” program illustrates instance methods. This example includes and introduces the concept of a “driver” and the idea of creating (instantiating) an object based on a class.
- Person - This simple class illustrates and introduces the idea of fields (member variables) of a class. This example includes and introduces the idea of a driver. The driver also illustrates how a single class can be used to instantiate multiple, distinct objects.
- Account - This simple class also illustrates fields. This example includes a driver.
- Comments - This class continues the illustration of the kinds of comments allowed in the programming language. This class builds on the previous example by showing comments for fields as well as methods.
- Student - This class introduces a slightly more complex class by offering more fields. This example includes two drivers. By using two drivers, this illustrates that separate “programs” can share/use the same class definition.
- The first driver is a simple “test driver” to ensure that the class is “working”. This introduces the student to the idea that it is necessary to “test” or prove that the code we are creating is valid.
- The second driver illustrates how the a single class can act as the basis for creating many distinct objects.
- Employee + Company - These are other classes similar to the Person and Student classes. These classes, however, “share” a driver, illustrating the fact that any given “program” typically uses more than one class. In addition, this driver introduces the ToString() and Parse() methods of the DateTime class.
Salutation & Driver
This adaptation of the classic “Hello World” program illustrates instance methods. This example includes and introduces the concept of a “driver” and the idea of creating (instantiating) an object based on a class.
Person
This simple class illustrates and introduces the idea of fields (member variables) of a class. This example includes and introduces the idea of a driver. The driver also illustrates how a single class can be used to instantiate multiple, distinct objects.
- Data Attributes of the Person class:
- FirstName : String
- LastName : String
- Age : Integer
Account
This simple class also illustrates fields. This example includes a driver.
* Data Attributes of the Account class:
* AccountNumber : Integer
* Balance : Real
* OverdraftLimit : Double
Comments
This class continues the illustration of the kinds of comments allowed in the programming language. This class builds on the previous example by showing comments for fields as well as methods.
Student
The Student class introduces a slightly more complex example by offering more fields.
- Data Attributes of the Student class:
- Name : String
- Gender : Character
- StudentId : Integer
- Program : String
- GradePointAverage : Double
- FullTime : Boolean
This example also includes two drivers. By using two drivers, this illustrates that separate “programs” can share/use the same class definition.
The first driver is a simple “test driver” to ensure that the class is “working”. This introduces the student to the idea that it is necessary to “test” or prove that the code we are creating is valid.
The second driver illustrates how the a single class can act as the basis for creating many distinct objects.
Employee & Company
The Employee and Company classes are similar to the Person and Student classes.
- Data Attributes of the Employee class:
- FirstName : String
- LastName : String
- SocialInsuranceNumber : Integer
- YearlySalary : Real
- EmploymentStartDate : Date
- Gender : Character
- Data Attributes of the Company class:
- Name : String
- City : String
- IsIncorporated : Boolean
- BusinessStartDate : Date
- NumberOfEmployees : Integer
- GrossIncomeToDate : Real
In the following driver, the Employee and Company classes are both being used, illustrating the fact that any given “program” typically uses more than one class. In addition, this driver introduces the overloaded ToString() method in the DateTime class to improve the output of displaying dates and the Parse() method to convert a string representation of a date to an actual DateTime instance.
Practice Exercises
- AnsweringMachine - Create the AnsweringMachine class so that it can provide a default answer for an incoming phone call as well as a customizable answer.
- Account - Extend the Account class from the example to include more information. Specifically, include an AccountType:String, BankName:String, BranchNumber:Integer, and InstitutionNumber:Integer.
- CanadianAddress - Create the CanadianAddress class so that it can represent the majority of possible addresses that some place may have in Canada.
- Course - Create the Course class so that it represents a post-secondary course.
- ExamResult - Create the ExamResult class so that it represents the results of an exam written by a student.
- LabResult - Create the labResult class so that it represents the results of a lab submitted by a student.
AnsweringMachine
The AnsweringMachine class provides a method to give an answer to an incoming phone call.
Problem Statement:
Create the AnsweringMachine class so that it can provide a default answer for an incoming phone call as well as a customizable answer. The methods should be named answer and they should return a String. There should be two methods in total, and both of them should be declared as instance members (non-static). The actual content of the default message can be whatever you choose, while the customizable method will receive a single String argument that has the name of the person receiving the message.
Also create a driver that demonstrates the AnsweringMachine in a Console environment
Create a driver for the AnsweringMachine class that creates an instance of the class and displays the results of calling the answer methods.
Account
Extend the Account class from the example to include more information.
Problem Statement:
Extend the Account class from the example to include more information. Specifically, include an AccountType:String, BankName:String, BranchNumber:Integer, and InstitutionNumber:Integer.
Also modify the driver to make use of the added information.
Notes
The branch number and the institution number together make up the Transit Number for a bank. “The bank transit number is 8 digits long. This is divided into a 5 digit branch number and 3 digit institution code, for example 10000-200.” (See http://en.wikipedia.org/wikiSort_code)
For more information on bank accounts and transit numbers in Canada, see http://en.wikipedia.org/wiki/Routing_transit_number#Canadian_transit_number.
CanadianAddress
This class represents an address for some place in Canada.
Problem Statement:
Create the CanadianAddress class so that it can represent the majority of possible addresses that some place may have in Canada. Design the class to have only public fields, as specified in this document.
- Data Attributes of the CanadianAddress class:
- Street : String
- Unit : String
- City : String
- Province : String
- PostalCode : String
- RuralRoute : String
- BoxNumber : String
Also create a driver for testing this class; you may use any name for the driver as long as it is not already mentioned in this package. In the driver, create instances of the CanadianAddress class that represent your current address as well as the address of your school (use hard-coded data).
Course
This class represents a post-secondary course with a theory (exam) and a lab portion.
Problem Statement:
Create the Course class so that it represents a post-secondary course. Design the class to have only public fields, as specified in this document.
- Data Attributes of the Course class:
- CourseName : String
- CourseNumber : String
- ExamCount : Integer
- LabCount : Integer
- ClassHours : Integer
Also create a driver for testing this class; you may use any name for your driver as long as it is not already mentioned in this package. In the driver, instantiate all of the first term classes you are taking and populate those objects with data (use hard-coded data).
ExamResult
This class represents the results of an exam for a student.
Problem Statement:
Create the ExamResult class so that it represents the results of an exam written by a student. Design the class to have only public fields, as specified in this document.
- Data Attributes of the ExamResult class:
- Name : String
- TotalMarks : Integer
- MarksEarned : Real
- ExamWeight : Integer
- StudentId : Integer
Also create a driver for testing this class; you may use any name for the driver as long as it is not already mentioned in this package. In the driver, instantiate all of the exams you have taken to date in this course and populate those objects with data (use hard-coded data).
LabResult
This class represents the results of an lab for a student.
Problem Statement:
Create the labResult class so that it represents the results of a lab submitted by a student. Design the class to have only public fields, as specified in this document.
- Data Attributes of the LabResult class:
- LabNumber : Integer
- TotalMarks : Integer
- MarksEarned : Real
- LabWeight : Integer
- StudentId : Integer
Also create a driver for testing this class; you may use any name for the driver as long as it is not already mentioned in this package. In the driver, instantiate all of the labs you have submitted to date in this course and populate those objects with data (use hard-coded data).
Topic C – Starting Classes
Overview
This topic introduces some of the basic syntax and grammar surrounding Object-Oriented Programming in C#. The following keywords are introduced. (Note that additional keywords from previous topics may also be present.)
• private
• this
• bool
• true
• false
• DateTime
This topic will also introduce the following grammars, syntax and language constructs. (Note that additional concepts from previous topics may also be present.)
• Private fields (encapsulation)
• Public properties (encapsulation)
• Constructor syntax
• Object instantiation with parameterized constructors
• Overriding the Object’s ToString() method
Daily LOGs
The following daily LOGs are covered in this package.
OOP Basics
• Define the term “encapsulation” as used in OOP
• Explain why encapsulation is a good design principle
• Define the term “access specifier” and identify where it occurs in the code for a class
• List the two access specifiers that are used in this course
• Describe the difference between “private” and “public” members of a class
• Describe the concepts of “properties” as used in OOP
• Define the term “backing store” as it applies to properties and fields
• Describe and explain the purpose and function of a constructor for a class
• Describe the syntax of a class constructor
• Identify when a class constructor is “called”
• Define the term “class scope” as used in OOP
• Define the term “local scope” as used in OOP
• Define the term “state” as applied to objects used in OOP
• Define the term “override” as used in OOP
• Explain the purpose and function of the ToString() method and why we sometimes want to change its default behaviour when we create classes
• Create simple class diagrams to represent classes
General Programming Concepts and Terms
• List three levels of scope
Code Samples
The following examples are used to illustrate this topic.
- Person - This simple class illustrates and introduces the idea of encapsulation (private fields with public properties). This example does not have a constructor. This example includes a driver. The driver is also used to illustrate how, with only fields & getter/setter methods, an object can be in an “unknown” (and invalid) state as soon as it is created; this idea is introduced to show the need for a constructor, and opens the discussion of the idea of a “default constructor”.
- Account - This simple class also illustrates encapsulation, but with some of the fields being read-only. This necessitates the presence of a constructor. This example includes a driver which shows that a class is in a “known state” as soon as it is created.
- Student - This class reinforces the idea of encapsulation and constructors. It also introduces the idea of overloading the default toString() method that every class inherits from the Object class. This example includes a driver with multiple objects, showing how the use of constructors makes it easier to create many objects in a few lines of code.
- Employee + Company - These are other classes similar to the Person and Student classes, but the Employee class uses Auto-Implemented properties (new since C# 3.0). Lastly, these classes “share” a driver, illustrating the fact that any given “program” typically uses more than one class.
The following class diagrams detail the design of these examples.
Person
This simple class illustrates and introduces the idea of encapsulation (private fields with public properties). This example does not have a constructor. This example includes a driver. The driver is also used to illustrate how, with only fields & getter/setter methods, an object can be in an “unknown” (and invalid) state as soon as it is created; this idea is introduced to show the need for a constructor, and opens the discussion of the idea of a “default constructor”.
Account
This simple class also illustrates encapsulation, but with some of the fields being read-only. This necessitates the presence of a constructor. This example includes a driver which shows that a class is in a “known state” as soon as it is created.
Student
This class reinforces the idea of encapsulation and constructors. It also introduces the idea of overloading the default ToString() method that every class inherits from the Object class. This example includes a driver with multiple objects, showing how the use of constructors makes it easier to create many objects in a few lines of code.
Company and Employee
These are other classes similar to the Person and Student classes, but the Employee class uses Auto-Implemented properties (new since C# 3.0). Lastly, these classes “share” a driver, illustrating the fact that any given “program” typically uses more than one class.
Practice Exercises
- Person - Extend the Person class to include a constructor.
- Account – Extend the Account class from the example to include more information. Specifically, include an AccountType:String, BankName:String, BranchNumber:Integer, and InstitutionNumber:Integer.
- CanadianAddress - This class represents an address for some place in Canada.
- Course - This class represents a post-secondary course with a theory (exam) and a lab portion.
- ExamResult - This class represents the results of an exam for a student.
- LabResult - This class represents the results of an lab for a student.
Person
This simple class illustrates and introduces the idea of encapsulation (private fields with public properties). This example uses a constructor for ensuring the state of an object when it is instantiated (created).
Extend the Person class from the example to include a constructor that takes in a first and last name as well as an age in the parameter list.
Account
Extend the Account class from the example to include more information.
Problem Statement:
Extend the Account class from the example to include more information. Specifically, include an AccountType:String, BankName:String, BranchNumber:Integer, and InstitutionNumber:Integer. Base your solution on the following class diagram.
Also modify the driver to make use of the added information.
Notes
The branch number and the institution number together make up the Transit Number for a bank. “The bank transit number is 8 digits long. This is divided into a 5 digit branch number and 3 digit institution code, for example 10000-200.” (See http://en.wikipedia.org/wikiSort_code)
For more information on bank accounts and transit numbers in Canada, see http://en.wikipedia.org/wiki/Routing_transit_number#Canadian_transit_number.
CanadianAddress
This class represents an address for some place in Canada.
Problem Statement:
Create the CanadianAddress class so that it can represent the majority of possible addresses that some place may have in Canada. Design the class to use auto-implemented properties as specified in this class diagram. No constructor is required for this class.
Also create a driver for testing this class; you may use any name for the driver as long as it is not already mentioned in this namespace. In the driver, create two instances of the CanadianAddress class that represent your current address as well as the address of your school (use hard-coded data).
Course
This class represents a post-secondary course with a theory (exam) and a lab portion.
Problem Statement:
Create the Course class so that it represents a post-secondary course. Design the class to have auto-implemented properties and a constructor as specified in this class diagram. Note that the auto-implemented properties should have their “set” implementations marked as private.
Also create a driver for testing this class; you may use any name for your driver as long as it is not already mentioned in this namespace. In the driver, instantiate all of the first term classes you are taking and populate those objects with data (use hard-coded data).
LabResult
This class represents the results of a lab for a student.
Problem Statement:
Create the LabResult class so that it represents the results of a lab submitted by a student. Design the class to have the fields, properties, and constructor as specified in this document. Also create a ToString() method that describes the information in any given instance of this class.
Use an auto-implemented property for the MarksEarned; use fields as the “backing store” for all other properties.
Note that the following properties should not have “set” implementations: TotalMarks, LabNumber, LabWeight, and StudentId.
Here are some XML comments to describe the ToString( ) method.
Also create a driver for testing this class; you may use any name for the driver as long as it is not already mentioned in this namespace. In the driver, instantiate all of the labs you have submitted to date in any course and populate those objects with data (use hard-coded data); if you haven’t had any labs as of yet, then make up some data.
### ExamResult
This class represents the results of an exam for a student.
Problem Statement:
Create the ExamResult class so that it represents the results of an exam written by a student. Design the class to have the properties and constructors as specified in this document. (Note that there are two constructors for this class.) Also create a ToString( ) method that describes the information in any given instance of this class.
Use auto-implemented properties for the entire class. Note that only the following properties should have private “set” implementations: TotalMarks, ExamName, ExamWeight, and StudentId.
Also create a driver for testing this class; you may use any name for the driver as long as it is not already mentioned in this namespace. In the driver, instantiate all of the exams you have taken to date in this course and populate those objects with data (use hard-coded data); if you need to, you may create fake data for the driver.
Topic D – Testing and Debugging
Overview
This topic introduces the concepts of testing and verifying that code is running correctly. Two approaches will be presented and compared: Ad-hoc test drivers and Unit Tests.
Test drivers that rely on user input/output are the traditional means of verifying that code runs properly. The biggest benefit of test drivers is that they provide quick “ad-hoc” tests and are simple to produce on any IDE. However, test drivers have some major downfalls.
Current programming techniques make use of Unit Tests. A unit test is a simple, isolated test of some part of a class. Unit tests provide a way to clearly show the success or failure of a particular section of code. Unit tests are a key part of Test Driven Development (TDD).
Regardless of whether the developer uses test drivers or unit tests, it is important to remember that the developer is still responsible to make sure that the tests (and thus, the test results) are valid. Improper use of test drivers and unit tests can lead to
* “false positives” - indicating that something works when it really doesn’t, and
* “false negatives” - indicating that something is broken when it really isn’t
Note: For this course, students will be provided with unit tests and will not be expected to have to write their own unit tests. The unit tests supplied to students will use nUnit 2.5.7 which is already installed in the lab. To use these libraries at home, simply install nUnit 2.5.7 on your home computer.
LOGs
The following daily LOGs are covered in this package.
General Programming Concepts and Terms
- Explain the role of testing as it applies to software development
- Define and compare the terms “compile-time error” and “run-time error”
- Define the term “test driver”
- Create simple ad-hoc test drivers to test for run-time errors
- Define the term “TDD”
- Compare and contrast Test Drivers and Unit Tests
- Define the terms “false positive” and “false negative”
- List three downfalls of using Test Drivers
- Identify four benefits of using Unit Tests
- Add unit testing libraries and unit testing packages to existing programs
- Use unit tests to validate the requirements of a class’ design and behaviour
- Diagnose and correct software problems by using unit tests
Code Samples
The following examples are used to illustrate this topic.
- Person - This simple class was used to introduce the idea of encapsulation (private fields with public getters and setters). This example uses a constructor for ensuring the state of an object when it is instantiated (created). In this topic, it is used to demonstrate unit testing.
- Account - This simple class also illustrates encapsulation, but with some of the fields being read-only. This class uses a constructor (which is also necessary for getting state into fields which do not have corresponding setter methods).
- Student - This class reinforces the idea of encapsulation and constructors. It also demonstrates the idea of overloading the default ToString() method that every class inherits from the Object class.
- Employee – The Employee class represents basic information about an employee of a company.
- Company - These are other classes similar to the Person and Student classes. These classes, however, “share” a driver, illustrating the fact that any given “program” typically uses more than one class.
Test Drivers (Console I/O)
TBA
Unit Tests (nUnit)
The unit tests for these classes are included in the solution folder under the “Demos + Practice” folder. Simply double-click the NUnit Test Project (.nunit) and the tests will open in the NUnit GUI.
Person
This simple class was used to introduce the idea of encapsulation (private fields with public getters and setters). This example uses a constructor for ensuring the state of an object when it is instantiated (created). In this topic, it is used to demonstrate unit testing. The following diagram represents the last design for the Person class, as completed in the exercises portion of the previous topic.
The supplied unit tests check all of this previously documented behaviour, but they also check to see if the Person class has overridden the ToString()
method. Because this requirement is new, and has not yet been implemented, the unit tests show this as a “failed test.” The following diagram is the design that the unit tests are evaluating.
Should…
* Instantiate (build) from constructor
* Get/Set First Name
* Get/Set Last Name
* Get/Set Age
* [NOT-YET-IMPLEMENTED] Override ToString() to get the person’s full name (as first name then last name)
The test for this method will report as “failed” because it has not yet been implemented.
Account
This simple class also illustrates encapsulation, but with some of the fields being read-only. This class uses a constructor (which is also necessary for getting state into fields which do not have corresponding setter methods).
Should…
* Get Bank Name
* Get Branch Number
* Get Institution Number
* Get Account Number
* Get Account Type
* Get/Set Balance
* Get/Set Overdraft Limit
Student
This class reinforces the idea of encapsulation and constructors. It also demonstrates the idea of overloading the default ToString() method that every class inherits from the Object class.
Should…
* Get/Set Name
* Get/Set Gender
* Get/Set Student Id
* Get/Set GPA
* Get/Set Program
* Get/Set Full-Time
* Override ToString() to get the student’s ID and name
Employee
Should…
* Get/Set First Name
* Get/Set Last Name
* Get/Set Gender
* Get/Set Employment Date
* Get/Set Salary
* Get Social Insurance Number
Company
Should…
* Get/Set Name
* Get/Set City
* Get/Set Incorporated
* Get/Set Number of Employees
* Get/Set Gross Income to Date
* Get Business Start Date
Practice Exercises
- Person - This simple class was used to introduce the idea of encapsulation (private fields with public properties). This example uses a constructor for ensuring the state of an object when it is instantiated (created). It also demonstrates the overriding of the ToString() method inherited from the Object method.
- CanadianAddress - This class represents an address for some place in Canada.
Update this class to now include a constructor and to override the ToString() method. - Course - This class represents a post-secondary course with a theory (exam) and a lab portion.
- ExamResult - This class represents the results of an exam for a student.
- LabResult - This class represents the results of a lab for a student.
For this exercise, take the code solutions you created in the previous topic and compare your results with the jUnit tests for these classes; check with your instructor to see where you can download the unit tests.
Person
This simple class was used to introduce the idea of encapsulation (private fields with public properties). This example uses a constructor for ensuring the state of an object when it is instantiated (created). It also demonstrates the overriding of the ToString() method inherited from the Object method.
Should…
* Instantiate (build) from constructor
* Get/Set First Name
* Get/Set Last Name
* Get/Set Age
* Override ToString() to get the person’s full name (as first name then last name)
CanadianAddress
Should…
* Instantiate (build) from Constructor
* Get/Set: Street, Unit, City, Province, Postal Code, Rural Route, Box Number
* Override ToString() to just show Street, City, Province, and Postal Code
Course
Should…
* Instantiate from Constructor
* Get: Course Name, Course Number, Exam Count, Lab Count, Class Hours
ExamResult
Should…
* Instantiate from Constructor
* Get/Set: Marks Earned
* Get: Name, Student Id, Total Marks, Exam Weight
* Override ToString() to show
“The student (studentId) received earnedMarks/totalMarks for this examName exam.”
LabResult
Should…
* Instantiate from Constructor
* Get/Set: Marks Earned
* Get: Lab Number, Student Id, Total Marks, Lab Weight
* Override ToString() to show
“The student (studentId) received earnedMarks/totalMarks for this lab.”
Expressions, Math, Exceptions, If-Else, and Case
Topic E – Expressions and Math
Overview
This topic introduces concepts surrounding the use of arithmetic expressions and common Math library routines.
This topic will introduce the following grammars, syntax and language constructs. (Note that additional concepts from previous topics may also be present.)
- Arithmetic expressions
- Assignment statements
- Automatic type conversion and casting
- Integer division
- String concatenation
- Math library routines and constants (such as Random, Square Root, and π)
This topic will also take a deeper look at the distinctive aspects of variables, values and data types as well as demonstrate the use of overloaded constructors. This is also the first foray into creating classes that actually “do something” – these classes not only maintain state but also provide methods to generate other information besides that which is stored in a class’ fields.
LOGs
OOP Basics
- Explain how method overloading is applied to constructors
General Programming Concepts and Terms
- Name at least three kinds of program statements commonly seen in all high-level programming languages
- Distinguish between Variables, Values and Data Types
- List the five basic rules governing variables, values and data types
- List the intrinsic (built-in) data types in C#
- Explain what is meant by the phrase “type extensibility”
- Define the terms Reference Type and Value Type as they apply to data types in C#
- Define the term Identifier
- Create and initialize variables in C#
- Explain what is meant by the terms “scope” and “lifetime” as they apply to variables
- Describe what is meant by the term “expression”
- Explain the difference between program statements and expressions as well as how the two relate to each other
- List the three general sets of operators in C#
- Describe the two basic rules of using arithmetic operators that every compiler must follow.
- Use the arithmetic operators to create arithmetic expressions
- Explain what is meant by “integer division” and how that can be useful in solving particular problems regarding numbers.
- Explain the purpose of the modulus (%) operator.
- List and describe how to use the various assignment operators in C#
- Explain the difference between binary and unary operators
- Demonstrate understanding of operator precedence and how to override the default precedence of the arithmetic operators
- Summarize and distinguish the rules for automatic type conversion involving arithmetic operators and the assignment operator
- Determine the final data type for expressions involving mixed data types and type conversion
- Describe “type casting” and explain how to use it appropriately.
- Compare and contrast the prefix and postfix behaviour of the unary arithmetic operators
- Identify and explain how to use the common Math library routines (for Power, Square Root, Absolute, random numbers, pi, trigonometry, and the various types of rounding)
- Use Math rounding methods with arithmetic expressions to round real numbers to a specific precision
- Create random whole number values within a specific range
- Use type casting appropriately in C#
- Create constants in C#
- Explain how arithmetic operations affect character values in C#
- List the most common math functions of the Math class
- Demonstrate how to use the various Math functions in simple programs
- List and describe the commonly used fields and methods of the String class
- Demonstrate how to use “String Arithmetic” (concatenation)
- List the equivalent “wrapper” classes for each of the primitive types.
Code Samples
The following examples are used to illustrate this topic.
- Calculator – This class introduces simple arithmetic operations, demonstrating simple addition and multiplication.
- Person - This adaptation of the person class changes the previous design by replacing the age field with a “read-only” field for the person’s birth date. The approximate age of the person is then calculated based on the current date and the birth date.
- Account - This class illustrates simple addition and calculation by allowing deposits and withdrawals. Note that changes to the balance can now only be made through deposits and withdrawals; the balance is now “read-only”.
- ElapsedTime – This class demonstrates overloaded constructors and introduces the concepts of operator precedence and integer division.
- ResolveExpressions – This class is used in conjuncture with several sample expressions that illustrate operator precedence and automatic type conversion.
- Circle - This class represents a simple circle of a specified diameter. The radius, area and circumference are calculated.
- Square - This class represents a simple square with a specified length for its side. The area and perimeter are calculated.
- Fraction - This class represents a fraction as a numerator and denominator. It provides the double equivalent of the fraction’s value as well as a string representation that uses the numerator and denominator. It demonstrates type casting and the integer division issue.
- Angle - This class represents an angle and provides the value in the following units: degrees, radians and grads. It also gives a simple example of unicode characters (for degrees).
- StockItem - This class represents an item that is part of an inventory. The item has an item name, a cost and a profit margin (which can be positive or negative). By using the profit margin, it can derive the price of the item. This example illustrates rounding.
- Die - This class represents a single six-sided die. This example is used to illustrate random number generation and casting.
- ParkingCounter - This class represents a simple counter to monitor whether a parking lot is full or not; it tracks vehicles entering and leaving the parking lot and allows the counter to be reset when the lot is full or empty. This class illustrates increment and decrement operators and/or the assignment increment or assignment decrement operators.
- QuadradicEquation - This class is used to solve for the two possible values of a quadratic formula where quadradic equals zero. This sample illustrates order of operations and parentheses.
Calculator
This class introduces simple arithmetic operations, demonstrating simple addition and multiplication.
Problem Statement
Write the code that will act as a calculator for doing math. This first version must be a working prototype; as a prototype, it does not have to support all of the features of the final product.
The solution must meet the following requirements:
- Should add two whole numbers.
- Should multiply two whole numbers.
Use the following class diagram when creating your solution. Since this class does not have properties or fields, make the methods static.
Person
This adaptation of the person class changes the previous design by replacing the age field with a “read-only” field for the person’s birth date. The approximate age of the person is then calculated based on the current date and the birth date.
Problem Statement
Write the code that will represent a person with a first and last name and a date of birth.
The solution must meet the following requirements (new requirements are in bold):
* Should get and set the first and last name
* Should get the birth date
* Should get the person’s approximate age (which is the age that the person will turn to in the current year)
* Should get the person’s initials
* Should override ToString() to get the person’s full name (as first name then last name)
Use the following class diagram when creating your solution.
Account
This class illustrates simple addition and calculation by allowing deposits and withdrawals. Note that changes to the balance can now only be made through deposits and withdrawals; the balance is now “read-only”.
Problem Statement
Write the code that will represent a simple bank account.
The solution must meet the following requirements (new requirements are in bold):
- Should get the bank name, branch number, institution number, account number, balance, overdraft limit, and account type
- Should allow the overdraft limit to be set
- Should support deposits and withdrawals
Use the following class diagram when creating your solution.
ElapsedTime
This class demonstrates overloaded constructors and introduces the concepts of operator precedence and integer division.
Problem Statement
Write the code that will represent a period of elapsed time for a competitor in a marathon. It should be able to represent its information in two forms:
- Hours, minutes and seconds, and
- Total seconds.
The solution must meet the following requirements:
- Should calculate the hours, minutes and seconds given the total seconds
- Should calculate the total seconds given the hours, minutes and seconds
Use the following class diagram when creating your solution.
ResolveExpressions
This class is used in conjuncture with several sample expressions that illustrate operator precedence and automatic type conversion.
Problem Statement
Write the code that will provide the final value for the expressions in the following exercise (as a solution for a student’s exercise).
Expressions Exercise
On a piece of paper, evaluate the following expressions to show the final value and the data type of the final value. Show the order in which the operations are evaluated.
- 10.0 + 15 / 2 + 4.3
- 8 / 5 + 1.25
- 10.0 + 15.0 / 2 + 4.3
- 3.0 * 4 / 6 + 6
- 3.0 * (4 % 6) + 6
- 3 * 4.0 / 6 + 6
- 20.0 - 2 / 6 + 3
- 10 + 17 % 3 + 4
- (10 + 17) % 3 +4.0
- 10 + 17 / 4.0 + 4
The solution for question 2 is provided as an example. Use the accompanying class diagram when creating your coded solution as proof of your final answers.
Circle
This class represents a simple circle of a specified diameter. The radius, area and circumference are calculated.
Problem Statement
Write the code for the Circle class. The solution must meet the following requirements:
- Should get and set the diameter
- Should calculate the area, radius, and circumference
Use the following class diagram when creating your solution.
Square
This class represents a simple square with a specified length for its side. The area and perimeter are calculated.
Problem Statement
Write the code for the Square class. The solution must meet the following requirements:
- Should get and set the length of the side of the square
- Should calculate the area and perimeter
Use the following class diagram when creating your solution.
Fraction
This class represents a fraction as a numerator and denominator. It provides the double equivalent of the fraction’s value as well as a string representation that uses the numerator and denominator. It demonstrates type casting and the integer division issue.
Problem Statement
Write the code for the Fraction class. The solution must meet the following requirements:
- Should get the string representation of the fraction, as “numerator/denominator”
- Should get the numeric value of the fraction (as a real number)
- Should get the reciprocal of the fraction
Use the following class diagram when creating your solution.
Angle
This class represents an angle and provides the value in the following units: degrees, radians and grads. It also gives a simple example of unicode characters (for degrees).
Problem Statement
Write the code for the Angle class. The solution must meet the following requirements:
- Should get and set the angle’s value (in degrees)
- Should calculate the equivalent angle in Radians and Grads, using the following formulas:
- Radians = Degrees * (π / 180)
- Grads = Radians * (200 / π)
- Should override the ToString() method to return the angle in degrees, in the following format:
- degrees°
- The Unicode character for the degrees symbol (°) is ‘\u00B0’
Use the following class diagram when creating your solution.
StockItem
This class represents an item that is part of an inventory. The item has an item name, a cost and a profit margin (which can be positive or negative). By using the profit margin, it can derive the price of the item. This example illustrates rounding.
Problem Statement
Write the code for the StockItem class. The solution must meet the following requirements:
- Should get and set the name, cost and profit margin of the stock item
- Should represent the profit margin as a percent; a value of 45 means 45%
- Should calculate the price of the item, to the nearest cent
- Use the rounding where values under a half-cent are rounded down and values greater than or equal to a half-cent are rounded up
Use the following class diagram when creating your solution.
Die
This class represents a single six-sided die. This example is used to illustrate random number generation and casting.
Problem Statement
Write the code for the Die class. The solution must meet the following requirements:
- Should generate a random value from 1 to 6, when initially created and when re-rolled
- Should get the face value of the die
Use the following class diagram when creating your solution. Note that this uses the Random class as a private static field.
ParkingCounter
This class represents a simple counter to monitor whether a parking lot is full or not; it tracks vehicles entering and leaving the parking lot and allows the counter to be reset when the lot is full or empty. This class illustrates increment and decrement operators and/or the assignment increment or assignment decrement operators.
Problem Statement
Write the code that will monitor vehicles entering and leaving a parking lot. The solution must meet the following requirements:
- Should track vehicles entering
- Should track vehicles leaving
- Should get total parking spots
- Should get open (empty) spots
- Should reset lot as full (that is, fill the parking lot)
- Should reset lot as empty (that is, clear all the parking spots of vehicles)
Use the following class diagram when creating your solution.
QuadradicEquation
This class is used to solve for the two possible values of a quadratic formula where quadratic equals zero. It is based off of the following formula.
This sample illustrates order of operations and parentheses.
Problem Statement
Write the code that will represent a quadratic equation that has a higher and lower root. It is to use the Quadratic formula, which states:
For ax^2 + bx + c = 0
$, the value of x is given by
More information on the quadratic formula can be found at http://www.purplemath.com/modules/quadform.htm.
The solution must meet the following requirements:
- Should get the lower root, using the quadratic formula
x=(-b-√(b^2-4ac))/2a
$ - Should get the higher root, using the quadratic formula
x=(-b+√(b^2-4ac))/2a
$ - Should overload the ToString() method to represent the quadratic formula showing the values for a, b and c in the following format:
a____x2 + ____b____x + ____c = 0
For example, given the values of 1, 3 and -4 for a, b and c respectively, the method should produce
1x2 + 3x + -4 = 0
Use the accompanying class diagram when creating your solution.
Practice Exercises
- Calculator – This exercise expands on the original Calculator class to perform subtraction and division with integers.
- Fraction – This exercise expands on the original Fraction class to include methods that allow for adding, subtracting, multiplying, and dividing fractions.
- Square – This exercise expands on the original Square class to include a method that determines the diagonal of a square.
- Coordinate – Not Yet Implemented The Coordinate class represents a geographical co-ordinate for either longitude or latitude. A Coordinate’s value can be expressed either as a real number or as degrees, minutes and seconds. It makes use of integer division and overloaded constructors.
- PaintEstimator – The PaintEstimator class is used to estimate the number of paint cans needed for painting a simple room (with or without a window). It makes use of a constant and the Math library; it also uses overloaded constructors.
- BulkItem – The BulkItem class represents products that are available in bulk and shows the cost of individual items. It makes use of rounding (to two decimal places).
- Die - This exercise expands on the original Die class to allow for creating a die of any number of sides, with a default of six sides. This means that there is an overloaded constructor.
- RoundingCalculator – This exercise demonstrates rounding using separate methods for each level of rounding precision.
- GenericRoundingCalculator – This exercise demonstrates rounding to the nearest whole number of fractional number using a couple of “generic” methods.
- PeopleCounter – This simple class is used to count the number of people who have entered a store in a single day. It takes a simplistic approach, counting each person entering and leaving, estimating the number of people for the day by dividing the total count by two.
- ScoreCard – Not Yet Implemented This class represents a scorecard for tracking bowling scores frame by frame. It produces a final tally for the game, as well as the current score and the current frame.
- Cylinder – The Cylinder class represents a cylindrical object whose height and radius is known. The class provides methods to calculate the volume and surface area.
- Cone – The Cone class represents a conical object whose height and base radius is known. The class provides methods to determine the volume and surface area with this information.
- GravityCalculator – The GravityCalculator provides static methods for determining an object’s weight for the various planets in our solar system, given the equivalent weight as found on Earth.
- CurrencyCalculator – The CurrencyCalculator allows the conversion of US dollars to four other currencies, given the current exchange rate of those currencies.
For these exercises, compare your results with the unit tests for these classes.
Calculator
This exercise expands on the original Calculator class to perform subtraction and division with integers.
Problem Statement
Write the code that will act as a calculator for doing math. This version is a follow-up on the previous prototype; now it should support subtraction and division.
The solution must meet the following requirements:
- Should add two whole numbers.
- Should multiply two whole numbers.
- Should subtract two whole numbers.
- Should divide two whole numbers.
Use the following class diagram when creating your solution.
Fraction
This exercise expands on the original Fraction class to include methods that allow for adding, subtracting, multiplying, and dividing fractions.
Problem Statement
Write the code needed to expand the capabilities of the Fraction class. The class must now support the ability to add, subtract, multiply, and divide fractions as well as allow the individual numerator and denominator values to be seen. The solution must meet the following requirements:
- Should get the string representation of the fraction, as “numerator/denominator”
- Should get the numeric value of the fraction (as a real number)
- Should get the reciprocal of the fraction
- Should get the numerator and denominator
- Should add another fraction to its existing value
- Should subtract another fraction from its existing value
- Should multiply its existing value by another fraction
- Should divide its existing value by another fraction
As an assist, the following code can be used for the multiplication method.
Use the following class diagram when creating your solution.
Square
This exercise expands on the original Square class to include a method that determines the diagonal of a square.
Problem Statement
Write the code needed to add the ability for a Square to determine the length of its diagonal. The solution must meet the following requirements:
- Should get and set the length of the side of the square.
- Should calculate the area, perimeter, and diagonal of the square.
Use the following class diagram when creating your solution.
Coordinate
Not Yet Implemented The Coordinate class represents a geographical co-ordinate for either longitude or latitude. A Coordinate’s value can be expressed either as a real number or as degrees, minutes and seconds. It makes use of integer division and overloaded constructors.
Problem Statement
Write the code needed to represent a geographical co-ordinate for longitude and latitude. The solution must meet the following requirements:
- Should get the type of coordinate (such as “longitude” or “latitude”)
- Given the hours, minutes and seconds, it
- should calculate coordinate value as a real number
- should get hours, minutes, and seconds
- Given the coordinate value as a real number, it
- should calculate the hours, minutes and seconds
- should get the coordinate value as a real number
Use the following class diagram when creating your solution.
PaintEstimator
The PaintEstimator class is used to estimate the number of paint cans needed for painting a simple room (with or without a window). It makes use of a constant and the Math library; it also uses overloaded constructors.
Problem Statement
Write the code needed to help a painter to estimate the number of paint cans to paint simple rooms (with or without a window). The solution must meet the following requirements:
- Uses a constant value of 8.0 for the paint coverage. (8 square metres per can)
- Should get room height, width, and length
- Should get and set the window’s width and height
- Should calculate the number of paint cans for the room and the window
- Should calculate the surface area to be painted
- The room’s height, length and width should have a private set property.
Use the following class diagram when creating your solution.
BulkItem
The BulkItem class represents products that are available in bulk and shows the cost of individual items. It makes use of rounding (to two decimal places).
Problem Statement
Write the code for the BulkItem class. The solution must meet the following requirements:
- Should get and set the cost and quantity of the bulk item
- Should calculate the cost for each item in the bulk item
- Should properly round values for currency (either the US or Canadian dollar)
- The Quantity property’s set should be private.
Use the following class diagram when creating your solution.
Die
This exercise expands on the original Die class to allow for creating a die of any number of sides, with a default of six sides. This means that there is an overloaded constructor.
Problem Statement
Modify the Die class from the examples to support multi-sided die other than just the standard six sides. The solution must meet the following requirements (new requirements are in bold):
- The Sides and FaceValue properties’ set should be private.
- Should generate a six-sided die by default
- Should get the number of sides of the die
- Should randomly generate each side (if rolled enough); for example, if the die has ten sides, it should eventually roll a 1, 2, 3, 4, 5 6, 7, 8, 9, and 10
Use the following class diagram when creating your solution.
RoundingCalculator
This exercise demonstrates rounding using separate methods for each level of rounding precision.
Problem Statement
Write the code needed to provide rounding of various degrees of accuracy. The solution must meet the following requirements:
- All of the methods should be static.
- Should correctly round up to the nearest thousand
- Should correctly round down to the nearest thousand
- Should correctly round up to the nearest hundred
- Should correctly round down to the nearest hundred
- Should correctly round up to the nearest ten
- Should correctly round down to the nearest ten
- Should correctly round up to the nearest one
- Should correctly round down to the nearest one
- Should correctly round up to the nearest tenth
- Should correctly round down to the nearest tenth
- Should correctly round up to the nearest hundredth
- Should correctly round down to the nearest hundredth
Use the following class diagram when creating your solution.
GenericRoundingCalculator
This exercise demonstrates rounding to the nearest whole number of fractional number using a couple of “generic” methods.
Problem Statement
Write the code needed to perform rounding to either whole numbers or fractions at a specified level of precision. The solution must meet the following requirements:
- All of the methods should be static.
- Should correctly round up or down to the nearest whole number, such as
- the nearest thousand
- the nearest hundred
- the nearest ten
- the nearest one
- Should correctly round up or down to the nearest fractional value, such as
- the nearest tenth
- the nearest hundredth
Use the following class diagram when creating your solution.
PeopleCounter
This simple class is used to count the number of people who have entered a store in a single day. It takes a simplistic approach, counting each person entering and leaving, estimating the number of people for the day by dividing the total count by two.
Problem Statement
Write the code needed to track people entering and leaving a store. It must be able to estimate the number of people that have entered the store at the end of the day. The solution must meet the following requirements:
- The Count property should have a private set.
- Should default to having a count of zero people when first started up
- Should increment by one (that is, count a single person)
- Should increment by a specified quantity (for when a group of people enter)
- Should adequately estimate the number of people who entered the store, assuming that each person who enters also leaves the store
- Each estimate should be rounded down; for example, if 15 people are counted, then the estimate should be for only 7 (not 7.5 or 8)
Use the following class diagram when creating your solution.
ScoreCard
Not Yet Implemented This class represents a scorecard for tracking bowling scores frame by frame. It produces a final tally for the game, as well as the current score and the current frame.
Problem Statement
Write the code needed to.
The solution must meet the following requirements:
- Should
Use the following class diagram when creating your solution.
### Cylinder
The Cylinder class represents a cylindrical object whose height and radius is known. The class provides methods to calculate the volume and surface area.
Problem Statement
Write the code for the Cylinder class that meets the following requirements:
- The Radius and Height properties should have a private set.
- Should get the radius and the height
- Should calculate the volume and the surface area
-
Volume of a Cylinder = pi * r^2 * h
$ -
Surface Area of a Cylinder = 2 * pi * r^2 + 2 * pi * r * h
$
-
Use the following class diagram when creating your solution.
Cone
The Cone class represents a conical object whose height and base radius is known. The class provides methods to determine the volume and surface area with this information.
Problem Statement
Write the code for the Cone class that meets the following requirements:
- The Radius and Height properties should have a private set.
- Should get the radius and the height
- Should calculate the volume and the surface area
-
Volume of a Cone = 1/3 * pi * r^2 * h
$ -
Surface Area of a Cone = pi * r^2 + pi * r * sqrt(r^2 + h^2)
$
-
Note that the portion sqrt(r^2 + h^2)
$ is known as the slant height.
Use the following class diagram when creating your solution.
GravityCalculator
The GravityCalculator provides static methods for determining an object’s weight for the various planets in our solar system, given the equivalent weight as found on Earth.
Problem Statement
Write the code needed to convert Earth weights to their equivalent for the other planets in our solar system. The solution must meet the following requirements:
- All the methods should be static.
- Should convert a weight in Earth kilograms to their equivalent weight on
- Mercury
- Venus
- Mars
- Jupiter
- Saturn
- Uranus
- Neptune
- For information on equivalent weights among the planets, see these URLS.
- NinePlanets.org
- http://www.serve.com/chunter/index/info/aweigh.html
Use the following class diagram when creating your solution.
CurrencyCalculator
The CurrencyCalculator allows the conversion of US dollars to four other currencies, given the current exchange rate of those currencies.
Problem Statement
A currency exchange store at the international airport needs a program to convert from US dollars to four other currencies: Canadian dollar, Euro, Japanese Yen, and the Great Britain Pound. The store uses a set exchange rate for each currency as established at the start of the day. Write the code for a class called CurrencyCalculator to meet this need. The solution must meet the following requirements:
- Should correctly convert US dollars to the
- British Pound (GBP)
- Canadian Dollar (CAD)
- Euro (EUR)
- Japanese Yen (JPY)
- Should use the correct level of precision when making the exchange; each currency uses a different number of significant digits:
- CAD, GBP and EUR use two digits
- JPY uses three digits
To illustrate the possible exchange rates, please refer to the following images.
Use the following class diagram when creating your solution.
As a starter, you can use the following code to begin creating your class.
Topic F – If-Else Structures
Overview
This topic introduces the if-else control flow statement and demonstrates both simple and complex conditional expressions. This topic will also introduce how to nest program statements.
LOGs
General Programming Concepts and Terms
- Describe and draw a diagram of the If-Then-Else logical structure
- Identify the programming statement that corresponds to the If-Then-Else logical structure
- Translate If-Then-Else structures into code
- Describe what is meant by a “conditional expression”
- List the operator precedence for mixing logical, arithmetic, and relational operations
- List the relational operators
- List the logical operators
- Use relational, logical, and arithmetic operators to construct conditional expressions
- Demonstrate an understanding of operator precedence in conditional expressions
- Use statement blocks to allow nesting program statements into control structures
- Define the term “boundary condition” as it relates to if-else statements and testing
- Identify the correct way to compare or check the contents of strings in conditional expressions
- List and describe the commonly used properties and methods of the String class that would be used in if-else statements
Code Samples
The following examples are used to illustrate this topic.
- StockItem - This class represents an item that is part of an inventory. The item has an item name, a cost and a profit margin (which can be positive or negative). By using the profit margin, it can derive the price of the item. The class can also report if the item is priced at or below cost.
- Account - This class illustrates simple if structure in handling withdrawals; withdrawals are only made when the amount does not exceed the balance and the overdraft. It also identifies if the account is overdrawn.
- Person - This adaptation of the person class checks the age of the person to see if the person’s life stage is infant, toddler, preschooler, school age, or adult.
- Fraction - This class now ensures that any negative denominators have their negative sign “moved” to the numerator. It also recognizes whether a fraction is proper (numerator less than denominator) or not and provides a method to express the fraction as a mixed number string.
- Angle - This version of the Angle class includes an attribute to identify the type of the angle as either acute, right, obtuse, straight, reflex, full rotation, or undefined.
- ParkingCounter - This class represents a simple counter to monitor whether a parking lot is full or not; it tracks vehicles entering and leaving the parking lot and allows the counter to be reset when the lot is full or empty. This class illustrates increment and decrement operators and/or the assignment increment or assignment decrement operators.
- MemoryAddress - This class represents a single memory address in both its base 10 and hexadecimal value.
- Color - This class represents a color as three base-10 RGB values and as a single hexadecimal value.
StockItem
This class represents an item that is part of an inventory. The item has an item name, a cost and a profit margin (which can be positive or negative). By using the profit margin, it can derive the price of the item. The class can also report if the item is priced at or below cost.
Problem Statement
Write the code for the StockItem class. The solution must meet the following requirements (new requirements are in bold):
- Should get and set the name, cost and profit margin of the stock item
- Should represent the profit margin as a percent; a value of 45 means 45%
- Should calculate the price of the item, to the nearest cent
- Use the rounding where values under a half-cent are rounded down and values greater than or equal to a half-cent are rounded up
- Should recognize when the stock item is priced at cost (that is, the profit margin is zero)
- Should recognize when the stock item is priced below cost (that is, the profit margin is negative)
Use the following class diagram when creating your solution.
Account
This class illustrates simple if structure in handling withdrawals; withdrawals are only made when the amount does not exceed the balance and the overdraft. It also identifies if the account is overdrawn.
Problem Statement
Write the code that will represent a simple bank account. The solution must meet the following requirements (new requirements are in bold):
- Should get the bank name, branch number, institution number, account number, balance, overdraft limit, and account type and allow the overdraft limit to be set
- Should support deposits
- Should only support withdrawals if the amount does not exceed the sum of the balance and the overdraft limit
- Should identify if the account is overdrawn
Use the following class diagram when creating your solution.
Person
This adaptation of the person class checks the age of the person to see if the person’s life stage is infant, toddler, preschooler, school age, or adult.
Problem Statement
Write the code that will represent a person with a first and last name and a date of birth. The solution must meet the following requirements (new requirements are in bold):
- Should get and set the first and last name
- Should get the birth date
- Should get the person’s approximate age (which is the age that the person will turn to in the current year)
- Should override ToString() to get the person’s full name (as first name then last name)
- Should get the life stage, based on the following table
Age Range (Years) | Life Stage |
---|---|
0 | Infant |
< 3 | Toddler |
< 5 | Preschooler |
< 18 | School age |
>= 18 | Adult |
Use the following class diagram when creating your solution.
Fraction
This class now ensures that any negative denominators have their negative sign “moved” to the numerator. It also recognizes whether a fraction is proper (numerator less than denominator) or not and provides a method to express the fraction as a mixed number string.
Problem Statement
Write the code for the Fraction class. The solution must meet the following requirements (new requirements are in bold):
- Should get the string representation of the fraction, as “numerator/denominator”
- Should get the numeric value of the fraction (as a real number)
- Should get the reciprocal of the fraction
- Should get the numerator and denominator
- Should add another fraction to its existing value
- Should subtract another fraction from its existing value
- Should multiply its existing value by another fraction
- Should divide its existing value by another fraction
- Should affix the sign for negative fractions onto the numerator only
- Should identify if the fraction is a proper fraction
- Should express the fraction as a mixed number string
Use the following class diagram when creating your solution.
Angle
This version of the Angle class includes an attribute to identify the type of the angle as either acute, right, obtuse, straight, reflex, full rotation, or undefined.
Problem Statement
Write the code for the Angle class. The solution must meet the following requirements (new requirements are in bold):
- Should get and set the angle’s value (in degrees)
- Should calculate the equivalent angle in Radians and Grads, using the following formulas:
- Radians = Degrees * (π / 180)
- Grads = Radians * (200 / π)
- Should override the toString() method to return the angle in degrees, in the following format:
- degrees°
- The Unicode character for the degrees symbol (°) is ‘\u00B0’
- Should get the type of angle, based on the following table
Angle Range | Angle Type |
---|---|
< = 0 or > 360 | Undefined |
> 0 and < 90 | Acute |
= 90 | Right |
> 90 and < 180 | Obtuse |
= 180 | Straight |
> 180 and < 360 | Reflex |
= 360 | Full Rotation |
Use the following class diagram when creating your solution.
ParkingCounter
This class represents a simple counter to monitor whether a parking lot is full or not; it tracks vehicles entering and leaving the parking lot and allows the counter to be reset when the lot is full or empty. This class illustrates increment and decrement operators and/or the assignment increment or assignment decrement operators.
Problem Statement
Write the code that will monitor vehicles entering and leaving a parking lot. The solution must meet the following requirements (new requirements are in bold):
- Should track vehicles entering
- Should track vehicles leaving
-
Should track the peak occupancy of the parking lot
- The peak occupancy represents the highest number of cars in the parking lot at any one time
- Should get total parking spots
- Should get open (empty) spots
- Should reset lot as full (that is, fill the parking lot)
- Should reset lot as empty (that is, clear all the parking spots of vehicles)
Use the following class diagram when creating your solution.
MemoryAddress
This class represents a single memory address in both its base 10 and hexadecimal value.
Base ten is the common number system that we use in every day life. Base ten uses the digits 0-9 and the concept of the position of a digit occupying some multiple of ten. Thus, for the number 129 there is a hundreds-position (102), a tens-position (101) and a ones-position (10^0).
Converting a value from one base to another (such as base-10 to base-16) involves thinking about the digit positions of the target base. Base 16 uses the digits 0-9 along with the letters A through F for the range of hex values zero to fifteen. Each digit position in a base-16 number can hold a value of 0 to F. Thus, a digit in the ones position is worth 1 times the digit. A two-digit hex value would have the sixteens-position (161) and a ones-position (160). A three-digit hex value would add onto that a two-hundred-and-fifty-sixth-position (16^2). For example, to convert the number 679 base 10 to a base 16, you would follow these steps.
- Divide the original number by the two-hundred-and-fifty-sixth-position (162). Then use the remainder in calculating the next position (161).
\include{longdiv} \longdiv{679}{256}
$ - Dividing the previous steps remainder (167) by 16 gives the result of 10, which is the hex-digit of
A
.\include{longdiv} \longdiv{167}{16}
$ - The remainder of that last step is the ones-position
- Thus, the base-10 value 679 is
2B9
in base-16.
The following class demonstrates a small memory address (up to four hexadecimal digits) as a short
and a string representation of hexadecimal.
Color
This class represents a color as three base-10 RGB values and as a single hexadecimal value.
Problem Statement
Create a data type to represent a color as both base-10 RBG values and as a hexadecimal value.
## Practice Excercises
- Rectangle – “All squares are rectangles, but not all rectangles are squares.” This class represents a simple rectangle with a height and width. From this information, the area, perimeter and diagonal can be obtained; it can also be determined if the rectangle is or is not square.
- HazardousMaterial – The HazardousMaterial class is a simple representation of the six main classes of hazardous materials (A through F). This class maps a classification code with a general description of the material’s classification:
- Class A – Compressed Gas
- Class B – Flammable and Combustible Material
- Class C – Oxidizing Material
- Class D – Poisonous and Infectious Material
- Class E – Corrosive Material
- Class F – Dangerously Reactive Material
- CurrencyCalculator – This exercise extends the previous CurrencyCalculator exercise by allowing the conversion from a foreign currency to US dollars.
- GravityCalculator – This exercise extends the previous GravityCalculator exercise by allowing the conversion to a weight on Earth from a weight on another planet in our solar system.
Rectangle
“All squares are rectangles, but not all rectangles are squares.” This class represents a simple rectangle with a height and width. From this information, the area, perimeter and diagonal can be obtained; it can also be determined if the rectangle is or is not square.
Problem Statement
Write the code for the Rectangle class. The solution must meet the following requirements:
- Should get and set the height and the width
- Should calculate the area, the perimeter and the diagonal
- The formula for the diagonal is √(〖width〗2 + 〖height〗2 )
- Should determine if the rectangle is a square
Use the following class diagram when creating your solution.
HazardousMaterial
The HazardousMaterial class is a simple representation of the six main classes of hazardous materials (A through F). This class maps a classification code with a general description of the material’s classification:
- Class A – Compressed Gas
- Class B – Flammable and Combustible Material
- Class C – Oxidizing Material
- Class D – Poisonous and Infectious Material
- Class E – Corrosive Material
- Class F – Dangerously Reactive Material
Problem Statement
Write the code for the HazardousMaterial class. The solution must meet the following requirements:
- Should return the class code as the classification
- Should get the description for the class, based on the following table
Class Code | Description |
---|---|
A | Compressed Gas |
B | Flammable and Combustible Material |
C | Oxidizing Material |
D | Poisonous and Infectious Material |
E | Corrosive Material |
F | Dangerously Reactive Material |
- Should override the ToString() method to get the full description and class code in the following format:
- “Class ClassCode - Description”
Use the following class diagram when creating your solution.
CurrencyCalculator
This exercise extends the previous CurrencyCalculator exercise by allowing the conversion from a foreign currency to US dollars.
Problem Statement
A currency exchange store at the international airport needs a program to convert from US dollars to four other currencies: Canadian dollar, Euro, Japanese Yen, and the Great Britain Pound. The store uses a set exchange rate for each currency as established at the start of the day. Write the code for a class called CurrencyCalculator to meet this need. The solution must meet the following requirements (new requirements are in bold):
- Should correctly convert US dollars to the
- British Pound (GBP)
- Canadian Dollar (CAD)
- Euro (EUR)
- Japanese Yen (JPY)
- Should convert an amount to US dollars from any of the supported currencies (GBP, CAD, JPY, and EUR)
- Should use the correct level of precision when making the exchange; each currency uses a different number of significant digits:
- CAD, GBP and EUR use two digits
- JPY uses three digits
To illustrate the possible exchange rates, please refer to the following images.
Use the following class diagram when creating your solution.
GravityCalculator
This exercise extends the previous GravityCalculator exercise by allowing the conversion to a weight on Earth from a weight on another planet in our solar system.
Problem Statement
Write the code needed to convert Earth weights to their equivalent for the other planets in our solar system. The solution must meet the following requirements (new requirements are in bold):
- Should convert a weight in Earth kilograms to their equivalent weight on
- Mercury
- Venus
- Mars
- Jupiter
- Saturn
- Uranus
- Neptune
- Should convert a weight from a specific planet back to the equivalent weight on Earth
Use the following class diagram when creating your solution.
Topic G – Raising Exceptions
Overview
This topic introduces the throw statement and exceptions. The following new keywords are introduced.
* throw
* throws
This topic will demonstrate objects that are capable of validating information supplied to them. This introduces the principle in object oriented design that well designed objects are capable of ensuring the validity of their internal state. When invalid information is supplied, either through the constructor or through other methods, objects can respond by raising exceptions.
LOGs
General Programming Concepts and Terms
- Describe how object-oriented programs run
- Describe what is meant by a “call stack”
- Define the term “Exception”
- Describe what is meant by the phrase “raising (or throwing) an exception”
- Describe the role that exceptions play in a computer program
- Identify the three most commonly used Exception types used in this course
OOP Basics
- Explain why methods (including constructors) should perform validation of their parameter values
- Explain the significance of raising an exception in the constructor
- Use exceptions as part of the validation of parameter values in methods and constructors
- Explain why property setters are a good place for throwing exceptions
- Identify when constructors should throw exceptions directly
Code Samples
- Fraction – The fraction class avoid the division by zero error by ensuring that the supplied denominator is not zero.
- Square – Only accepts positive, non-zero lengths for the side.
- Circle – Only accepts positive, non-zero diameters.
- Die – Only accepts from 4 to 20 sides for a die.
- Person – First and last names cannot be empty and the birth date cannot be in the future. This illustrates putting the validation on the setters and calling the setters from the constructor (to reduce the duplication of code).
- Student – Gender must be ‘M’ or ‘F’ (and will be converted to upper-case). The student name and program cannot be empty. The student ID must be 9 digits. The GPA must be between 0.0 and 9.0 (inclusive).
- ParkingCounter – Only accepts positive, non-zero counts for available parking spaces and number of cars. Also, the number of cars must not exceed the number of parking spaces. The rules for the number of cars must also apply for cars entering and leaving the parking lot.
- StockItem – Represents an inventory item that is kept in stock. The item’s description, cost and profit margin are all part of the class design. Empty descriptions and zero or negative costs, as well as profit margins less than -100, are not allowed.
- Account – The following account information is now verified when the class is created:
- Bank name and account type cannot be empty
- The opening balance must be greater than zero
- The overdraft limit cannot be negative
- The institution number must be 3 digits
- The branch number must be 6 digits
- Attempts to withdraw amounts beyond the overdraft limit should throw an “Insufficient Funds” exception
Fraction
The fraction class avoids the division by zero error by ensuring that the supplied denominator is not zero.
Problem Statement
Write the code for the Fraction class. The solution must meet the following requirements (new requirements are in bold):
* Should get the string representation of the fraction, as “numerator/denominator”
* Should get the numeric value of the fraction (as a real number)
* Should get the reciprocal of the fraction
* Should get the numerator and denominator
* Should add another fraction to its existing value
* Should subtract another fraction from its existing value
* Should multiply its existing value by another fraction
* Should divide its existing value by another fraction
* Should affix the sign for negative fractions onto the numerator only
* Should identify if the fraction is a proper fraction
* Should reject zero denominators
Use the following class diagram when creating your solution.
Square
Only accepts positive, non-zero lengths for the side.
Problem Statement
Write the code needed to add the ability for a Square to determine the length of its diagonal. The solution must meet the following requirements (new requirements are in bold):
* Should get and set the length of the side of the square
* Should calculate the area, perimeter, and diagonal of the square
* Should only accept positive, non-zero lengths for the side
Use the following class diagram when creating your solution.
Circle
Only accepts positive, non-zero diameters.
Problem Statement
Write the code for the Circle class. The solution must meet the following requirements (new requirements are in bold):
- Should get and set the diameter
- Should calculate the area, radius, and circumference
- Should only accept positive, non-zero lengths for the diameter
Use the following class diagram when creating your solution.
Die
Only accepts from 4 to 20 sides for a die. This class represents a single six-sided die. This example is used to illustrate random number generation and casting.
Problem Statement
Write the code for the Die class. The solution must meet the following requirements (new requirements are in bold):
- Should generate a random value from 1 to the number of sides on the die, when initially created and when re-rolled
- Should get the face value of the die
- Should get the number of sides of the die
- Should randomly generate each side (if rolled enough); for example, if the die has ten sides, it should eventually roll a 1, 2, 3, 4, 5 6, 7, 8, 9, and 10
- Should only accept 4 to 20 sides for the die
Use the following class diagram when creating your solution.
Person
First and last names cannot be empty and the birth date cannot be in the future. This illustrates putting the validation on the setters and calling the setters from the constructor (to reduce the duplication of code).
This adaptation of the person class includes a check of the age of the person to see if the person’s life stage is infant, toddler, preschooler, school age, or adult.
Problem Statement
Write the code that will represent a person with a first and last name and a date of birth. The solution must meet the following requirements (new requirements are in bold):
- Should get and set the first and last name
- Should get the birth date
- Should get the person’s approximate age (which is the age that the person will turn to in the current year)
- Should override toString() to get the person’s full name (as first name then last name)
- Should get the life stage, based on the following table
Age Range (Years) | Life Stage |
---|---|
0 | Infant |
< 3 | Toddler |
< 5 | Preschooler |
< 18 | School age |
>= 18 | Adult |
- Should ensure the first and last names are not empty (or null)
- Should trim leading and trailing spaces from the first and last names
- Should reject birthdates that are in the future
Use the following class diagram when creating your solution.
Student
Gender must be ‘M’ or ‘F’ (and will be converted to upper-case). The student name and program cannot be empty. The student ID must be 9 digits. The GPA must be between 0.0 and 9.0 (inclusive).
Problem Statement
Write the code for the Student class. The class must now ensure that the supplied information is valid. The solution must meet the following requirements (new requirements are in bold):
- Should get and set the student’s name, gender, GPA, program of studies, and whether or not the student is full-time.
- Should override the toString() method to get the student’s ID and name in this format:
(ID) Name - Should n * longer allow the student ID to be set (it’s only set through the constructor)
- Should reject empty text (and null values) for the student’s name and program of studies.
- Should trim the student’s name and the program name
- Should only accept ‘M’ and ‘F’ as valid genders
- Should set the gender to upper-case
- Should reject negative GPAs and GPAs over 9
- Should require a nine-digit student ID
This class reinforces the idea of encapsulation and constructors. It also demonstrates the idea of overloading the default ToString()
method that every class inherits from the Object class.
ParkingCounter
Only accepts positive, non-zero counts for available parking spaces and number of cars. Also, the number of cars must not exceed the number of parking spaces. The rules for the number of cars must also apply for cars entering and leaving the parking lot.
Problem Statement
Write the code that will monitor vehicles entering and leaving a parking lot. The solution must meet the following requirements (new requirements are in bold):
- Should track vehicles entering
- Should track vehicles leaving
- Should track the peak occupancy of the parking lot
- The peak occupancy represents the highest number of cars in the parking lot at any one time
- Should get total parking spots
- Should get open (empty) spots
- Should reset lot as full (that is, fill the parking lot)
- Should reset lot as empty (that is, clear all the parking spots of vehicles)
- Should only allow a positive (non-zero) number of parking spots
- Should not allow a negative number of cars (when using the overloaded constructor), and should not allow more cars than parking spots
-
Should not allow available (open) parking spots to go negative or to exceed the actual number of parking spots
- Should raise an error when trying to enter a full parking lot
- Should raise an error if trying to leave a parking lot that is already empty
Use the following class diagram when creating your solution.
StockItem
The StockItem class represents an inventory item that is kept in stock. The item’s description, cost and profit margin are all part of the class design. Empty descriptions and zero or negative costs, as well as profit margins less than -100, are not allowed.
Problem Statement
Write the code for adding validation to the StockItem class. The solution must meet the following requirements (new requirements are in bold):
- Should get and set the name, cost and profit margin of the stock item
- Should represent the profit margin as a percent; a value of 45 means 45%
- Should calculate the price of the item, to the nearest cent
- Use the rounding where values under a half-cent are rounded down and values greater than or equal to a half-cent are rounded up
- Should recognize when the stock item is priced at cost (that is, the profit margin is zero)
- Should recognize when the stock item is priced below cost (that is, the profit margin is negative)
- Should reject an empty (or null) item name
- Should trim excess spaces from the ends of the item name
- Should require cost to be greater than zero
- Should only allow negative profit margins up to 100% (which is a full mark-down)
Use the following class diagram when creating your solution.
Account
The following account information is now verified when the class is created:
- Bank name and account type cannot be empty
- The opening balance must be greater than zero
- The overdraft limit cannot be negative
- The institution number must be 3 digits
- The branch number must be 6 digits
- Attempts to withdraw amounts beyond the overdraft limit should throw an “Insufficient Funds” exception
Problem Statement
Write the code that will add validation to the Account class. The solution must meet the following requirements (new requirements are in bold):
* Should get the bank name, branch number, institution number, account number, balance, overdraft limit, and account type and allow the overdraft limit to be set
* Should support deposits
* Should only support withdrawals if the amount does not exceed the sum of the balance and the overdraft limit, otherwise an exception stating “Insufficient Funds” should occur
* Should identify if the account is overdrawn
* Should require bank name and account type (that is, they cannot be empty or null)
* Should trim the bank name and account type
* Should verify that the branch number is six digits and the institution number is three digits
* Should require an opening balance
* Should not allow a negative overdraft limit
Use the following class diagram when creating your solution.
Practice Exercises
- Account – Add one more point of validation to the Account class: Do not allow depositing or withdrawing non-positive amounts.
- Rectangle – The height and width of the rectangle must be greater than zero.
- Cone – The radius and height of the cone must be greater than zero.
- Course – The course name and number cannot be blank, and the exam and lab counts must be greater than zero. The class hours must also be greater than zero.
- Cylinder – The radius and height of the cylinder must be greater than zero.
- HazardousMaterial – The class code for the hazardous material can only be the letters ‘A’ through ‘F’, inclusive.
- ExamResult - Requires a positive, non-zero value for the total marks and weight. The weight cannot be over 50. The marks earned can be between zero and the total marks, inclusive. The student ID must be 9 digits, and the exam name cannot be an empty string.
- LabResult – Requires a positive, non-zero value for the lab number, total marks and weight. The weight cannot be over 50. The marks earned can be between zero and the total marks, inclusive. The student ID must be 9 digits.
- PeopleCounter – Does not allow adding a negative number of people to the counter.
- BulkItem – The description cannot be blank and the cost and quantity values must be greater than zero.
Account
Problem Statement
Write the code that will add another piece of validation to the Account class. The solution must meet the following requirements (new requirements are in bold):
- Should get the bank name, branch number, institution number, account number, balance, overdraft limit, and account type and allow the overdraft limit to be set
- Should support deposits
- Should only support withdrawals if the amount does not exceed the sum of the balance and the overdraft limit, otherwise an exception stating “Insufficient Funds” should occur
- Should identify if the account is overdrawn
- Should require bank name and account type (that is, they cannot be empty or null)
- Should trim the bank name and account type
- Should verify that the branch number is six digits and the institution number is three digits
- Should require an opening balance
- Should not allow a negative overdraft limit
- Should only allow positive, non-zero amounts when performing a deposit or withdrawal
Use the following class diagram when creating your solution.
Rectangle
The height and width of the rectangle must be greater than zero.
Problem Statement
Write the code to provide validation for the Rectangle class. The solution must meet the following requirements (new requirements are in bold):
- Should get and set the height and the width
- Should calculate the area, the perimeter and the diagonal
- The formula for the diagonal is √(〖width〗2 + 〖height〗2 )
- Should determine if the rectangle is a square
- Should require the height and width to be greater than zero
Use the following class diagram when creating your solution.
Cone
The radius and height of the cone must be greater than zero.
Problem Statement
Write the code to provide validation for the Cone class that meets the following requirements (new requirements are in bold):
- Should get the radius and the height
- Should calculate the volume and the surface area
- Should require the radius and height to be greater than zero
Use the following class diagram when creating your solution.
Course
The course name and number cannot be blank, and the exam and lab counts must be greater than zero. The class hours must also be greater than zero.
Problem Statement
Write the code to provide validation for the Course class. The solution must meet the following requirements (new requirements are in bold):
- Should get the course name and number, the number of exams and labs, and the class hours for the course.
- Should not allow empty course names or numbers
- Should trim spaces from the course name and number
- Should require class hours as well as lab and exam counts to be greater than zero
Cylinder
The radius and height of the cylinder must be greater than zero.
Problem Statement
Write the code to provide validation for the Cylinder class that meets the following requirements (new requirements are in bold):
- Should get the radius and the height
- Should calculate the volume and the surface area
- Should make sure the radius and height are greater than zero
Use the following class diagram when creating your solution.
HazardousMaterial
The class code for the hazardous material can only be the letters ‘A’ through ‘F’, inclusive.
Problem Statement
Write the code to provide validation for the HazardousMaterial class. The solution must meet the following requirements (new requirements are in bold):
- Should return the class code as the classification
- Should make sure only class codes ‘A’ through ‘F’ are allowed (in either upper or lower case)
- Should make sure the classification is stored in upper case
- Should get the description for the class, based on the following table
Class Code | Description |
---|---|
A | Compressed Gas |
B | Flammable and Combustible Material |
C | Oxidizing Material |
D | Poisonous and Infectious Material |
E | Corrosive Material |
F | Dangerously Reactive Material |
- Should override the toString() method to get the full description and class code in the following format:
- “Class ClassCode - Description”
Use the following class diagram when creating your solution.
ExamResult
Requires a positive, non-zero value for the total marks and weight. The weight cannot be over 50. The marks earned can be between zero and the total marks, inclusive. The student ID must be 9 digits, and the exam name cannot be an empty string.
Problem Statement
Write the code to provide validation for the ExamResult class. The solution must meet the following requirements (new requirements are in bold):
- Should get the name, student Id, total marks, and exam weight
- Should get and set the marks earned
- Should override the toString() to show “The student (studentId) received earnedMarks/totalMarks for this examName exam.”
- Should require the total marks and weight to be a positive, non-zero value
- Should not allow a weight over 50
- The marks earned must be between zero and the total possible marks, inclusive
- The student ID must be nine digits
- The exam name cannot be an empty string (and must be trimmed)
LabResult
Requires a positive, non-zero value for the lab number, total marks and weight. The weight cannot be over 50. The marks earned can be between zero and the total marks, inclusive. The student ID must be 9 digits.
Problem Statement
Write the code to provide validation for the LabResult class. The solution must meet the following requirements (new requirements are in bold):
- Should get the lab number, student Id, total marks, and lab weight
- Should get and set the marks earned
- Should override the toString() to show “The student (studentId) received earnedMarks/totalMarks for this lab.”
- Should require the total marks and weight to be a positive, non-zero value
- Should not allow a weight over 50
- The marks earned must be between zero and the total possible marks, inclusive
- The student ID must be nine digits
- The lab number must be a positive number
### PeopleCounter
Does not allow adding a negative number of people to the counter.
Problem Statement
Write the code needed to track people entering and leaving a store. It must be able to estimate the number of people that have entered the store at the end of the day. The solution must meet the following requirements (new requirements are in bold):
- Should default to having a count of zero people when first started up
- Should increment by one (that is, count a single person)
- Should increment by a specified quantity (for when a group of people enter)
- Should adequately estimate the number of people who entered the store, assuming that each person who enters also leaves the store
Each estimate should be rounded down; for example, if 15 people are counted, then the estimate should be for only 7 (not 7.5 or 8) - Should not allow a zero or negative quantity when adding to the counter
Use the following class diagram when creating your solution.
BulkItem
The description cannot be blank and the cost and quantity values must be greater than zero.
Problem Statement
Write the code for the BulkItem class. The solution must meet the following requirements (new requirements are in bold):
- Should get and set the cost and quantity of the bulk item
- Should get and set the description
- Should ensure the description is not empty (and that it is trimmed)
- Should ensure the cost and quantity values are greater than zero
- Should calculate the cost for each item in the bulk item
- Should properly round values for currency (either the US or Canadian dollar)
Use the following class diagram when creating your solution.
Topic H – Case Structures
Overview
This topic introduces the C# switch statement which is used to code case structures. The following new keywords are introduced.
* switch
* case
* default
* break
LOGs
General Programming Concepts and Terms
- Describe the syntax of the switch statement
- Discuss the limitations of the switch statement in C# and the available alternatives for the times that we can’t use a switch statement
Code Samples
- LetterGrade - Represents a letter grade as assigned in Quebec universities. Source: http://en.wikipedia.org/wiki/Letter_grade
- Rating – Represents a rating on a scale of 1 to 5, where 1 is “very bad”, 2 is “bad”, 3 is “average”, 4 is “good”, and 5 is “very good”.
LetterGrade
Problem Statement
Write the code for the LetterGrade class that represents a letter grade as assigned in Quebec universities (Source: http://en.wikipedia.org/wiki/Letter_grade). The solution must meet the following requirements (note – you do not need to perform validation for this sample):
- Should get and set the grade (as a single letter)
- Should ensure the grade is in upper-case
- Should get the appropriate descriptions for the grade, based on the following table:
Grade | Description |
---|---|
A | A - 80-100% - Greatly Above Standards |
B | B - 70-79% - Above Standards |
C | C - 60-69% - At Government Standards |
D | D - 50-60% - Lower Standards |
F | F - 0-49% - Failure |
Use the following class diagram when creating your solution.
Rating
Problem Statement
Write the code for the Rating class that represents a rating on a survey where the rating is done using the values 1 through 5, inclusive. The solution must meet the following requirements (note – you do not need to perform validation for this sample):
- Should get and set the rating value
- Should get the appropriate string result when using the ToString() method, based on the following table:
Value | Description |
---|---|
1 | very bad |
2 | bad |
3 | average |
4 | good |
5 | very good |
Use the following class diagram when creating your solution.
Practice Exercises
- BahamaLetterGrade - Represents a letter grade as assigned in the Bahamas universities. Source: http://en.wikipedia.org/wiki/Letter_grade
- Feedback – Represents an opinion rating as feedback, where 1 is “no opinion”, 2 is “strongly disagree”, 3 is “disagree”, 4 is “agree, and 5 is “strongly agree”.
- HazardousMaterial – Represents the six hazardous material codes of ‘A’ through ‘F’.
BahamaLetterGrade
Problem Statement
Write the code for the BahamaLetterGrade class that represents a letter grade as assigned in the Bahamas universities (Source: http://en.wikipedia.org/wiki/Letter_grade). Each letter grade is also associated with a grade value. The solution must meet the following requirements (note – you do not need to perform validation for this exercise):
- Should get and set the grade (as a single letter)
- Should ensure the grade is in upper-case
- Should get the appropriate descriptions for the grade, based on the following table:
Grade | Grade Value | Description |
---|---|---|
A | 4 | A-4 - 90-100% |
B | 3 | B-3 - 71-89% |
C | 2 | C-2 - 56-70% |
D | 1 | D-1 - 46-55% |
F | 0 | F-0 - 0-45% |
Use the following class diagram when creating your solution.
Feedback
Represents an opinion rating as feedback, where 1 is “no opinion”, 2 is “strongly disagree”, 3 is “disagree”, 4 is “agree, and 5 is “strongly agree”.
Problem Statement
Write the code for the Feedback class that represents a rating on a survey where the feedback is given using the values 1 through 5, inclusive. The solution must meet the following requirements (note – you do not need to perform validation for this sample):
- Should get and set the feedback value
- Should get the appropriate string result when using the ToString() method, based on the following table:
Value | Description |
---|---|
1 | no opinion |
2 | strongly disagree |
3 | disagree |
4 | agree |
5 | strongly agree |
Use the following class diagram when creating your solution.
HazardousMaterial
The class code for the hazardous material can only be the letters ‘A’ through ‘F’, inclusive.
Problem Statement
Rewrite the code for the HazardousMaterial class to make use of the switch statement. The solution must meet the following requirements (new requirements are in bold):
- Should return the class code as the classification
- Should make sure only class codes ‘A’ through ‘F’ are allowed (in either upper or lower case)
- Should make sure the classification is stored in upper case
- Should get the description for the class, based on the following table
You must use a switch statement to get the results
Class Code | Description |
---|---|
A | Compressed Gas |
B | Flammable and Combustible Material |
C | Oxidizing Material |
D | Poisonous and Infectious Material |
E | Corrosive Material |
F | Dangerously Reactive Material |
- Should override the ToString() method to get the full description and class code in the following format:
- “Class ClassCode - Description”
Use the following class diagram when creating your solution.
Adding Complexity + Looping
Topic I –Enumerations and Composition
Overview
This topic introduces enumerations and composition. The following new keywords are introduced.
- enum
This topic will reveal how enumerations allow programmers to define new data types and values to represent conceptual information. This topic also discusses the concept of composition, describing how composition is simply the use of objects as fields in a class.
### LOGs
General Programming Concepts and Terms
- Define the term Enumeration
- List the benefits of using enumerations
- Describe where and when enumerations are used
- Compare and contrast enumerations and classes
- Use enumerations to define simple data types that are conceptual in nature and that have a limited set of possible “values”
OOP Basics
- Describe the idea of Composition
- List the benefits of Composition
Code Samples
Enumerations:
- Coin + CoinFace – The CoinFace enumeration helps to describe the tw * sides of a Coin, which can then be used in a tossing game.
- Account + AccountType – The Account’s account type is n * longer being represented as a string, but is its own enumeration: AccountType.
- LetterGrade + QuebecLetterGrade – The QuebecLetterGrade uses the simple LetterGrade enumeration and assigns specific ranges of percentage marks for the possible LetterGrade values.
Composition:
- Address + Student + GenderType – This revised version of the Student class now has an Address field. The address field is new; although similar to the CanadianAddress class, the Address class is simpler and more “generic” (having “State” instead of “Province” and “ZipCode” instead of “PostalCode”).
- ImproperFraction + MixedNumber + ProperFraction – In this sample, the idea of a Fraction class is made more specific by replacing it with three more specific types of numbers: MixedNumber, ProperFraction, and ImproperFraction. A MixedNumber is made up of a whole number and a ProperFraction. A MixedNumber can also be expressed as or derived from an ImproperFraction. The reciprocal of a ProperFraction is an ImproperFraction and the reciprocal of an ImproperFraction is a ProperFraction.
Coin + CoinFace
The CoinFace enumeration helps to describe the tw * sides of a Coin, which can then be used in a tossing game.
Problem Statement
Write the code needed to represent a coin that could be used in a coin-toss game. The solution must meet the following requirements.
- Should randomly generate the coin face that is showing when creating the coin
- Should get the side of the coin that is face showing
- Should allow the coin to be tossed to randomly generate a new coin face
- Should only support tw * sides for a coin’s face: Heads and Tails
Use the following diagram when creating your solution.
Account + AccountType
The Account’s account type is n * longer being represented as a string, but is its own enumeration: AccountType.
Problem Statement
Write the code that will make the account type a type-safe value for the Account class. The solution must meet the following requirements (new requirements are in bold):
- Should get the bank name, branch number, institution number, account number, balance, overdraft limit, and account type and allow the overdraft limit to be set
- Should support deposits
- Should only support withdrawals if the amount does not exceed the sum of the balance and the overdraft limit, otherwise an exception stating “Insufficient Funds” should occur
- Should identify if the account is overdrawn
- Should require bank name and account type (that is, they cannot be empty or null)
- Should trim the bank name
- Should ensure that the Account Type is type-safe and that it is supplied when creating the account (that is, it cannot be null)
- Should support the following types of accounts: Chequing, Saving, Credit Card, and Line of Credit
- Should verify that the branch number is six digits and the institution number is three digits
- Should require an opening balance
- Should not allow a negative overdraft limit
Use the following class diagram when creating your solution.
LetterGrade + QuebecLetterGrade
The QuebecLetterGrade uses the simple LetterGrade enumeration and assigns specific ranges of percentage marks for the possible LetterGrade values.
Problem Statement
Write the code for the LetterGrade class that represents a letter grade as assigned in Quebec universities (Source: http://en.wikipedia.org/wiki/Letter_grade). The solution must meet the following requirements (new requirements are in bold):
- Should get and set the grade as a type-safe value
- Should get the appropriate descriptions for the grade, based on the following table:
Grade | Description |
A | A - 80-100% - Greatly Above Standards |
B | B - 70-79% - Above Standards |
C | C - 60-69% - At Government Standards |
D | D - 50-60% - Lower Standards |
F | F - 0-49% - Failure |
Use the following class diagram when creating your solution.
Address + Student + GenderType
This revised version of the Student class now has an Address field. The address field is new; although similar to the CanadianAddress class, the Address class is simpler and more “generic” (having “State” instead of “Province” and “ZipCode” instead of “PostalCode”).
Problem Statement
Extend the Student class to now include information about the student’s home address. Create an Address class to represent a simple, generic address. In addition, the Student class must represent the Gender using an enumeration.
The solution must meet the following requirements (new requirements are in bold):
-
The Address class must
- Get and set the city, state, street, unit and zip code.
- The Student class must
- Verify that an address object is supplied (is not null)
- Represent the Gender as a GenderType of Male and Female
- Should get and set the student’s name, gender, GPA, program of studies, and whether or not the student is full-time.
- Should override the toString() method to get the student’s ID and name in this format:
(ID) Name - Should n * longer allow the student ID to be set (it’s only set through the constructor)
- Should reject empty text (and null values) for the student’s name and program of studies.
- Should trim the student’s name and the program name
- Should only accept ‘M’ and ‘F’ as valid genders
- Should set the gender to upper-case
- Should reject negative GPAs and GPAs over 9
Use the following class diagrams to guide your design.
ImproperFraction + MixedNumber + ProperFraction
In this sample, the idea of a Fraction class is made more specific by replacing it with three more specific types of numbers: MixedNumber, ProperFraction, and ImproperFraction. A MixedNumber is made up of a whole number and a ProperFraction. A MixedNumber can also be expressed as or derived from an ImproperFraction. The reciprocal of a ProperFraction is an ImproperFraction and the reciprocal of an ImproperFraction is a ProperFraction.
Problem Statement
The generic Fraction class is now being replaced with a set of three more specific classes for numbers with fractional values: ProperFraction, ImproperFraction, and MixedNumber. Create these classes, using the following requirements and class diagrams.
- ProperFraction should ensure that it is indeed a proper fraction
- ImproperFraction should ensure that it is indeed an improper fraction
- MixedNumber should construct either from a whole number and a proper fraction or from an improper fraction
- ProperFraction and ImproperFraction should get their respective reciprocals as well as their values as real numbers
- ProperFraction and ImproperFraction should ensure that the denominator is always positive
- MixedNumber should get the whole number portion as well as the fractional portion
- MixedNumber should get its value as a real number as well as get its value as an ImproperFraction
Practice Exercises
- LetterGrade + BahamaLetterGrade – The BahamaLetterGrade also uses the LetterGrade enumerated type, assigning its own specific ranges and values to the possible LetterGrade values.
- HazardousMaterial + ClassCode – The various types of hazardous materials are now identified by their ClassCode enumerated type.
- CanadianAddress + Province – The CanadianAddress class is modified to now use a Province enumeration to properly capture the provinces and territories of Canada.
- Employee/Student + CanadianAddress + Province – The Employee and Student classes now have address information.
- Company + Month + CanadianAddress + Province – The Company now has a CanadianAddress.
LetterGrade + BahamaLetterGrade
The BahamaLetterGrade also uses the LetterGrade enumerated type, assigning its own specific ranges and values to the possible LetterGrade values.
Problem Statement
Write the code for the BahamaLetterGrade class and the LetterGrade enumeration to represent a letter grade as assigned in universities in the Bahamas. (For more information on Bahaman letter grades, see http://en.wikipedia.org/wiki/Letter_grade.) . The solution must meet the following requirements (new requirements are in green, bold italic font):
- The LetterGrade enumeration should be “generic” and support letter grades of A through F inclusive
- The BahamaLetterGrade should reject the LetterGrade of E (which is not allowed in the Bahamas) as well as any null letter grade
- The BahamaLetterGrade should get the appropriate descriptions for the grade, based on the following table:
Grade | Grade | Value Description |
---|---|---|
A | 4 | A-4 - 90-100% |
B | 3 | B-3 - 71-89% |
C | 2 | C-2 - 56-70% |
D | 1 | D-1 - 46-55% |
F | 0 | F-0 - 0-45% |
Use the following class diagram when creating your solution.
HazardousMaterial + ClassCode
The various types of hazardous materials are now identified by their ClassCode enumerated type.
Problem Statement
Rewrite the code for the HazardousMaterial class to make use of the ClassCode enumerated type. The solution must meet the following requirements (new requirements are in green, bold italic font):
- Should return the class code as the classification
- Should make sure the class code is supplied (is not null)
- Should get the description for the class, based on the following table
You must use a switch statement to get the results
Class Code | Description |
---|---|
A | Compressed Gas |
B | Flammable and Combustible Material |
C | Oxidizing Material |
D | Poisonous and Infectious Material |
E | Corrosive Material |
F | Dangerously Reactive Material |
- Should override the ToString() method to get the full description and class code in the following format:
- “Class ClassCode - Description”
Use the following class diagram when creating your solution.
CanadianAddress + Province
The CanadianAddress class is modified to now use a ProvinceType enumeration to properly capture the provinces and territories of Canada.
Problem Statement
Modify the CanadianAddress class to now use an enumeration for the province. Use the following class diagram as a guide in creating the class and the enumeration.
Employee/Student + CanadianAddress + Province
The Employee and Student classes now have address information.
Problem Statement
Modify the Employee and Student classes to now use the CanadianAddress type for their addresses.
Company + Month + CanadianAddress + Province
The Company now has a CanadianAddress.
Problem Statement
Modify the Company class to now make use of the CanadianAddress type for the address. Also, create an enumeration for the months of the year for representing the company’s fiscal year end.
Topic J - Looping Structures
Overview
LOGs
General Programming Concepts and Terms
- Identify the C# statements that correspond to Do-While and Do-Until logical structures
- Translate Do-While structures into code
- Translate Do-Until structures into code
- Identify and distinguish the parts of the for statement in code
- Describe the common situations in which the for statement is used
- Demonstrate how the various looping statements can be interchanged with slight alterations of the logical structures to maintain the overall logic of a given routine.
Code Samples
- Aggregator – The Aggregator class supports static methods for doing various types of aggregation on Numbers. (Numbers is a supplied class that stores a series of real values that can be obtained by calling getNext().) The common aggregations of totalItems(), averageItems(), maxValue() are given as examples of simple looping.
- Math – fibonnacciNumber() and isPerfect()
- Fraction – The fraction example is now enhanced to simplify the fractional value by finding the greatest common denominator of the fraction’s numerator and denominator.
- GuessingGame – This example uses a supplied class called SecretNumber, which picks a random whole number between a specific range. An instance of the SecretNumber is passed to the constructor of the GuessingGame, and the GuessingGame’s guess() method attempts to get the correct guess within a specified number of tries.
Aggregator
The Aggregator class supports static methods for doing various types of aggregation on Numbers. (Numbers is a supplied class that stores a series of real values that can be obtained by calling GetNext().) The common aggregations of TotalItems(), AverageItems(), MaxValue() are given as examples of simple looping.
The Numbers Class
The methods of the Aggregator class all take a single argument: a Numbers object. The Numbers object contains a bunch of real numbers and it has two methods that the Aggregator methods will use:
- HasNext() – This returns a Boolean indicating that the object has another number available.
- GetNext() – This returns a real number from the set of values in the Numbers object.
For example, if a Numbers object has a set of four values such as 1.0, 5.5, 3.2, and 5.3, then the GetNext() method can be called four times to retrieve each value. Any attempt to call GetNext() when the object’s HasNext() is false will cause an exception.
Note: This class is already coded for you in the samples.
Aggregator Methods
The following Aggregator methods will demonstrate looping logic and syntax. All of these examples demonstrate the while statement.
- TotalItems() – This method loops through all the values inside of the Numbers object to calculate and return the total of the values.
- AverageItems() – This method gets all the values inside of the Numbers object so as to calculate the average value. If no values exist in the Numbers object, then the method returns an average of zero.
- MaxValue() – This method examines the Numbers object’s values to see which value is the largest. If there are no values inside of the Numbers object, then the method returns the smallest possible value that is supported by the programming language.
Math
The Math class provides methods for the following mathematical algorithms not found in the default Math class.
- FibonacciNumber() – Returns a number from the Fibonacci sequence for a given position. If the given position is less than or equal to zero, then this method should throw an exception. This example demonstrates the for statement. An alternative version illustrates the do-while statement.
- IsPerfect() – Indicates whether or not a number is a “perfect” number. A perfect number is a number where the sum of all the factors equals the number. By definition, any number less than 1 (including all negative numbers) are not perfect numbers. This example uses a while statement. An alternative version illustrates a do-while statement.
Fraction
The fraction example is now enhanced to simplify the fractional value by finding the greatest common denominator of the fraction’s numerator and denominator.
The Fraction class represents a fraction as two integers: one for the numerator and one for the denominator. When a Fraction object is created, the class must now simplify the fractional values by calculating the greatest common denominator.
- GreatestCommonDenominator() – This private method is used by the Simplify() method to get the greatest common denominator for the numerator and denominator. Remember to allow for dealing with negative fractions (in which case, the negative sign will be on the numerator). This example uses a while statement (though it is possible to re-work the logic to use a do-while statement).
GuessingGame
This example uses a supplied class called SecretNumber, which picks a random whole number between a specific range. An instance of the SecretNumber is passed to the constructor of the GuessingGame, and the GuessingGame’s Guess() method attempts to get the correct guess within a specified number of tries.
In a regular number guessing game between two people, one person asks another person to guess a whole number between a pair of values. In this example, two different classes take on the roles of the two people in the game: The SecretNumber class takes on the role of the person who has picked a value between some lower and upper limit, while the GuessingGame class takes on the role of the person who has to guess what that hidden number is.
Note: This class is already coded for you in the samples.
SecretNumber
Objects of this class will store a hidden value between some upper and lower limit (inclusive). The SecretNumber class supports three public methods:
- GetLowerLimit() – Returns a number representing the lower end (inclusive) of the range of possible values for the hidden value.
- GetUpperLimit() – Returns a number representing the upper end (inclusive) of the range of possible values for the hidden value.
- Guess() – Returns true if the supplied value matches the hidden value, otherwise it returns false.
GuessingGame
This class simulates the action of guessing what hidden value is stored inside of a SecretNumber object. The SecretNumber is supplied to the constructor of the GuessingGame, and the following methods attempt to find out what that number is.
- GuessNumber() – This first method simply tries to guess the hidden value of the SecretNumber. It has “unlimited” guesses, and it will return the number of attempts it took to find out what that hidden number is.
- GuessNumber(MaxAttempts : Integer) – This method will also try to guess the SecretNumber’s hidden value, but it is limited to a maximum number of guesses. This method will return the actual number of attempts it took to find the hidden value, or it will return the GUESS_FAILED constant if it was unable to guess the secret number.
Try creating alternate versions of this GuessingGame that will use a while statement and a for statement in the GuessNumber methods.
Practice Exercises
- Aggregator – countItems(), minValue()
- Math – factorial(), isPrime(), power(), and greatestCommonDenominator()
- GuessingGame – This example expands on the sample GuessingGame by 1) re-working the guess() method to make guessing more efficient by using the SecretNumber’s guessWithHint() method, and 2) overloading the guess() method to continue guessing until the correct answer is found (and returning the number of attempts it took to find the correct answer).
Aggregator
Expand on the sample Aggregator class to perform the following aggregations.
- CountItems() – Return the count of the number of values in the Numbers object.
- MinValue() – Return the smallest value in the set of values of the supplied Numbers object. If no values exist in the Numbers object, return the largest whole number supported by the programming language.
Math
Expand on the sample Math class by providing methods to perform the following.
- Factorial() – Calculate the factorial of a given number. If the given number is negative, throw an exception. If the calculated value for the factorial will exceed the maximum integer value (int.MaxValue), then return zero (0) as a result.
- IsPrime() – Identify if a number is or is not a prime number. A prime number is a number with only two divisors: 1 and the number itself. By definition for this problem, numbers less than one are not considered prime numbers.
- Power() – Calculate the value of x to the power of y (as in xy). Use looping logic (addition) to get the result.
- GreatestCommonDenominator() – Find the greatest common denominator between two whole numbers.
GuessingGame
This example expands on the sample GuessingGame by adding more guess methods to make guessing more efficient by using the SecretNumber’s guessWithHint() method.
In a regular number guessing game between two people, one person asks another person to guess a whole number between a pair of values. In this example, two different classes take on the roles of the two people in the game: The SecretNumber class takes on the role of the person who has picked a value between some lower and upper limit, while the GuessingGame class takes on the role of the person who has to guess what that hidden number is.
SecretNumber
Objects of this class will store a hidden value between some upper and lower limit (inclusive). The SecretNumber class supports these public methods:
- GetLowerLimit() – Returns a number representing the lower end (inclusive) of the range of possible values for the hidden value.
- GetUpperLimit() – Returns a number representing the upper end (inclusive) of the range of possible values for the hidden value.
- GuessWithHint() – Returns CORRECT if the supplied value matches the hidden value, otherwise it returns GUESS_HIGHER if the attempted guess was too low or GUESS_LOWER if the attempted guess was too high.
Note: This class is already coded for you in the samples.
GuessingGame
This class simulates the action of guessing what hidden value is stored inside of a SecretNumber object. The SecretNumber is supplied to the constructor of the GuessingGame, and the following methods attempt to find out what that number is.
- GuessUsingHints() – This first method simply tries to guess the hidden value of the SecretNumber. It has “unlimited” guesses, and it will return the number of attempts it took to find out what that hidden number is. Use the GuessWithHint method of the SecretNumber object.
- GuessUsingHints(MaxAttempts : Integer) – This method will also try to guess the SecretNumber’s hidden value, but it is limited to a maximum number of guesses. This method will return the actual number of attempts it took to find the hidden value, or it will return the GUESS_FAILED constant if it was unable to guess the secret number. Use the GuessWithHint method of the SecretNumber object.
As an additional exercise, create a simple driver that instantiates a SecretNumber object and supplies it to a GuessingGame object. Show how many attempts it took to guess the hidden number when using hints.
Interlude 2 - A General-Purpose Driver
The following is a nice general-purpose console-based driver that can be used for the remaining topics.
Topic K - Looping and Collections
Overview
This topic provides further examples of looping logic, but involves the use of collections. All of these samples make use of the List<T> class to maintain a collection or group of objects.
The List<T> class is a Generic class, meaning that the item in the angled brackets - <T> - is a placeholder for the name of the actual class that is being managed in the List. For example, to have a list of Integers we would declare that as List<Integer>. Likewise, if we wanted a list of Student objects, it would be declared as List<Student>.
The List<T> class supports a number of methods and properties for working with the collection.
- Add() – Used to add an item to the collection
- [index] – Used to retrieve an item in the collection
- Remove() – Used to remove an item from the collection
- Count – Identifies the number of items in the collection
LOGs
OOP Basics
- Define the term Generics as used in C# and give an example
General Programming Concepts and Terms
- Describe what is meant by a “collection” class and give an example
- List and describe the common methods of collection classes such as the List<T>
- Identify the parts of the foreach statement
- Describe the common situations in which the foreach statement is typically used
- Identify the major benefit of using Generics
- List the major benefits of using collections instead of arrays
Code Samples
- Math – The Math class is expanded to produce a collection of Integers for the sequence of Fibonacci numbers.
- PhoneBook – The PhoneBook class provides simple management of a collection of PhoneNumber objects. The PhoneBook allows for adding and retrieving phone numbers; it supports search for phone numbers by the telephone number or by a last name.
- ClassList – The ClassList example maintains a list of students for a particular course by offering methods to add and remove students from a course. In addition, the ClassList ensures that students are not added to the course twice (based on the student’s Id).
- MultipleChoiceMarker – This class is used for marking multiple choice exams. It takes a collection of MultipleChoice objects as the answer key when it is first created. It provides a method to mark the student’s answers.
- BankStatement – This class represents a bank statement for a BankAccount for a given month. The statement allows BankTransaction objects to be added, and performs deposits and withdrawals on the BankAccount. The statement reports the starting and ending balance and also summarizes the total amount deposited and withdrawn for the month.
- DeckOfCards –The DeckOfCards class represents a complete deck of cards. When the deck is first created, a card is created for each suit. The DeckOfCards supports a method to draw a card.
Math
This sample leverages the existing calculation of a number in the Fibonacci sequence to generate the sequence itself.
- FibonacciSequence(Length : Integer) – This method generates the sequence of Fibonacci numbers to a specific length in the sequence.
PhoneBook
The PhoneBook class provides simple management of a collection of PhoneNumber objects. The PhoneBook allows for adding and retrieving phone numbers; it supports search for phone numbers by the telephone number or by a last name.
- Constructor – Create a new List<PhoneNumber> for the numbers field.
- AddPhoneNumber(Entry : PhoneNumber) – Add a PhoneNumber to the collection. Ensure that the PhoneNumber exists (is not null) before adding; if the PhoneNumber is null, throw an exception.
- GetPhoneNumber(Index : Integer) : PhoneNumber – Get an item from a particular position in the collection.
- GetCount() : Integer – Get the size of the collection, which is a count of how many PhoneNumber objects are in the collection.
- FindPhoneNumber(TelephoneNumber : String) : PhoneNumber – Look through the collection and return a PhoneNumber with a matching telephone number. If none is found, return null.
- FindPhoneNumbersByLastName(LastName : String) : List<PhoneNumber> - Look through the collection for PhoneNumber objects with a matching last name. Add those objects to a new collection and return the collection of matching PhoneNumbers. If no items are found, return an empty collection.
Supporting Classes
The PhoneNumber class used in this sample is provided for you. The following class diagram shows the design of this class.
PhoneBook Class
ClassList
The ClassList example maintains a list of students for a particular course by offering methods to add and remove students from a course. In addition, the ClassList ensures that students are not added to the course twice (based on the student’s Id). The following parts of the ClassList must be coded to complete the solution.
- Constructor – Set the course id and the collection of students. Ensure that the supplied arguments are valid
- CourseId cannot be empty or null, and must be trimmed of leading or trailing spaces
- The collection object cannot be null and cannot have more students than the constant CLASS_LIMIT
- There cannot be any duplicate students in the collection (where a duplicate is defined as two or more Student objects with identical Ids)
- AddStudent() – Add the supplied student object to the collection. Ensure that
- The Student object is not null
- The class limit is not exceeded
- The Student object does not already exist in the collection (that is, there are no duplicates allowed, based on the student’s id)
- FindStudent() – Search the collection for a Student with a matching Id. If none is found, return null.
- RemoveStudent() – Search for a Student with a matching Id; if one is found, remove it from the collection.
Student
ClassList
MultipleChoiceMarker
This class is used for marking multiple choice exams. It takes a collection of MultipleChoice objects as the answer key when it is first created. It provides a method to mark the student’s answers. The following methods must be coded to complete the solution.
- MarkExam() – This method takes the supplied exam answers and compares them against the answers in the marking key. It then constructs a Mark object, based on the earned marks and the possible marks (each answer is worth one mark). Before marking the exam, the method must ensure that
- The supplied collection of multiple choice answers is not null
- The supplied collection of multiple choice answers has the same number of answers as the MultipleChoiceMarker’s answer key
MultipleChoice and Mark Classes
MultipleChoiceMarker
BankStatement
This class represents a bank statement for a BankAccount for a given month. The statement allows BankTransaction objects to be added, and performs deposits and withdrawals on the BankAccount. The statement reports the starting and ending balance and also summarizes the total amount deposited and withdrawn for the month. The following methods must be coded to complete the solution.
- GetTotalDeposits() – Loop through the collection of transactions to total all those transactions with a positive amount.
- GetTotalWithdrawals() – Loop through the collection of transactions to total all those transactions with a negative amount.
Account and BankTransaction
Bank Statement
DeckOfCards
The DeckOfCards class represents a complete deck of cards. When the deck is first created, a card is created for each suit. The DeckOfCards supports a method to draw a card. The following methods must be coded to complete the solution.
- Constructor – Create all the cards for all the CardSuit values and all the CardValue values.
- DrawCard() – Return the card at the “top” of the deck (that is, at position zero). If the deck is empty, return a null.
- Shuffle() – Mix up the order of the PlayingCards in the Cards list. Shuffle should work regardless of the number of cards still in the deck.
PlayingCard and DeckOfCards
Practice examples
- PhoneBook – This extends the PhoneBook class by ensuring that duplicate phone numbers are not added to the collection.
- Registrar – The Registrar class is responsible to support the enrollment of students. This class maintains the student body as a collection of Student objects. It supports the ability to find and remove students, switch students to another program, and get the number of students enrolled in a specific program.
- BookBag – The BookBag class represents a simple “shopping cart” for a book consignment store. Books are sold on consignment, and customers can add or remove books from their BookBag as well as search their BookBag for books by ISBN. Customers can also see the total price of the items in their BookBag.
- DeckOfCards –The DeckOfCards class represents a complete deck of cards. When the deck is first created, a card is created for each suit. The DeckOfCards supports two public methods: Draw() and Shuffle().
PhoneBook
This extends the PhoneBook class by ensuring that duplicate phone numbers are not added to the collection. Make the following additions and modifications to complete the solution.
- AddPhoneNumber() – This method must be modified to ensure that the telephone number does not already exist; that is, no duplicate phone numbers are allowed, and an exception must be thrown if the supplied PhoneNumber already exists.
Registrar
The Registrar class is responsible to support the enrolment of students. This class maintains the student body as a collection of Student objects. It supports the ability to find and remove students, switch students to another program, and get the number of students enrolled in a specific program. Code the following methods to complete the Registrar class.
- Add() – This method takes the supplied Person information as well as the program of study to create a new Student, adding them to the collection of students in the student body. It returns the student Id for the new enrolment.
- FindStudent() – This method searches the student body for a student with a matching Id.
- RemoveStudent() – This method searches the student body for a student with a matching Id and removes them from the collection, if found.
- GetProgramEnrollment() – This method searches the collection of students to find out how many are enrolled in a particular program. Validate the Program name before performing the search.
BookBag
The BookBag class represents a simple “shopping cart” for a book consignment store. Books are sold on consignment, and customers can add or remove books from their BookBag as well as search their BookBag for books by ISBN. Customers can also see the total price of the items in their BookBag. Code the following methods to complete the BookBag class.
- GetTotal() – Loop through the collection of books and total the price of all the books. Ensure that the amount is rounded to two decimal places (for dollars and cents).
- FindBook() – Look through the collection of books to find a book with the specified ISBN number. Throw an exception if the supplied ISBN is null.
- RemoveBook() – Remove the book with a matching ISBN from the collection of book consignments.
DeckOfCards
Modify the DeckOfCards class to support shuffling of the deck.
- Shuffle() – This method resets the deck to a full deck of cards and then “shuffles” the deck until the cards are randomly distributed through the collection.
Topic L - Arrays - Not Sorted
Overview
All of these classes are used to illustrate working with un-sorted arrays.
LOGs
General Programming Concepts and Terms
- Describe what makes an array different from other collections in C#
- Describe the situations in which arrays are preferable over the use of collections
Code Samples
- Math - The Math class is modified to produce the sequence of Fibonacci numbers as an array.
- PhoneBook - The PhoneBook class provides simple management of an array of PhoneNumber objects. The PhoneBook allows for adding and retrieving phone numbers; it supports search for phone numbers by the telephone number or by a last name. The PhoneBook is initialized with a physical limit for the arrays, and the actual number of entries in the phone book is tracked as phone numbers are added to the phone book.
- ClassList - The ClassList example maintains an array of students for a particular course by offering methods to add and remove students from a course. In addition, the ClassList ensures that students are not added to the course twice (based on the student’s Id). The internal array is of a fixed size (ClassList.CLASS_LIMIT) as well as the logical size of the array. If the ClassList is provided with an array of students in its constructor, that list is “copied” to the internal array of the ClassList object.
- MultipleChoiceMarker - This class is used for marking multiple choice exams. It takes an array of MultipleChoice objects as the answer key when it is first created. It provides a method to mark the student’s answers.
- BankStatement - This class represents a bank statement for a BankAccount for a given month. The statement allows BankTransaction objects to be added, and performs deposits and withdrawals on the BankAccount. The statement reports the starting and ending balance and also summarizes the total amount deposited and withdrawn for the month.
- DeckOfCards -The DeckOfCards class represents a complete deck of cards. When the deck is first created, a card is created for each suit. The DeckOfCards supports a method to draw a card.
Math
This sample leverages the existing calculation of a number in the Fibonacci sequence to generate the sequence itself. The sequence is returned as an array of integers.
- FibonacciSequence(Length : Integer) - This method generates the sequence of Fibonacci numbers to a specific length in the sequence; the results are returned as an array of integers. If the specified length is negative, throw an exception.
PhoneBook
The PhoneBook class provides simple management of an array of PhoneNumber objects. The PhoneBook allows for adding and retrieving phone numbers; it supports search for phone numbers by the telephone number or by a last name. The PhoneBook is initialized with a physical limit for the arrays, and the actual number of entries in the phone book is tracked as phone numbers are added to the phone book.
- Constructor - Create a new array of PhoneNumber objects for the numbers field.
- AddPhoneNumber(Entry : PhoneNumber) - Add a PhoneNumber to the array. Ensure that the PhoneNumber exists (is not null) before adding; if the PhoneNumber is null, throw an exception. If there isn’t any room to add the PhoneNumber, then throw an exception.
- GetPhoneNumber(Index : Integer) : PhoneNumber - Get an item from a particular position in the array.
- Count : Integer - Get the logical size of the array.
- FindPhoneNumber(TelephoneNumber : String) : PhoneNumber - Look through the array and return a PhoneNumber with a matching telephone number. If none is found, return null.
- FindPhoneNumbersByLastName(LastName : String) : PhoneNumber[ ] - Look through the array for PhoneNumber objects with a matching last name. Add those objects to a new array and return it. If no items are found, return an empty array.
- Supporting Classes
- ArrayFullException - This class is used to indicate that the array is full and no more items can be added to the array.
- PhoneNumber - This class contains a telephone number and the name that the number is registered to.
ClassList
The ClassList example maintains an array of students for a particular course by offering methods to add and remove students from a course. In addition, the ClassList ensures that students are not added to the course twice (based on the student’s Id). The internal array is of a fixed size (ClassList.CLASS_LIMIT) as well as the logical size of the array. If the ClassList is provided with an array of students in its constructor, that list is “copied” to the internal array of the ClassList object.
- Constructor - Set the course id and the array of students. Ensure that the supplied arguments are valid
- CourseId cannot be empty or null, and must be trimmed of leading or trailing spaces
- The array of students cannot be null and cannot have more students than the constant CLASS_LIMIT
- There cannot be any duplicate students in the array (where a duplicate is defined as two or more Student objects with identical Ids)
- AddStudent() - Add the supplied student object to the array. Ensure that
- The Student object is not null
- The class limit is not exceeded
- The Student object does not already exist in the array (that is, there are no duplicates allowed, based on the student’s id)
- FindStudent() - Search the array for a Student with a matching Id. If none is found, return null.
- RemoveStudent() - Search for a Student with a matching Id; if one is found, remove it from the array.
- Supporting Classes
- Student -
MultipleChoiceMarker
This class is used for marking multiple choice exams. It takes an array of MultipleChoice objects as the answer key when it is first created. It provides a method to mark the student’s answers. The following methods must be coded to complete the solution.
- MarkExam() - This method takes the supplied exam answers and compares them against the answers in the marking key. It then constructs a Mark object, based on the earned marks and the possible marks (each answer is worth one mark). Before marking the exam, the method must ensure that
- The supplied array of multiple choice answers is not null
- The supplied array of multiple choice answers has the same number of answers as the MultipleChoiceMarker’s answer key
- Supporting Classes
- MultipleChoice -
BankStatement
This class represents a bank statement for a BankAccount for a given month. The statement allows BankTransaction objects to be added, and performs deposits and withdrawals on the BankAccount. The statement reports the starting and ending balance and also summarizes the total amount deposited and withdrawn for the month. The following methods must be coded to complete the solution.
- Add() - Add a BankTransaction to the array of transactions. If the BankTransaction is null, throw an exception. Otherwise, process the transaction as either a deposit or a withdrawal, depending on if the amount is positive or negative, and then add it to the array of transactions.
- GetTotalDeposits() - Loop through the array of transactions to total all those transactions with a positive amount.
- GetTotalWithdrawals() - Loop through the array of transactions to total all those transactions with a negative amount.
- Sort() - Sorts the array of transactions by date.
- Supporting Classes
- Account -
- BankTransaction -
- AccountType -
DeckOfCards
The DeckOfCards class represents a complete deck of cards. When the deck is first created, a card is created for each suit. The DeckOfCards supports a method to draw a card. The following methods must be coded to complete the solution.
- Constructor - Create all the cards for all the CardSuit values and all the CardValue values.
- DrawCard() - Return the card at the “top” of the deck (that is, at position zero). If the deck is empty, return a null.
- IsEmpty - Returns true if the logical size of the deck of cards has reached zero, otherwise it returns false.
- Supporting Classes
- PlayingCard - The PlayingCard class, along with its two enumerated types CardSuit and CardValue, are the basis for the DeckOfCards class.
Practice Exercises
- PhoneBook – This extends the PhoneBook class by ensuring that duplicate phone numbers are not added to the collection.
- Registrar – The Registrar class is responsible to support the enrollment of students. This class maintains the student body as a collection of Student objects. It supports the ability to find and remove students, switch students to another program, and get the number of students enrolled in a specific program.
- BookBag – The BookBag class represents a simple “shopping cart” for a book consignment store. Books are sold on consignment, and customers can add or remove books from their BookBag as well as search their BookBag for books by ISBN. Customers can also see the total price of the items in their BookBag.
- DeckOfCards –The DeckOfCards class represents a complete deck of cards. When the deck is first created, a card is created for each suit. The DeckOfCards supports two public methods: Draw() and Shuffle().
- CD – [Not yet implemented] This class represents an array of Songs. It supports the ability to calculate the total running time and the average running time of all the songs on the CD.
PhoneBook
This extends the PhoneBook class by ensuring that duplicate phone numbers are not added to the collection. Make the following additions and modifications to complete the solution.
- AddPhoneNumber() – This method must be modified to ensure that the telephone number does not already exist; that is, no duplicate phone numbers are allowed, and an exception must be thrown if the supplied PhoneNumber already exists.
Registrar
The Registrar class is responsible to support the enrolment of students. This class maintains the student body as a collection of Student objects. It supports the ability to find and remove students, switch students to another program, and get the number of students enrolled in a specific program.
The Registrar class is responsible to support the enrolment of students. This class maintains the student body as an array of Student objects. It supports the ability to find and remove students, switch students to another program, and get the number of students enrolled in a specific program. Code the following methods to complete the Registrar class.
- Add() – This method takes the supplied Person information as well as the program of study to create a new Student, adding them to the array of students in the student body. It returns the student Id for the new enrolment.
- FindStudent() – This method searches the student body for a student with a matching Id.
- RemoveStudent() – This method searches the student body for a student with a matching Id and removes them from the collection, if found.
- GetProgramEnrollment() – This method searches the array of students to find out how many are enrolled in a particular program. Validate the Program name before performing the search.
BookBag
The BookBag class represents a simple “shopping cart” for a book consignment store. Books are sold on consignment, and customers can add or remove books from their BookBag as well as search their BookBag for books by ISBN. Customers can also see the total price of the items in their BookBag. Code the following methods to complete the BookBag class.
- GetTotal() – Loop through the array of books and total the price of all the books. Ensure that the amount is rounded to two decimal places (for dollars and cents).
- FindBook() – Look through the array of books to find a book with the specified ISBN number. Throw an exception if the supplied ISBN is null.
- RemoveBook() – Remove the book with a matching ISBN from the array of book consignments.
DeckOfCards
The DeckOfCards class represents a complete deck of cards. When the deck is first created, a card is created for each suit. The DeckOfCards supports two public methods: Draw() and Shuffle(). Modify the DeckOfCards class to support shuffling of the deck.
- Shuffle() – This method resets the deck to a full deck of cards and then “shuffles” the deck until the cards are randomly distributed through the collection.
CD
[Not yet implemented]
This class represents an array of Songs. It supports the ability to calculate the total running time and the average running time of all the songs on the CD.
Topic M - Arrays - Sorted
Overview
The focus of this topic is sorting arrays and performing searches, inserts, and deletions from a sorted array.
LOGs
Code Samples
- BranchVault - The BranchVault class manages safety deposit boxes. Each box has a unique number, and may be assigned to a particular account number. This class demonstrates the bubble sort and the binary search.
- PhoneBook - The PhoneBook class provides simple management of an array of PhoneNumber objects. The PhoneBook allows for adding and retrieving phone numbers; it supports search for phone numbers by the telephone number or by a last name. The PhoneBook is initialized with a physical limit for the arrays, and the actual number of entries in the phone book is tracked as phone numbers are added to the phone book. The AddPhoneNumber() method demonstrates adding to an array that is to be sorted.
- ClassList -The ClassList example maintains an array of students for a particular course by offering methods to add and remove students from a course. In addition, the ClassList ensures that students are not added to the course twice (based on the student’s Id). The internal array is of a fixed size (ClassList.CLASS_LIMIT) as well as the logical size of the array. If the ClassList is provided with an array of students in its constructor, that list is “copied” to the internal array of the ClassList object.
- BankStatement - [Not Implemented - Sorting by Date] This class represents a bank statement for a BankAccount for a given month. The statement allows BankTransaction objects to be added, and performs deposits and withdrawals on the BankAccount. The statement reports the starting and ending balance and also summarizes the total amount deposited and withdrawn for the month.
BranchVault
The BranchVault class manages safety deposit boxes. Each box has a unique number, and may be assigned to a particular account number. This class demonstrates the bubble sort and the binary search. Code the following methods to complete this class.
- SortBoxes() - This private method performs a bubble sort of the array of SafetyDepositBox objects. The boxes are to be sorted in ascending order according to their box number.
- Find() - This public method performs a binary search of the SafetyDepositBox array for a particular box number. If the box number is not found, it returns a null.
PhoneBook
The PhoneBook class provides simple management of an array of PhoneNumber objects. The PhoneBook allows for adding and retrieving phone numbers; it supports search for phone numbers by the telephone number or by a last name. The PhoneBook is initialized with a physical limit for the arrays, and the actual number of entries in the phone book is tracked as phone numbers are added to the phone book. The AddPhoneNumber() method demonstrates adding to an array that is to be sorted. Code the following methods to complete this class.
- BubbleSortByPhoneNumber() - This private method performs a bubble sort of the array of PhoneNumber objects. They are to be sorted in ascending order, based on the telephone number.
- InsertSortedByPhoneNumber() - This private method will insert a PhoneNumber object into the array while preserving the sort order of the array (based on the array being sorted in ascending order by telephone number).
ClassList
The ClassList example maintains an array of students for a particular course by offering methods to add and remove students from a course. In addition, the ClassList ensures that students are not added to the course twice (based on the student’s Id). The internal array is of a fixed size (ClassList.CLASS_LIMIT) as well as the logical size of the array. If the ClassList is provided with an array of students in its constructor, that list is “copied” to the internal array of the ClassList object (to ensure that the internal array has the appropriate physical size).
All of the Student objects in the ClassList are stored in a sorted array, based on the student Id. Code the following methods to complete this class.
- AddInsertSortedByStudentId() - This private method is called by both the AddStudent method and the constructor. It adds a student to the class list while maintaining a sort order (sorted by student Id in ascending order).
- FindStudent() - This public method performs a binary search of the array of Student objects. If the Student is found, this method will return that Student object, otherwise it will return null.
- RemoveStudent() - This public method will remove a particular Student from the array, while maintaining the sort order of the array. If the Student is found, this method will return that Student object, otherwise it will return a null.
- GetStudents() - This public method will generate and return a copy of the array of Student objects.
Supporting Classes
Practice Exercises
- PhoneBook - [Sorting by Name and Sorting by Phone Number] This extends the PhoneBook class by adding sorting by phone number and a binary search.
- Registrar - [Sorting by ID] The Registrar class is responsible to support the enrollment of students. This class maintains the student body as a collection of Student objects. It supports the ability to find and remove students, switch students to another program, and get the number of students enrolled in a specific program.
- CDLibrary - [Not Yet Implemented] This class manages a collection of CDs that are stored in alphabetical order. It supports searching the library for CDs based on the title.
PhoneBook
Extend the PhoneBook class by adding sorting by name and a binary search when sorted by number.
- FindPhoneNumberUsingBinarySearch() - This private method will do a binary search of the PhoneNumber array to find a PhoneNumber with a matching telephone number.
- BubbleSortByLastNameThenFirstName() - This private method sorts the array by first name within last name, in ascending alphabetical order.
Registrar
[Sorting by ID] The Registrar class is responsible to support the enrolment of students. This class maintains the student body as an array of Student objects. It supports the ability to find and remove students, switch students to another program, and get the number of students enrolled in a specific program. Code the following methods to complete the Registrar class.
- RemoveStudent() - This method must be modified to maintain the sort order when removing a student from the array.
CDLibrary
[Not Yet Implemented]
OOP - Getting Real
Topic N - Modularization and Utility Classes
Overview
This topic examines the technique of modularization as a way to simplify complex logic. In addition, this topic looks at the use of “utility” classes to aid in simplifying code and enabling better “code-reuse” for repetitive tasks. This technique of “simplifying” complex logic is also known as “refactoring”.
Modularization will be examined in the context of simplifying the internal working of a class’ methods and in the context of organizing and simplifying a driver. For classes, modularization is simply taking any repetitive or overly-complex code out of the class’ existing methods and placing that code inside of private methods. For console drivers (which typically consist of static methods), to be continued…
LOGs
Code Sample
- CD - This class represents a CD of Songs. It supports a public method to get long songs (which are songs whose RunningTime is greater than the average running time). A private method called resizeArray() is created to “modularize” this public method’s behaviour.
- RunningTime - This class represents an amount of time in minutes and seconds, and is used as the length of time for a Song. This class introduces the Scanner class for one of its constructors, which generates the running time from a string that is formatted as “minutes:seconds” (such as “5:39”, which would be 5 minutes and 30 seconds).
This class also uses an internal private method called paddZero() that ensures the seconds whose values are less than 10 have a leading zero when converted to a string. - TextFormatter - This class provides static methods for common string formatting needs, such as right and left justifying strings.
- MusicInfoFormatter - This class provides static methods for formatting CD information, such as the CD’s title and the information for a given Song. This kind of formatting is commonly seen, for example, in CD players that display the currently playing song.
CD
This class represents a CD of Songs. It supports a public method to get long songs (which are songs whose RunningTime is greater than the average running time). A private method called resizeArray() is created to “modularize” this public method’s behaviour.
- getLongSongs() - Refactor this method so that the code that resizes the array of songs is put into its own private method, called resizeArray.
RunningTime
This class represents an amount of time in minutes and seconds, and is used as the length of time for a Song. This class introduces the Scanner class for one of its constructors, which generates the running time from a string that is formatted as “minutes:seconds” (such as “5:39”, which would be 5 minutes and 30 seconds).
This class also uses an internal private method called paddZero() that ensures the seconds whose values are less than 10 have a leading zero when converted to a string.
- toString() - Refactor this method to use a new private method called paddZero. The paddZero method will return a string version of the integer value passed in and will ensure that the string has a leading zero character if the supplied value is less than ten.
TextFormatter
This class provides static methods for common string formatting.
- leftJustify() - This static method will append spaces after the supplied text until the supplied text is at least the required length. Note that this method will not trim the supplied text if it is longer than the required length.
- rightJustify() - This static method will prepend spaces before the supplied text until the supplied text is at least the required length. Note that this method will not trim the supplied text if it is longer than the required length.
- shortenWithEllipsis() - This static method will check the length of the supplied text against the desired length. If the supplied text is longer, it will trim off trailing characters and append an ellipsis (…) , while ensuring that the resulting text is the desired length.
MusicInfoFormatter
This class provides static methods for formatting CD information, such as the CD’s title and the information for a given Song. This kind of formatting is commonly seen, for example, in CD players that display the currently playing song.
- formatTitleInfo() - This method generates a string that has title information for a CD that includes the CDs title and the name of the performing artist. The title name must be shortened with an ellipsis to a maximum length of 30 characters. The generated string must use the following overall format:
Title: ‘CD Title’ (Artist: Performing Artist Name) - formatSongInfo() - This method generates a string that follows specific formatting for a given song. It must use the following format:
Track Number: Song Title [Running Time] (by Song Writer)
The running time must be in the format of MM:SS.
## Practice Exercises
- TextFormatter - This class provides static methods for formatting strings. Expand this class by providing overloaded versions of the leftJustify() and rightJustify() methods that allow the padding character to be supplied through the parameter list.
TextFormatter
This class provides static methods for formatting strings. Expand this class by providing overloaded versions of the leftJustify() and rightJustify() methods that allow the padding character to be supplied through the parameter list.
Topic O - File I/O
Overview
All of the File I/O examples follow the same design pattern. This pattern is a form of what is called the Adapter Pattern. Each class has the following characteristics.
All of the text files used in this topic have their records delimited by carriage returns and their fields delimited by commas. File input is achieved through use of the Scanner class. The Scanner is also used to parse each record by specifying the use of the comma delimiter.
File Input/Output (IO) is all about using files for storing and retrieving data. These files exist “outside” of your program, and code is required to transfer the data between the file and the objects/variables in your program.
Data inside of files can be stored in either a binary or a text format. All of these examples will deal with text files. When using text files, the text itself needs to follow some pattern in order to distinguish the individual pieces of data from each other.
An old and traditional way of storing the text is to use a CSV (Comma-Separated Values) format. In this format, each row of information (also known as a “record” of data) is broken down into individual pieces of data by separating each piece (or “field”) by a comma. Rows are separated by a new line (or “carriage return”) marker. This format has had the advantage of being easy for humans to read and edit directly; it is also fairly flexible and programmers can easily adapt the form to fit the shape of the data they wish to store.
A more modern approach is to store the textual information in XML (eXtensible Markup Language) format. While also being human-readable and easy to format, XML has the advantage of making it easier for computer programs to read and save data that is both relational and complex. XML format uses “tags” (or “mark-up”) to wrap data. For example, if information on a person (“Bob Smith”) is stored in an XML file, it might look like this.
File Input/Output is best performed by having one class (or object) deal with formatting the data and a separate class to deal with directly accessing the file. In the following examples, the CSVFileIO and XMLFileIO classes read and write directly to the files, while the other classes (which end with “Adapter”) are concerned with determining the format of the data.
CSVFileIO
XMLFileIO
LOGs
- A constructor that takes a valid File object
- A method called fill() that takes an array as the parameter and returns the number of entries created in the array from having read the file.
Code Samples
- PhoneNumberFileAdapter - This reads a text file to fill a list of PhoneNumber data.
- StudentFileAdapter - This reads a text file to fill a list of Student data.
- BookFileAdapter - This reads a text file to fill a list of Book data. Note that in this example, not every field in the record structure is used in creating the required Book objects; unused fields are simply read and “discarded” by the BookFileAdapter. In addition, the Book class and text file used in this example allows for a book to have multiple authors.
- SafetyDepositBoxFileAdapter - This reads a text file to fill a list of SafetyDepositBox data. Note that in this particular example, some records may not have an account number for the box number.
PhoneNumberFileAdapter
This reads a text file to fill a list of PhoneNumber data.
- Record Structure:
- Telephone Number : String
- Last Name : String
- First Name : String
StudentFileAdapter
This reads a text file to fill a list of Student data.
- Record Structure:
- Student Id : Integer
- Name : String
- Gender : GenderType
BookFileAdapter
This reads a CSV text file to fill a list of Book data. Note that in this example, not every field in the record structure of CSV files is used in creating the required Book objects; unused fields are simply read and “discarded” by the BookFileAdapter. In addition, the Book class and text file used in this example allows for a book to have multiple authors.
- Record Structure in the file:
- Title : String
- Authors : String - delimited by semi-colons
- Publication year : Integer
- Publisher : String
- Format : String
- Dewey Decimal Number : String - could be empty in the file
- ISBN : String
- Number of Pages : Integer
- Cover Price : Money
SafetyDepositBoxFileAdapter
This reads a text file to fill a list of SafetyDepositBox data. Note that in this particular example, some records may not have an account number for the box number.
- Record Structure:
- Box Number : Integer
- Account Number : Integer - may not exist
Practice Exercises
- PersonFileAdapter – This reads a text file to fill an array of Person data.
- SongFileAdapter – This reads a text file to fill an array of Song data.
PersonFileAdapter
This reads a text file to fill an array of Person data.
- Record Structure:
- Record Structure:
- First Name : String
- Last Name : String
- Gender : GenderType
- Birthdate : Date
SongFileAdapter
This reads a text file to fill an array of Song data.
- Record Structure:
- Record Structure:
- Title : String
- Song Writer : String
- Length : RunningTime
Topic P - Exception Handling
Overview
The code for real-world programs is typically divided into distinct “layers” in order to make programs more flexible and maintainable. The layer that corresponds to the drivers we have seen so far is called the “Presentation Layer”. It’s called a “presentation layer” because its sole purpose is to interact with a user.
Almost all exception handling should take place in the driver, or presentation layer, of an application. Part of the reason for this is because the presentation layer is the “top” or “front-most” part of the program. When exceptions are handled at this layer, an application can report on the errors that occurred and give opportunity for the user to choose their desired response to the error.
LOGS
Code Samples
- DemoTestingForExceptions - This class is a very simple demonstration of how to catch exceptions and how exception handling allows a program to keep running even when a problem occurs.
- DemoBookFileAdapter - This class demonstrates displaying book information read from a file. It also demonstrates attempting to read from a file or directory that does not exist, and capturing an exception thrown by classes within the .Net Framework.
- (Not Available) MediaPlayer - This class demonstrates a complete working program that presents the user with a menu of various tasks related to loading and displaying songs on a CD. This class also demonstrates exception handling.
DemoTestingForExceptions
This class is a very simple demonstration of how to catch exceptions and how exception handling allows a program to keep running even when a problem occurs.
DemoBookFileAdapter
This class demonstrates displaying book information read from a file. It also demonstrates attempting to read from a file or directory that does not exist, and capturing an exception thrown by classes within the .Net Framework.
MediaPlayer
This class demonstrates a complete working program that presents the user with a menu of various tasks related to loading and displaying songs on a CD. This class also demonstrates exception handling.
Practice Exercises
- Testing Suite - Using any two classes from previous topics, create a program that tests the construction of objects with complete exception handling.
- Working Program - With the help of the UserPrompt class, create a simple program involving any classes from previous topics. Be sure to include exception handling in your program.
Testing Suite
Using any two classes from previous topics, create a program that tests the construction of objects with complete exception handling.
Working Program
With the help of the UserPrompt class, create a simple program involving any classes from previous topics. Be sure to include exception handling in your program.
Appendix
Learning Outcome Guide Review
Learning Outcome Guides (LOGs) are an excellent tool for instructors in the classroom and for students when they are reviewing their notes for each class. A Learning Outcome Guide is simply a list of specific things that the student (or reader, in this case) should be able to do or understand as a result of learning the material. LOGs give the learner the ability to self-check their progress (“Can I do X?”).
This appendix provides a shortened review of what each learning outcome is all about. For instructors, it provides a quick summary of the intent of the LOG. For students, it provides the answers. :)
Topic A - Starting Classes
OOP Basics
- Define the term “class” as used in OOP
- A class is a template that acts as the basis for creating objects and describes a) What it “looks like”, and b) How it “works”.
- Explain the purpose of classes in computer programs
- Classes allow programmers to define their own complex data types, complete with their own internal data (fields) and behaviours (methods).
- Describe the primary keywords used in declaring a class
- public/private - identifies whether the class is visible to other code; public means all other code can see this class, while private means
- class - declares that this is a class definition (tells the compiler to interpret this as a new “classification” or “kind” of data type; that is, a complex )
- Define the term “method” and give an example
- A method is a set of instructions that manipulate information.
- Create simple classes with methods only
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Explain what is meant by “overloading”
- Overloading describes creating more than one method with the same method name (but different parameters) in the same class. Overloaded methods allow for alternate ways to perform the same general behaviour of an object or a class.
- Describe the syntax of a method declaration (aka, method implementation)
- …
- Explain how classes with identical names can be distinguished from each other
- Different classes can have identical names provided that they exist in different namespaces. (Namespaces help to prevent a “collision” or confusion on the part of a compiler when confronted with two or more classes with the same name.)
General Programming Concepts and Terms
- Define the term “keyword” as it applies to programming languages
- A keyword is a special command or instruction that is part of (that is, built in to, or “intrinsic” to) a programming language. Keywords are “reserved” words, meaning that they cannot be used for other purposes, such as being a name of a class, a variable, or a method.
- Define the term “identifier” as it applies to programming languages
- An identifier is a programmer-supplied name for some item, either a class name, a field or local variable, or a method.
- Define the term “program statement” and identify how the computer recognizes a program statement
- A program statement is combination of keywords, identifiers, punctuation, and/or literal values that, together, represent a single “instruction” or “command” that a compiler can translate into executable code.
- Define the term “syntax” as it applies to programming languages.
- Syntax refers to the rules of a programming language that define how to combine keywords, identifiers, literal values, and other symbols to create specific instructions for a computer program.
- Identify the entry point for every computer program
- Every computer program must supply a method called “main” as the method that the operating system will call (execute) to begin running the program.
- Perform simple output to the console using System.Console
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Identify the basic structure of a simple C# program
- A C# program must consist of at least one class and have a single “main” method as the program’s entry point. Typically, a C# program will consist of a “driver” (the class that has the main method) and other classes used to represent objects (data) in the program.
- Explain what is meant by a “driver”
- A “driver” is a method or set of methods that “run” or control the execution of a program; it’s responsibility is to direct and maintain the “lifetime” of a program, and makes use of classes and objects to achieve the purpose of the program. Every driver begins its execution in a method called “main” (which is the name given for the entry point for every computer program).
- Explain what is meant by a “case-sensitive” programming language
- A case-sensitive language is one where the compiler regards “identical” words as different if they differ only in their case. In other words, a case-sensitive language will regard two identifiers are distinct if the only difference is whether they are spelled with upper- or lower-case characters. For example, HelloWorld and Helloworld will be regarded as different because one has a capital “W” and the other has a lower-case “w”.
- Explain what is meant by a “strongly-typed” programming language
- A “strongly-typed” programming language is one that requires all variables or objects to always match or conform to whatever data type they were originally declared as. Some programming languages (such as C#Script) allow variables or objects to “change” their data type whenever a programmer wishes to use it for other purposes; strongly-typed languages do not allow variables to “change” the type of data they can store. Strongly-typed programming languages provide better security and make it more difficult for programmers to inadvertently corrupt, mangle or expose important data.
- Explain what “string concatenation” is and how it works
- String concatenation is where two or more strings are “combined” to create a longer string; this is represented in C# by “adding” strings together. For example, given two literal strings whose values are “One” and “word”, the result of “One” + “word” would be the string “Oneword”.
- Define and distinguish the terms “argument” and “parameter”
- A parameter is a variable declared in the parameter list of a method declaration; parameters are used to hold or capture information (values) that are sent or passed into the method. An argument is some value (either a literal value, a value stored in a variable, or some other value that comes from an “expression”) that is sent (passed) into a method; arguments appear in the syntax of method calls.
- Use single-line, multi-line and XML comments as a means of documenting code
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- List the four pieces of information to include in comments at the top of every source file
- The file name, author, creation date, and purpose of the file.
Intro to IDEs
- Define the term “IDE” as it relates to software development tools
- The acronym “IDE” stands for “Integrated Development Environment”. An IDE brings together separate, but complementary, software development tools that aide developers in creating computer programs. For example, most IDEs bring a sophisticated text editor together with file management tools, a compiler, a debugger, and a program execution environment, as well as other useful tools.
- Define the terms “project” and “solution”
- A solution is a folder in which one or more projects can be managed and “kept together”; it is the “area” in which a developer can create projects. A project is as set of classes designed to achieve a particular purpose; a project may represent a complete program in and of itself, or may be a single component of a larger, more complex computer program.
- Identify the various parts of the IDE (Integrated Development Environment) used in this course
- [– image here –]
- Create a new project in the IDE for this course
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Create new source files in the IDE for this course
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Add existing files to a project
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
Topic B - Starting Classes
OOP Basics
- Define the term “object” as used in OOP
- An object is an instance of a class.
- Each object has its own “state” (values) for its fields, and all objects based on any given class are distinct from each other. The internal values (state)for an object’s fields can be changed independently of other objects of the same class.
- Describe the syntax of static and non-static method declarations
- Static methods are “shared” by all instances (objects) of a class, and can be called without even having to have an instance of the method’s class.
- Non-static methods, on the other hand, can only be called for a given object (instance of a class) and will “act on” (or “act on behalf of”) that object (and any other objects are not affected by the given object’s method call).
- Compare and contrast the syntax for method declarations (implementations) vs. method calls (for both static and non-static methods)
- …
- Define the term “field” as used in OOP and give an example
- A field is a variable that belongs to a class and is shared by all the methods of a class.
private static int sharedCount; // a static field
private String name; // an instance field
- Compare and contrast instance and class members, for both methods and fields
- Static methods are shared by all instances of a class, and can be called independently from instances of a class, whereas instance methods can only be called if you have an object (an instance of a class).
- Static fields are shared by and fully accessible to all methods, but non-static fields, also called “instance” fields, are only shared by other non-static methods. In the following example, the first field is a static fields and the second is an instance field.
- Identify the “member access operator” and explain it’s purpose
- The member access operator (also called the “dot” operator) is the period. It is used to access a field or method of an object or a class.
- Create (instantiate) objects
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Distinguish between classes and objects in OOP
- A class is merely a “template” for creating objects and describes a) what the object will “look like” (that is, what fields or data it will contain) and b) how the object works (that is, what methods can be called and what those methods will do).
- Explain what is meant by the phrase “type extensibility” and how classes represent “complex” data types in OOP
- “Type extensibility” is where a programming language allows developers to create or design their own “data types”.
- All programming languages support the “simplistic” data types (for numeric information, such as whole and real numbers, as well as simple text or character representation). These simple data types are intrinsic (“build into”) the programming language. A language that supports “type extensions” simply allows the programmer to create or design new data types; these new types are typically “complex” types made up of zero or more other (either simple or complex) data types.
General Programming Concepts and Terms
- List the three categories of simple information
- Textual, numeric, and conceptual
- Define the term “intrinsic data type”
- An “intrinsic data type” is one that is built into the programming language.
- Explain what an assignment statement is and how it works
- An assignment statement is an instruction to the computer to store a value into a specific variable (or object). The variable (or object) is placed on the left-hand side of the equals sign, and the value to be stored in the object is placed on the right-hand side of the equals sign.
- The left-hand side of an assignment statement must be a single variable, whereas the right-hand side can be any expression as long as the resulting value of the expression is the same (or compatible) data type as the variable on the left-hand side.
- Compare and contrast the three categories of simple information with the complex types of information which classes represent
- Simple information is either textual (such as a name), numeric (such as a total or count), or “conceptual” (that is, representing a more “abstract” value that is neither textual or numeric). An example of a textual type in C# is the char type. An example of a numeric type in C# is the double type. An example of a “conceptual” type is the boolean type. A variable whose type is one of the simple types can only hold a single, simple value at any given time.
- Complex types of information can be constructed from many simple types or from simple and complex types. A “class” is used to describe or define what the complex type looks like and how it works. Complex types are the basis for creating objects, and while any given “object variable” can only reference a single object, that object may consist of many distinct “values”, each one being stored in a field that is “hidden” (or “encapsulated”) inside the object.
- Explain the advantages of using classes and objects instead of only using intrinsic data types
- By using objects, the programmer is no longer limited to having to only deal with simple values. By designing and using complex types (classes), the programmer is able to create complex programs using less code in a way that is simpler and more reliable than if the programmer were only allowed to use the simple types built into the programming language.
Topic C - Starting Classes
OOP Basics
- Define the term “encapsulation” as used in OOP
- Encapsulation is a design principle of “hiding” fields in a class by always making them private and only allowing a “controlled access” to the values in fields by providing getters and setters.
- Encapsulation also includes the idea that all of the items (methods and fields) in a class always have access to each other (inside the class) while also providing the ability to control the external access to those items (methods and fields) by other classes.
- Explain why encapsulation is a good design principle
- Encapsulation is a good design principle because every object must be responsible for its own data; if an object “exposes” its fields for direct access (that is, makes those fields public), then that object cannot exert any control over the values that may be stored in those fields.
- Encapsulation, as a part of Object Oriented design, also facilitates the aspect of “grouping together” related fields and methods.
- Define the term “access specifier” and identify where it occurs in the code for a class
- An access specifier is a keyword in a programming language that identifies whether an item, such as a field or a method, is or is not accessible from outside of an object.
- List the two access specifiers that are used in this course
- The two main access specifiers of this course are public and private.
- Describe the difference between “private” and “public” members of a class
- Private methods and fields are not accessible from outside the class (they are only internally accessible). Public methods and fields are accessible both from outside and from inside the class.
- Describe the concepts of “Properties” as used in OOP
- “Properties” allow for controlled access to read (get) or change (set) fields in a class. When fields are used with properties, the field is sometimes called a “backing store” because it stores the actual value that is accessed through the property.
- Properties do not have to have fields as “backing stores”; a property can calculate its value (during a get) instead of simply retrieving it from a field. These types of properties are sometimes called “derived” properties.
- A “getter” is a method whose sole responsibility is to provide a means for retrieving (getting) a value. Getters are typically (that is, almost always) public, thereby allowing information (typically stored in a field) to be “extracted” from outside the class.
- A “setter” is a method whose sole responsibility is to control the storing of a value into a field of a class. Setters are typically (that is, almost always) public, thereby allowing code that is outside the class a way to store information inside the class (that is, inside of the fields of a class).
- Define the term “backing store” as it applies to properties and fields
- A “backing store” is a field that stores information which is accessible only through a property; the field variable is used in the “background” by the property’s get and/or set.
- Describe and explain the purpose and function of a constructor for a class
- A constructor has one purpose: To ensure that all the fields of the class are filled with meaningful values at the moment that an object is created (instantiated).
- Describe the syntax of a class constructor
- TBA…
- Identify when a class constructor is “called”
- A class’ constructor is never explicitly “called” in the traditional sense of “invoking” (calling) a method on a class. Rather, the class constructor is “called” when an object is instantiated using the new keyword.
- Define the term “class scope” as used in OOP
- Class scope is the idea that all of the methods and fields of a class are accessible to all the methods in a class.
- Define the term “local scope” as used in OOP
- Local scope is the idea that all of the variables declared in a parameter list and inside of the body of a method are only accessible from within that method. In other words, parameters and “local variables” are only accessible from within the method (or block of code) in which they were declared.
- Define the term “state” as applied to objects used in OOP
- “State” is the idea that objects have an “internal value” at any given point in time (as viewed from outside the class’ methods). In other words, an object has “state” simply because it has fields which hold (or “store”) information.
- Objects are responsible for ensuring that their internal state is always valid (or “stable”) at the end of each method’s execution.
- Define the term “override” as used in OOP
- Override is a term used to indicate that a class is re-defining the behaviour of a method that it is inheriting from a base class.
- All classes inherit from a class called Object. The Object class defines common methods that every class must have, such as equals() and ToString(). Any class has the option to “override” or re-define how the equals() and ToString() methods work for that class.
- Explain the purpose and function of the ToString() method and why we sometimes want to change its default behaviour when we create classes
- The ToString() method is meant to provide a way to represent any given object as a String. This method is inherited from the Object class; the Object class, by default, returns the data type of the object, expressed as a String.
- A programmer may choose to override the default behaviour of the ToString() method in order to represent the internal state of the object (that is, the values stored in some select fields) as a String.
- Create simple class diagrams to represent classes
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
General Programming Concepts and Terms
- List three levels of scope
- Class scope - All the fields and methods of a class share a common class scope, and are accessible to any and all methods of a class.
- Local scope - All the variables and parameters of a method are accessible only to the body of the method; local variables and parameters are not accessible to other methods.
- Block scope - All the variables declared inside a block are only accessible to the code inside that block. A block of code is denoted by being enclosed by a set of curly braces - { }.
Topic D - Debugging and Testing
General Programming Concepts and Terms
- Explain the role of testing as it applies to software development
- Testing is a form of “quality assurance” in which steps are taken to verify that a program is behaving or working as intended.
- Testing has traditionally been treated as a “follow-up” activity that takes place after a program is written. Currently, other approaches, such as TDD (Test-Driven Development) place the “testing” activity at the start of the programming efforts, thereby integrating tests as a driving element in software development.
- Define and compare the terms “compile-time error” and “run-time error”
- A “compile-time error” is an error where a program does not “compile”. In other words, compile-time errors are due to code that does not follow the rules of the programming language.
- A “run-time error” is an error where a running program does not produce the desired result. In other words, run-time errors are due to logical errors in a computer program. Sometimes these errors are “fatal” (meaning that they result in “abnormal program termination” - the program “crashes”). Other times the errors are subtle; the subtle errors can be the most difficult to detect, particularly when relying on “manual” verification, such as found with ad-hoc test drivers.
- Define the term “test driver”
- A “test driver” is a driver (which is a program with a main method) whose sole purpose is to “test” the components of a program. Test drivers provide a simple “ad-hoc” form of testing. Test drivers are basically considered “throw-away” programs, as their usefulness does not extend beyond the tests.
- Create simple ad-hoc test drivers to test for run-time errors
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Define the term “TDD”
- TDD refers to “Test Driven Development”. Test Driven Development is an approach to programming that starts with creating “unit tests” that reflect the business and design requirements of a program, and then writing the classes and methods that will pass the unit tests. The idea here is to help drive the programmer toward code that “stays on focus” of the needs of and solutions for a given problem.
- Compare and contrast Test Drivers and Unit Tests
- Test Drivers are small programs with a main() that tests classes by creating objects, calling methods, and output results to the console.
- Unit Tests are small “units” (which are simply methods) that calls methods on objects and classes and “tests” if they work as intended by running “asserts”. Unit Tests display the results of the test in Red/Green/Yellow results, where Red ( ) represents run-time errors, Yellow ( ) represents ignored tests, and Green ( ) represents success.
- Define the terms “false positive” and “false negative”
- A “false positive” is a test result that shows success for a test which in reality should be a failure.
- A “false negative” is a test result that shows failure for a test which in reality should be a success.
- Both false positives and false negatives are a result of an error in the unit test (or test driver).
- List three downfalls of using Test Drivers
- Can grow in size and become bulky & difficult to use (poor scalability)
- Requires considerable effort to generate and distinguish tests
- Difficult to verify tests due to heavy manual intervention & verification
- Identify four benefits of using Unit Tests
- Provides simple tests to verify specific parts of a system
- Easy to verify tests (minimal manual effort); errors are clearly identified and described
A clear graphical response it provided, leading to the popular development mantra “Keep it Green” - Increases productivity (by focusing development efforts)
- Captures design requirements in code
- Increases programmer confidence when modifying and expanding code
- Easily isolate smaller parts (“units”) of the program for verification; these tests can run independently of each other (even if the other tests have compiler issues with the classes being tested).
- Add unit testing libraries and unit testing packages to existing programs
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Use unit tests to validate the requirements of a class’ design and behaviour
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Diagnose and correct software problems by using unit tests
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
## Topic E - Expressions and Math
OOP Basics
- Explain how method overloading is applied to constructors
- Using overloaded constructors means that it is possible to create objects in “different ways”. What this means is that when a class has an overloaded constructor, anytime we wish to instantiate (create) an object using the new keyword, we must be explicit about which constructor we are invoking (calling).
General Programming Concepts and Terms
- Name at least three kinds of program statements commonly seen in all high-level programming languages
- Declaration/initialization statements
- Assignment statements
- Flow-control statements
- Distinguish between Variables, Values and Data Types
- Variables act as “containers” for values, allowing programmers to access and manipulate the value via the variable.
- A Value is simply a piece of data. It may be simple (such as a number or some text), or it may be complex (that is, based on a class).
- Data Types are “blueprints” or “templates” that describe what values “look like” and how they “work”.
- When a variable is declared to be of a certain data type, this means that the variable is only allowed to hold values that match that data type.
- List the five basic rules governing variables, values and data types
- Any value stored in a variable must be of the same data type as the variable.
- Variables must be declared before they can be used.
- A variable’s data type can only be declared once.
- Each primitive variable can only hold one value at a given time.
- Each variable must have a distinct name.
- List the intrinsic (built-in) data types in C#
- Textual data types: char
- Numeric data types: int, long, short, byte, double, float
- Conceptual data types: bool
- Explain what is meant by the phrase “type extensibility”
- Modern languages are able to define brand new data types (classes), thereby increasing the ability to manage complex information and perform complex operations.
- Define the terms Reference Type and Value Type as they apply to data types in C#
- A “value type” is a data type whose information is stored “directly” in a variable. In memory, this means that there is only one direct location in memory where the data is stored. The only data types that are “value types” are those that are built into the language: char, int, byte, short, long, double, float, and bool.
For example, in the following statements,
double count;
count = 0;
the value of zero is stored directly at the memory location labelled count. - A “reference type” is a data type whose information is stored and accessible only “indirectly” through the variable. Whenever you declare a “reference type” variable, that variable actually stores a memory address of where the “actual” object is stored. The “actual” object is only created using the new keyword; that new keyword will set aside a separate, “new”, area of memory which is then accessed “indirectly” through the “reference type” variable.
For example, in the following statements,
Car hotrod;
hotrod = new Car();
the first line creates a single memory location, labelled hotrod, which will store the address to a Car object. The second line creates the actual Car object somewhere in memory and then the address to that object is stored in the hotrod variable.
All classes, whether those supplied in the C# library (such as the String and Date classes) or those created by you, are “reference types”. - Define the term Identifier
- An identifier is the term used to refer to some “named” item in your code. Typically, we apply the term identifier to variable names, field names, property names and method names.
- Create and initialize variables in C#
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Explain what is meant by the terms “scope” and “lifetime” as they apply to variables
- Scope refers to the “context” in which a variable is accessible. Fields are variables with “Class Scope”, meaning that they are accessible to all the methods from within the class; properties, methods and constructors also have class scope. Parameters and variables declared within a method have “local scope”, meaning that they can only be accessed by code from within the method that they are declared in.
- Lifetime refers to the period of time from when a variable is first declared to the time that they are “destroyed”. For fields, their lifetime exists as long as the object in which they are declared. For parameters and local variables, they exist only as long as there is still code to be executed in the method to which they belong; as soon as the method “returns” or exits, these local variables and parameters are destroyed.
- Describe what is meant by the term “expression”
- An expression is any combination of literal values (literal strings or numbers), variables, operators, or method calls which return a value. An expression can be as simple as a single variable or literal value, or it can be as complex as a long mathematical expression that involves calls to various methods.
- An important characteristic of expressions is to note that they are always reduced to a single value by the computer.
- Explain the difference between program statements and expressions as well as how the two relate to each other
- A program statement represents a complete instruction that the compiler can translate into machine language. An analogy can be made that a program statement is like a sentence: Just as a sentence communicates a complete thought or idea, so a program statement represents a complete instruction.
- An expression is not a complete instruction; rather it is a combination of values, variables, method calls, and operators that will eventually be reduced to a single “value” by the computer. Expressions are often a part of program statements, and their role in program statements is akin to the role of phrases in English sentences: A phrase is not, by itself, a complete thought or idea; it must exist in a context in order to have its complete meaning. In a similar way, expressions play a part in the grammar of various kinds of program statements, providing a way of generating values that will be used by the program statement.
- List the four general sets of operators in C#
- Assignment, Arithmetic, Relational, and Logical
- Describe the two basic rules of using arithmetic operators that every compiler must follow.
- For an arithmetic operator to work, both sides of the operator must be the same data type.
- The resulting value of an arithmetic operation is of the same data type as the operands involved in the operation.
- Use the arithmetic operators to create arithmetic expressions
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Explain what is meant by “integer division” and how that can be useful in solving particular problems regarding numbers.
- “Integer division” is the term for what occurs when both operands of a division operation are whole numbers (which can apply to byte, int, short, and long data types). Because the basic rule of arithmetic operations is that the data type of the final value of an operation is the same as that of the operands, any time we divide one whole number by another whole number, the result is a whole number: Any “fractional portion” is automatically discarded.
For example,1 / 3 -> 0
7 / 2 -> 3
- Explain the purpose of the modulus (%) operator.
- The modulus operator (%) is used to find the remainder of a division operation. This operator is typically used with integers.
- List and describe how to use the various assignment operators in C#
- All assignment operators require a variable on the left side and an expression on the right side. Assignment operators take the final value of the expression on the right and stores that value in the variable on the left.
- =
This is the standard assignment operator. It performs a simple assignment of the value on the right to the variable on the left. - +=
This assignment operator takes the value on the right and adds it to the value already existing in the variable on the left. For examplex += y;
is the same as writingx = x + y;
- -=
This assignment operator takes the value on the right and subtracts it from the value already existing in the variable on the left. For examplex -= y;
is the same as writingx = x - y;
- *=
This assignment operator takes the value on the right and multiplies it to the value already existing in the variable on the left. For examplex *= y;
is the same as writingx = x * y;
- /=
This assignment operator takes the value on the right and divides it into the value already existing in the variable on the left. For examplex /= y;
is the same as writingx = x / y;
- %/
This assignment operator takes the value on the right and divides it into the value already existing in the variable on the left to generate the remainder. For examplex %= y;
is the same as writingx = x % y;
- Explain the difference between binary and unary operators
- Binary operators require a variable or value on both sides of the operator.
- Unary operators only require a variable or value on one side of the operator for it to perform its operation.
- Demonstrate understanding of operator precedence and how to override the default precedence of the arithmetic operators
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Summarize and distinguish the rules for automatic type conversion involving arithmetic operators and the assignment operator
- Automatic type conversion takes place for the data types that are built in to the programming language.
- The basic rule of automatic type conversion involving arithmetic operators is that if two operands of a binary operator are of different data types, then the value of the one that is “smaller” is “upsized” to the data type of the “larger” operand.
- For the assignment operators, automatic type conversion is limited to only allowing the right-hand value to be converted to the data type of the variable on the left-hand side. Type conversion cannot convert a value from the right hand side if that value’s data type is larger than the data type of the left hand side; if the right hand side is a larger data type than the left hand side, then a compiler error is generated that states the value cannot “fit” into the variable on the left.
- Determine the final data type for expressions involving mixed data types and type conversion
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Describe “type casting” and explain how to use it appropriately.
- Type casting is where we explicitly tell the compiler to “re-interpret” a particular value as a different data type than what the compiler is currently treating it as.
- Type casting is appropriate when we want to avoid integer division in order to retain the fractional portion in the final result. For example, if the variables numerator and denominator are both integers, it is appropriate to use type casting in the following line of code:
double value = (double) (numerator) / denominator;
- It is also appropriate when we need to “downcast” a value from a larger data type to a smaller data type (and can do so safely).
- Note that type casting does not (and can not) change the data type of a variable; type casting is only a re-interpretation of a value.
- Note that type casting is not the same as converting. For example, any attempt to type cast the char value of the character ‘4’ to an int will not produce a numeric value of 4; rather, it will produce the numeric value of 52 (which is the underlying decimal value for the character ‘4’).
- Compare and contrast the prefix and postfix behaviour of the unary arithmetic operators
- …
- Identify and explain how to use the common Math library routines (for Power, Square Root, Absolute, random numbers, pi, trigonometry, and the various types of rounding)
- …
- Use Math rounding methods with arithmetic expressions to round real numbers to a specific precision
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Create random whole number values within a specific range
- …
- Use type casting appropriately in C#
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Create constants in C#
- Constants in C# are created using the keyword final and assigning the value to the constant at the time that it is declared. For example, to create a constant value for GST, you could code the following:
const double GST = 0.05; // 5% GST
- Explain how arithmetic operations affect character values in C#
- Because the char data type is regarded as one of the “integral” data types, along with int, short and long, any arithmetic operations on char values will manipulate the underlying value as if it were simply a whole number.
(Note: “Integral” data types are data types without a fractional component.) - List the most common math functions of the Math class
- …
- Demonstrate how to use the various Math functions in simple programs
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- List and describe some of the commonly used fields and methods of the String class
- .ToUpper() - returns a String that has all the characters converted to their upper-case equivalents.
- .ToLower() - returns a String that has all the characters converted to their lower-case equivalents.
- .Trim() - returns a String that has all leading and trailing whitespace removed from the original String.
- .Chars(int index) - returns the character at the index position for the string. (All strings have the first character as index position zero.)
- .Substring(int beginIndex) and .Substring(int beginIndex, int endIndex) - returns a string that is the text inside of the first string beginning at a specified index. (All strings have the first character as index position zero.)
- .Length - returns the total number of characters in the string.
- Demonstrate how to use “String Arithmetic” (concatenation)
- To concatenate two or more strings, use the plus (+) operator.
- To append one string onto another, simply add the string to be appended to the end of the original string. For example, to append “ing” to the word “end” in order to produce “ending”, you could do the following:
String myWord = "end";
myWord = myWord + "ing"
- To pre-pend one string on another, add the string to be prepended to the start of the original string. For example, to prepend “un” to the word “imaginative” so as to produce “unimaginative”, you would do the following:
String myWord = "imaginative";
myWord = "un" + myWord;
- List the equivalent “wrapper” classes for each of the primitive types.
-
Integer
forint
-
Character
forchar
-
Double
fordouble
-
Byte
forbyte
-
Long
forlong
-
Short
forshort
-
Float
forfloat
## Topic F - If-Else Structures
General Programming Concepts and Terms
- Describe and draw a diagram of the If-Then-Else logical structure
- …
- Identify the programming statement that corresponds to the If-Then-Else logical structure.
- The if-else statement is used for If-Then-Else logical structures in C#.
- Translate If-Then-Else structures into code.
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Describe what is meant by a “conditional expression”
- A conditional expression is any expression which results in a Boolean (true/false) value.
- List the operator precedence for mixing logical, arithmetic, and relational operations
- Logical Not (!), Arithmetic, Relational, Logical And/Or
- List the relational operators
-
, >=, ==, <=, <
- Greater Than (>)
- Greater Than or Equal To (>=)
- Is Equal To (==)
- Less Than or Equal To (<=)
- Less Than (<)
- List the logical operators
-
&&, , ! - And (&&)
-
Or ( ) - Not (!)
- Use relational, logical, and arithmetic operators to construct conditional expressions
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Demonstrate an understanding of operator precedence in conditional expressions
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Use statement blocks to allow nesting program statements into control structures
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Define the term “boundary condition” as it relates to if-else statements and testing
- A “boundary condition” is also known as a “fence-post condition”.
- The “boundary” is the border of a range of values that are regarded as “acceptable” for some variable. For example, if a particular variable named percent is to represent a range of values from zero through 100 inclusive, then the “boundary” values are zero and 100.
A boundary condition, then, is a test or comparison that is made against some boundary values. For example,
- Identify the correct way to compare or check the contents of strings in conditional expressions
- The proper way to compare String values is to use the .Equals(), .CompareTo() and .IsNullOrEmpty() methods.
- Generally, the relational operators (==, <, etc) should not be used directly on string values.
- List and describe the commonly used fields and methods of the String class that would be used in if-else statements
- .Equals(String) - returns a Boolean, true or false, as to whether the two strings are or are not equal.
- .Equals(String, StringComparison) - returns a Boolean, true or false, as to whether the two strings are or are not equal, according to the value of the StringComparison argument (which includes values to ignore any differences in upper and lower case).
- .CompareTo(String anotherString) - compares two strings “lexographically” (that is, alphabetically) and returns either
a. a zero if they are the same, or
b. a negative number if anotherString is greater than the first string, or
c. a positive number if anotherString is less than the first string - .IsNullOrEmpty(string) - returns true if the string is an empty string (“”), false if it is not an empty string.
- .IsNullOrWhiteSpace(string) - (only in .NET 4.0 and higher) returns true if the string is null, an empty string (“”), or all “whitespace” (tab, newline) characters; false if it is not.
- .Length - returns the total number of characters in the string.
Topic G - Raising Exceptions
General Programming Concepts and Terms
- Describe how object-oriented programs run.
- A computer program is a set of instructions for manipulating information.
- Every computer program has a single starting (or entry) point called the Main method. The main method is responsible to “run” (or “execute) the program, and when the main method exits, the program closes and stops “running”. The length of time that a program is running is called its “lifetime”. As a program runs, it executes instructions in the program’s code, one at a time; this is called the “flow of execution”.
- During the lifetime of a program, the main method creates objects and calls methods on those objects. Each method is basically a set of instructions to be executed. Those methods can, in turn, create other objects and call other methods.
- Whenever a method is called, the operating system keeps track of which method is calling another method through something known as a “call stack”.
- Describe what is meant by a “call stack”.
- A call stack is a mechanism to track which method is calling another method while the program is running. Call stacks allow the computer to know where to “return to” when a method exits.
- At any given point during the program’s execution, the method that is currently executing is referred to as the “called method”. When the called method exits, it returns control back to the “calling method”, thereby allowing the calling method to continue its own execution.
- Whenever a method calls another method, the “calling method” pauses and waits for the “called method” to finish; once the called method returns, the calling method will resume executing its own code.
- Define the term “Exception”
- An exception is a message of some run-time error that has occurred in a program. Exceptions interrupt the normal flow of execution in methods, forcing the method to immediately exit. Methods generate exceptions by using the “throw” statement. Any method may generate an exception (including constructors).
- The throw statement exits the method by sending back an object (called an “Exception Object”, or simply an “Exception”) that can be used to identify the kind of error that has occurred. The calling method then has the opportunity to deal with (or “handle”) the error; if the calling method does not handle the error, then the exception continues to go back through the call stack until it finds a method that will handle the exception.
- If none of the methods on the call stack handle the exception that was thrown, control is eventually returned to the main method. If the main method does not handle the exception, then the exception is sent to the operating system. At that point, the operating system shuts down the program in what is called an “abnormal program termination”.
- Describe what is meant by the phrase “raising (or throwing) an exception”
- To “raise an exception” (or “throw an exception”) is to force a method to exit prematurely by sending back an “Exception object”.
- Describe the role that exceptions play in a computer program
- An exception is an interruption in the normal flow of execution in a method; exceptions force the method to immediately exit.
- Exceptions provide a way for methods (including constructors) to report errors to whatever code that is calling the method. These errors are usually (but not always) due to invalid information passed into the methods via the parameters. Through the use of exceptions, methods can perform validation on their parameters and report any errors that might prevent the method from being able to perform its task.
- Identify the three most commonly used Exception types used in this course.
- .NET Framework
ArgumentException - Typically thrown whenever an argument passed to a parameter does not have an “acceptable” value.
For example, if a method has a parameter called “percent”, it might perform validation to ensure that the value is not less than zero or greater than 100. If the value is not valid, it may be appropriate to throw an ArgumentException.
ArgumentNullException - Typically thrown whenever an object is checked for and found to be “null”.
(ArgumentNullException will be discussed in the topic on Enumerations and Composition.)
*Exception** - This represents a general exception, and can be used whenever there is no other suitable exception type.
OOP Basics
- Explain why methods (including constructors) should perform validation of their parameter values
- One of the principles of object-oriented design is that objects must be responsible for ensuring that their fields have correct values. This is known as ensuring a “valid state” for an object.
- Typically, the values that are stored in fields come in through the parameters of a constructor or a method. Because objects are responsible for ensuring a valid state, the class’ methods must check all parameters that might allow “invalid” data to be passed in.
- Explain the significance of raising an exception in the constructor
- If an exception is thrown from a constructor (or passes through it on its way up the call stack), the object’s construction is interrupted. Because the construction of that object is interrupted, the object does not get instantiated.
- Use exceptions as part of the validation of parameter values in methods and constructors.
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Explain why property setters are a good place for throwing exceptions.
- Generally speaking, the only method that should directly modify a field is the property setter associated with the field. Because of this principle, the property setter is the ideal place to perform validation and to throw exceptions. When validation takes place here, it eliminates the need for duplicating the validation elsewhere in the class.
- Validating input and throwing exceptions from within setters is a good way to follow the DRY principle: “Don’t Repeat Yourself”.
- Identify when constructors should throw exceptions directly.
- Whenever a field has a property setter that performs validation, the constructor should use that setter to store information in the field. If no property setter is available to process the parameter values sent through the constructor, then the constructor should perform its own validation on those parameters.
Topic H - Case Structures
General Programming Concepts and Terms
- Describe the syntax of the switch statement
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
- Discuss the limitations of the switch statement in C# and the available alternatives for the times that we can’t use a switch statement
- Switch statements only work with integral data types (such as char and int).
- The individual case values must be constants; variable expressions are not allowed for the matching cases.
- Switch statements only check for exact matches (meaning that there is an implied “is-equal-to” comparison); other relational comparisons, such as greater than or less than, are not allowed.
- When it is not possible to use switch statements, case structures can be written up as nested and/or stacked if-else structures to make use of the if-else statement.
Topic I Enumerations and Composition
General Programming Concepts and Terms
- Define the term Enumeration
- An enumeration is a simple program-defined data type with a limited set of possible values. Enumerations represent information that is “conceptual” in nature (as opposed to information that is textual, numeric, or complex).
For example, a coin has two sides that are commonly referred to as HEADS and TAILS. Rather than creating two constant strings with the values “Heads” and “Tails” and a String variable called FaceShowing, you can use an enumeration. - There are two parts to enumerations:
a. The name of the enumeration, which is the name of the new data type; this is sometimes referred to as the “enumerated data type”.
b. A set of enumerated values, which represent all of the possible values for a variable of that enumerated type. These values act like “constants” that can be used to distinguish different “states” for the conceptual data. - List the benefits of using enumerations
- Type Safety - Enumerations allow a program to be “type safe”, meaning that there is less likelihood of creating logical errors or other mistakes through the use of constants or literal values.
- Code Readability - Enumerations make code more readable than constants or literal values (like integers).
- Compatibility with Switch Statements - Enumerations are compatible with switch statements, making them a great option for describing distinct values for matching against different cases. Because enumerations are like constants, and are treated as integral data types by the compiler, they are often the best choice when working with case structures.
-
Useful as Flags - Because the programmer can associate specific integer values to the enumerated values, it is possible to use an enumerated data type to create complex “flags” for representing distinct “states” that can be combined together.
For example, if you had an enumerated type such as the following, you could combine two states together in a variable’s value to represent distinct but compatible states, such asACTIVE
andMEDIUM_PRIORITY
.
- Describe where and when enumerations are used
- Enumerations are used wherever a clearly defined set of values can be “classified” and used to represent conceptual information.
- Enumerations should be used whenever you might be inclined to use a set of two or more constants to represent distinct “states” or values for comparing against a variable.
For example, if you want to represent the two sides of a coin, it is better to use an enumeration for the coin’s HEADS and TAILS sides, rather than declaring some String or integer constants. The problem with using strings or simple integers to represent possible coin faces is that any variable that is defined as a string or integer can have values that won’t match either of the declared constants, as in this example:
An enumerated type called
CoinFace
with the valuesHEADS
andTAILS
is better, because any variable declared of typeCoinFace
can only have the values defined in the enumeration.```csharp
// The only other possible value that could be assigned is
// CoinFace.TAILS
CoinFace theFaceShowingOnTheCoin = CoinFace.HEADS;
`` - Compare and contrast enumerations and classes
- Enumerations and classes both define new data types that can be used by our program to represent custom data. However, while classes are useful in describing complex data types (that is, data types with assorted fields and that can perform various tasks), enumerations are only used to create new data types that are simple in nature. Enumerations only define a set of possible values, and each variable defined by an enumeration can only hold one of those possible values at any given time; there is no inner complexity to enumerations, as there is with classes.
- Use enumerations to define simple data types that are conceptual in nature and that have a limited set of possible “values”
- Demonstrate your ability to meet this learning outcome guide by completing the related assignments.
OOP Basics
- Describe the idea of Composition
- Composition refers to the situation where one or more of the fields in a class are objects. All classes are capable of having objects as fields, rather than just using the simple primitive types that come with the language.
For example, if a class called Address represents some mailing address, another class, such as Student, Company, School, or Business, could have a field whose data type is Address. - List the benefits of Composition
- Composition is an aspect of modern programming languages that makes it possible to create new data types (classes) of ever increasing complexity.
- Composition also makes it possible to get better code re-use, because classes can be more distinct, rather than overlapping. For example, both Students and Employees can have Addresses, so it is not necessary to code individual fields in the Student and Employee class for each part of an address; rather, a common type (called Address) can be shared by both classes.
These could be coded like this:
: advanced benefit - Composition promotes the “Has-A” approach to designing objects rather than the “Is-A” design approach that makes use of inheritance. This “Has-A” approach gives programmers more flexibility in their coding and promotes the use of Design Patterns.
Topic J - Looping Structures
General Programming Concepts and Terms
- Identify the C# statements that correspond to Do-While and Do-Until logical structures
- TBA
- Translate Do-While structures into code
- TBA
- Translate Do-Until structures into code
- TBA
- Identify and distinguish the parts of the for statement in code
- TBA
- Describe the common situations in which the for statement is used
- TBA
- Demonstrate how the various looping statements can be interchanged with slight alterations of the logical structures to maintain the overall logic of a given routine.
- TBA
Topic K - Looping and Collections
OOP Basics
- Define the term Generics as used in C# and give an example
- TBA
General Programming Concepts and Terms
- Describe what is meant by a “collection” class and give an example
- TBA
- List and describe the common methods of collection classes such as the List<T>
- TBA
- Identify the parts of the foreach statement
- TBA
- Describe the common situations in which the foreach statement is typically used
- TBA
- Identify the major benefit of using Generics
- Type Safety –
- List the major benefits of using collections instead of arrays
- Collections such as List<T> can grow in capacity whenever items are added
- Collections such as List<T> do not need special management of the boundaries (such as the index of the upper limit of an array)
- Collections provide simple methods for quick adding and removing of “elements”
Topic L - Arrays - Not Sorted
General Programming Concepts and Terms
- Describe what makes an array different from other collections in C#
- The programmer is responsible to manage the boundaries of the array when manipulating the array’s contents.
- Arrays do not automatically “re-size” when “adding” or “removing” elements; the size of the array is fixed at the time the array is created.
- Describe the situations in which arrays are preferable over the use of collections
- Arrays are preferred whenever multi-dimensional data is required (such as 2D, 3D, or n-dimensional data is needed)
- Arrays are sometimes preferred whenever the size (number of elements) is unlikely to change and/or whenever the array is based on a primitive data type or an enumeration.
Notes
Thinking In OOP
What is a Computer Program
1The rule that “once a variable’s data type has been declared, its data type cannot be changed” is true for those programming languages which are described as type-safe, such as C#, Java and VB.Net. Other modern languages, such as JavaScript, do not have this rule.↩