150 likes | 263 Views
Tutorial #1 - your first compiled code. the learning objectives for this tutorial are very modest compile and execute three VB.NET console applications on the NEXUS system program 1 – Hello World! program 2 – obtaining the user’s name from the console
E N D
Tutorial #1 - your first compiled code • the learning objectives for this tutorial are very modest • compile and execute three VB.NET console applications on the NEXUS system • program 1 – Hello World! • program 2 – obtaining the user’s name from the console • 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. Tutorial #1
using the Windows Start Menu, select: >>All Programs >>Programming >>Microsoft Visual Studio.NET 2003 >>Visual Studio. NET Tools >> Visual Studio .NET 2003 Command Prompt Tutorial #1
your screen should now display the Visual Studio .NET Command Prompt • your prompt should indicate that you are presently working on the N: drive • if not, type n: and hit the Enter key Tutorial #1
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. Tutorial #1
type cls to clear the screen display • let’s make a working directory (folder) on your n: drive • type md cive121 and hit the Enter key. • type cd cive121 and hit the Enter key • 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 Tutorial #1
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.) • 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 • save your untitled document as hello.vb and be sure to save it in the cive121 directory Tutorial #1
at the command prompt, type dirand hit the Enter key. You should now see your newly created text file name hello.vbwithin the directory listing • type vbc hello.vb and hit the Enter key. Hopefully, your program compiles successfully. • at the command prompt, type dirand hit the Enter key. You should now see your newly created hello.exe executable Tutorial #1
at the command prompt, type hello(or hello.exe) and hit the Enter key. • 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! Tutorial #1
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 • large programs may involve dozens if not hundreds of programmers • most programs undergo: • significant modification and/or upgrading • fixing bugs/errors • it is important to comment our code in order for other people to understand how our program works, today or several years from now. • 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 (`) Tutorial #1
comment your previous code as follows. ‘ ============================================================================ ‘ Program #1 ‘ Written by: Bob McKillop ‘ Date: January 03, 2006 ‘ ============================================================================ Option Strict On ‘ impose strict type checking Imports System ‘ import system namespace Module MyFirstProgram Public Sub Main() Console.WriteLine("Hello World!") ‘ direct output stream to ‘ the console End Sub End Module • compile and execute your program and observe the program output Tutorial #1
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) • 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: Tutorial #1
enter the following code. Compile and execute the program. When prompted, type in your name and hit the <Enter> 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 ‘ ============================================================================ Option Strict On‘ impose strict type checking Imports System‘ import System namespace Imports System.IO‘ import System.IO namespace Module Program_02 Public Sub Main() Console.WriteLine() Dim name As String ‘ Declare String object Console.Write("Please type your first name: ") name = Console.ReadLine() ‘ Obtain input stream Console.WriteLine() Console.WriteLine ("Hello " & name) Console.WriteLine ("Welcome to the course!") End Sub End Module Tutorial #1
Program #3 • 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 • a = 1 • b = 3, and • c = -18 Tutorial #1
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 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 Tutorial #1
Assignment #1 • Modify program #3 in order for the following output to be exactly produced: • Before Friday January 06 (at noon), submit to the WEEF assignment box: • 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. • a screen capture illustrating your program output Tutorial #1