Upload
elinor-robbins
View
212
Download
0
Embed Size (px)
Citation preview
(c) Bob McKillop, 2006
Tutorial #1
T1-1Tutorial #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 you are NOT expected to understand anything that you
type!type!
Assignment #1Assignment #1
each student is asked modify program #3 and submit an improved version of program #3 before noon on Friday.
(c) Bob McKillop, 2006
Tutorial #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
(c) Bob McKillop, 2006
Tutorial #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
if not, type n:n: and hit the Enter key
(c) Bob McKillop, 2006
Tutorial #1
T1-4
type vbcvbc 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.
(c) Bob McKillop, 2006
Tutorial #1
T1-5
type clscls to clear the screen display
let’s make a working directory (folder) on your n: drive
type md cive121md cive121 and hit the Enter key.
type cd cive121cd cive121 and hit the Enter key
type dirdir 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
(c) Bob McKillop, 2006
Tutorial #1
T1-6Program #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 NotePadNotePad 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.vbhello.vb and be sure to save it in the cive121cive121 directory
(c) Bob McKillop, 2006
Tutorial #1
T1-7
at the command prompt, type dirdir and hit the Enter key. You should now see your newly created text file name hello.vbhello.vb within the directory listing
type vbc hello.vbvbc hello.vb and hit the Enter key. Hopefully, your program compiles successfully.
at the command prompt, type dirdir and hit the Enter key. You should now see your newly created
hello.exehello.exe executable
(c) Bob McKillop, 2006
Tutorial #1
T1-8
at the command prompt, type hellohello (or hello.exehello.exe) and hit the Enter key.
you should now see the message Hello World!Hello World! displayed on your console
congratulations, you have successfully created, compiled and executed your first VB.NET program!
(c) Bob McKillop, 2006
Tutorial #1
T1-9Commenting 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 (`̀)
(c) Bob McKillop, 2006
Tutorial #1
T1-10
comment your previous code as follows.
‘ ‘ ========================================================================================================================================================
‘ ‘ Program #1Program #1
‘ ‘ Written by: Bob McKillopWritten by: Bob McKillop
‘ ‘ Date: January 03, 2006Date: January 03, 2006
‘ ‘ ========================================================================================================================================================
Option Strict On ‘ impose strict type checking‘ impose strict type checking
Imports System ‘ import system namespace‘ import system namespace
Module MyFirstProgram
Public Sub Main()
Console.WriteLine("Hello World!") ‘ direct output stream to ‘ direct output stream to
‘ ‘ the consolethe console
End Sub
End Module
compile and execute your program and observe the program output
(c) Bob McKillop, 2006
Tutorial #1
T1-11Program #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.vbhello.vb program as intro.vbintro.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:
(c) Bob McKillop, 2006
Tutorial #1
T1-12
enter the following code. Compile and execute the program. When prompted, type in your name and hit the <Enter> key.
‘ ‘ ========================================================================================================================================================‘ ‘ Program #2Program #2‘ ‘ Written by: Bob McKillopWritten by: Bob McKillop‘ ‘ Date: January 03, 2006Date: January 03, 2006‘ ‘ ‘ ‘ Modified: January 03, 2006 by Bob McKillopModified: January 03, 2006 by Bob McKillop‘ ‘ Added ReadLine method in order to obtain users first nameAdded ReadLine method in order to obtain users first name‘ ‘ from the Console from the Console ‘ ‘ ========================================================================================================================================================Option Strict On ‘ impose strict type checking‘ impose strict type checkingImports System ‘ import System namespace‘ import System namespaceImports System.IOImports System.IO ‘ import System.IO namespace‘ import System.IO namespaceModule Program_02 Public Sub Main() Console.WriteLine()Console.WriteLine() Dim name As String ‘ Declare String objectDim 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 SubEnd Module
(c) Bob McKillop, 2006
Tutorial #1
T1-13Program #3
save your intro.vbintro.vb program as quadratic.vbquadratic.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
a
acbbx
2
42
02 cbxax
(c) Bob McKillop, 2006
Tutorial #1
T1-14
enter the following code. Compile and execute the program. ' ==========================================================================' ==========================================================================' Program #3 ' Program #3 ' Written By: Bob McKillop' Written By: Bob McKillop' Date: January 03, 2006' Date: January 03, 2006' Purpose To compute quadratic roots' Purpose To compute quadratic roots' Inputs Constants a, b and c' Inputs Constants a, b and c' Outputs The 2 real roots ' Outputs The 2 real roots ' Limitations This program does not check for the existence of' Limitations This program does not check for the existence of' non-existing or complex roots ' non-existing or complex roots ' ==========================================================================' ==========================================================================Option Strict OnImports SystemImports System.Math 'Import System.Math namespace'Import System.Math namespaceModule 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 SubEnd Module
(c) Bob McKillop, 2006
Tutorial #1
T1-15Assignment #1
Modify program #3 in order for the following output to be exactly produced:
Before Friday January 06Friday 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