Zhao/EGR 126/Class Notes... · Web viewEngineering Dictionary Definition Noun: The branch of...

14
EGR 126 Programming for Engineers Fall 2016 [#1 – Intro: August 23, 2016] Engineering Dictionary Definition Noun: 1. The branch of science and technology concerned with the design, building, and use of engines, machines, and structures. o the work done by, or the occupation of, an engineer. o the action of working artfully to bring something about. Wikipedia Definition Engineering is the application of mathematics , empirical evidence and scientific , economic , social, and practical knowledge in order to invent , innovate, design , build, maintain , research , and improve structures , machines , tools , systems , components , materials , processes and organizations . Problem Solving Talking points: - What is the most difficult problem you have solved (or want to solve)? - What is your (problem solving) approach? Add your notes here… Programming Computer programming (often shortened to programming) is a process that leads from an original formulation of a computing problem to executable computer programs . Programming involves activities such as analysis, developing understanding, generating algorithms , verification of requirements of algorithms including their correctness and resources consumption, and 1

Transcript of Zhao/EGR 126/Class Notes... · Web viewEngineering Dictionary Definition Noun: The branch of...

EGR 126 Programming for Engineers Fall 2016

[#1 – Intro: August 23, 2016]

Engineering Dictionary DefinitionNoun:

1. The branch of science and technology concerned with the design, building, and use of engines, machines, and structures.

o the work done by, or the occupation of, an engineer.o the action of working artfully to bring something about.

Wikipedia DefinitionEngineering is the application of mathematics, empirical evidence and scientific, economic, social, and practical knowledge in order to invent, innovate, design, build, maintain, research, and improve structures, machines, tools, systems, components, materials, processes and organizations.

Problem SolvingTalking points:

- What is the most difficult problem you have solved (or want to solve)?

- What is your (problem solving) approach?

Add your notes here…

ProgrammingComputer programming (often shortened to programming) is a process that leads from an original formulation of a computing problem to executable computer programs.

Programming involves activities such as analysis, developing understanding, generating algorithms, verification of requirements of algorithms including their correctness and resources consumption, and implementation (commonly referred to as coding[1][2]) of algorithms in a target programming language. Source code is written in one or more programming languages. The purpose of programming is to find a sequence of instructions that will automate performing a specific task or solving a given problem. The process of programming thus often requires expertise in many different subjects, including knowledge of the application domain, specialized algorithms and formal logic.

Reading for the next class: the Jeroo book (https://home.cc.gatech.edu/dorn/uploads/41/JerooDocJava.pdf)

- Read the first two chapters carefully. We will go over key concepts in Chapter 2 on Thursday.- Look into Chapter 3 briefly and be prepared for class exercises in the first few weeks.

1

EGR 126 Programming for Engineers Fall 2016

[#2 - Getting Started with Programming: August 25, 2016]In this class, you are learning to solve non-trivial problems: the answer to which is not a simple sentence.

See a real world example: how to get there?

The need for solving these problems is not new to human kind: and an effective strategy is to divide and conquer.

In the Book of Tao & Teh (DaoDe Jing) , Lao Tzu (the Old Master) suggestedA nine-storey terrace rise from (layering) a mound of earth;A journey of a thousand or mile starts from accumulating steps forward.

Real world projects tend to be complicated:

- A car consists of tens of thousands parts;- A company (or enterprise, like J.C. Penny) has numerous branches and employees; etc.

Programming is an important skill, because it is the way to realize the power of computing, the goal of which is to automate real world information processing procedures by using computers.

Software solutions (another way to refer to programs) should reflect this nature.

solution tasks for computer to carry out

specs & design lines of code : instructions for computer

organized around objects (s/w entities) reflecting business concepts

Leading into concepts listed in Chapter 2 of the Jeroo Tutorial…

Starting the Jeroo environment…

Our first program in Jeroo:

- Prepare your workspace: be organized- Boiler plate code (think about syntax, semantics, case-sensitivity, method!)

method main() {

//real contents go here

}

- Add a Jeroo into the world (think about object, class, and instantiation) - Let the Jeroo do something (think about behavior, attribute, and pre- & post-condition; message

passing and method invocation)- Execute the program (compiler, )

2

EGR 126 Programming for Engineers Fall 2016

[#3 – First Programming Assignment: August 30, 2016]- Work more with the Jeroo IDE: create a map.- Create Jeroo and ask it to do things: by message passing or invoking methods- Add comment lines to improve code readability - Plan for first assignment: 4-Jeroo team relay

[#4 – Automating with Control Structures: September 1, 2016]- Solutions to non-trivial problems are not always straight forward: there are loops and branches- Introduce the following from Chapter 6

o Boolean expression and Boolean methods in Jerooo Repetition structure in Jeroo: while loopo Decision structure in Jeroos: if / if-else / if-else if…

- Use control structures to run one Jeroo around the square-shaped track

- Getting started with Assignment 2, featuring a Olympic style tracko modify the track: add semi-circle to the left and right

[#5 – More on Control Structures: September 6, 2016]

while-structure:

syntax and semantics flow chart

3

EGR 126 Programming for Engineers Fall 2016

if[-else]-structure:

syntax and semantics flow chart- Cascade if structure

if( Louisa.isFlower(AHEAD) ){ Louisa.hop(); Louisa.pick();}else if ( Louisa.isNet(AHEAD) ){ Louisa.toss();}else if ( Louisa.isWater(AHEAD) ){ Louisa.plant();}else if ( Louisa.isJeroo(AHEAD) ){ Louisa.give(AHEAD);}else{ Louisa.hop(); Louisa.turn(LEFT);}

- Compound conditions (pp 57-58 in the tutorial)

[#6 – Wrapping up Programming Basics w/ Jeroo: September 8, 2016]- (User written) Method- Review Sheet for Test1

4

EGR 126 Programming for Engineers Fall 2016

[#7 – Test#1: September 13, 2016]- Written Part in class, with design for Prog in Part II

[#8 – Moving onto C++: September 15, 2016]- Intro to the C++ language (and other C-family languages)

List of [some] C-family programming languagesFrom Wikipedia, the free encyclopedia(https://en.wikipedia.org/wiki/List_of_C-family_programming_languages)

LanguageYear

startedCreated by (at) Comments

C 1969- Dennis Ritchie(Bell Was an enhancement of Ken Thompson's B

5

EGR 126 Programming for Engineers Fall 2016

LanguageYear

startedCreated by (at) Comments

1973 Labs) language.

C shell/tcshlate 1970s

Bill Joy (UC Berkeley)

Scripting language and standard Unix shell.

C++ 1979Bjarne Stroustrup(Bell Labs)

Named as "C with Classes" and renamed C++ in 1983; it began as a reimplementation of static object orientation in the tradition of Simula 67, and through standardization and wide use has grown to encompass generic programming as well as its original object-oriented roots.

Objective-C 1986Brad Cox and Tom Love

It is an object-oriented dynamic language based heavily on Smalltalk. A loosely defined de facto standard library by the original developers has now largely been displaced by variations on theOpenStep FoundationKit.

C* 1987 Thinking Machines object-oriented, data-parallel superset of ANSI C

Perl 1988 Larry WallScripting language used extensively for system administration, text processing, and web server tasks.

Java 1991 James Gosling (Sun Microsystems)

Created as the Oak, and released to the public in 1995. It is an OODL based inspired heavily by Objective-C, though with a syntax based somewhat on C++. It also compiles to its own bytecode, a standard part of the language specification. It is

6

EGR 126 Programming for Engineers Fall 2016

LanguageYear

startedCreated by (at) Comments

strongly typed, a feature that is enforced by the VM.

PHP 1995 Rasmus LerdorfWidely used as a server-side HTML scripting language. C-like syntax.

C-- 1997Simon Peyton Jones, Norman Ramsey

generated mainly by compilers for very high-level languages

C# 1999Anders Hejlsberg(Microsoft)

Created under the name "Cool", it is syntactically very similar to Java, though with a Smalltalk-like unified type system.

C++From Wikipedia, the free encyclopedia(https://en.wikipedia.org/wiki/C%2B%2B)

C++ (pronounced cee plus plus, /ˈsiː   plʌs   plʌs/ ) is a general-purpose programming language. It has imperative, object-oriented and generic programming features, while also providing facilities for low-level memory manipulation.

It was designed with a bias toward system programming and embedded, resource-constrained and large systems, with performance, efficiency and flexibility of use as its design highlights.[5] C++ has also been found useful in many other contexts, with key strengths being software infrastructure and resource-constrained applications,[5] including desktop applications, servers (e.g. e-commerce, web search or SQL servers), and performance-critical applications (e.g. telephone switches or space probes).[6] C++ is a compiled language, with implementations of it available on many platforms and provided by various organizations, including the Free Software Foundation (FSF's GCC), LLVM, Microsoft, Intel and IBM.

C++ is standardized by the International Organization for Standardization (ISO), with the latest standard version ratified and published by ISO in December 2014 as ISO/IEC 14882:2014 (informally known as C++14).[7] The C++ programming language was initially

7

EGR 126 Programming for Engineers Fall 2016

standardized in 1998 as ISO/IEC 14882:1998, which was then amended by the C++03, ISO/IEC 14882:2003, standard. The current C++14 standard supersedes these and C++11, with new featuresand an enlarged standard library. Before the initial standardization in 1998, C++ was developed by Bjarne Stroustrup at Bell Labs since 1979, as an extension of the C language as he wanted an efficient and flexible language similar to C, which also provided high-level features for program organization.

Many other programming languages have been influenced by C++, including C#, D, Java, and newer versions of C (after 1998).

- problem solving with a general-purpose language like C++- creating a project using the CodeBlocks IDE

[#9 – Formatted Output in C++: September 20, 2016]- boiler code of a C++ program

o The [#]include statemento The using namespace phrase

- commentso single line styleo multi-line style

- the main functiono return type of a function and the return statement

- format and output results using the cout objecto the << operatoro chained insertion using << … << …o special characters

endl "\n" "\t"

[#10 – Variables in C++: September 22, 2016]- variables are

o place holders for values used in codeo declared with a typeo a value that matches the declared type may be assigned to the variable

assignment operator = don’t confuse it with == the equality operator

o a variable can only be used after it’s initialized - data types (and how are the values stored)

o into doubleo char

8

EGR 126 Programming for Engineers Fall 2016

- the cin objecto functions, such as get(), ignore()o the >> operator

- integer division

[#11 – A real project: September 27, 2016]- Your next project (A4) is a BMI Calculator, featuring

o prompt user to provide data in the forms of text string or single character, numbers;o determine among multiple valid and invalid values;o arithmetic calculation, with potential data conversion as neededo formatted output, including visualization using a well formatted text pattern

The following language features are needed, in addition to what we discussed in the previous two class periods (such as cout, cin, variable declaration with appropriate types, value assignment, and certain functions)

- the getline() function (of the std namespace)o to get the full content in the input line, use getline(cin, str_var_name)

variable in the string typeo to get a portion separated by a marker character(say ','), use getline(cin, str, ',')

- functions of the string classo find(<pattern>), such as user_name.find(" ") returns the indexo substr(start_index, length), such as user_name.substr(0, index_of_space) returns

first_nameo replace(index, len, <pattern>), such as bmiScale.replace(index, 1, "|")

- if statement (known from Jeroo)o if (boolean expression) {

//…} [else [if-statement //if used] { //…}]

o inherited from c if (1 or nonzero) is equivalent to if (true) if (0) is equivalent to if (false)

- the bool type and boolean expressiono only two possible values: true and false o boolean expressions using relational operators: >, <, ==, !=, >=, <=o don’t use true or false in boolean expressions

- Boolean operators (known from Jeroo)o &&o ||

9

EGR 126 Programming for Engineers Fall 2016

o !

[#12 – sample code for language features from #11: September 29, 2016]- see details in the sample folder

o and online at cplusplus.org (http://www.cplusplus.com/reference/)

[#13 – review for C++ Part 1 #8~12 + while loop: October 4, 2016]- while(???) { … }- use a boolean variable to check for whether to stop the loop- use a counter variable to check …

[#14 – Test #2 October 11, 2016]- written part and prog part

[#15 – loops and control as used in the Tic-Tac -Toe game October 13, 2016]- more sample code

[#16 – going over Test #2 & more loops October 18, 2016]- reading numbers and calculate average, using one variable to take input values- using a while loop- using a do-while loop- using a for loop- break and continue statements

[#17 – more on loops October 20, 2016]- variable types: numeric types

o into doubleo char

- special characters- comparing loops

o for vs. while- Using all of these in the Election project

[#18 – switch statement and reading data from a file October 25, 2016]- Features that you may use in your Election project

o using a switch statement (pp105-108) instead of a multi-way if-else structure switch (candidate) {

case 1: count1++;

break; case 2:

… break;

…default: …

break; //optional

10

EGR 126 Programming for Engineers Fall 2016

}

o using a text file (pp411-414) to provide (valid & invalid ballots) to test your vote counts>>shared template#include <iostream>#include <fstream>

using namespace std;

int main(){

ifstream file_reader("test.txt");// as given in the line above, the file need to be save under

// the project directory: right mouse on the project icon and // select Properties to check out that directory if you don’t// know where is your project located

if ( ! file_reader.is_open()) { cout << "Could not open file!" << endl;

}

// adding code here

return 0;}

>>read a number int number;

if(file_reader >> number) { cout << "The value is: " << number << endl; }

>>read a number int number;

while (file_reader >> number) { cout << "The value is: " << number << endl; }

- Open-closed principleYour program should be open for extension, but closed for modification

o we will see next how to improve reusability by using methodso write another version to try out switch and/or file features if you already have a

(partially) working version of the Election project

11