Presentation is loading. Please wait.

Presentation is loading. Please wait.

(c) Bob McKillop, 2006Tutorial #1 T1-1 Tutorial #1 - your first compiled code  the learning objectives for this tutorial are very modest q compile and.

Similar presentations


Presentation on theme: "(c) Bob McKillop, 2006Tutorial #1 T1-1 Tutorial #1 - your first compiled code  the learning objectives for this tutorial are very modest q compile and."— Presentation transcript:

1 (c) Bob McKillop, 2006Tutorial #1 T1-1 Tutorial #1 - your first compiled code  the learning objectives for this tutorial are very modest q compile and execute three VB.NET console applications on the NEXUS system q program 1 – Hello World! q program 2 – obtaining the user’s name from the console q program 3 – computing the real roots of a quadratic equation  the purpose of the tutorial is to provide practice writing, compiling and executing simple code snippets.  you are NOT expected to understand anything that you type! Assignment #1  each student is asked modify program #3 and submit an improved version of program #3 before noon on Friday.

2 (c) Bob McKillop, 2006Tutorial #1 T1-2  using the Windows Start Menu, select: >>All Programs >>Programming >>Microsoft Visual Studio.NET 2003 >>Visual Studio. NET Tools >> Visual Studio.NET 2003 Command Prompt

3 (c) Bob McKillop, 2006Tutorial #1 T1-3  your screen should now display the Visual Studio.NET Command Prompt  your prompt should indicate that you are presently working on the N: drive n:  if not, type n: and hit the Enter key

4 (c) Bob McKillop, 2006Tutorial #1 T1-4 vbc  type vbc and hit the Enter key  your screen should now include a listing of the Visual Basic compiler options  it is not as scary as it looks. We will be using only a few of the compiler options.

5 (c) Bob McKillop, 2006Tutorial #1 T1-5 cls  type cls to clear the screen display  let’s make a working directory (folder) on your n: drive md cive121  type md cive121 and hit the Enter key. cd cive121  type cd cive121 and hit the Enter key dir  type dir and hit the Enter key. The display should list the contents of your newly created directory (folder). In our case, there should be no files located within the directory…yet

6 (c) Bob McKillop, 2006Tutorial #1 T1-6 Program #1  you will need a code editor for this course  there are plenty of free code editors available on the web (NotePad2, EditPad Lite, etc.) NotePad  for today, use the default NotePad editor provided with Windows. From the Windows Start Menu, choose: >>All programs >>Accessories >>NotePad  open the text editor and carefully enter the following code hello.vbcive121  save your untitled document as hello.vb and be sure to save it in the cive121 directory

7 (c) Bob McKillop, 2006Tutorial #1 T1-7 dir hello.vb  at the command prompt, type dir and hit the Enter key. You should now see your newly created text file name hello.vb within the directory listing vbc hello.vb  type vbc hello.vb and hit the Enter key. Hopefully, your program compiles successfully. dir hello.exe  at the command prompt, type dir and hit the Enter key. You should now see your newly created hello.exe executable

8 (c) Bob McKillop, 2006Tutorial #1 T1-8 hellohello.exe  at the command prompt, type hello (or hello.exe) and hit the Enter key. Hello World!  you should now see the message Hello World! displayed on your console  congratulations, you have successfully created, compiled and executed your first VB.NET program!

9 (c) Bob McKillop, 2006Tutorial #1 T1-9 Commenting your code  It is important to appreciate this simple fact…a computer program is to be executed by a computer but it will be read by other people q large programs may involve dozens if not hundreds of programmers q most programs undergo: significant modification and/or upgrading fixing bugs/errors q it is important to comment our code in order for other people to understand how our program works, today or several years from now. q in many cases, a programmer will revisit his/her own program after several months ors year and it is easy to forget some of the important details about your own code `  in VB.NET, the compiler ignores any instructions located to the right of an apostrophe (`)

10 (c) Bob McKillop, 2006Tutorial #1 T1-10  comment your previous code as follows. ‘ ============================================================================ ‘ Program #1 ‘ Written by: Bob McKillop ‘ Date: January 03, 2006 ‘ ============================================================================ ‘ impose strict type checking Option Strict On ‘ impose strict type checking ‘ import system namespace Imports System ‘ import system namespace Module MyFirstProgram Public Sub Main() ‘ direct output stream to Console.WriteLine("Hello World!") ‘ direct output stream to ‘ the console End Sub End Module  compile and execute your program and observe the program output

11 (c) Bob McKillop, 2006Tutorial #1 T1-11 Program #2  program #1 provided an easy introduction to compiling and executing a VB program but…the program did not accept any input and it did not make use of any variables (data) hello.vbintro.vb  save your hello.vb program as intro.vb  we will now modify the previous program in order to prompt the user for his/her first name  the program will then offer an introductory greeting to the user as illustrated below:

12 (c) Bob McKillop, 2006Tutorial #1 T1-12  enter the following code. Compile and execute the program. When prompted, type in your name and hit the key. ‘ ============================================================================ ‘ Program #2 ‘ Written by: Bob McKillop ‘ Date: January 03, 2006 ‘ ‘ Modified: January 03, 2006 by Bob McKillop ‘ Added ReadLine method in order to obtain users first name ‘ from the Console ‘ ============================================================================ ‘ impose strict type checking Option Strict On ‘ impose strict type checking ‘ import System namespace Imports System ‘ import System namespace Imports System.IO‘ import System.IO namespace Imports System.IO ‘ import System.IO namespace Module Program_02 Public Sub Main() Console.WriteLine() Dim name As String ‘ Declare String object Dim name As String ‘ Declare String object Console.Write("Please type your first name: ") Console.Write("Please type your first name: ") name = Console.ReadLine() ‘ Obtain input stream name = Console.ReadLine() ‘ Obtain input stream Console.WriteLine() Console.WriteLine() Console.WriteLine ("Hello " & name) Console.WriteLine ("Hello " & name) Console.WriteLine ("Welcome to the course!") Console.WriteLine ("Welcome to the course!") End Sub End Module

13 (c) Bob McKillop, 2006Tutorial #1 T1-13 Program #3 intro.vbquadratic.vb  save your intro.vb program as quadratic.vb  our previous programs did not perform any computations and program #2 made use of a single String type.  let’s write a program capable of computing the roots of an expression of the form:  recall that the closed-form solution is given by:  for this problem, assume that real roots always exist  test your program using q a = 1 q b = 3, and q c = -18

14 (c) Bob McKillop, 2006Tutorial #1 T1-14  enter the following code. Compile and execute the program. ' ========================================================================== ' Program #3 ' Written By: Bob McKillop ' Date: January 03, 2006 ' Purpose To compute quadratic roots ' Inputs Constants a, b and c ' Outputs The 2 real roots ' Limitations This program does not check for the existence of ' non-existing or complex roots ' ========================================================================== Option Strict On Imports System 'Import System.Math namespace Imports System.Math 'Import System.Math namespace Module Program_03 Public Sub Main() Dim a As Double = 1 Dim b As Double = 3 Dim c As Double = -18 Dim root1, root2 As Double Dim term As Double term = b ^ 2 - 4 * a * c root1 = (-b + Sqrt(term)) / (2 * a) root2 = (-b - Sqrt(term)) / (2 * a) Console.WriteLine ("The first root is {0,6:F2}", root1) Console.WriteLine ("The second root is {0,5:F2}", root2) End Sub End Module

15 (c) Bob McKillop, 2006Tutorial #1 T1-15 Assignment #1  Modify program #3 in order for the following output to be exactly produced: Friday January 06  Before Friday January 06 (at noon), submit to the WEEF assignment box: q a listing of your program code. Make sure your code includes a fully completed and signed program header, available on the course web site (see page 3-9 of the course notes for more details). If you are having difficulty with console output, refer to some of the example code provided in Chapter 5 of the course notes. q a screen capture illustrating your program output


Download ppt "(c) Bob McKillop, 2006Tutorial #1 T1-1 Tutorial #1 - your first compiled code  the learning objectives for this tutorial are very modest q compile and."

Similar presentations


Ads by Google