Advanced Programming - Part 1
Advanced Programming - Part 1
Advanced Programming
Spring 2025
Gebze Technical University
Department of Aerospace Engineering
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Who am I?
• Current Position: Assistant Professor at Gebze Technical University
• Work experience:
• Over 15 years research and industrial experiences
• Four years in academia
• Education:
• BSc in Aerospace Engineering from Sharif University of Technology – 1999
• MSc in Aerospace Engineering from Sharif University of Technology - 2001
• PhD in Aerospace Engineering from Sharif University of Technology – 2017
• Fields of Interest:
• Flight Dynamics and Control
• Guidance, Navigation and Control
• Design and Optimization
• System Concepts and System Engineering
• Space Robotics
• System Identification
• Room: Aerospace Dept. – Room 212
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Course Syllabus
Week 1 Introduction, Applications, Programming Necessity, Programming Languages, IDE
Week 2 Project Structure, Data Types, Concept of Object
Week 3 C# Programming Fundamental topics: Arrays, Loops, Conditionals, Debugging Techniques
Week 4 C# Programming advanced topics: Exception Handling, Methods
Week 5 C# Programming advanced topics: Passing by reference, Method overloading, Method Delegating
Week 6 Streams, File Handling, Introductory topics on using the database
Week 7 Example of application of computer programming techniques in a complicated aerospace problem
Week 8 Midterm Exam
Week 9 Programming paradigms, Classes and Objects, Lists and Constructors
Week 10 OOP concepts – Dynamic Link Libraries, Encapsulations
Week 11 OOP concepts – Inheritance
Week 12 OOP concepts – Abstraction, Polymorphism
Week 13 Example of application of OOP techniques in a complicated aerospace problem
Week 14 Introduction to Windows Form Application Development
Week 15 ----
Week 16 Final Exam
Textbook Learning Object-Oriented Programming in C# 5.0, BM Harwani
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Evaluation Method
The initial assessment plan – It be finalized during the course
depending on how course is going on
• Assignments: 0 / 100 (Optional)
• Midterm: 50 / 100
• Project: 0/100 (Optional)
• Final: 50/100
IMPORTANT NOTE: The direct score for assignments and the project is
0 (as stated above). However, you will receive little to no score from
the midterm and final exams if you do not complete them. While they
may seem optional, you must complete them to pass.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Rules
• Attendance is mandatory as per GTU rules (up to 3 absences are acceptable). If you have any excuses, such as a health problem,
please present them to the department secretary. If approved, the absence will not be considered in the assessment.
• The class consists of two parts: Theory (2 hours) and Laboratory (2 hours). Theory sessions will follow the
planned schedule. In the laboratory, you will write code on a computer according to the schedule provided in each
class.
• For the laboratory, you will be divided into groups, each working under the supervision of a TA. The laboratory
sessions will be online by default after class, but the timing can be adjusted based on mutual agreement.
• The midterm exam is scheduled for the 8th session (April 19, 2025) during the class time.
• The final exam is scheduled to be in the first week of the exams during the class time
• The minimum required score in the mid-term exam is 30 (Below this grade your grade will be VF)
• Exclusively utilize C# as the programming language and Visual Studio 2022 as the IDE in this course.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Conversion scales
• The conversion of numeric scores to alphabetical grades normally will
be carried out according to the scales provided on the GTU website.
Numeric Range Score
[0-39) FF 0.0
[39-53) DD 1.0
[53-63) DC 1.5
[63-68) CC 2.0
[68-78) CB 2.5
[78-83) BB 3.0
[83-89) BA 3.5
[89-100) AA 4.0
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
• Throughout the term, a class example focusing on rocket simulation will be solved step
by step.
• As part of your assignments, you are asked to apply the same steps demonstrated in the
example to your project.
• Projects and assignments are for practice only (they are not graded). However, it is
highly recommended to complete them if you want to pass this course.
Manager Software's
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
• 3D Modeling
• Flight Analysis
• Sizing Modeling • Aerodynamic • User Interface
Rough Analysis Impleme
Analysis • Timing And • Hydrodynamic ntation
• Control and
Management
• …. Analysis Analysis
• Underwater
Movement Analysis
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Mechanical /
Available Software Engineers
Aerospace
Software's / Other colleagues
Engineer
Product
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Analytical Approaches
Formulation
Numerical Approaches
Evaluation
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Mechanical / Aerospace
Engineer Code Improvement /
Creating GUI
Software Engineer
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Introduction – Applications?
Applications – Physical Perspective
(Not comprehensive just to make the subject clear)
Computer
Programming Software and Codes
Hardware
Microprocessors Design Analysis
Development
and
Microcontrollers Kits AutoCad NASTRAN
Arduino
ARM Arduino
CATIA FLUENT
AVR Raspberry Pi
Siemens NX ABAQUS
Intel BeagleBone Black
Developed Codes Developed Codes
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Introduction – Applications?
Applications – Functional Perspective
(Not comprehensive just to make the subject clear)
Introduction – Applications?
Applications – Hardware Development Kit
The Hardware Development Kit is a collection of hardware design resources that
have been gathered to assist in the development of custom hardware
Arduino Uno R3 SMD CH340 Chipset Klon (Usb Kablo Dahil) | SAMM Market
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Introduction – Applications?
Applications – Intelligent Systems
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Introduction – Applications?
Applications – Software Development
Low-level languages are used to write programs that relate to the specific architecture and
hardware of a particular type of computer. They are closer to the native language of a
computer (binary)
They are mostly used to develop operating systems, device drivers, databases and
applications that require direct hardware access
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Introduction – Computer Programming
Languages
Low Level Languages High Level Languages
Compiled
Human brain Computer interface
Language
Introduction – IDE
Note: In advanced programming projects it may be wanted to develop a huge software
Examples
Are not necessarily accurate – Just to make sense
Introduction – IDE
These software's are developed by a team of engineers over a long period of time
Advanced programming tools are developed to support the projects done by a team
Even for simple works these tools define a project by default with a predefined structure
For complex projects the structure will be changed by the development team
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Introduction – IDE
IDE : Integrated Development Environment
Code
Debugger
editor
Integrated development environments (IDE) are
applications that facilitates the development of other Class
Compiler hierarchy
applications. diagram
IDE
Build Class
automation browser
tools
Object
browser
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Introduction – IDE
• Support multiple programming languages
Multi-language IDE • Visual Studio, Visual Studio Code
Introduction – IDE
In This course we use Visual Studio 2022 IDE
Advantages:
• It is very powerful in developing, editing and compiling of codes
• It supports Multi-Language Programming
• It has many plug-ins which enables it to be used for other applications (e.g.
Arduino Programming)
• It is free
Disadvantages:
• It requires lots of space
• It is relatively slow
• It may make you dependent on using Windows as the Operating System
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Homework
Install Visual Studio 2022 on your computer
Search about FORTRAN, MATLAB, C#, Python, Java and C++ and
compare them to each other considering the concepts presented in this
session. (1-2 pages)
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Is a computer intelligent ?
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Process / Instruction
• In flowcharts, those steps are usually displayed
in shapes and process boxes with arrows.
Decision
Connector / Arrow
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
• Step 1: Initialize X as 0,
• Step 2: Increment X by 1,
• Step 3: Print X,
• Step 4: If X is less than 20 then go back to step 2.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
• Step 1: Input n
• Step 2: Initialize X as 1 and R as 1,
• Step 3: Increment X by 1,
• Step 4: R = R * X
• Step 5: If X is less than n+1 then go back to step 2.
• Step 6: Print R
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Algorithm:
Metal workshop
Application
Types
Mobile Web
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
3- Look to the ”Create New Project” 1st page Window more in deep
4- Select C# and find “Console App (.NET Framework)” then press “Next”
5- Select the name of your project and its location and press “Next”
Obj
HelloWorld.exe
Properties
Debug HelloWorld.pdb
Bin
HelloWorld Release
HelloWorld App.config
Helloworld.csproj
HelloWorld.sln
Program.cs
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Console.WriteLine(text);
or
Console.Write (text);
The first form writes something and then goes to the next line while the second one just writes
the text without going to the next line
Data Types
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
DataTypes
0 1 1 1 0 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 1 0 1
0 0 1 1 0 1 1 0 1 0 0 1 1 0 1 0 1 1 1 1 1 0 0 1 1 0 1 0 1 0 0 0 1 0 0
Memory
Hardware – bits - 0 and 1
Software (Program)– Addresses
Data Can hold different values at
Constants do not change value different points in time.
over the lifetime of a program
Constant Variable
Data Types
Pre-defined Data Types
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Data Types
Pre-defined Data Types
Commonly used data types related to the numbers
Type Description Range Size
Int Signed integer -2,147,483,648 to 2,147,483,647 4
Uint Unsigned integer 0 to 4,294,967,295 4
Float Floating point -3.4E38 to 3.4E38 4
Double Double precision floating point -1.8E308 to 1.8E308 8
Decimal Fixed precision number -7.9E28 to 7.9E28 16
Data Types
A decimal variable
contains a floating decimal (0-9) point type.
• The precision of the decimal number is higher than that of float and double,
• Much slower than float and double.
• In financial applications, mainly due to its high precision and to minimize rounding errors.
A float variable
• Contains a floating binary (0-1) point type.
• Use in graphics libraries mainly due to the high demand for processing power and where
preventing rounding errors is not very important
A double variable
• Contains a floating binary point (0-1) type but has double precision.
• It is the data type that is mainly used for real values, but it is not recommended in situations
in which we require high precision and accuracy
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Data Types
Numerical and logical variables definition
In C#, every variable should be defined first then its value can be determined and then
operations can be done over the variable.
int a = -10;
uint b = 5;
float c = 103; or float c = 103.0f;
decimal d = 145; or decimal = 145.7m;
double e = 231.47; or 2.56E5;
bool f = true; or f = false;
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Data Types
String and char variables
Character (char) is a predefined data type in C# but string is not. However, you can easily
define both types of variables in C#.
For defining a character use the following format. Notice that we are using single quotation
mark for character definition. The character should contain only one character.
Example: char c = ‘a’;
String is actually an array of characters. It can be defined as follows. Pay attention to double
quotation mark.
Example: string s = “My name is Yaser”;
You can read a string from the Console using following command:
string s = Console.ReadLine();
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Data Types
Name of variables – in C#
• It is recommended to use the first letter of variable name be in lowercase and if you are
attaching more words to variable name then start them with uppercase
camelCase bookTitle petName
• It is better that the name not be too short. For example if a variables relates to the
temperature we can use both of these names but temp is better
t VS temp
• It is better that the name be clear not vague. For previous example both of these names
can be assign but temp can stands for both temporal and temperature
temp vs Temperature
• The similar variables may be used so make the name as specific as possible.
roomTemp vs outTemp
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Main Example
Course Main Example: Receive Rocket Data
𝑑2 𝑥
𝑀𝑎𝑥 = 𝑀 2 = (𝑇 − 𝐷) cos 𝜃
𝑑𝑡
𝑑2 𝑦
𝑀𝑎𝑦 = 𝑀 2 = 𝑇 − 𝐷 sin 𝜃 − 𝑀𝑔
𝑑𝑡
𝑑𝑉𝑥
𝑎𝑥 =
𝑑𝑡
𝑑𝑉𝑦
𝑎𝑦 =
𝑑𝑡
𝑉𝑦
𝜃 = atan
𝑉𝑥
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Main Example
Course Main Example: Receive Rocket Data
𝑑2 𝑥 Input
𝑀𝑎𝑥 = 𝑀 2 = (𝑇 − 𝐷) cos 𝜃 Description Initial Value
𝑑𝑡 Parameter
𝑑2 𝑦 𝑀 Mass of the full rocket (Kg): 20
𝑀𝑎𝑦 = 𝑀 2 = 𝑇 − 𝐷 sin 𝜃 − 𝑀𝑔
𝑑𝑡 𝑀𝑝 Mass of the propellant (Kg): 10
𝑑𝑉𝑥
𝑎𝑥 = 𝑉0 Initial Velocity of the Rocket (m/s) 25
𝑑𝑡
𝑑𝑉𝑦 𝜃0 Initial flight path of the rocket (Deg) 45
𝑎𝑦 = 𝑇 Average thrust of the rocket (N) 400
𝑑𝑡
𝑉𝑦 𝐶𝐷 Drag Coefficient : 0.3
𝜃 = atan
𝑉𝑥 𝑡𝑏 Burn time (Seconds): 3
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Data Types
Exercise:
Write a code that asks the user somethings about himself/herself and also about his/her pet
(like type, name, age and …) and then writes a sentence about the user.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Data Types
String operations
• Addition of two strings
• String s1 = “My Name is ”;
• String s2 = “Karl”;
• String s = s1 + s2;
• Access to substring and chars
• Char c1 = s1[4] Character M y N a m e i s
• String s3 = s1.Substring(6); index 0 1 2 3 4 5 6 7 8 9 10
• String s4 = s1.Insert(6, “here”);
• String s5 = s1.Remove();
• Information about string
• Int len = s1.Length();
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Homework
1- Write the following simple game:
1. Change the foreground and background color of the console.
2. Clear the console
3. Write a sentence (For example: “I love C# programming!!!”)
4. Wait for 5 seconds (You can use this command for waiting:
System.Threading.Thread.Sleep(5000); )
5. Ask the user to choose between four different integers (for example 2, 6, 8, 11). The selected
integer is the number of letter in the sentence that the user should remember. (For example
1 is I and 5 is v in the above sentence)
6. Ask the user to enter the selected letter.
7. Show the entered and true answers to the user
2- Use your own ideas and only the methods available in the Console and String to
make the game more attractive. You can even add your own rules to the game.
Project-Based Homework
1- For the satellite simulation project, study about satellite orbits (planar) and its parameters
- semi-major axis (s)
- eccentricity (s)
- anomaly (𝜃).
Also study about the meaning and purpose of satellite simulation. Write a 2 pages report for
your studies.
2- In your report add the general algorithm for satellite simulation and draw the flowchart.
2- Start your term project by writing the code that receives these parameters from the user as
variables
Object Concept
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Object Concept
Object-Oriented Programming is based on a concept called Object-Oriented thinking. So we
can look it as a Philosophy, a way that we can look to our world. Object-Oriented thinking is
defined as:
Way of thinking where we consider each component of a problem in
terms of object.
What is the meaning of an object?
Look your around :
- Cars - Animals
- Buildings - Factories
- Cell Phone - Stores
- Trees - Table
- Humans - Clothes
How do we discriminate between them?
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Object Concept
Object Concept
Object Concept
Example: What are you checking when you want to buy a television
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Object Concept
Object Concept
In programming we are using objects to separate unnecessary parts.
Object Concept
• The behavior of an object is what the object can do.
• In OO programming terminology these behaviors are contained in methods
• You invoke a method by sending a message to it
Data or
Behavior Object
Attributes
Object Concept
Exersice:
Assess the Console object in Visual Studio and write some of its Attributes and Methods
For seeing some behaviors and attributes of Console object it is enough to write Console in
Visual Studio IDE and put a dot (.) after. You will see a list of behaviors and attributes.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Object Concept
Example:
Console.WriteLine("Window height is: ");
Console.WriteLine(Console.WindowHeight);
Console.WindowHeight = 20;
Console.ReadLine();
Console.Clear();
string s1 = "Enter your name: ";
Console.Write(s1);
string p1 = Console.ReadLine();
Console.WriteLine("Your Name is :" + p1);
Console.ReadLine();
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Before continuing
Comments
Writing comments is a very important part in developing a code
Arrays
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Arrays
Arrays
One dimensional int array definition (first method)
int[] intArr = new int[4]; // Creates 4 dimensional integer array
intArr[0] = -1; intArr[1] = 3; intArr[2] = 12; intArr[3] = 7;
Exercise 3: Check some of the methods that can be applied to an integer array
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Arrays
Arrays
Two dimensional int array definition (first method)
int[,] intArr = new int[2,2]; // Creates 2*2 dimensional integer array
intArr[0,0] = -1; intArr[0,1] = 3;
intArr[1,0] = 12; intArr[1,1] = 7;
One dimensional int array definition (second method)
int[] intArr = new int[]{{-1, 3}, {12, 7}};
Exercise 1: Use the same approach to define an array of doubles
Exercise 2: Compare the length properties of one dimensional array with two
dimensional array. Check the GetLength method of the int array variable.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Arrays
Exercise
• Define two 3 dimensional vectors as arrays
Conditional Statements
If statement:
if (condition1)
{
if condition1 is true: Do this part
}
else
{
if condition1 is false : Do this part
}
Variables scope – if a parameter defined in blocks of if, it is accessible only in that block
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Conditional Statements
If statement:
if (condition1)
{
if condition1 is true: Do this part
}
else if (condition2)
{
if condition1 is false and condition 2 is true: Do this part
}
else
{
if condition1 and condition 2 are false : Do this part
}
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Conditional Statements
If statement:
C# supports the usual logical conditions from mathematics:
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• Equal to a == b
• Not Equal to: a != b
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Conditional Statements
Switch statement:
Switch (expression)
{
case x:
// Code block Variables scope – if a parameter defined in blocks of
break; switch, it is accessible only in that block
case y:
// Code block
break;
case z:
// Code block
break;
default:
// Code block
break;
}
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Loops
For statement:
for (loop variable initialization ; testing condition; increment / decrement)
{
// statements to be executed
}
Example: for (int i=100; i>=0; i--)
{
Console.Writeline(“Counter : ” + i);
}
Example: double X = 0;
double dx = 0.1;
Variables scope – if a parameter defined in the
for (int k=0; k<=1000; k++) block of For loop, it is accessible only in that
{ block
X = X + 3 * X * X * dx;
}
Console.Writeline(“Result is: ” + X);
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Loops
Exercise
• Define two vectors as arrays
Loops
While statement:
while (boolean condition) do
{ {
loop statements... statements..
} }while (condition);
Exercise
Read a special character from the Console
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Mathematical operations
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Mathematical Operations
Basic Operations
Increment and Decrement operators
Mathematical Operations
Basic Operations
Other basic operators
Operation Operator Operation Operator
Summation + Subtraction -
Multiplication * Division /
Remainder % Compound assignment X op= Y
int a = 5;
X op= Y example:
a += 9; Console.WriteLine(a); // output: 14
a -= 4; Console.WriteLine(a); // output: 10
a *= 2; Console.WriteLine(a); // output: 20
a /= 4; Console.WriteLine(a); // output: 5
a %= 3; Console.WriteLine(a); // output: 2
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Mathematical Operations
Basic Operations
Other basic operators Be careful about the type of numbers
In division operation:
If one of the operands is decimal another operand can be neither float nor double
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Mathematical Operations
Basic Operations Be careful about the type of numbers
Other basic operators
In remainder operation:
For the float and double operands, the result of z=x % y for the finite x and y is the
value z such that
• The sign of z, if non-zero, is the same as the sign of x.
• The absolute value of z is the value produced by |x| - n * |y| where n is the largest possible
integer that is less than or equal to |x| / |y| .
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Mathematical Operations
Precedence from the highest to the lowest
Postfix increment Prefix increment Multiplicative operators Additive operators
(x++ , x-- ) (++x , - -x ) (*, /, and % ) (+ and -)
1 2 3 4
Binary arithmetic operators are left-associative. That is, operators with the
same precedence level are evaluated from left to right.
Mathematical Operations
Logical and Conditional Operations
NOT Operator: ! bool passed = false;
Console.WriteLine(!passed); // output: True
Console.WriteLine(!true); // output: False
Mathematical Operations
Logical and Conditional Operations
Exclusive OR Operator: ^ Console.WriteLine(false & true); // Output: True
Console.WriteLine(true & true); // Output: False
Console.WriteLine(false & false); // Output: False
Console.WriteLine(true & false); // Output: True
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Mathematical Operations
Math Operations
There is library “Math” in C# that includes many mathematical functions
Mathematical Operations
Class Exercise
Euler equation:
𝑓 𝑥 + ∆𝑥 = 𝑓 𝑥 + 𝑓′(𝑥)∆𝑥
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Mathematical Operations
Class Exercise
Write a program that uses Euler integration method to calculate this
expression:
𝜋
𝑦 = න 𝑥sin(2𝑥)𝑑𝑥
0
Compare with the analytical result
𝑥 cos 2𝑥 sin 2𝑥
𝑦=− +
2 4
Calculate the following integral using Newton Integration Method
1
𝑒 −sin 𝑥
න 3 𝑑𝑥
𝑥 + cos(𝑥)
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Mathematical Operations
Creating Random Numbers in C#
To create random numbers you can use Random Object.
Random rand = new Random();
Console.WriteLine(rand.Next());
Console.WriteLine(rand.NextDouble());
Mathematical Operations
Creating Random Numbers in C#
To create random numbers you can use Random Object.
Random rand = new Random();
Console.WriteLine(rand.Next());
Console.WriteLine(rand.NextDouble());
Debugging techniques
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Debugging Techniques
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Debugging Techniques
Bugs
Input Errors
Logic/Formulation Errors
Implementation Errors
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Debugging Techniques
• Watch Windows
• The Watch Window allows you to see value of variables and
expressions while debugging
• Up to 4 Watch Windows can be opened
• You can access the Watch Windows through Debug/Windows/Watch
• To add items to watch do any of the following:
• Write them manually in a new row of the Watch window
• Right-click on a variable choose “Add Watch” in the context menu
• Right-click on a variable in the DataTip and choose “Add Watch”
• “Add Watch” button from QuickWatch
• Although you can see the variables value in DataTip of variable but if
you want to see some variables simultaneously you have to use
Watch Window
• Local Window
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Debugging Techniques
Breakpoint
Breakpoints are one of the most important debugging tools in your
developer's toolbox.
You can set breakpoints before debugging and even while debugging
To set a breakpoint
• Click in the far left margin next to a line of code
• Select the line and press F9
• Select Debug > Toggle Breakpoint
• Right-click and select Breakpoint > Insert breakpoint.
The breakpoint appears as a red dot in the left margin.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Debugging Techniques
• Line by Line Running
• During the debugging process you can go line by line both forward and
backward
• Step over (F10) – It runs just one line and goes forward. If the current line
calls a method, this action assumes the call as a line so it runs the whole
method without going inside the method
• Step into (F11) – It runs just one line and goes forward. If the current line calls
a method, this action going inside the method and let you to debug the inside
of method
• Step Backward (Alt+]) – It runs backward to the previous line.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Debugging Techniques
Breakpoint - Conditional
You can control when and where a breakpoint executes by setting
conditions.
To set a breakpoint condition:
1. Right-click the breakpoint symbol and select Conditions
2. hover over the breakpoint symbol, select the Settings icon, and then select Conditions in
the Breakpoint Settings window.
Breakpoint Management
You can use the Breakpoints window to see and manage all the breakpoints
in your solution.
• Debug > Windows > Breakpoints,
• Or press Ctrl+Alt+B.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Debugging Techniques
• Class Exercise
• Write a code to calculate the following integral
1 1
න න 𝑥 2 − 3𝑦 2 + 𝑥𝑦 3 𝑑𝑥𝑑𝑦
0 0
1 1 𝑥=1 𝑦=1 13
3 3 2 4
𝑥 𝑦−𝑦 𝑥+ 𝑥 𝑦 =− ≈ −0.5417
3 8 𝑥=0 𝑦=0 24
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Main Example
Course Main Example: Solve the equations of motion
𝑑2 𝑥
𝑀𝑎𝑥 = 𝑀 2 = (𝑇 − 𝐷) cos 𝜃
𝑑𝑡
𝑑2 𝑦
𝑀𝑎𝑦 = 𝑀 2 = 𝑇 − 𝐷 sin 𝜃 − 𝑀𝑔
𝑑𝑡
𝑑𝑉𝑥
𝑎𝑥 =
𝑑𝑡
𝑑𝑉𝑦
𝑎𝑦 =
𝑑𝑡
𝑉𝑦
𝜃 = atan
𝑉𝑥
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Main Example
Course Main Example: Receive Rocket Data
𝑑2 𝑥 Input
𝑀𝑎𝑥 = 𝑀 2 = (𝑇 − 𝐷) cos 𝜃 Description Initial Value
𝑑𝑡 Parameter
𝑑2 𝑦 𝑀 Mass of the full rocket (Kg): 20
𝑀𝑎𝑦 = 𝑀 2 = 𝑇 − 𝐷 sin 𝜃 − 𝑀𝑔
𝑑𝑡 𝑀𝑝 Mass of the propellant (Kg): 10
𝑑𝑉𝑥
𝑎𝑥 = 𝑉0 Initial Velocity of the Rocket (m/s) 25
𝑑𝑡
𝑑𝑉𝑦 𝜃0 Initial flight path of the rocket (Deg) 45
𝑎𝑦 = 𝑇 Average thrust of the rocket (N) 400
𝑑𝑡
𝑉𝑦 𝐶𝐷 Drag Coefficient : 0.3
𝜃 = atan
𝑉𝑥 𝑡𝑏 Burn time (Seconds): 3
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Today’s Outline
• Debugging techniques
• Exception handling in C#
• Methods
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Exception Handling
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Exception Handling
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Exception Handling
• Understanding and using exceptions is crucial to creating readable code that
responds correctly to errors at runtime.
• What is an Exception?
Exceptions
An exception is a problem that arises
during the execution of a program.
Interactions
Example exception:
• Division by Zero Executions
• Conversion Exception
• File Not Found Exception
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Exception Handling
C# exception handling is built upon four keywords:
• try − A try block identifies a block of code for which particular exceptions is
activated. It is followed by one or more catch blocks.
• catch − A program catches an exception with an exception handler at the
place in a program where you want to handle the problem. The catch
keyword indicates the catching of an exception.
• finally − The finally block is used to execute a given set of statements,
whether an exception is thrown or not thrown. For example, if you open a
file, it must be closed whether an exception is raised or not.
• throw − A program throws an exception when a problem shows up. This is
done using a throw keyword.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Exception Handling
try {
// statements causing exception
} catch( ExceptionName e1 ) {
// error handling code
} catch( ExceptionName e2 ) {
// error handling code
} catch( ExceptionName eN ) {
// error handling code
} finally {
// statements to be executed
}
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Debugging Techniques
• Class Exercise
• In the double integration exercise, ask user to enter the range of x and y for
calculation of
𝑦𝑓 𝑥𝑓
න න 𝑥 2 − 3𝑦 2 + 𝑥𝑦 3 𝑑𝑥𝑑𝑦
𝑦0 𝑥0
• Write the code such that when the user enters a wrong value asks him to
retry
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Methods
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Methods
• A method is a code block that contains a series of statements.
• Methods are used to perform certain actions, and they are also known
as functions.
• To reuse code:
• define the code once, and use it many times.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Methods
Variables
Method
Interface
Interface Local Variables
Inputs Outputs
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Passing by reference To pass a value-type instance by reference, use the ref keyword
When an object of a reference type is passed to a method, a reference to the object is passed.
That is, the method receives not the object itself but an argument that indicates the location of
the object.
If you change a member of the object by using this reference, the change is reflected
in the argument in the calling method, even if you pass the object by value.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
It is common to have method definitions that simply return immediately with the
result of an expression, or that have a single statement as the body of the method..
Example:
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Methods – Comments
Using // you can put ordinary comments in your method to make it reusable for
future works.
Using /* and */ you can convert some lines of the code to the comment.
Using /// before the start of the method you can put some comments about the
method, its arguments and return value. These comments will be shown to the user
of the method as a tipbox when he is using the method
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Enumeration
Enums
In C#, an enum (or enumeration type) is used to assign constant names to a
group of numeric integer values.
Example: enum WeekDays
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
Methods –Overloading
Method Overloading
With method overloading, multiple methods can have the same name with different
arguments number/type/sequence.
Two
numbers Two
Vectors
Add
Two
Matrices
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Methods –Overloading
Method Overloading
Exercise
Define two enums for doors and windows of the car (FrontLeft, FrontRight,
RearLeft, RearRight)
Use method overloading to define Open and Close methods for doors and windows.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Syntax:
[access modifier] delegate [return type] [delegate name]([parameters])
Example:
public delegate double Function2D(double x, double y);
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Project-Based Homework Y
D
Use the following equations to simulate the dynamics of a planar
y
satellite
𝑑𝑥 𝑑𝑦 𝑑𝑉𝑥 𝑑𝑉𝑦 V
= 𝑉𝑥 = 𝑉𝑦 = 𝑎𝑥 = 𝑎𝑦 W
𝑑𝑡 𝑑𝑡 𝑑𝑡 𝑑𝑡
Where x and y are related to the position of the satellite and 𝑉𝑥
and 𝑉𝑦 are velocity components and 𝑎𝑥 and 𝑎𝑦 are acceleration Earth
x X
terms
1 1
𝑎𝑥 = σ 𝐹𝑥 𝑎𝑦 = σ 𝐹𝑦
𝑚 𝑚
σ 𝐹𝑥 and σ 𝐹𝑦 are the summation of forces acting on satellite on x and y directions. Assume only gravity and
aerodynamic forces are available and assume constant gravity with 𝑔 = 9.8 and 𝑚 = 50𝐾𝑔, 𝐶𝐷 = 0.3, 𝜌 =
10−7 , 𝑆 = 0.4𝑚2 and use the following equation to calculate the aerodynamic force:
1
𝐷 = 𝜌𝑉 2 𝑆𝐶𝐷 and 𝑉= 𝑉𝑥2 + 𝑉𝑦2
2
Hints:
• Gravity is towards the center f earth
• Drag is in the opposite direction f the velocity
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
File Handling
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Bytes – [1 0 0 1 1 0 0 1]
characters bytes
human Machin
understandable understandable
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
sr.close() sw.close()
fs.close()
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Project-based Homework
𝑑𝑥 𝑑𝑦 𝑑𝑉𝑥 𝑑𝑉𝑦
• Calculate the derivative ( , , , ) in a separate method
𝑑𝑡 𝑑𝑡 𝑑𝑡 𝑑𝑡
• Add methods to your code for calculation of gravity constant (g) and drag
coefficient
• Assume drag coefficient and gravity as constant values.
• Use comment for the method
• Use method overloading and define anther method for calculation of gravity
constant (g) which uses the following equation
𝑅 2
𝑔 = 𝑔0
𝑅+ℎ
Where, 𝑔0 = 9.81 R is the radius of the earth and 𝑅 + ℎ is the distance f
satellite from the earth center.
• Add the option of write/read the data of the satellite (that you entered in the
console) in/from a file.
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Example Problem
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Example Problem
Double Mass-Spring-Damper Dynamics
Consider a system containing two masses m and M that are connected by a spring with
stiffness K and a damper with damping of c. The system is initially at rest. At t=0 a force
𝐹 𝑡 = 𝐹0 . exp(𝑝. 𝑡) ∗ sin 𝜔. 𝑡 is applied to the system. Simulate the dynamic behaviour
of the system. (Test case: K=10, c=0.2, M=1, m = 0.5, p=0.3, 𝐹0 = 0.1 and 𝜔 = 2)
𝑚 M F(t)
c
y
𝑦2
𝑦1
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Example Problem
Double Mass-Spring-Damper Dynamics
Modeling
K(𝑦1 − 𝑦2 ) K(𝑦1 − 𝑦2 )
𝑚 M F(t)
c(𝑦ሶ 1 − 𝑦ሶ 2 ) c(𝑦ሶ 1 − 𝑦ሶ 2 )
1 1
𝑥ሶ 2 = 𝑥4 𝑥ሶ 4 = K 𝑦1 − 𝑦2 + c 𝑦ሶ 1 − 𝑦ሶ 2 = K 𝑥1 − 𝑥2 + c 𝑥3 − 𝑥4
𝑚 𝑚
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Example Problem
Double Mass-Spring-Damper Dynamics
Modeling
𝑋Ԧ = [𝑥1 , 𝑥2 , 𝑥3 , 𝑥4 ]
𝑥ሶ 1 = 𝑥3
𝑥ሶ 2 = 𝑥4
1
𝑥ሶ 3 = F(t) − K 𝑥1 − 𝑥2 − c 𝑥3 − 𝑥4
𝑀
1
𝑥ሶ 4 = K 𝑥1 − 𝑥2 + c 𝑥3 − 𝑥4
𝑚
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Example Problem
Double Mass-Spring-Damper Dynamics
Solution Approach – Numerical Integration
Euler Method
ሶ Ԧ
𝑋Ԧ 𝑡 + ∆𝑡 = 𝑋Ԧ 𝑡 + 𝑋Ԧ (𝑋, 𝑡)∆𝑡
Standard 4th order Runge-Kutta Method
∆𝑡
𝑋 𝑡 + ∆𝑡 = 𝑋 𝑡 + (𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4 )
6
ሶ 1 ∆𝑡
ሶ K 3 = 𝑋 (𝑋 + 𝐾2 ∆𝑡, 𝑡 + )
K1 = 𝑋 (𝑋, 𝑡) 2 2
ሶ 1 ∆𝑡 ሶ (𝑋 + 𝐾 ∆𝑡, 𝑡 + ∆𝑡)
K 2 = 𝑋 (𝑋 + 𝐾1 ∆𝑡, 𝑡 + ) K 4 = 𝑋 3
2 2
Advanced Programming (AERO 108 / ME 108) Seyed Yaser Nabavi
Homework
Solve the Mass-Spring-Damper Problem for three objects
K1 K2
𝑀1 M2 M3 F(t)
c1 c2
y
𝑦3
𝑦2
𝑦1