Download presentation
Presentation is loading. Please wait.
Published byHoratio Hall Modified over 6 years ago
1
Running Programs & IDEs Reference: COS240 Syllabus
COS240 O-O Languages AUBG, COS dept Lecture 30b Title: Running Programs & IDEs (C#) Reference: COS240 Syllabus 5/19/2018 Assoc. Prof. Stoyan Bonev
2
Assoc. Prof. Stoyan Bonev
Lecture Contents: C# applications classified Processing C# source texts How C# works IDEs to run C# MS Visual Studio SharpDevelop Demo programs 5/19/2018 Assoc. Prof. Stoyan Bonev
3
Types of Applications Developed with C#
Web applications Windows graphical user interface (GUI) applications Console-based applications Class libraries and stand-alone components (.dlls) Smart device applications Services 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 3 Source: Longman dictionary 1987 3
4
Assoc. Prof. Stoyan Bonev
Windows Applications Applications designed for the desktop Designed for a single platform Use classes from System.Windows.Form Applications can include menus, pictures, drop- down controls, buttons, text boxes, and labels Use drag-and-drop feature of Visual Studio 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 4 Source: Longman dictionary 1987 4
5
Windows Applications (continued)
Figure 1-15 Windows application written using C# 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 5 Source: Longman dictionary 1987 5
6
Assoc. Prof. Stoyan Bonev
Console Applications Normally send requests to the operating system Display text on the command console Easiest to create Simplest approach to learning software development Minimal overhead for input and output of data 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 6 Source: Longman dictionary 1987 6
7
Exploring the First C# Program
From Example 1-1 Comments in green line // This is traditionally the first program written. line using System; line namespace HelloWorldProgram line { line class HelloWorld line { line static void Main( ) line { line Console.WriteLine(“Hello World!”); line } line } line } Keywords in blue 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 7 Source: Longman dictionary 1987 7
8
Assoc. Prof. Stoyan Bonev
C# - High Level PL In order to run any HLL program, it be transformed to executable form Three ways to convert source code into executable form: Compiler – generate object code Interpreter – interpret the source code Hybrid Compiler of semi-interpreting type 5/19/2018 Assoc. Prof. Stoyan Bonev
9
Assoc. Prof. Stoyan Bonev
data Object program Executing computer Results Source program compiler Run time Compile time 5/19/2018 Assoc. Prof. Stoyan Bonev
10
Assoc. Prof. Stoyan Bonev
Data Source program Interpreter Results Run time 5/19/2018 Assoc. Prof. Stoyan Bonev
11
Assoc. Prof. Stoyan Bonev
data Object program in IL form Interpreter Results Source program compiler 5/19/2018 Assoc. Prof. Stoyan Bonev
12
Assoc. Prof. Stoyan Bonev
How does C# work? Java is compiled to intermediate byte code. Byte code processed by interpreter JVM. C# is compiled to intermediate MSIL code. MSIL code processed by interpreter. 5/19/2018 Assoc. Prof. Stoyan Bonev
13
Processing in .NET The C# compiler job is to turn the source code into a working program. .NET uses an intermediate language MSIL (MicroSoft Intermediate Language (or IL only). The compiler reads your source code and produces MSIL code. The .NET JIT compiler then reads your MSIL code and produces an executable application in memory.
14
IDEs to run C# How to Compile/Run C# Programs
Using MS-DOS command window C# compiler is: csc Using C# IDEs MS Visual Studio SharpDevelop Java Programming: From Problem Analysis to Program Design, 3e
15
Creating, Compiling and Running C# from Command Window
First: Open a Command Window Click Start; Select All Programs; Select MS Visual Studio; Select Visual Studio Tools; Select and Click Developer Command Prompt for Visual Studio Second: Create a folder on C or Q drive where to save .cs files md COS240Progs cd COS240Progs Third: Create and save C# source file Notepad/Write/Textpad Welcome.cs Fourth : Compile C# source file csc Welcome.cs Fifth : Run Welcome 5/19/2018 Source: Longman dictionary 1987
16
Assoc. Prof. Stoyan Bonev
IDE MS Visual Studio 5/19/2018 Assoc. Prof. Stoyan Bonev
17
Develop Console Application
Begin by opening Visual Studio Create new project Select New Project on the Start page OR use File → New Project option 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 17 Source: Longman dictionary 1987 17
18
Code Automatically Generated
Figure 1-19 Code automatically generated by Visual Studio 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 18 Source: Longman dictionary 1987 18
19
Typing Your Program Statements
Type statements like this: Console.WriteLine(“Hello, C# World”); … Console.Read(); // or Console.ReadLine(); // or Console.ReadKey(); // or 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 19 Source: Longman dictionary 1987 19
20
Compile and Run Application
To Compile – Build > Build/Rebuild solution To run or execute application: Debug > Start Debugging Debug > Start Without Debugging Shortcut – if executing code that has not been compiled, automatically compiles first Start option does not hold output screen → output flashes quickly Last statement in Main( ), add Console.Read( ); 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 20 Source: Longman dictionary 1987 20
21
Build Visual Studio Project
Figure 1-21 Execution of an application using Visual Studio 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 21 Source: Longman dictionary 1987 21
22
Rename Source Code Name
By default source code file name is: Program.cs By default the class name is: Program If you wish, you can Change the name of the class and the source code filename Use the Solution Explorer Window to change the source code filename View > Solution Explorer 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 22 22 Source: Longman dictionary 1987 22 22
23
Rename Source Code Name
Clicking Yes causes the class name to also be renamed Figure 1-20 Changing the source code name from Program 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 23 23 Source: Longman dictionary 1987 23 23
24
Debugging an Application
Types of errors Syntax errors Typing error Misspelled name Forget to end a statement with a semicolon Run-time errors Failing to fully understand the problem More difficult to detect 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 24 Source: Longman dictionary 1987 24
25
Error Listing Pushpin Errors reported
Missing ending double quotation mark Pushpin Errors reported Figure 1-22 Syntax error message listing 5/19/2018 C# Programming: From Problem Analysis to Program Design Assoc. Prof. Stoyan Bonev 25 Source: Longman dictionary 1987 25
26
Assoc. Prof. Stoyan Bonev
Visual Studio . NET 2005/…/2012 Go to the File menu and choose New, then Project…. A dialog will appear. In the left pane, choose Visual C# Projects; from the right pane, choose Console Application. It is possible to specify other information about the project in the bottom portion of this dialog (i.e., the name and location of the project). After entering all the necessary information, click OK to create the project. The project is created, and the code window is opened for editing. 5/19/2018 Assoc. Prof. Stoyan Bonev
27
Windows Application in C#
Although the first programs display output in the command prompt, most C# applications use windows or dialogs to display output. Dialogs are windows that typically display important messages to the user of an application. The .NET Framework Class Library includes class MessageBox for creating dialogs. Class MessageBox is defined in namespace System.Windows.Forms. 5/19/2018 Assoc. Prof. Stoyan Bonev
28
Hello World! Console Application using Forms
5/19/2018 Assoc. Prof. Stoyan Bonev
29
Assoc. Prof. Stoyan Bonev
Class MessageBox is located in assembly System.Windows.Forms.dll. We must add a reference to this assembly to use class MessageBox in our program. 5/19/2018 Assoc. Prof. Stoyan Bonev
30
Assoc. Prof. Stoyan Bonev
Add Reference… To begin, make sure you have an application open. Select the Add Reference… option from the Project menu, or right click the References folder in the Solution Explorer and select Add Reference… from the popup menu that appears. This opens the Add Reference dialog. Double click System.Windows.Forms.dll to add this file to the Selected Components list at the bottom of the dialog, then click OK. Notice that System.Windows.Forms now appears in the References folder of the Solution Explorer 5/19/2018 Assoc. Prof. Stoyan Bonev
31
Assoc. Prof. Stoyan Bonev
5/19/2018 Assoc. Prof. Stoyan Bonev
32
Assoc. Prof. Stoyan Bonev
5/19/2018 Assoc. Prof. Stoyan Bonev
33
Assoc. Prof. Stoyan Bonev
Practical session Task: Write a program to input two integral values and to compute and display the result of their addition, subtraction, multiplication and division Langugage: C# IDE: MS Visual Studio 5/19/2018 Assoc. Prof. Stoyan Bonev 33
34
Assoc. Prof. Stoyan Bonev
5/19/2018 Assoc. Prof. Stoyan Bonev
35
Assoc. Prof. Stoyan Bonev
Practical session Problem: To accumulate sum of stream of input integers terminated by end-of-data (CTRL/Z) indicator Language: C# IDE: MS Visual Studio 5/19/2018 Assoc. Prof. Stoyan Bonev
36
Assoc. Prof. Stoyan Bonev
Practical session Console.WriteLine("Hello World!"); int sum=0; string pom; while ((pom = Console.ReadLine()) != null) { sum += Convert.ToInt32(pom); } Console.WriteLine("Final sum = {0}",sum); Console.Write("Press any key to continue "); Console.ReadKey(true); 5/19/2018 Assoc. Prof. Stoyan Bonev 36
37
Visual Studio – configuration B.Doyle, C#, App A, pp993-1005
38
Assoc. Prof. Stoyan Bonev
Customizing the IDE Default Start page 5/19/2018 Assoc. Prof. Stoyan Bonev
39
Using menu: Tools > Options
15 folders opened 5/19/2018 Assoc. Prof. Stoyan Bonev
40
Using menu: Tools > Options
folder node: Environment, General 5/19/2018 Assoc. Prof. Stoyan Bonev
41
Using menu: Tools > Options
folder node: Environment, Fonts and Colors 5/19/2018 Assoc. Prof. Stoyan Bonev
42
Using menu: Tools > Options
folder node: Environment, Import&Export Settings 5/19/2018 Assoc. Prof. Stoyan Bonev
43
Using menu: Tools > Options
folder node: Environment, Startup 5/19/2018 Assoc. Prof. Stoyan Bonev
44
Using menu: Tools > Options
folder node: Projects and Solutions, General 5/19/2018 Assoc. Prof. Stoyan Bonev
45
Using menu: Tools > Options
folder node: Text Editor, 5/19/2018 Assoc. Prof. Stoyan Bonev
46
Using menu: Tools > Options
folder node: Text Editor, All Languages, Tabs 5/19/2018 Assoc. Prof. Stoyan Bonev
47
Using menu: Tools > Options
folder node: Text Editor, C#, Intellisense IntelliSense is Microsoft's implementation of autocompletion, In addition to completing the symbol names the programmer is typing, IntelliSense serves as documentation and disambiguation for variable names, functions and methods using reflection Autocomplete involves the program predicting a word or a phrase that the user wants to type in without the user actually typing it in completely 5/19/2018 Assoc. Prof. Stoyan Bonev
48
Using menu: Tools > Options
folder node: Text Editor, C#, Intellisense 5/19/2018 Assoc. Prof. Stoyan Bonev
49
IntelliSense Figure 3-2 Console class members
After typing the dot, list of members pops up Method signature(s) and description 3-D fuchsia colored box —methods aqua colored box — fields (not shown) Figure 3-2 Console class members C# Programming: From Problem Analysis to Program Design 49 49
50
IntelliSense Display Figure 3-3 IntelliSense display
string argument expected string parameter 18 different Write( ) methods Figure 3-3 IntelliSense display C# Programming: From Problem Analysis to Program Design 50 50
51
IntelliSense Display (continued)
Figure 3-4 Console.Read ( ) signature Figure 3-5 Console.ReadLine ( ) signature C# Programming: From Problem Analysis to Program Design 51 51
52
Code Editor Tools B.Doyle, C#, App B, pp 1007-1017
Code Snippets Code Snippets are templates that you can use to insert code. Built-in snippets are stored as XML files and grouped in categories: ASP.NET MVC OFFICE DEVELOPMENT OTHER TEST VISUAL C# 5/19/2018 Assoc. Prof. Stoyan Bonev
53
Assoc. Prof. Stoyan Bonev
Code Editor Tools Code Snippets are ACTIVATED: By Typing snippet alias and pressing TAB key Or By Pressing CTRL+K+X to activate the code snippet list and selecting the desired snippet alias By Pressing CTRL+’SpaceBar’ to display more exhaustive list of snippets 5/19/2018 Assoc. Prof. Stoyan Bonev
54
Exercise on Code Snippets
Type two loop statement level control structures using Code Snippet for Code Snippet forr 5/19/2018 Assoc. Prof. Stoyan Bonev
55
Exercise on Code Snippets
Type two loop statement level control structures using Code Snippet for Code Snippet forr Test two more code snippets Code Snippet class Code Snippet ctor 5/19/2018 Assoc. Prof. Stoyan Bonev
56
Assoc. Prof. Stoyan Bonev
Code Editor Tools Refactoring Code refactoring is "disciplined technique for restructuring an existing body of source code, altering its internal structure without changing its external behaviour”. Edit > Refactor 5/19/2018 Assoc. Prof. Stoyan Bonev
57
Assoc. Prof. Stoyan Bonev
Code Editor Tools Refactoring Extract method Rename Other options 5/19/2018 Assoc. Prof. Stoyan Bonev
58
Exercise on Refactoring
Test the Rename… option Type source text in C# Select a variable name Activate the Rename… option 5/19/2018 Assoc. Prof. Stoyan Bonev
59
Exercise on Refactoring
Test the Extract method… option Type source text in C# Select a statement or a group of statements Activate the Extract method… option Sample source texts Text1: c= a+b; what method return type is expected? Text2: { c= a+b; Console.WriteLine(”c=“+c); } What method return type is expected? Text3: type spaghetti code for factorial, GCD, Fibonacci and refactor it to separate methods. 5/19/2018 Assoc. Prof. Stoyan Bonev
60
Assoc. Prof. Stoyan Bonev
Code Editor Tools Working with Class Diagrams Right clicking on the project in Solution Explorer opens list of options Select View Class Diagram Class Details View 5/19/2018 Assoc. Prof. Stoyan Bonev
61
Assoc. Prof. Stoyan Bonev
Code Editor Tools Working with Class Diagrams Using the Class Diagram to Add Members How? Right clicking on any of the classes in the class diagram reveals options to add additional methods, properties, fields, or events. 5/19/2018 Assoc. Prof. Stoyan Bonev
62
IDE SharpDevelop #develop
5/19/2018 Assoc. Prof. Stoyan Bonev
63
SharpDevelop – introduction
SharpDevelop (also styled as #develop) is a free and open source IDE for the Microsoft .NET, Mono, Gtk# and Glade# platforms, and supports development in C#, Visual Basic .NET, Boo, F#, IronPython and IronRuby programming languages. It includes features for project management, code editing, application compiling and debugging. SharpDevelop is written entirely in C#. 5/19/2018 Assoc. Prof. Stoyan Bonev
64
SharpDevelop - How to use?
To open SharpDevelop Start Menu > All Programs > SharpDevelop To create a new project .sln file File > New > Project/Solution Select project Category /depends on ProgLan/ Select project Template Name the project Select location for the project 5/19/2018 Assoc. Prof. Stoyan Bonev
65
SharpDevelop - How to use
To add a new source file to an open project File > New > File OR Project > Add > New Item… Type the source text of C# program Save the file File > Save As 5/19/2018 Assoc. Prof. Stoyan Bonev
66
SharpDevelop - How to use
To add an existing file to an open project: Project > Add > Existing Item… Locate and then click the name of the file you want to add to the project, and then click the Open button 5/19/2018 Assoc. Prof. Stoyan Bonev
67
SharpDevelop - How to use
To run a C# program: Type your source text Save your project: How? (File > Save All) Compile the source: How? (Build > Build Solution) Read, Analyze, Task List pane, Errors pane Run the program: How? (Debug > Run) 5/19/2018 Assoc. Prof. Stoyan Bonev
68
SharpDevelop - How to use
To close a project: File > Close > Solution FYI: Earlier SharpDevelop versions use the term “Combine” as a collection of one or more projects, i.e. a synonym of a solution. 5/19/2018 Assoc. Prof. Stoyan Bonev
69
SharpDevelop - How to use
To open an existing project, .sln file: File > Open > Project/Solution Open File dialog box gets opened Navigate to the folder that contains the project .sln file Click Open button 5/19/2018 Assoc. Prof. Stoyan Bonev
70
SharpDevelop – Panels and Views
. 5/19/2018 Assoc. Prof. Stoyan Bonev
71
Assoc. Prof. Stoyan Bonev
SharpDevelop How to Configure? 5/19/2018 Assoc. Prof. Stoyan Bonev
72
SharpDevelop - how to configure
Note: The asterisk on the Program.cs tab indicates that the changes made to the file have not been saved. 5/19/2018 Assoc. Prof. Stoyan Bonev
73
SharpDevelop - how to configure
Internationalization You can change your preferred UI language (English by default) Tools > Options > General > UI language 5/19/2018 Assoc. Prof. Stoyan Bonev
74
SharpDevelop - how to configure
Customizations for Coding SharpDevelop offers a wide range of features for making the coding process more efficient and easier like: completing chunks of code /Intellisense/, inserting chunks of code /Code Snippets/, highlighting keywords, etc. 5/19/2018 Assoc. Prof. Stoyan Bonev
75
SharpDevelop - code templates
These utilities reduce the amount of effort involved in writing recurring snippets of code. As the results of these utilities provide the basic constructs, all that is left to do is to fill in the gaps. There is a list of code templates. If we are not sure of a template's shortcut text, we can type Ctrl+J, when the caret (the keyboard cursor) is active in the edit view. This will pop up a listbox containing the complete list of template constructs available to us. From within this list, we can then select the desired template. 5/19/2018 Assoc. Prof. Stoyan Bonev
76
SharpDevelop - code wizards
Object-oriented programming languages require classes. As classes tend to get complex. SharpDevelop provides us with a class wizard that offers several options to save us from having to write standard code. The class wizard is available through File > New > File… A screen gets opened and you have to select Category (depends on the language) Template (Class among many others) 5/19/2018 Assoc. Prof. Stoyan Bonev
77
SharpDevelop - how to configure
Tools > Options > General Tools > Options > Coding Tools > Options > Debugging Tools > Options > Text Editor Tools > Options > Tools Tools > Options > Form Designer 5/19/2018 Assoc. Prof. Stoyan Bonev
78
Assoc. Prof. Stoyan Bonev
Exercises/Tasks SharpDevelop. Write a C# program to: display your personal data incl name, age, gpa, address Read two numeric data items Compute and display result of their addition, subtraction, multiplication, division and modulus (in case of integer data). 5/19/2018 Assoc. Prof. Stoyan Bonev
79
Assoc. Prof. Stoyan Bonev
Exercises/Tasks Console.WriteLine("Hello World!"); int a, b, sum=0, sub = 0, mul = 0, div = 0; string pom; pom = Console.ReadLine(); a = Convert.ToInt32(pom); b = Convert.ToInt32(pom); sum = a + b; Console.WriteLine(“sum is = {0}“, sum); … Console.Write("Press any key to continue "); Console.ReadKey(true); 5/19/2018 Assoc. Prof. Stoyan Bonev
80
Assoc. Prof. Stoyan Bonev
Exercises/Tasks Console.WriteLine("Hello World!"); int sum=0; string pom; while ((pom = Console.ReadLine()) != null) { sum += Convert.ToInt32(pom); } Console.WriteLine("Final sum = {0}",sum); Console.Write("Press any key to continue "); Console.ReadKey(true); 5/19/2018 Assoc. Prof. Stoyan Bonev
81
Assoc. Prof. Stoyan Bonev
Exercises/Tasks Test refactoring in SharpDEvelop: Extract method Given the source text from last slide: Select the source text to be extracted as a method int sum=0; string pom; while ((pom = Console.ReadLine()) != null) { sum += Convert.ToInt32(pom); } Console.WriteLine("Final sum = {0}",sum); How: Refactor > Extract Method 5/19/2018 Assoc. Prof. Stoyan Bonev
82
Assoc. Prof. Stoyan Bonev
Exercises/Tasks Test refactoring in SharpDEvelop: rename Given the source text from last slide: Rename the ‘sum’ identifier to ‘toto’ How: Refactor > Rename 5/19/2018 Assoc. Prof. Stoyan Bonev
83
Assoc. Prof. Stoyan Bonev
Exercises/Tasks Write COmpactProg project to calculate Factorilal: facti(7), factr(7) GCD: gcdi(40,15), gcdr(40,15) Fibonacci: fibi(5), fibr(5) All methods incl. Main() in the same file 5/19/2018 Assoc. Prof. Stoyan Bonev
84
Assoc. Prof. Stoyan Bonev
Exercises/Tasks Source text of the methods: static int factr(int n) { if (n==0) return 1; return n*factr(n-1); } 5/19/2018 Assoc. Prof. Stoyan Bonev
85
Assoc. Prof. Stoyan Bonev
Exercises/Tasks Sample source text of the Main() method for(int i=0; i<10; i++) { Console.WriteLine(“{0}”, factr(i)); } Two alternatives to fill the source text: Typing by hand all the text char by char Using Code Templates Type Ctrl+J to list all the templates Select for and press Tab key 5/19/2018 Assoc. Prof. Stoyan Bonev
86
Assoc. Prof. Stoyan Bonev
Exercises/Tasks SharpDevelop as language converter Tools > Convert code to > … 5/19/2018 Assoc. Prof. Stoyan Bonev
87
Assoc. Prof. Stoyan Bonev
Thank You For Your Attention! 5/19/2018 Assoc. Prof. Stoyan Bonev
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.