Skip to content

Commit

Permalink
Updates Content/Chapter-1-first-steps-in-programming/how-to-write-con…
Browse files Browse the repository at this point in the history
…sole-app/typical-mistakes-in-csharp-programs.md

Auto commit by GitBook Editor
  • Loading branch information
Svetlin Nakov committed Jan 14, 2019
1 parent 593d34d commit d4e3bad
Show file tree
Hide file tree
Showing 49 changed files with 547 additions and 402 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
## Console, Graphical and Web Applications
##

With **console applications**, as you can figure out yourselves, **all operations** for reading input and printing output are **done through the console**. **The input data is inserted** in the console, which is then read by the application, also in it, and the **output data is printed** on the console after or during the runtime of the program.

While a console application **uses the text console**, web applications **use web-based user interface**. To **execute them**, two things are needed - **a web server** and **a web browser**, as **the browser** plays the main role in **the visualization of the data and the interaction with the user**. Web applications are much more pleasant for the user, they visually look better, and a mouse and touch screen can be used (for tablets and smartphones), but programming stands behind all of that. And this is why **we have to learn to program** and we have already made our first very little steps towards that.

Graphical (GUI) applications have **a visual user interface**, directly into your computer or mobile device, without a web browser. Graphical applications (also known as desktop applications) **contain one or more graphical windows**, in which certain **controllers** are located (text fields, buttons, pictures, tables and others), **serving for dialog** with the user in a more intuitive way. Similar to them are the mobile applications in your telephone or your tablet: we use forms, text fields, buttons and other controls and we control them by programming code. This is why we are learning how to write code now: **the code is everywhere in software development**.
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
## Exercises: First Steps in Coding
# Exercises: First Steps in Coding

Welcome to the **exercises**. Now we are going to write a couple of console applications, by which we are going to make a few more steps into programming. After that we will show how we can program something more complex - programs with graphical user interface.

## What We Learned in This Chapter?

First we have learned **what is programming** - **giving commands, written in a computer language**, which the machine understands and is able to execute. We understood what is **a computer program** - it represents a **sequence of commands**, arranged one after another. We got familiar with **the language for programming C\#** on a base level and how **to create simple console applications** with Visual Studio. We followed **the structure of the programming code in the C\# language**, as for example, the fact that commands are mainly given in the section `static void Main(string[] args)` between **the opening and closing curly parentheses**. We saw how to print with `Console.WriteLine(…)` and how to start our program with \[**Ctrl + F5**\]. We learned how to test our code in **SoftUni Judge**.

## The Exercises

Let's get started with the **exercises**. You didn't forget that programming is learned by writing a lot of code and solving problems, did you? Let's solve a few problems to confirm what we have learned.

* [Problem: Console Application “Expression”](https://legacy.gitbook.com/book/software-university-foundation/programming-basics-csharp-en/edit#)
* [Problem: Numbers from 1 to 20](/Content/Chapter-1-first-steps-in-programming/exercises-first-steps-in-coding/numbers-1-to-20.md)
* [Problem: A Triangle Made Out of 55 Stars](/Content/Chapter-1-first-steps-in-programming/exercises-first-steps-in-coding/triangle-of-stars.md)
* [Problem: Calculate Rectangle Area](/Content/Chapter-1-first-steps-in-programming/exercises-first-steps-in-coding/rectangle-area.md)
* [Problem: A Square Made Out of Stars](/Content/Chapter-1-first-steps-in-programming/exercises-first-steps-in-coding/square-of-stars.md)



Welcome to the exercises. Now we are going to write a couple of console applications, by which we are going to make a few more steps into programming. After that we will show how we can program something more complex - programs with graphical user interface.
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
### Problem: Console Application “Expression”
# Problem: Console Application “Expression”

Write a C# console program that **calculates** and **prints** the value of the following numerical expression:

<p align="center"> (3522 + 52353) * 23 - (2336 * 501 + 23432 - 6743) * 3 </p>
```
(3522 + 52353) * 23 - (2336 * 501 + 23432 - 6743) * 3
```

Note: **it is not allowed to previously calculate the value** (for example with Windows Calculator).

#### Tips and Tricks
## Tips and Tricks

Create **a new C# console project** with title "**Expression**". Find the method **``static void Main(string[] args)``** and **go into its body** between **`{`** and **`}`**. After that, **write the code** that calculates the above numerical expression and prints its value on the console. Put the above numerical expression inside the brackets of the command **``Console.WriteLine(…)``**:
Create **a new C# console project** with title "**Expression**". Find the method **``static void Main(string[] args)``** and **go into its body** between **`{`** and **`}`**. After that, **write the code** that calculates the above numerical expression and prints its value on the console. Put the above numerical expression inside the brackets of the command **``Console.WriteLine(…)``**:

![](/assets/chapter-1-images/02.Expression-01.png)

Start the program with [**Ctrl+F5**] and check if the result is the same as the one in the picture:

![](/assets/chapter-1-images/02.Expression-02.png)

#### Testing in the Judge System
## Testing in the Judge System

Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/503#1](https://judge.softuni.bg/Contests/Practice/Index/503#1).

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
### Problem: Numbers from 1 to 20
# Problem: Numbers from 1 to 20

Write a C# console program that **prints the numbers from 1 to 20** on separate rows on the console.
Write a C\# console program that **prints the numbers from 1 to 20** on separate rows on the console.

#### Tips and Tricks
## Tips and Tricks

Create **a C# console application** with name “**Nums1To20**“:
Create **a C\# console application** with name “`Nums1To20`“:

![](/assets/chapter-1-images/03.Numbers-1-to-20-01.png)

Inside the **`static void Main()`** method write 20 commands **``Console.WriteLine(…)``**, each on a separate row, in order to print the numbers from 1 to 20 one after another. Some of you may be wondering if there is a smarter way. Relax, there is, but we will mention it later on.
Inside the `static void Main()` method write 20 commands `Console.WriteLine(…)`, each on a separate row, in order to print the numbers from 1 to 20 one after another. Some of you may be wondering if there is a smarter way. Relax, there is, but we will mention it later on.![](/assets/chapter-1-images/03.Numbers-1-to-20-02.png)Now **we start the program** and we check if the result is what it is supposed to be:

![](/assets/chapter-1-images/03.Numbers-1-to-20-02.png)

Now **we start the program** and we check if the result is what it is supposed to be:
```
1
2
20
```

#### Testing in the Judge System
## Testing in the Judge System

Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/503\#2](https://judge.softuni.bg/Contests/Practice/Index/503#2).

Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/503#2](https://judge.softuni.bg/Contests/Practice/Index/503#2).
Now think whether we can write the program **a smarter way**, so we don't repeat the same command 20 times. Seek out information on the Internet about "[**for loop C\#**](https://www.google.bg/search?q=for+loop+C%23&oq=for+loop+C%23)".

Now think whether we can write the program **a smarter way**, so we don't repeat the same command 20 times. Seek out information on the Internet about "**[for loop C#](https://www.google.bg/search?q=for+loop+C%23&oq=for+loop+C%23)**".
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
### Problem: Calculate Rectangle Area
# Problem: Calculate Rectangle Area

Write a C# program that **reads** from the console **two numbers, a and b**, **calculates** and **prints** the area of a rectangle with sides **a** and **b**.
Write a C\# program that **reads** from the console **two numbers, a and b**, **calculates** and **prints** the area of a rectangle with sides **a** and **b**.

#### Sample Input and Output

| a | b | area |
| :---: | :---: | :---: |
| 2 | 7 | 14 |
| 7 | 8 | 56 |
| 12 | 5 | 60 |
| 2 | 7 | 14 |
| 7 | 8 | 56 |
| 12 | 5 | 60 |

#### Tips and Tricks
## Tips and Tricks

Create a new **console C# program**. To **read both of numbers**, use the following commands:
Create a new **console C\# program**. To **read both of numbers**, use the following commands:

![](/assets/chapter-1-images/05.Rectangle-area-01.png)

What remains is to finish the program above, to calculate the area of the rectangle and to print it. Use the command that is already known to us **`Console.WriteLine()`** and put inside its brackets the multiplication of the numbers **a** and **b**. In programming, multiplication is done using the operator **`*`**.
What remains is to finish the program above, to calculate the area of the rectangle and to print it. Use the command that is already known to us `Console.WriteLine()` and put inside its brackets the multiplication of the numbers **a** and **b**. In programming, multiplication is done using the operator `*`.

#### Test your solution
## Test Your Solution

Test your solution with a few examples. You have to receive an output, similar to this one (we enter 2 and 7 as input and the program prints result 14 - their multiplication):
Test your solution with a few examples. You have to receive an output, similar to this one \(we enter 2 and 7 as input and the program prints result 14 - their multiplication\):

```
2
7
14
```

#### Testing in the Judge System
## Testing in the Judge System

Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/503#4](https://judge.softuni.bg/Contests/Practice/Index/503#4).
Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/503\#4](https://judge.softuni.bg/Contests/Practice/Index/503#4).

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
### \* Problem: A Square Made Out of Stars
# \* Problem: A Square Made Out of Stars

Write a C# console program that **reads** from the console **an integer N** and **prints** on the console **a square made out of N stars**, like in the examples below.

#### Sample Input and Output
## Sample Input and Output

| Input | Outup | Input | Output | Input | Output |
|-----|-----------|-----|-----------|-----|----------|
| 3 |<code>\*\*\*</code><br><code>\*&nbsp;\*</code><br><code>\*\*\*</code>| 4 |<code>\*\*\*\*</code><br><code>\*&nbsp;&nbsp;\*</code><br><code>\*&nbsp;&nbsp;\*</code><br><code>\*\*\*\*</code>| 5 |<code>\*\*\*\*\*</code><br><code>\*&nbsp;&nbsp;&nbsp;\*</code><br><code>\*&nbsp;&nbsp;&nbsp;\*</code><br><code>\*&nbsp;&nbsp;&nbsp;\*</code><br><code>\*\*\*\*\*</code>|

#### Tips and Tricks
## Tips and Tricks

Create a new **console C# program**. To read the number N (2 ≤ N ≤100), use the following code:

Expand All @@ -18,6 +18,6 @@ Finish the program above, so that it prints a square, made out of stars. It migh

**Attention**: this task is harder than the rest and is given now on purpose, and it's marked with a star, in order to provoke you to look for information on the Internet. This is one of the most important skills that you have to develop while you're learning programming: **looking for information on the Internet**. This is what you're going to do every day, if you work as a developer, so don't be scared, try it out. If you have any difficulties you can also ask for help in the SoftUni forum: https://softuni.bg/forum.

#### Testing in the Judge System
## Testing in the Judge System

Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/503#5](https://judge.softuni.bg/Contests/Practice/Index/503#5).
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Problem: A Triangle Made Out of 55 Stars
# Problem: A Triangle Made Out of 55 Stars

Write a C# console program that **prints a triangle made out of 55 stars** on 10 rows:
Write a C\# console program that **prints a triangle made out of 55 stars** on 10 rows:

```
*
Expand All @@ -15,17 +15,19 @@ Write a C# console program that **prints a triangle made out of 55 stars** on 10
**********
```

#### Tips and Tricks
## Tips and Tricks

Create **a new console C\# application** with name “**`TriangleOf55Stars`**”. Inside it, write code that prints the triangle of stars, for example through 10 commands, as the ones pointed out below:

Create **a new console C# application** with name “**TriangleOf55Stars**”. Inside it, write code that prints the triangle of stars, for example through 10 commands, as the ones pointed out below:
```csharp
Console.WriteLine("*");
Console.WriteLine("**");
```

#### Testing in the Judge System
## Testing in the Judge System

Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/503\#3](https://judge.softuni.bg/Contests/Practice/Index/503#3).

Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/503#3](https://judge.softuni.bg/Contests/Practice/Index/503#3).
Try to **improve your solution**, so that it doesn't have many repeating commands. Could it be done with a `for` loop? Did you find a smart solution \(for example with a loop\) of the previous task? With this task you can also use something similar, but a bit more complex \(two loops, one inside the other\). If you don't succeed, there is no problem, we will be learning loops in a few chapters and you will be reminded of this task then.

Try to **improve your solution**, so that it doesn't have many repeating commands. Could it be done with a **`for`** loop? Did you find a smart solution (for example with a loop) of the previous task? With this task you can also use something similar, but a bit more complex (two loops, one inside the other). If you don't succeed, there is no problem, we will be learning loops in a few chapters and you will be reminded of this task then.
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
## Exercises: Graphical and Web Applications
# Exercises: Graphical and Web Applications

Now we are about to build one simple **web application** and one simple **graphical application**, in order to take a look at what we will be able to create when we progress with programming and software development. We are not going to look through the details about the used techniques and constructions fundamentally. Rather than that, we are just going to take a look at the arrangement and functionality of our creation. After we progress with our knowledge, we will be able to do bigger and more complex software applications and systems. We hope that the examples given below will **straighten your interest**, rather than make you give up.

## Console, Graphical and Web Applications

With **console applications**, as you can figure out yourselves, **all operations** for reading input and printing output are **done through the console**. **The input data is inserted** in the console, which is then read by the application, also in it, and the **output data is printed** on the console after or during the runtime of the program.

While a console application **uses the text console**, web applications **use web-based user interface**. To **execute them**, two things are needed - **a web server** and **a web browser**, as **the browser** plays the main role in **the visualization of the data and the interaction with the user**. Web applications are much more pleasant for the user, they visually look better, and a mouse and touch screen can be used \(for tablets and smartphones\), but programming stands behind all of that. And this is why **we have to learn to program** and we have already made our first very little steps towards that.

Graphical \(GUI\) applications have **a visual user interface**, directly into your computer or mobile device, without a web browser. Graphical applications \(also known as desktop applications\) **contain one or more graphical windows**, in which certain **controllers** are located \(text fields, buttons, pictures, tables and others\), **serving for dialog** with the user in a more intuitive way. Similar to them are the mobile applications in your telephone or your tablet: we use forms, text fields, buttons and other controls and we control them by programming code. This is why we are learning how to write code now: **the code is everywhere in software development**.

## Exercises: GUI and Web Applications

In the next exercises we shall create a GUI and a Web application:

* [Problem: Graphical Application "Sumator" \(Calculator\)](/Content/Chapter-1-first-steps-in-programming/exercises-graphical-and-web-apps/sumator-graphical/sumator-graphical.md)
* [Problem: Web Application "Sumator" \(Calculator\)](/Content/Chapter-1-first-steps-in-programming/exercises-graphical-and-web-apps/sumator-web/sumator-web.md)



Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
#### Adding Text Fields and a Button
####

We download from the bar on the left (Toolbox) **three text boxes** (**`TextBox`**), **two labels** (**`Label`**) and **a button** (**`Button`**), afterwards we arrange them in the window of the application. Then we **change the names of each of the controls**. This is done from **the window “Properties”** on the right, by changing the field (**`Name`**):

![](/assets/chapter-1-images/07.Numbers-sum-04.png)

* Names of the text boxes: **`textBox1`**, **`textBox2`**, **`textBoxSum`**
* Name of the button: **`buttonCalculate`**
* Name of the form: **`FormCalculate`**

**We change the headings** (the **`Text`** property) of the controls:

* buttonCalculate -> Calculate
* label1 -> +
* label2 -> =
* Form1 -> Sumator

![](/assets/chapter-1-images/07.Numbers-sum-05.png)
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
#### Solving the Problem and Retesting the Application
####

The problem comes from **the conversion of the text field into a number**. If the value inside the field **is not a number, the program throws an exception**. We can rewrite the code in order to fix this problem:

![](/assets/chapter-1-images/07.Numbers-sum-14.png)

The code above **catches the errors when working with numbers** (it catches exceptions) and in case of an error **it gives a value `error`** in the field with the result. We start the program again with [**Ctrl+F5**] and try if it works. This time **by entering a wrong number the result is `error`** and the program doesn't break:

![](/assets/chapter-1-images/07.Numbers-sum-15.png) ![](/assets/chapter-1-images/07.Numbers-sum-16.png)

Is it complicated? It is normal to seem complex, of course. We are just beginning to get into programming. The example above requires much more knowledge and skills, which we are going to develop through this book and even afterwards. Just allow yourself to have some fun with desktop programming. If something doesn't work, watch **the video in the beginning of this chapter** or ask in **the SoftUni forum**: https://softuni.bg/forum. Or move on bravely forward to the next example or to the next chapter of the book. A time will come when it is going to be easy for you, but you really have to put **an effort and be persistent**. Programming is learnt slowly with lots and lots of practice.
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
#### Creating a New C# Project
####

In Visual Studio we create **a new C# project of type „Windows Forms Application“**:

![](/assets/chapter-1-images/07.Numbers-sum-02.png)

When creating a Windows Forms application **an editor for user interface** will be shown, in which **different visual elements** could be put (for example textboxes and buttons):

![](/assets/chapter-1-images/07.Numbers-sum-03.png)
Loading

0 comments on commit d4e3bad

Please sign in to comment.