• Tidak ada hasil yang ditemukan

C# 1: Basic Syntax and Semantics

N/A
N/A
Nguyễn Gia Hào

Academic year: 2023

Membagikan "C# 1: Basic Syntax and Semantics"

Copied!
167
0
0

Teks penuh

The programmer's task is to write a program as text in the chosen programming language, which here is C#. If you click on the item, the program starts and Visual Studio opens the following window:.

THE KINGS

The Digits program

3 COMMANDS AND

CONSOLE PROGRAMS

  • COMMANDS
  • PRINTADDRESS
  • Print a recall
    • CONSOLE PROGRAMS
  • Another recall

The program is called PrintAddress and it takes four arguments to pass on the command line. The first instruction in the program prints a text on the screen, telling the user to enter the name.

4 VARIABLES AND DATA TYPES

Sum of four numbers

  • OPERATORS

It then executes a++, that is, the value of a is printed, and then the variable a is incremented by 1. Since it is false, the statement prints the value of b, which is the smaller of the two numbers a and b.

Operations

Arithmetic

  • LITERALS
  • FORMATTING OUTPUT
  • OBJECTS
  • THEADDRESS

The class has a property named Value whose value is the value of the banknote, which is the value of the value variable. Then the class methods determine what is possible with the class object (an object is a specified address).

TheBook

  • INHERITANCE
  • NAMESPACES

This happens in the class constructor by calling base(100), which means that the base class constructor is executed. It is defined in the BankNote class, but is inherited by the BankNote100 and BankNote200 classes. This means that the class must have a Value property and a Print() method - it must implement the methods defined by the interface.

More banknotes

Employees

  • EXAMPLE: CUBES

Create an Employee object, which is an accountant named Gudrun Jensen with a monthly salary of 45000. Create an Employee object that is a janitor named Karlo Hansen with a monthly salary of 35000. Then there is a ToString() method that returns the value of the cube as the text.

Coins

  • ARRAYS

Here you create an array named arr that holds 10 elements of type int. The program has a random number generator and creates an array with space for 5 elements of type int. In particular, you should consider how it relates to the number of elements in the array.

ArrayDemo

  • EXAMPLE: CUPPROGRAM
  • MULTIDIMENSIONAL ARRAYS

The variable arr is an array of type Cube, but it is not created in the declaration, but in the constructor. An array as described above is sometimes called a one-dimensional array, meaning that it is a number or sequence of elements in which the elements are identified by a single index. The two methods Fill() and Print() are typical examples of using 2-dimensional arrays and the syntax for traversing such an array with two for loops.

Another ArrayDemo

5 PROGRAM CONTROL

THE IF STATEMENT

In particular, attention should be paid to the fact that if a variable is declared in a block, then it is known only within the block. Another control statement is called an if-else statement, but it is little more than an extension of the if statement. In practice, if statements and if-else statements are the most common control statements, and it is difficult to imagine a program where these statements do not occur.

Sort to elements

The if statement and the if-else statement are also called for selections (or branches) corresponding to making the execution of statements dependent on a condition.

Sort three elements

A quadratic equation

  • DO AND WHILE STATEMENTS

Note that this time the input value must be converted to a double and therefore the method must also return a double. If true, the block executes again and repeats until the condition becomes false. A do statement is an example of an iteration or loop, meaning that the statement or block is executed multiple times.

The sum command

This means that if one of the statements in the try block returns an error, the try block is aborted and control is transferred to the catch block. In this case, the catch block should do nothing but print an error message. You need to end the try block, so you use a while statement to determine the sum of all integers from a to b and print the result.

Enter numbers

  • THE FOR STATEMENT

Number of numbers entered: 10 Sum of numbers entered: 129 Smallest number: 2 Largest number: 29 Average of numbers entered: 12.9. It is the most flexible of the three control statements for loops and also the most widely used. This variant of the for statement works with an iterator, but more on that later.

Printsum

So far, the only advantage to writing the statement that way is a slightly shorter note, and perhaps the statement is also quite a bit more readable and easier to understand.

The Fibonacci numbers

A prime

  • THE SWITCH STATEMENT

You must write a program where the user must enter an integer and if it is a legal integer, the program must print where the number is prime. When the statement is executed, the expression is evaluated and control is transferred to the case input whose value is identical to the value of the expression. If there is no case entry corresponding to the value of the expression, there are two options.

Seasons

  • RETURN STATEMENT
  • BREAK, CONTINUE AND GOTO

Generally, the statement interrupts a method and passes the control to where the method was called. However, if the method has a type, then after the return there must be an expression of the same type as the method. This can be done with break, and the method continues with the first statement after the loop.

A palindrome

Then the method iterates through the string in a for loop to build a substring consisting of all characters that are digits. This means that you transfer control to the end of the block, thereby skipping the statement. Also note that the problem in this case can be solved other than using continue.

Craps

Add a class game that represents the game itself, simulating that the game is played a certain number of times and printing the result. Write the code for the main class where the user can enter the number of times the game should be played. The above version of the game is somewhat simplified and there are more rules associated with the game.

6 LIST

Standard deviation

The input should continue until 0 is entered, after which the program should output the standard deviation of the numbers.

7 COMPARISON AND SORTING

A sorted cup

When you run the program, it shows the values ​​of the five cubes in random order. In the class cup, the method toss() must sort the cubes after the cubes are rolled.

8 FILES

TEXT FILES

The first statement opens the file for writing, and in the program the file is called file (the variable that refers to the file). On disk, the file is called numbers, and it is saved in the program's current directory. The subsequent for-loop starts by reading the first line of the file in the initialization.

A file containing names

  • SERIALIZATION OF OBJECTS

The serialized object is the object returned by the CreateNames() method, which is a list of four Name objects. Please note that the contents of the file are not clear text this time, and trying to open it will give you a result that is not easy to understand. It returns an Object, so with a typecast it is necessary to convert the object to a List before sending it to the PrintNames() method.

Write an array to a file

Again, there is not much to explain, but you can see that a file where to load objects is represented by an object of type FileStream, but this time opened for reading. Then write a method Test2() to deserialize the object from the beginning (the array with the 100 numbers) and print the sum of the numbers on the screen.

9 MORE ABOUT CLASSES

THE PROGRAM STACK

It is also the heap manager, which automatically deletes an object when it is no longer needed. An object is created from a class and is then assigned a reference that identifies the object. The methods of the class determine what you can do with the object and thus the behavior of the object.

CLASS EXAMPLES

In this case, the class has two variables, both of type string (string or String is itself a class). This method is statically defined, which means it can be used without having created an object of the class. When you print an object using WriteLine(), it is the value of the object's ToString() method that is printed.

A Publisher

  • MORE CLASSES

You should be aware that the Score property may throw an exception and therefore the Print() method may throw an exception, and if it does, the method will terminate and the Exception object will be passed to the Test1() method. The last one is of type List, and should be used for the courses the student has completed or is taking. It means that the Student class can be used with the same syntax as an array.

An Author

Finally, there should be a ToString() method that returns an author's first and last name separated by a space. A number of authors, which must be Author objects (the number of authors must be 0 if no authors are known). The classbook must have a ToString() method which returns the book's ISBN and title separated by a space.

Validate ISBN

  • METHODS
  • OBJECTS
  • VISIBILITY
  • STATIC MEMBERS

For example, the GetCourses() method in the Student class returns a List, and precisely, it is a reference to such an object. You should specifically note that within the class in which the method is defined, it can be referenced in the same way as any other method of the class. Because the runtime system does not create an object of the Program class, the method must be static.

10 FINAL EXAMPLE: LOTTO

DESIGN

Additionally, the figure shows that the class must have a getNumber() method to return the lottery number value. With this class available, it is clear that the LottoNumber class and its methods can verify whether a lottery number is correct. Last is the Lotto class, which is the class with the Main() method.

PROGRAMMING AND TEST

Referensi

Dokumen terkait

We show that the probability distribu- tions of returns and trading volume generated by computer simulations of the model have power-law tails when the interaction among the interacting