tag:blogger.com,1999:blog-32037518366044280142009-04-26T08:50:17.365-07:00CS 80 Problem Solving with ComputersBlog for CS 80 Course at Queens College
(c) - Jamie LevyJLjamie.levy@gmail.comBlogger24125tag:blogger.com,1999:blog-3203751836604428014.post-19231710657478680842008-05-08T08:34:00.000-07:002008-05-08T09:23:50.838-07:00SubroutinesSubroutines and functions allow us to package similar instructions together. We've seen built-in subroutines before like:<br /><br /><tt><font color="blue">Math.Sqrt()<br />Int()<br />Math.Round()<br />FormatNumber()<br />FormatCurrency()<br />FormatPercent()<br />str.IndexOf()<br />str.Length()<br />str.Substring()<br />...etc...</font></tt><br /><br />Each one of these subroutines/functions does something useful. We can use them to do a certain job, whether it is changing a number value in some way, formatting output or figuring out certain characteristics of strings. <br /><br />We also learned that we can make our own functions and subroutines to do things that we decide. So let's say that we want to write a print subroutine that will just print out our report, and a calculate() function that will take in three of the expenses and add them together and return the answer. Take a look at the code:<br /><a href="http://i158.photobucket.com/albums/t115/gleeda/CS%2080/function2.jpg">Click for larger image</a>.<br /><br /><img src="http://i158.photobucket.com/albums/t115/gleeda/CS%2080/function2.jpg" width="450" height="300"><br /><br />You just have to make sure to write the function and subroutine code outside of the <tt><font color="blue">button1_click()</font></tt> subroutine and inside the <tt><font color="blue">Class Form1</font></tt> block.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-1923171065747868084?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-85391982893894490482008-05-06T06:14:00.000-07:002008-05-08T09:22:26.005-07:002-Dimensional ArraysWe can declare 2-Dimensional arrays by using the following statement:<br /><pre><br /> <font color="blue">Dim</font> arrayName(r,c) <font color="blue">As Type</font><br /></pre><br />Where <b>arrayName</b> is the name of the array you have created and <b>Type</b> is the array's type. The subscripts <b>r</b> and <b>c</b> represent the <b><i>Row</i></b> and <b><i>Column</i></b> of the array.<br /><br />So we could declare an array as type double to hold students' grades:<br /><pre><br /> <font color="blue">Dim</font> grades(3,3) <font color="blue">As Double</font><br /> <font color="green">'Four students with four grades</font><br /></pre><br /><br />This creates a 4x4 matrix with rows and columns starting at 0 and ending at 3:<br /><pre><br /> 0 1 2 3<br /> +---+---+---+---+<br />0 | | | | |<br /> +---+---+---+---+<br />1 | | | | |<br /> +---+---+---+---+<br />2 | | | | |<br /> +---+---+---+---+<br />3 | | | | |<br /> +---+---+---+---+<br /></pre><br />We can set any cell in the array by issuing a call such as:<br /><pre><br /> grades(1,1) = 90<br /> grades(3,0) = 99<br /> grades(1,0) = 100<br /></pre><br />With the above code our resulting matrix will look like:<br /><pre><br /> 0 1 2 3<br /> +---+---+---+---+<br />0 | | | | |<br /> +---+---+---+---+<br />1 |100| 90| | |<br /> +---+---+---+---+<br />2 | | | | |<br /> +---+---+---+---+<br />3 |99 | | | |<br /> +---+---+---+---+<br /></pre><br />If we wanted to set all the cells to a particular value, such as 0 for example, we would have to use a nested loop:<br /><pre><br /> <font color="blue">For</font> i <font color="blue">As Integer</font>=0 <font color="blue">To</font> 3<br /> <font color="blue">For</font> j <font color="blue">As Integer</font>=0 <font color="blue">To</font> 3<br /> grades(i,j) = 0<br /> <font color="blue">Next</font><br /> <font color="blue">Next</font><br /></pre><br /><br />We can also declare the 2-dimensional array without a size as we can with regular arrays:<br /><pre><br /> <font color="blue">Dim</font> arrayName(,) <font color="blue">As Type</font><br /> <font color="blue">Dim</font> array2(,) <font color="blue">As Type</font> = {{ROW0},{ROW1},{ROW2},...,{ROWm}}<br /><br /> <font color="blue">ReDim</font> arrayName(r,s)<br /> <font color="blue">ReDim Preserve</font> arrayName(r,s)<br /></pre><br />Real Examples:<br /><pre><br /> <font color="blue">Dim</font> grades(,) = {{99,50,80},{100,80,70},{90,88,90}}<br /> <font color="blue">ReDim Preserve</font> grades(3,4)<br /></pre><br /><span style="font-weight:bold;">GetUpperBound</span><br /><br />There are two different <font color="blue"><span style="font-weight:bold;">GetUpperBound</span></font> calls for 2-dimensional arrays:<br /><pre><br /> arrayName<font color="blue">.GetUpperBound(0)</font><br /> arrayName<font color="blue">.GetUpperBound(1)</font><br /></pre><br />The first call gives you the upperbound on the number of rows, while the second one gives you the upperbound on the number of columns.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-8539198289389449048?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-70925689264367527202008-05-01T09:37:00.000-07:002008-05-08T09:24:26.814-07:00Some Exam ProblemsProblems from the <a href="http://venus.cs.qc.edu/~jlevy/cs80/docs/sample-Test.pdf">first sample test</a> are as follows (Hopefully without typos):<br /><br />1) D<br />2) A<br />3) C<br />4) C<br />5) C<br />6) C<br />7) C<br />8) D<br />9) B<br />10) B<br /><br />Discussion:<br /><pre>Code from #10:<br />Dim place, i As Integer<br />Dim character, sentence, phrase(9) As String<br />sentence = "Every path hath a puddle."<br />place = 0<br />i = 0<br />character = sentence.Substring(place, 1)<br />Do<br /> If character = " " Then<br /> <font color="maroon"><b>phrase(i) = sentence.Substring(0, place)</b></font><br /> i += 1<br /> End If<br /> place += 1<br />character = sentence.Substring(place, 1)<br />Loop Until character = "."<br /></pre><br /><br />The answer is ``Every path". <br /><pre><br />We have the following variables:<br />i 'The current index of the array<br />place 'The current location in the sentence string<br />character 'The current character at which we are looking<br />sentence 'String: "Every path hath a puddle."<br />phrase() 'Array of size 10</pre><br /><br />What the code does, loosely, is going through the <b>sentence</b> string one character at a time and every time it hits a space, it saves the all of the <span style="font-weight:bold;">i+1</span> words in the current array index <b>i</b> into the array <b>phrase()</b>.<br /><br />If we look at the part of the code that actually changes the array (highlighted above) then we can see that every time the array is updated it starts at the very beginning and goes up to <i><b>place</b></i> characters. Therefore the final array should look like:<br /><pre><br /> 0 1 2 ...<br />+-------+------------+-----------------+----<br />| Every | Every path | Every path hath | ...<br />+-------+------------+-----------------+----<br /></pre><br />Where every cell has one extra word in it from the beginning. The program stops when <b>character = "."</b> which is at the end of the <b>sentence</b> string.<br /><br />Programming Problem:<br /><pre>a)<br /><br />Dim arr(1000) As Integer<br />For i As Integer = 1 To 1000<br /> arr(i) = CInt(InputBox("Enter number: " ,""))<br />Next<br /><br />For j As Integer = 1000 To 1, Step -1<br /> lstBoxOut.Items.Add( arr(j) )<br />Next<br /><br />b)<br />Dim arr(1000) As Integer<br />For i As Integer = 1 To 1000<br /> arr(i) = CInt(InputBox("Enter number: " ,""))<br />Next<br /><br />For j As Integer = 1000 To 1, Step -1<br /> <font color="maroon"><b>If arr(j) Mod 2 = 0 Then</b></font><br /> lstBoxOut.Items.Add( arr(j) )<br /> <font color="maroon"><b>End If</b></font><br />Next</pre><br /><font size="4">Second Practice Exam</font><br /><br />You can find a past <a href="http://venus.cs.qc.edu/~jlevy/cs80/exam2.doc">exam 2 here</a>. I made a mistake on the first question when I gave you the answers. Here are the correct answers:<br /><br />1) D<br />2) C<br />3) B<br />4) B<br />5) D<br />6) D<br />7) C<br />8) B<br />9) C<br />10) A<br /><br />Part II<br />a) 70^2 = 4900<br />b) 24^2 = 576<br /><br />Part III<br />Answer: 3<br /><br />Discussion:<br />The first two loops simply save all of the numbers from the files into arrays a() and b()<br /><pre><br />sr = IO.File.OpenText("DATA1.TXT")<br />For k As Integer= 0 To 19<br /> a(k) = CDbl(sr.ReadLine)<br />Next<br />sr.Close()<br />sr = IO.File.OpenText("DATA2.TXT")<br />For k As Integer = 0 To 19<br /> b(k) = CDbl(sr.ReadLine)<br />Next</pre><br /><br />After this, a() contains:<br />3, 2, 5, 1, 7, 8, 3, 5, 6, 2, 3, 6, 1, 6, 5, 5, 7, 2, 5, 3<br /><br />and b() contains:<br />5, 3, 3, 4, 8, 2, 3, 5, 9, 5, 3, 7, 3, 7, 6, 3, 2, 1, 3, 4<br /><br />This is in order so a(0) equals 3 and a(1) equals 2; b(0) equals 5 and b(1) equals 3 etc...<br /><br />The last loop is what changes variable <b>c</b> before it is output:<br /><pre><br />For k As Integer = 0 To 19<br /> <font color="maroon"><b>If a(k) = b(k) Then<br /> c += 1<br /> End If</b></font><br />Next<br />lstBox.Items.Add(c)</pre><br /><br />What the loop is doing is simply traversing all of the indeces (cells) of the arrays one at a time. Variable <b>c</b> only changes if the numbers in the cells are equal. Therefore we only need to count when the numbers are equal to each other:<br /><br />a() contains:<br />3, 2, 5, 1, 7, 8, <font color="blue"><b>3, 5</b></font>, 6, 2, <font color="blue"><b>3</b></font>, 6, 1, 6, 5, 5, 7, 2, 5, 3<br /><br />b() contains:<br />5, 3, 3, 4, 8, 2, <font color="blue"><b>3, 5</b></font>, 9, 5, <font color="blue"><b>3</b></font>, 7, 3, 7, 6, 3, 2, 1, 3, 4<br /><br />This occurs three times, so the answer is 3.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-7092568926436752720?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-65769499744880155732008-04-08T12:27:00.000-07:002008-04-08T09:25:15.777-07:00StructuresInstead of using parallel or 2-dimensional arrays for storing various information that we would like to manipulate. For example, universities have various information about them that is universal. Each university has the following properties:<br /><ul><br /><li>Name</li><li>State in which the university is located</li><li>Date that the university was founded</li><li>Population</li><br /></ul><br />Of course there are many more things that universities have, but this is a start.<br /><br />Now we could use parallel arrays to store this information (like in our ATM project). Doing so, would mean that we would have 4 arrays to store the above information and that we could use the same index to reference information for a particular university:<br /><pre><br /> <font color="blue">Dim</font> uName(3) <font color="blue">As String</font><br /> <font color="blue">Dim</font> uState(3) <font color="blue">As String</font><br /> <font color="blue">Dim</font> uFounded(3) <font color="blue">As String</font><br /> <font color="blue">Dim</font> uPopulation(3) <font color="blue">As String</font><br /></pre><br />Let's say that we set the following:<br /><pre><br /> uName(1) = "Queens College"<br /> uState(1) = "NY"<br /> uFounded(1) = "1960" <font color="green">'I'm not sure about the date</font><br /> uPopulation(1) = "5,000" <font color="green">'Or the population :-)</font><br /></pre><br />So all of Queens College's information is accessible by accessing index 1 of each array. Thus the parallel Nature.<br /><br />But suppose that we do not want to keep track of four arrays. We could then create a <b><i>Structure</i></b> or a container to keep all of this information in one place:<br /><pre><br /> <font color="blue">Structure</font> uInfo<br /> <font color="blue">Dim</font> name <font color="blue">As String</font><br /> <font color="blue">Dim</font> state <font color="blue">As String</font><br /> <font color="blue">Dim</font> founded <font color="blue">As String</font><br /> <font color="blue">Dim</font> population <font color="blue">As String</font><br /> <font color="blue">End Structure</font><br /></pre><br />Now we have created our own type and we can declare a variable of this type like this:<br /><pre><br /> <font color="blue">Dim</font> college <font color="blue">As</font> uInfo<br /></pre><br />In order to set (or use) any property of colleges, we use the ``dot'' method:<br /><pre><br /> college.name = "Queens College"<br /> college.state = "NY"<br /> college.founded = "1960"<br /> college.population = "5,000"<br /></pre><br /><br /><b>Declaring an Array of type uInfo</b><br /><br />We can declare arrays of any type, including our own defined types.<br /><pre><br /> <font color="blue">Dim</font> colleges(10) <font color="blue">As</font> uInfo<br /> colleges(0).name = "Queens College"<br /> colleges(0).state = "NY"<br /> colleges(0).founded = "1960"<br /> colleges(0).population = "5,000"<br /></pre><br />The resulting array will look like (except up to 10):<br /><pre> 0 1 2 ...<br />+---------------+----+----+<br />| Queens College| | |<br />+---------------+----+----+<br />| NY | | |<br />+---------------+----+----+<br />| 1960 | | |<br />+---------------+----+----+<br />| 5,000 | | |<br />+---------------+----+----+</pre><br /><br />You can see that every cell is broken up into 4 parts that contain the items from the structure itself.<br /><br />Of course if we had an array of things, it would make more sense to take in the information from a file:<br /><pre><br /> <font color="blue">For</font> i <font color="blue">As Integer = </font>0 <font color="blue">To</font> 10<br /> colleges(i).name = sr.Readline()<br /> colleges(i).state = sr.Readline()<br /> colleges(i).founded = sr.Readline()<br /> colleges(i).population = sr.Readline()<br /> <font color="blue">Next</font><br /></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-6576949974488015573?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-32692542558785155932008-04-08T11:47:00.000-07:002008-04-08T09:17:17.941-07:00Student Grades ExampleToday we talked about dealing with student grades using arrays. We would have an array that would hold the students' identifying information (such as name or ID number) and then arrays for all of the students' grades. Remember, these are called <b><i>Parallel Arrays</i></b>.<br /><br />Example:<br /><pre><br /><font color="blue">Public Class</font> Form1<br /> <font color="green">'Global Variables:</font><br /> <font color="blue">Dim</font> students(5) <font color="blue">As String</font><br /> <font color="blue">Dim</font> hw1(5) <font color="blue">As Double</font><br /> <font color="blue">Dim</font> hw2(5) <font color="blue">As Double</font><br /> <font color="blue">Dim</font> hw3(5) <font color="blue">As Double</font><br /> ...<br /> ...<br /></pre><br /><br />We have declared these arrays as <b>Global</b> so that we will be able to use them in all subroutines, and also so that we will not loose the information stored in them for the duration of the program. Here are what the resulting arrays look like:<br /><pre><br />students()<br /> 0 <font color="red"><b> 1</b></font> 2 3 4<br />+----<font color="red"><b>+----+</b></font>----+----+----+<br />| <font color="red"><b>| | </b></font> | | |<br />+----<font color="red"><b>+----+</b></font>----+----+----+<br /><br />hw1()<br /> 0 <font color="red"><b> 1</b></font> 2 3 4<br />+----<font color="red"><b>+----+</b></font>----+----+----+<br />| <font color="red"><b>| | </b></font> | | |<br />+----<font color="red"><b>+----+</b></font>----+----+----+<br /><br />hw2()<br /> 0 <font color="red"><b> 1</b></font> 2 3 4<br />+----<font color="red"><b>+----+</b></font>----+----+----+<br />| <font color="red"><b>| | </b></font> | | |<br />+----<font color="red"><b>+----+</b></font>----+----+----+<br /><br />hw3()<br /> 0 <font color="red"><b> 1</b></font> 2 3 4<br />+----<font color="red"><b>+----+</b></font>----+----+----+<br />| <font color="red"><b>| | </b></font> | | |<br />+----<font color="red"><b>+----+</b></font>----+----+----+</pre><br /><br />As we can see, <font color="red"><b>Student1</b></font>'s information will reside in <font color="red"><b>index 1</b></font> of all the arrays.<br /><br /><font size="4">Code</font><br /><br />We wrote two subroutines, one to get the information from the user and one to find and print the information for a particular student.<br /><br />Here is the code for getting the student information:<br /><pre><br /> <font color="blue">Sub</font> form_load()<br /> <font color="blue">For</font> i <font color="blue">As Integer</font>=0 <font color="blue">To</font> students.GetUpperBound(0)<br /> students(i) = InputBox("Enter student " & _<br /> CStr(i) & "'s name:")<br /> hw1(i) = CDbl(InputBox("Enter homework 1:"))<br /> hw2(i) = CDbl(InputBox("Enter homework 2:"))<br /> hw3(i) = CDbl(InputBox("Enter homework 3:"))<br /> <font color="blue">Next<br />End Sub</font><br /></pre><br />Here is the code for a linear search of a student:<br /><pre><br /> <font color="blue">Sub</font> btnSearch_Click()<br /> <font color="blue">Dim</font> sname <font color="blue">As String</font><br /> <font color="blue">Dim</font> found <font color="blue">As Boolean = False</font> <br /> sname = InputBox("Enter student's name:")<br /> <font color="blue">For</font> i <font color="blue">As Integer</font> = 0 <font color="blue">To</font> students.GetUpperBound(0)<br /> <font color="blue">If</font> sname = students(i) <font color="blue">Then</font><br /> listbox1.Items.Add(hw1(i))<br /> listbox1.Items.Add(hw2(i))<br /> listbox1.Items.Add(hw3(i))<br /> found = <font color="blue">True<br /> Exit For</font> <br /> <font color="blue">End If<br /> Next</font><br /> <br /> <font color="blue">If Not</font> found <font color="blue">Then</font><br /> MsgBox("Student not found",,"")<br /> <font color="blue">End If<br />End Sub</font></pre><br /><br /><font size="4">2-Dimensional Conversion</font><br /><br />We can convert the 1-dimensional grade parallel arrays into a two dimensional array. The names of the students must remain a separate 1-dimensional array, however since its type is different. Here are the declarations:<br /><br /><pre><br /><font color="blue">Public Class</font> Form1<br /> <font color="green">'Global Variables:</font><br /> <font color="blue">Dim</font> students(5) <font color="blue">As String</font><br /> <br /> <font color="green">Declare hw() with 3 rows and 5 columns:</font><br /> <font color="blue">Dim</font> hw(3,5) <font color="blue">As Double</font><br /> ...<br /> ...<br /></pre><br />Resulting in the following arrays:<br /><pre><br />students()<br /> 0 <font color="red"><b> 1</b></font> 2 3 4<br /> +----<font color="red"><b>+----+</b></font>----+----+----+<br /> | <font color="red"><b>| | </b></font> | | |<br /> +----<font color="red"><b>+----+</b></font>----+----+----+<br /><br />hw()<br /> 0 <font color="red"><b> 1</b></font> 2 3 4<br /> +----<font color="red"><b>+----+</b></font>----+----+----+<br />0 | <font color="red"><b>| | </b></font> | | |<br /> +----<font color="red"><b>+----+</b></font>----+----+----+<br />1 | <font color="red"><b>| | </b></font> | | |<br /> +----<font color="red"><b>+----+</b></font>----+----+----+<br />2 | <font color="red"><b>| | </b></font> | | |<br /> +----<font color="red"><b>+----+</b></font>----+----+----+</pre><br /><br /><font size="4">Rewritten Code</font><br /><br />In the hw() array, each row represents a HW grade and each column represents the grades for each student. Now we will rewrite our subroutines:<br /><pre><br /> <font color="blue">Sub</font> Form_Load()<br /> <font color="blue">For</font> i <font color="blue">As Integer</font>=0 <font color="blue">To</font> students.GetUpperBound(0)<br /> students(i) = InputBox("Enter student " & _<br /> CStr(i) & "'s name:")<br /> hw(0,i) = CDbl(InputBox("Enter homework 1:"))<br /> hw(1,i) = CDbl(InputBox("Enter homework 2:"))<br /> hw(2,i) = CDbl(InputBox("Enter homework 3:"))<br /> <font color="blue">Next<br />End Sub</font><br /></pre><br />Here is the code for a linear search of a student:<br /><pre><br /> <font color="blue">Sub</font> btnSearch_Click()<br /> <font color="blue">Dim</font> sname <font color="blue">As String</font><br /> <font color="blue">Dim</font> found <font color="blue">As Boolean = False</font> <br /> sname = InputBox("Enter student's name:")<br /> <font color="blue">For</font> i <font color="blue">As Integer</font> = 0 <font color="blue">To</font> students.GetUpperBound(0)<br /> <font color="blue">If</font> sname = students(i) <font color="blue">Then</font><br /> listbox1.Items.Add(hw(0,i))<br /> listbox1.Items.Add(hw(1,i))<br /> listbox1.Items.Add(hw(2,i))<br /> found = <font color="blue">True<br /> Exit For</font> <br /> <font color="blue">End If<br /> Next</font><br /> <br /> <font color="blue">If Not</font> found <font color="blue">Then</font><br /> MsgBox("Student not found",,"")<br /> <font color="blue">End If<br />End Sub</font></pre><br /><br />If we wanted to reduce the three lines dealing with the HW grades into one line, we could add another <font color="blue"><b>For</b></font> loop to loop through the grades:<br /><pre><br /> <font color="blue">Sub</font> Form_Load()<br /> <font color="blue">For</font> i <font color="blue">As Integer</font>=0 <font color="blue">To</font> students.GetUpperBound(0)<br /> students(i) = InputBox("Enter student " & _<br /> CStr(i) & "'s name:")<br /> <font color="blue">For</font> j <font color="blue">As Integer</font>=0 <font color="blue">To</font> <font color="red"><b>hw.GetUpperBound(0)</b></font><br /> hw(j,i) = CDbl(InputBox("Enter homework " & CStr(j)))<br /> <font color="blue">Next</font><br /> <font color="blue">Next<br />End Sub</font><br /></pre><br />Here is the code for a linear search of a student:<br /><pre><br /> <font color="blue">Sub</font> btnSearch_Click()<br /> <font color="blue">Dim</font> sname <font color="blue">As String</font><br /> <font color="blue">Dim</font> found <font color="blue">As Boolean = False</font> <br /> sname = InputBox("Enter student's name:")<br /> <font color="blue">For</font> i <font color="blue">As Integer</font> = 0 <font color="blue">To</font> students.GetUpperBound(0)<br /> <font color="blue">If</font> sname = students(i) <font color="blue">Then</font><br /> <font color="blue">For</font> j <font color="blue">As Integer</font>=0 <font color="blue">To</font> <font color="red"><b>hw.GetUpperBound(0)</b></font><br /> listbox1.Items.Add(hw(j,i))<br /> <font color="blue">Next</font><br /> found = <font color="blue">True<br /> Exit For</font> <br /> <font color="blue">End If<br /> Next</font><br /> <br /> <font color="blue">If Not</font> found <font color="blue">Then</font><br /> MsgBox("Student not found",,"")<br /> <font color="blue">End If<br />End Sub</font></pre><br /><br /><font size="4">.GetUpperBound</font><br /><br />Remember for 1-dimensional arrays the <tt>.GetUpperBound()</tt> function returns the number of the last possible index so:<br /><pre> students.GetUpperBound(0)</pre><br />returns 4.<br /><br />For 2-dimensional arrays we have two indeces in which we are interested: one that indicates the highest index of rows, and one that indicates the highest index of columns. Therefore there are two possible <tt>GetUpperBound()</tt> calls:<br /><pre><br /> <font color="green">'Get the last index of rows</font><br /> hw.GetUpperBound(0)<br /> <font color="green">'Get the last index of columns</font> <br /> hw.GetUpperBound(1)<br /></pre><br />We have used this in our rewritten subroutines above.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-3269254255878515593?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-64719546896266994942008-04-07T16:20:00.000-07:002008-12-12T17:11:48.239-08:00mp3 Files in VB .NET<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_55uSCYxbQ8M/R0tleR0muCI/AAAAAAAAAAw/B8Vgt-Ibwzs/s1600-h/player.JPG"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_55uSCYxbQ8M/R0tleR0muCI/AAAAAAAAAAw/B8Vgt-Ibwzs/s200/player.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5137311370800707618" /></a><br />You can <a href="http://www.codeproject.com/useritems/Play_sound_in_VBnet_with.asp">play mp3 files</a> in VB .NET also. The link above shows how to add a Windows Media Player to your project and to hide if you like. Otherwise you can show the player like shown at left.<br /><br />You only have to change one line of code if you want it to allow the user to search for files (shown in red):<br /><pre><br />Private Sub Button1_Click(...)<br /><br />Const DATA_FILE_EXTENSION As String = ".mp3"<br /> Dim dlgFileDialog As New OpenFileDialog<br /> With dlgFileDialog<br /> .Filter = DATA_FILE_EXTENSION & _<br /> " files (*" & DATA_FILE_EXTENSION & "|*" & _ <br /> DATA_FILE_EXTENSION<br /><br /> .FilterIndex = 1<br /> .RestoreDirectory = True<br /> If .ShowDialog() = <font color="red"><b>Windows.Forms.DialogResult.OK</b></font> Then<br /> 'Play the sound file<br /> Me.AxWindowsMediaPlayer1.URL = dlgFileDialog.FileName<br /> End If<br /> End With<br /><br />End Sub</pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-6471954689626699494?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-80659283612815615322008-04-07T12:08:00.000-07:002008-04-08T07:13:51.161-07:00Using Two FormsSince there is some confusion about how to use two forms, I have made a video to help clear this up. First of all you have to add a second form to your project before you can use it. To do this, go to:<br /><br />-> Projects<br />--> Add Windows Form<br />---> Add<br /><br />Then in the Solution Explorer [usually top right] you should see two forms. You can double click one of them to begin design on it. So you have to double click ``Form2'' in order to add things to it. You will have to add a listbox to it.<br /><br />From form1, you have to add one button. You have to program the button so that it will show form2. <br /><br />When you run the program, only the first form will be shown by default. Therefore, you will have to manually make the second form appear by writing the following code in the button:<br /><br /><tt><font color="blue">Form2.Show()</font></tt><br /><br />If you want the first form to disappear, you will have to do so manually by writing the following code: <br /><br /><tt><font color="blue">Me.Hide()</font></tt><br /><br />Notice that it does not say ``Form1.Hide().'' This is because a form can only refer to itself using ``Me'' kind of like in English. You wouldn't talk about yourself in the third person, would you?<br /><br />In order to skip lines you can simply Add nothing to the listbox:<br /><br /><tt><font color="blue">Form2.Listbox1.Items.Add("")</font> <font color="green">'Skips a line</font></tt><br /><br />Since you will be adding several lines, you may want to use the <tt>With</tt> option:<br /><br /><tt><font color="blue">With Form2.Listbox1.Items<br /> .Add("Business Expense Report")<br /> .Add("")<br /> .Add("Information for Conference")<br /> .Add(txtBox1.Text)<br /> .Add(txtBox2.Text & " in " & txtBox3.Text)<br /> .Add("")<br /> ...etc...<br />End With</font></tt><br /><br /><font size="4">Video of Two Forms</font><br />You may click on <a href="http://s158.photobucket.com/player.swf?refURL=http://s158.photobucket.com/albums/t115/gleeda/CS%252080/%3faction=view¤t=hw4-2.flv/&file=http://vid158.photobucket.com/albums/t115/gleeda/CS%252080/hw4-2.flv&t=1173886735&os=1&ap=1">this link</a>, where you can watch it full screen.<br /><br /><embed width="430" height="389" type="application/x-shockwave-flash" wmode="transparent" src="http://s158.photobucket.com/player.swf?file=http://vid158.photobucket.com/albums/t115/gleeda/CS%2080/hw4-2.flv"></embed><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-8065928361281561532?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-73085305835652273832008-04-07T10:07:00.000-07:002008-04-08T07:11:31.752-07:00Extras - ImagesWe learned that we can add images to our projects by changing the <span style="font-weight:bold;">BackgroundImage </span>property for the form, or the <span style="font-weight:bold;">Image </span>property for other objects:<br /><br /><img src="http://i187.photobucket.com/albums/x231/levyQC/formProperties.jpg" border="0" alt="Photo Sharing and Video Hosting at Photobucket"><br /><br /><img src="http://i187.photobucket.com/albums/x231/levyQC/btnProperties.jpg" border="0" alt="Photo Sharing and Video Hosting at Photobucket"><br /><br />For the <span style="font-weight:bold;">BackgroundImage</span>, we can also set the image to be <span style="font-weight:bold;">Tiled </span>or <span style="font-weight:bold;">Stretched </span>however we like.<br /><br />We also learned that we could change the images as the program runs. For example to set the button image to something else when it is clicked, we can write inside the Button_Click() subroutine:<br /><pre><br /> Button.Image = Image.FromFile("nameofnewimagefile.jpg")<br /></pre><br /><br />If we place our image files in the debug folder we can simply write the name of the file without having to put the entire path:<br /><br /><img src="http://i187.photobucket.com/albums/x231/levyQC/debugfolder.jpg" border="0" alt="Photo Sharing and Video Hosting at Photobucket"><br /><br />Suppose we want to write a program that will display pictures of different albums depending on which button is clicked. An example is below:<br /><br /><img src="http://i187.photobucket.com/albums/x231/levyQC/animalsProg.jpg" border="0" alt="Photo Sharing and Video Hosting at Photobucket"><br /><br />We could use the following code:<br /><pre><br />Private Sub Btncat_Click(ByVal sender As System.Object, _<br />ByVal e As System.EventArgs) Handles btnCat.Click<br /> Me.BackgroundImage = Image.FromFile("cat.jpg")<br /> Me.BackgroundImageLayout = ImageLayout.Stretch<br />End Sub<br /><br />Private Sub BtnDog_Click(ByVal sender As System.Object, _<br />ByVal e As System.EventArgs) Handles btnDog.Click<br /> Me.BackgroundImage = Image.FromFile("dog.jpg")<br /> Me.BackgroundImageLayout = ImageLayout.Stretch<br />End Sub<br /><br />Private Sub BtnIguana_Click(ByVal sender As System.Object, _<br />ByVal e As System.EventArgs) Handles btnIguana.Click<br /> Me.BackgroundImage = Image.FromFile("iguana.jpg")<br /> Me.BackgroundImageLayout = ImageLayout.Stretch<br /><br />End Sub<br /><br /><br />Private Sub BtnRandom_Click(ByVal sender As System.Object, _<br />ByVal e As System.EventArgs) Handles btnRandom.Click<br /> Dim randnum As New Random, temp As Integer<br /> temp = randnum.Next(1, 4)<br /> Select Case temp<br /> Case 1<br /> Me.BackgroundImage = Image.FromFile("iguana.jpg")<br /> Me.BackgroundImageLayout = ImageLayout.Stretch<br /> Case 2<br /> Me.BackgroundImage = Image.FromFile("cat.jpg")<br /> Me.BackgroundImageLayout = ImageLayout.Stretch<br /> Case 3<br /> Me.BackgroundImage = Image.FromFile("dog.jpg")<br /> Me.BackgroundImageLayout = ImageLayout.Stretch<br /> End Select<br />End Sub</pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-7308530583565227383?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-49777304777603538672008-04-07T09:16:00.000-07:002008-04-08T07:11:49.777-07:00Extras - SoundBelow is the code for the sound. You will have to replace the <font color="red"><b>red text</b></font> below with the sound file's name (remember that if you put the sound files in your debug folder you can access it with only the name and not the entire path). <i>Note: This only works with <font color="red">.wav</font> files</i>:<br /><br /><tt><font color="green"><br />'Alex Maureau, Queens College<br />'Credits: <br />'http://www.codeproject.com/vb/net/SoundClass.asp <br />'by Angelo Cresta<br /></font><br /><br /><font color="blue">Imports</font> System<br /><font color="blue">Imports</font> System.Runtime.InteropServices<br /><font color="blue">Imports</font> System.Resources<br /><font color="blue">Imports</font> System.IO<br /><br /><font color="blue">Public Class Sound</font></tt><br /><pre><br /> <font color="blue">Declare Auto Function</font> PlaySound <font color="blue">Lib</font> <font color="maroon">"winmm.dll"</font> (<font color="blue">ByVal</font> name _<br /> <font color="blue">As</font> String, <font color="blue">ByVal</font> hmod <font color="blue">As</font> Integer, _<br /><font color="blue">ByVal</font> flags <font color="blue">As Integer</font>) <font color="blue">As Integer</font><br /><br /> <font color="blue">Public Const</font> SND_SYNC = &H0 <font color="green">'play synchronously</font> <br /> <font color="blue">Public Const</font> SND_ASYNC = &H1 <font color="green">'play asynchronously </font><br /> <font color="blue">Public Const</font> SND_LOOP = &H8 <font color="green">'play with a loop</font><br /><br /> <font color="blue">Public Shared Sub</font> PlayWaveFile( _<br /><font color="blue">ByVal</font> <font color="red"><b>fileWaveFullPath</b></font> <font color="blue">As</font> String)<br /> <font color="blue">Try</font><br /> PlaySound(<font color="red"><b>fileWaveFullPath</b></font>, 0, SND_ASYNC)<br /> <font color="blue">Catch</font><br /> <font color="blue">End Try</font><br /> <font color="blue">End Sub<br />End Class</font><br /></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-4977730477760353867?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-82019722672339441582008-04-03T06:20:00.000-07:002008-04-03T09:14:30.143-07:00ArraysWe learned that we can use arrays as containers to hold several variables of the same type. We also learned about why we might want to use such a structure, for example, sorting 10000 names from within a file.<br /><br />There are a few ways to declare an array:<br /><ul><tt><br /><li><font color="blue">Dim</font> arr( size ) <font color="blue">As Type</font></li><br /><li><font color="blue">Dim</font> arr( ) <font color="blue">As Type</font> = {item1, item2, ...., itemN}</li><br /><li><font color="blue">Dim</font> arr( ) <font color="blue">As Type</font><br />...<br />...<br /><font color="blue">Redim</font> arr( newSize )</li></ul></tt><br />In each of the examples above, we have the array name and the type (<font color="blue">String</font>, <font color="blue">Integer</font>, <font color="blue">Double</font> etc) in the declaration statement. In the first example, we indicate the size of the array with an <b>Integer</b> value. This will give us an array with <b>size + 1</b> cells. This is because we will have subscripts/indexes/cells numbered from 0 to Size.<br /><br />In the last two examples, the size is not specified in the parentheses. In the second example, the size is figured out by the compiler by looking at the number of items in the list being assigned to each cell. In the third example, the size is set sometime later using a <tt><font color="blue">Redim</font></tt> statement.<br /><br />The <tt><font color="blue">Redim</font></tt> statement allows us to reset the size of the array as the program runs. It can be useful for things like taking items in from a file, where the total number of items is unknown, or expanding the array when we need to add more items to it. If we have something in the array before we call <tt><font color="blue">Redim</font></tt>, the previous items will be lost. We can save them, however, using the <font color="blue"><tt>Preserve</tt></font> command:<br /><br /><tt><font color="blue">Redim Preserve</font> arr( newSize )</tt><br /><br />If the new size is <b><i>smaller</i></b> than the original size, all items after the new upperbound will be lost.<br /><br />We can find the upperbound of an array at anytime with the following code:<br /><br /><tt>arrayname.<font color="blue">GetUpperBound</font>(0)</tt><br /><br />Where <tt>arrayname</tt> is replaced by the name of your array.<br /><br /><font size="4">Real Examples</font><br /><tt><font color="blue">Dim</font> names( 10 ) <font color="blue">As String</font><br /><font color="blue">Dim</font> names2( ) <font color="blue">As String</font> = {"Allen", "Stacy", "Simon"}</tt><br /><br />The result of these declaration statements create two arrays, <tt>names()</tt> which is of size 11 and <tt>names2()</tt> size 3. The contents of <tt>name2()</tt> look like this:<br /><pre> <br /> 0 1 2<br />+---------+---------+---------+<br />| "Allen" | "Stacy" | "Simon" |<br />+---------+---------+---------+ </pre><br /><br />Now if we want to extend <tt>names2()</tt> we can use the <font color="blue"><tt>Redim</tt></font> statement:<br /></tt><br /><font color="blue">Redim Preserve</font> names2( 4 )<br />names2( 3 ) = "Max"</tt><br /><br />The contents of the array will now be:<br /><pre> <br /> 0 1 2 3 4<br />+---------+---------+---------+--------+--------+<br />| "Allen" | "Stacy" | "Simon" | "Max" | |<br />+---------+---------+---------+--------+--------+ </pre><br /><br />Notice that we used the command <font color="blue"><tt>Preserve</tt></font> so that we would not loose the previous contents of our array.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-8201972267233944158?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-49939174601738162402008-04-01T09:54:00.000-07:002008-04-08T07:13:20.263-07:00ATM Project - Part 1Now that we have learned about File I/O, loops and decisions we can start to work on a real project. We will create an ATM simulation and we will continue adding things to it throughout the rest of this course.<br /><br />First we have to decide how an ATM works. We know that it will require the following operations:<br /><br />1) Validation (pin # and account number)<br />2) Deposit Money<br />3) Withdraw Money<br />4) Account Status (balance etc)<br /><br />We will have a text file that contains the account numbers and pins in it:<br /><pre>acctinfo.txt<br /><br />123456789<br />1234<br />234567890<br />9876</pre><br /><br />This is kept in the debug folder. When the program begins,<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-4993917460173816240?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-4809876297705677632008-03-25T12:07:00.000-07:002008-03-25T09:18:12.288-07:00Boxes of Stars (Loops continued)We learned how we could use loops to print out stars in various patterns like a <a href="http://venus.cs.qc.edu/~jlevy/cs80/docs/labOct24.htm">box of stars</a> [<a href="http://venus.cs.qc.edu/~jlevy/cs80/docs/star1.txt">code</a>]:<br /><br /><img src="http://venus.cs.qc.edu/~jlevy/cs80/docs/fall2005/stars4.jpg"><br /><br />Or even an empty box of stars <!-- [<a href="http://venus.cs.qc.edu/~jlevy/cs80/docs/star2.txt">code</a>] -->:<br /><br /><img src="http://venus.cs.qc.edu/~jlevy/cs80/images/emptystars.JPG"><br /><br />We have to use a <b><u>Nested For Loop</u></b> in order to print out lines of stars row by row. The outer loop represents each row, and the inner loop represents each column. Each row was constructed by using a <font color=blue><b>String</b></font>, since we can only print to a listbox one line at a time.<br /><br />In the empty stars example we have to use <font color=blue><b>If/Else</b></font> statements in order to tell if we were printing out one of the borders. <font color=blue><b>If</b></font> we were at a border (outer or inner control variable is equal to 1 or num, where num is the total number of rows and columns) then we would print out a star, <font color=blue><b>Else</b></font> we will only print out a space.<br /><br />Your HW is to print out the empty box of stars. You must ask the user if what number they like and then check to make sure that the number is in a range of 3 to 20.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-480987629770567763?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-7834444504646877982008-03-18T10:34:00.000-07:002008-03-18T09:44:17.797-07:00LoopsWe finished up Chapter 6.1 in class today. We learned that we could use loops in order to accomplish tasks that should be done several times using Loops. We learned about three types of loop structures:<br /><br /><font color="blue">Do Until<br />Do While<br />For Next</font><br /><br />We can write a loop using any of these three structures and any loop written in one of these structures can be rewritten using one of the other two. The basic syntax for each of these loops are:<br /><pre><br /><br /><font color="blue">For</font> variable = (starting point) To (ending point)<br /> <font color="green">'Do statements until we reach the ending point</font><br /><font color="blue">Next</font> <font color="green">'Update condition variable</font><br /><br /><font color="green">'**************************************</font><br /><br /><font color="green">'Set condition before entering loop</font><br /><font color="blue">Do While</font> condition<br /> <font color="green">'Do statements until condition is false</font><br /> <font color="green">'Update the condition</font><br /><font color="blue">Loop</font><br /><br /><font color="green">'**************************************</font><br /><br /><font color="green">'Set condition before entering loop</font><br /><font color="blue">Do </font> <br /> <font color="green">'Do statements until condition is true</font><br /> <font color="green">'Update the condition</font><br /><font color="blue">Loop Until</font> condition</pre><br /><br />In all three loop structures we have three parts that are the same:<br />1) A starting point for the condition we are checking<br />2) An endpoint for the condition<br />3) An update for the condition<br /><br />We must have all three things for the loop to execute and end execution. Without (3) from above for example, we would have an infinite loop which is <b>definitely not</b> what was intended.<br /><br />Also notice that the <font color="blue">Do While</font> and the <font color="blue">Do Until</font> loops are opposites. <font color="blue">Do While</font> loops until the <tt>condition</tt> is <font color="blue">False</font> whereas the <font color="blue">Do Until</font> loop will loop <font color="red"><b>until the <tt>condition</tt> is <font color="blue"><u><i>True</i></u></font></b></font>. Also the <font color="blue">Do Until</font> is <i>always guaranteed to loop at least once</i>. We have no such guarantee with the <font color="blue">Do While</font> loop.<br /><br /><font size="4"><font color="blue">Do While</font> Examples</font><br /><pre><br /><font color="blue">Dim</font> i <font color="blue">As Integer</font> = 1<br /><font color="blue">Do While</font> i <font color="blue"><= 10</font> <br /> <font color="green">'List out the numbers from 1 to 10</font><br /> lstOut.Items.Add( i )<br /> i <font color="blue">+=</font> 1<br /><font color="blue">Loop</font></pre><br /><pre><br /><font color="blue">Dim</font> i <font color="blue">As Integer</font> = 1<br /><font color="blue">Dim</font> num <font color="blue">As Integer</font><br />num = <font color="blue">CInt(InputBox</font>(<font color="maroon">"Enter a #:"</font>,<font color="maroon">""</font>))<br /><font color="blue">Do While</font> i <font color="blue"><=</font> num<br /> <font color="green">'List out the numbers from 1 to num</font><br /> lstOut.Items.Add( i )<br /> i <font color="blue">+=</font> 1<br /><font color="blue">Loop</font></pre><br /><font size="4"><font color="blue">Do Until</font> Examples</font><br /><pre><br /><font color="blue">Dim</font> i <font color="blue">As Integer</font> = 1<br /><font color="blue">Do</font> <br /> <font color="green">'List out the numbers from 1 to 10</font><br /> lstOut.Items.Add( i )<br /> i <font color="blue">+=</font> 1<br /><font color="blue">Loop Until</font> i <font color="blue">> 10</font></pre><br /><pre><br /><font color="blue">Dim</font> i <font color="blue">As Integer</font> = 1<br /><font color="blue">Dim</font> num <font color="blue">As Integer</font><br />num = <font color="blue">CInt(InputBox</font>(<font color="maroon">"Enter a #:"</font>,<font color="maroon">""</font>))<br /><font color="blue">Do</font> <br /> <font color="green">'List out the numbers from 1 to num</font><br /> lstOut.Items.Add( i )<br /> i <font color="blue">+=</font> 1<br /><font color="blue">Loop Until</font> i <font color="blue">> num</font></pre><br /><font size="4"><font color="blue">For Next</font> Examples</font><br /><pre><br /><font color="blue">For</font>( i <font color="blue">As Integer = </font> 1 <font color="blue">To</font> 10)<br /> <font color="green">'List out the numbers from 1 to 10</font><br /> lstOut.Items.Add( i )<br /><font color="blue">Next</font></pre><br /><pre><br /><font color="blue">Dim</font> num <font color="blue">As Integer</font><br />num = <font color="blue">CInt(InputBox</font>(<font color="maroon">"Enter a #:"</font>,<font color="maroon">""</font>))<br /><font color="blue">For</font>( i <font color="blue">As Integer = </font> 1 <font color="blue">To</font> num)<br /> <font color="green">'List out the numbers from 1 to num</font><br /> lstOut.Items.Add( i )<br /><font color="blue">Next</font></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-783444450464687798?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-38707185797527856902008-03-18T09:35:00.000-07:002008-03-18T09:22:52.069-07:00Writing to FilesIn order to write to files, you will have to create a <font color="blue">StreamWriter</font>. Here is an example:<br /><pre><br /><font color="blue">Dim</font> sw <font color="blue">As</font> IO.StreamWriter<br />sw = <font color="blue">IO.File.</font>CreateText("newfilename.txt")<br />sw.WriteLine("This will go in the new file")<br />sw.Close()<br /></pre><br />If you use <font color="blue">CreateText</font> as shown above, then the file in quotes will be created and ready for use. If the file existed before you opened it, the file contents will be deleted, even if nothing is writen to the file. If you just want to place the new text at the end of the existing file, you can use <b><i>AppendText()</i></b> instead of <b><i>CreateText()</i></b>. <br /><br /><span style="font-weight:bold;">Another thing you should know, is that the text will not be written to the file unless you Close() the file.<span style="font-style:italic;"></span></span><br /><br />If you simply place the name of the file in quotes without an absolute path, then the file will be saved in the <span style="font-weight:bold;">MyDocuments\Visual Studio 2005\Projects\[project name]\[project name]\bin\Debug</span> folder, where [project name] is the name of your project.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-3870718579752785690?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-88953177751110198062008-03-18T08:06:00.000-07:002008-03-18T09:22:14.197-07:00File I/O [Reading a File]<st1:city st="on"><st1:place st="on"><b>Reading</b></st1:place></st1:City><b> Data From Text Files:<br /><br /></b>There are a few rules to follow when trying to read data from a text file. Text files are written using the standard program Notepad or recently Notepad++. They have the extension .txt. So here are the rules to use them in a VB program.<br /><br />1. Declare a variable that will handle the reading. For the purpose of the examples below:<br /><br /> </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">Dim</span><span style="font-size: 10pt; font-family: "Courier New";"> sr <span style="color: rgb(51, 102, 255);">As</span> IO.StreamReader</span><span style="font-family: Arial;"><o:p></o:p></span></p> <p class="MsoNormal"><span style="font-family: Arial;"><br /> The StreamReader is part of the IO (input.output) library. This will read a stream of characters coming from the internet or a disk. The text file will be located on the disk drive.<br /><br />2. Then, using the variable, execute the statement:<br /><br /></span><span style="font-size: 10pt; font-family: "Courier New";"> sr = IO.File.OpenText( <i>filename</i> )</span><span style="font-family: Arial;"><br /><br /> where filename is the exact name and path of the text file. For the purpose of the course, ALL text files must be in the bin\Debug folder where your project is located. Here is an example path:<br /><br /> My Documents\Visual Studio 2005\Projects\program\program\bin\Debug<br /><br /> That is indeed a long path. Place the text file in that folder so you don't need to type out the whole name each time.<br /><br />3. Then, read the data lines IN ORDER, the same order they appear in the file.<br /><br /> To place data from the file into a variable, use the ReadLine function available from the StreamReader library. Using the rule about input from the InputBoxes, <b>everything read from a file will be stored as a string.</b> If you need to gather a numeric value from the file, use the appropriate conversion function from the previous section.<br /><br /></span><span style="font-size: 10pt; font-family: "Courier New";"> strVar = sr.ReadLine <span style="color: rgb(51, 204, 0);">'any string variable</span><br /> numVar = <span style="color: rgb(51, 102, 255);">CDbl</span>( sr.ReadLine ) <span style="color: rgb(51, 204, 0);">'a double type variable</span><span style="color: black;"><br /><br /></span></span><span style="font-family: Arial; color: black;"> If all the statements in the file have been read, each subsequent ReadLine call will get a value of </span><span style="font-family: Arial; color: rgb(51, 102, 255);">Nothing</span><span style="font-family: Arial; color: black;"> by default.<br /><br />4. Finally, close the file and continue with the program (if anything else is left).<br /><br /></span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"> sr.Close()<br /></span><span style="font-family: Arial; color: black;"><br />That is it in terms of gathering data from a text file. Here is a short program from the textbook example. The text file is called "PAYROLL" and contains the following data IN ORDER:<br /><br />Mike Jones<br />7.35<br />35<br />John Smith<br />6.75<br />33<br /></span><span style="font-family: "Courier New"; color: black;"><br />Program:</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"><br /><br /> </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">Private Sub</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"> btnCompute_Click(...) </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">Handles</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"> btnCompute.Click<br /><br /> </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">Dim</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"> sr </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">As</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"> IO.StreamReader = IO.File.OpenText(</span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(204, 0, 0);">"PAYROLL.TXT"</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;">)<br /> </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 204, 0);">'The file is in the bin subfolder in Debug</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"><br /><br /> </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">Dim</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"> name </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">As String</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"><br /> </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">Dim</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"> hourlyWage, hoursWorked, salary </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">As Double</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"><br /><br /> name = sr.ReadLine<br /> hourlyWage = </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">CDbl</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;">(sr.ReadLine)<br /> hoursWorked = </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">CDbl</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;">(sr.ReadLine)<br /> salary = hourlyWage * hoursWorked<br /> lstPayroll.Items.Add(name & </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(204, 0, 0);">" "</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"> & FormatCurrency(salary))<br /><br /> name = sr.ReadLine<br /> hourlyWage = </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">CDbl</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;">(sr.ReadLine)<br /> hoursWorked = </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">CDbl</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;">(sr.ReadLine)<br /> sr.Close()<br /><br /> salary = hourlyWage * hoursWorked<br /> lstPayroll.Items.Add(name & </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(204, 0, 0);">" "</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"> & FormatCurrency(salary))<br /><br /> </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">End Sub</span></p> <span style="font-family: arial;"><span style="font-size:85%;"><span style="font-family: courier new;"><span style="font-family: arial;font-size:100%;" ><span style="font-size:85%;"><span style="font-family: courier new;"><span style="color: rgb(51, 204, 0);"><span style="color: rgb(0, 0, 0);"><span style="font-size:100%;"><span style="font-family: arial;"><span style="color: rgb(51, 102, 255);"><span style="color: rgb(0, 0, 0);"><span style="font-size:85%;"><span style="font-family: courier new;"><span style="color: rgb(51, 102, 255); font-family: courier new;"></span><br /><span style="font-weight: bold;"><span style="font-family: arial;"></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br /><br />We already know how to read from a file, by creating a <font color="blue">StreamReader</font>. For example:<br /><pre><br /><font color="blue">Dim</font> sr <font color="blue">As IO.StreamReader</font><br />sr = <font color="blue">IO.File.OpenText</font>("nameOfFile.txt")<br /></pre><br />We can then read through the file by using a loop:<br /><pre><br /><font color="blue">Do While</font> sr.Peek <> -1<br /> lstBox.Items.Add(sr.ReadLine)<br /><font color="blue">Loop</font><br /></pre><br /><br /><font size="4">Imports</font><br /><br />Ever get tired of writing ``<font color="blue">IO</font>'' in front of everything dealing with files? You can import it at the top of your code and then you will not have to type it ever again:<br /><pre><br /><font color="blue">Imports System.IO<br />Public Class</font> Form1<br /></pre><br />Notice that it goes BEFORE the class declaration of Form1. Now the code for declaring and using a StreamReader changes:<br /><pre><br /><font color="blue">Dim</font> sr <font color="blue">As StreamReader</font><br />sr = <font color="blue">File.OpenText</font>("nameOfFile.txt")<br /></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-8895317775111019806?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-77441394760811043802008-03-04T08:12:00.000-08:002008-03-04T09:34:34.660-08:00Select CaseWe will finish Chapter 5.3 on 3/6 which ends with <font color="blue">Select Case</font>. Remember that <font color="blue">Select Case</font> is just another structure that works like <font color="blue">If</font> statements. For example:<br /><pre><br /><font color="blue">Dim</font> num <font color="blue">As Integer</font><br />num = <font color="blue">CInt</font>(txtIn.Text)<br /><font color="blue">Select Case</font> num<br /><font color="blue">Case</font> 1 <br /><font color="blue">MsgBox</font>("You entered 1",,"")<br /><br /><font color="blue">Case</font> 2 <font color="blue">To</font> 5<br /><font color="blue">MsgBox</font>("You entered a number from 2-5",,"")<br /><br /><font color="blue">Case</font> 7,9,11<br /><font color="blue">MsgBox</font>("You entered an odd number from 7-11",,"")<br /><br /><font color="blue">Case Is</font> > 12<br /><font color="blue">MsgBox</font>("You entered a number greater than 12",,"")<br /><br /><font color="blue">Case Else</font><br /><font color="blue">MsgBox</font>("You entered some other number",,"")<br /><br /><font color="blue">End Select</font><br /></pre><br />All of this works like the classic <font color="blue">If/ElseIf/Else</font> structure. so we could rewrite the above using <font color="blue">If</font> statements:<br /><pre><br /><font color="blue">Dim</font> num <font color="blue">As Integer</font><br />num = <font color="blue">CInt</font>(txtIn.Text)<br /><font color="blue">If</font> num = 1 <font color="blue">Then</font><br /> <font color="blue">MsgBox</font>("You entered 1",,"")<br /><font color="blue">ElseIf</font> 2 <= num <font color="blue">And</font> num <= 5 <font color="blue">Then</font><br /> <font color="blue">MsgBox</font>("You entered a number from 2-5",,"")<br /><font color="blue">ElseIf</font> num = 7 <font color="blue">Or</font> num = 9 <font color="blue">Or</font> num = 11 <font color="blue">Then</font><br /> <font color="blue">MsgBox</font>("You entered an odd number from 7-11",,"")<br /><font color="blue">ElseIf</font> num > 12 <font color="blue">Then</font><br /> <font color="blue">MsgBox</font>("You entered a number greater than 12",,"")<br /><font color="blue">Else</font><br /> <font color="blue">MsgBox</font>("You entered some other number",,"")<br /><font color="blue">End If</font></pre><br /><br />You can also use <font color="blue">Select Case</font> to compare <font color="blue">Strings</font> just like you can with <font color="blue">If</font> statements.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-7744139476081104380?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-80139708900176367222008-02-29T10:47:00.000-08:002008-02-28T09:43:52.918-08:00If Statements and StringsWe are able to compare strings using <font color="blue">If</font> statements. This means that we can see if two strings are the same string [i.e. "george" = "george"], but more importantly we are able to compare the relationship between strings. That is, we can tell if a certain string comes before another string:<br /><pre><br /><font color="blue">Dim</font> strA, strB <font color="blue">As String</font><br />strA = "cat"<br />strB = "dog"<br /><br /><font color="blue">If</font> strA < strB <font color="blue">Then</font><br /> MsgBox(strA & " comes before " & strB, "")<br /><font color="blue">ElseIf</font> strA > strB <font color="blue">Then</font><br /> MsgBox(strA & " comes after " & strB, "")<br /><font color="blue">Else</font><br /> MsgBox(strA & " equals " & strB, "")<br /><font color="blue">End If</font><br /></pre><br /><br />Gives answer of "cat comes before dog"<br /><br />One important thing to remember, however is that capital letters come before lowercase letters. This is because of the <a href="http://www.natural-innovations.com/computing/asciiebcdic.html" target="_blank">ASCII/ANSI</a> coding of each letter in the computer.<br /><br />The computer does not understand the word "cat" as we humans do. For the computer each letter has a <a href="http://en.wikipedia.org/wiki/Binary_numeral_system" target="_blank">binary value</a> [binary = 1's and 0's]. Letters are given numbers in sequential order from 65-90 for A-Z and 97-122 for a-z. Since the capital letters have lower values, they are less than the lowercase letters and therefore "come first."<br /><br /><pre><br /><font color="blue">Dim</font> strA, strB <font color="blue">As String</font><br />strA = "cat"<br />strB = "Zebra"<br /><br /><font color="blue">If</font> strA < strB <font color="blue">Then</font><br /> MsgBox(strA & " comes before " & strB, "")<br /><font color="blue">ElseIf</font> strA > strB <font color="blue">Then</font><br /> MsgBox(strA & " comes after " & strB, "")<br /><font color="blue">Else</font><br /> MsgBox(strA & " equals " & strB, "")<br /><font color="blue">End If</font><br /></pre><br /><br />Gives answer of "Zebra comes before cat"<br /><br />If you want to eliminate case, you can use the <b>.ToUpper</b> and <b>.ToLower</b> functions.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-8013970890017636722?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-1957770863146428182008-02-29T10:46:00.000-08:002008-02-28T09:43:35.914-08:00If / ElseIf / Else [Continued]We learned in class that If statements could be more complicated than just one case or the other. We learned that there may be more than one thing that we want to check, and we can do this by using a different If/ElseIf/Else structure:<br /><br /><pre><font color="blue">If <font color="red">condition1</font> Then<br /> <font color="green">'Do these statements for condition 1</font><br />ElseIf <font color="red">condition2</font> Then<br /> <font color="green">'Do these statements for condition 2</font><br />Else<br /> <font color="green">'Do these default statements </font><br />End If</font></pre><br /><br />We also learned that there can be several <font color="blue"><tt>ElseIf</tt></font> statements before hitting the final <font color="blue"><tt>Else</tt></font>.<br /><br /><font size="4">More Boolean Operators</font><br />We learned that we can check more than one relationship at a time by combining them using one of two special operators:<br /><br /><pre>Operator Example<br /><font color="blue">And</font> 0 <= x <font color="blue">And</font> x <= 10<br /><font color="blue">Or</font> 0 <= x <font color="blue">Or</font> x <= 10<br /><font color="blue">Not</font> <font color="blue">Not</font> ( x = y )<br /></pre><br /><br />The <font color="blue">And</font> operator is only true if both conditions on either side are true. If either side is false, then the entire combined expression is false. <br /><br />The <font color="blue">Or</font> operator is different, because it only need one of the expressions to be true in order for the entire expression to be true. So if one side is true then the entire expression is true, and if both sides are false then the entire combined expression is false.<br /><br /><font size="4">Truth Tables</font><br />We can make truth tables in order to clarify these expressions.<br /><br />Suppose we use <span style="font-weight:bold;">x</span> to represent <span style="font-weight:bold;">Condition 1</span> and <span style="font-weight:bold;">y</span> to represent <span style="font-weight:bold;">Condition 2</span>. So we have the following relationships:<br /><br /><pre><br />+-----+-----+--------+----------+----------+<br />| x | y | Not x | x And y | x Or y | <br />+-----+-----+--------+----------+----------+<br />| T | T | F | T | T |<br />+-----+-----+--------+----------+----------+<br />| T | F | F | F | T |<br />+-----+-----+--------+----------+----------+<br />| F | T | T | F | T |<br />+-----+-----+--------+----------+----------+<br />| F | F | T | F | F |<br />+-----+-----+--------+----------+----------+<br /></pre><br /><br />T represents <font color="blue">True</font><br />F represents <font color="blue">False</font><br /><br />So if the conditions <font color="blue">x</font> and <font color="blue">y</font> are combined with an <font color="blue">And</font> operator, the entire combined condtion is only true if both <font color="blue">x</font> and <font color="blue">y</font> are <font color="blue">True</font> [T]. We can see this in the table above. And of course: <tt>x <font color="blue">And Not</font> x</tt> results in a column of all <font color="blue">False</font> entries.<br /><br />Also, anytime that <font color="blue">x</font> or <font color="blue">y</font> are <font color="blue">True</font> and are combined with an <font color="blue">Or</font> operator the entire combined condition is <font color="blue">True</font>. We can see this relationship in the table as well.<br /><br /><font color="blue">Not</font> is an operator that takes the current condition and makes its value the opposite. That is, if a condition is <font color="blue">True</font> then <font color="blue">Not condition</font> makes it <font color="blue">False</font>.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-195777086314642818?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-39082874698644028562008-02-28T15:40:00.000-08:002008-02-28T09:44:56.503-08:00If StatementsWe are covering <font color="blue"><tt>If</tt></font> Statements this week. Let's see how if statements work:<br /><br /><pre><font color="blue">If <font color="red">condition</font> Then<br /> <font color="green">'Do these statements </font><br />Else<br /> <font color="green">'Do these alternate statements </font><br />End If</font></pre><br /><br />The condition is what we have to check, and if it is true then we do the statements that are immediately inside the If statement, otherwise we do what is in the Else statement. An English pseudocode example could be:<br /><br /><pre><font color="blue">If <font color="red">It's sunny outside</font> Then<br /> <font color="green">'Go play frisbee outside </font><br />Else<br /> <font color="green">'Stay inside and read </font><br />End If</font></pre><br /><br />Of course this is only an English example and will not make sense to the computer in this format.<br /><br /><font size="4">Operators</font><br />Most often what we are trying to figure out has to do with relationships of things. For example, we had a calculator problem with a potential bug of division by 0. We could use If statements to decide If we are trying to divide by 0. In order to do this, we need to use some <span style="font-weight:bold;">boolean operators<span style="font-style:italic;"></span></span>. These operators allow us to figure out the relationship between two things, for example, is a number <span style="font-style:italic;">larger than<span style="font-weight:bold;"></span></span> another number, <span style="font-weight:bold;">smaller than<span style="font-style:italic;"></span></span> the other number or <span style="font-weight:bold;">equal<span style="font-style:italic;"></span></span> to it?<br /><br />Our operators are:<br /><pre><br />= Equal to<br /><> Not Equal to<br />< Less Than<br />> Greater Than<br /><= Less Than or Equal to<br />>= Greater Than or Equal to<br /></pre><br /><br />These operators compare the item on the left to the item on the right and return <font color="blue">True</font> if the relationship is true or <font color="blue">False</font> if the relationship is false. The items that we compare could be Numbers or Strings. <br /><br />Therefore, if we want to see if a number is Equal to another number (in this case zero) we would use the equals operator. If num2 is equal to zero then we output an error message, otherwise we do the division (which is not shown here):<br /><br /><pre><font color="blue">If <font color="red"><b>num2 = 0</b></font> Then<br /> MsgBox("Division by zero error!",,"ERROR")<br />Else<br /> <font color="green">'Do Division </font><br />End If</font></pre><br /><br />Now we have a piece of code that we can use, or modify if we like. We could have used a different method by using the <b>Not Equals</b> Operator:<br /><br /><pre><font color="blue">If <font color="red"><b>num2 <> 0</b></font> Then<br /> <font color="green">'Do Division </font><br />Else<br /> MsgBox("Division by zero error!",,"ERROR") <br />End If</font></pre><br /><br />This says that if num2 is NOT equal to zero then do the division, otherwise output an error message.<br /><br /><i>Note: We do not have to have an <font color="blue">Else</font> with each <font color="blue">If</font> statement. We could just write:</i><br /><br /><pre><font color="blue">If <font color="red"><b>num2 <> 0</b></font> Then<br /> <font color="green">'Do Division </font><br />End If</font></pre><br /><br /><i>But nothing happens if a division by zero is attempted. The user might like to know why nothing happened, especially if they accidentally wrote 0 instead of another number like 10</i><br /><br />In the next class we will go over <a href="http://venus.cs.qc.edu/~jlevy/cs80/calculator.txt">this longer example.</a><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-3908287469864402856?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-69590218689962340172008-02-28T12:27:00.000-08:002008-02-28T09:53:06.841-08:00String formatting<p class="MsoNormal"> <span style="font-family: Arial;">As promised, here are the notes on section 3.5 Input/Output (by Alex)<br />________________________________________________________________<br /><br /><b>Formatting:</b><br /><br />There are various ways of formatting output in VB. One way is with some more built in functions:<br /><br /> </span><span style="font-family: "Courier New";"> </span><span style="font-size: 10pt; font-family: "Courier New";">FormatNumber( number or variable to format, places to round )<br /><br /></span><span style="font-family: Arial;"> This will take a number and format it to a certain number of decimal places. Below is an example of using this function:<br /><br /></span><span style="font-size: 10pt; font-family: "Courier New";"> FormatNumber( 12345.628, 2) will evaluate to 12,345.63</span><span style="font-family: Arial;"><br /></span><span style="font-size: 10pt; font-family: "Courier New";"> FormatNumber( 444212.333, 0) will evaluate to 444,212<br /><br /></span><span style="font-family: Arial;"> </span><span style="font-family: "Courier New";"> </span><span style="font-size: 10pt; font-family: "Courier New";">FormatCurrency( number or variable to format, places to round )<br /><br /></span><span style="font-family: Arial;"> This will take a number and format it to a currency value to a certain number of decimal places. Below is an example of using this function:<br /><br /></span><span style="font-size: 10pt; font-family: "Courier New";"> FormatCurrency( 12345.628, 2) will evaluate to $12,345.63</span><span style="font-family: Arial;"><br /></span><span style="font-size: 10pt; font-family: "Courier New";"> FormatCurrency( 345.4426, 3) will evaluate to $345.443<br /><br /></span><span style="font-family: Arial;"> </span><span style="font-family: "Courier New";"> </span><span style="font-size: 10pt; font-family: "Courier New";">FormatPercent( number or variable to format, places to round )<br /><br /></span><span style="font-family: Arial;"> This will take a number and format it to a percentage value to a certain number of decimal places. Below is an example of using this function:<br /><br /></span><span style="font-size: 10pt; font-family: "Courier New";"> FormatPercent( 0.185, 2) will evaluate to 18.50%<br /><br /></span><span style="font-family: Arial;"><br />Now, notice with the Percent and Currency function, the appropriate symbol is added to the outputted value. There are other Format functions available but these are again the most frequently used.<br /><br />To format a string output, you use something called <span style="color: rgb(51, 102, 255);">zones.</span><span style="color: black;"> Since I cannot draw once again, I will have to refer you to the textbook example 1 in section 3.5. But here is how the formatting variable will look:<br /><br /></span></span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"> </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">Dim</span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"> fmtStr </span><span style="font-size: 10pt; font-family: "Courier New"; color: rgb(51, 102, 255);">As String</span><span style="font-family: "Courier New"; color: black;"><br /></span><span style="font-size: 10pt; font-family: "Courier New"; color: black;"> fmtStr = "{0,15}{1,8}{2,9}"<br /></span><br /><span style="font-family: Arial;">Here is how that looks. Notice that there is no spaces between the curly braces inside the string. Also notice each section has two numeric values. The first value is the zone number so in the above example they would be 0...1...2. The second value is the number of spaces to format so these would be 15...8...9. Here is how to use it in a program:<br /><br /></span><span style="font-size: 10pt; font-family: "Courier New";"> lstOut.Items.Add( String.Format( fmtStr,item 1, item 2, item 3 ) )<br /><br /></span><span style="font-family: Arial;">The number of items in the above line needs to match the number of zones you declare in the formatting string. Again, see example 1 for the full diagram as well as a short program.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-6959021868996234017?l=csci80.blogspot.com'/></div>Alexhttp://www.blogger.com/profile/18345997568670314403noreply@blogger.com0tag:blogger.com,1999:blog-3203751836604428014.post-24111636675833158662008-02-11T18:18:00.000-08:002008-02-11T17:45:19.734-08:00String Lecture notes (from Alex)<span style="font-size:100%;"><span style="font-family:arial;">I am available for tutoring in A205. My schedule is posted online at the department's website.<br />_____________________________________________________________________________<br /><br />Formally, a <span style="color: rgb(51, 102, 255);">string </span>is a sequence of characters that can be treated as a single item. In other words, anything and everything together. Here are some examples:<br /><br />"34huwnacbui23h4r"<br />"The quick brown fox."<br />"My name is: John Doe"<br /><br />Here is how to declare a string variable:<br /><br /><span style="color: rgb(51, 102, 255);"> Dim </span><span style="font-style: italic;">varName</span> <span style="color: rgb(51, 102, 255);">As String<br /><br /><span style="color: rgb(0, 0, 0);">By default, the string variable is automatically set to empty, or <span style="color: rgb(51, 102, 255);">Nothing.<span style="color: rgb(0, 0, 0);"><br /><br />(See example 1 in 3.4 for a basic example)<br /><br />For the purpose of the HW assignment (37-40 in section 3.4), you were gathering certain information from textboxes. Again, by default, anything you gather from a textbox is a stored as a string. So as a brief example, say you entered a number and tried to place it in the appropriate variable. This will not work since a string cannot be placed into a variable declared of a numeric type. There is a way to fix this. In VB, there are built-in functions that will correctly convert a string to a number or certain other variable types. Below are some examples of that:<br /><br /> <span style="color: rgb(51, 102, 255);">CDbl</span>() -- means Convert to a double. This is the most commonly seen conversion function in the textbook since most variables are declared as a <span style="color: rgb(51, 102, 255);">Double</span> type. This will convert the string entered into a double.<br /><br /> <span style="color: rgb(51, 102, 255);">CInt</span>() -- means Convert to an integer. Will take a string and convert to the <span style="color: rgb(51, 102, 255);">Integer</span> type.<br /><br /> <span style="color: rgb(51, 102, 255);">CLng</span>() -- means Convert to a Long integer. Will take a string and convert to the <span style="color: rgb(51, 102, 255);">Long</span> type.<br /><br /> <span style="color: rgb(51, 102, 255);">CStr</span>() -- means Convert to a String. Yes, there is a function that will "go the other way" so to speak. This will be used for output purpose mostly though there may be an occasion to use for input purpose.<br /></span></span></span></span><br />Below is a short program to demonstrate some conversion functions:<br /><br /><span style="font-size:85%;"><span style="font-family:courier new;"><span style="color: rgb(51, 102, 255);">Private Sub</span> btnAdd_Click(...) <span style="color: rgb(51, 102, 255);">Handles</span> btnAdd.Click</span><br /><br /><span style="font-family:courier new;"><span style="color: rgb(51, 204, 0);">'This program will gather two numbers from the user and output the sum<br /><span style="color: rgb(0, 0, 0);"><span style="color: rgb(51, 102, 255);">Dim</span> num1, num2, sum <span style="color: rgb(51, 102, 255);">As Double</span></span><br /><br /><span style="color: rgb(0, 0, 0);">num1 = <span style="color: rgb(51, 102, 255);">CDbl<span style="color: rgb(0, 0, 0);">(</span></span>txtNum1.Text)</span><br /><span style="color: rgb(0, 0, 0);">num2 = <span style="color: rgb(51, 102, 255);">CDbl</span>(txtNum2.Text)</span><br /></span></span><span style="color: rgb(51, 102, 255);font-family:courier new;" ><br /><span style="color: rgb(0, 0, 0);">sum = num1+num2<br /><br />txtOut.Text = <span style="color: rgb(51, 102, 255);">CStr</span>(<span style="color: rgb(204, 0, 0);">"The sum is: "</span> & sum)<br /></span><br />End Sub<br /><br /><span style="color: rgb(0, 0, 0);font-family:arial;font-size:100%;" >Now, just one note from the above program. In the last line, you see a & symbol. This is called the <span style="color: rgb(51, 102, 255);">concatenation</span> symbol. It will join two or more pieces of data together. So in this case, the user will see the following message in the textbox if the sum was 7: The sum is: 7 Concatenation will be seen quite frequently for the rest of the semester.<br /><br />(See example 3 for an example of concatenation).<br /><br /><span style="font-weight: bold;">STRING FUNCTIONS:<br /><br /><span style="font-weight: bold;"></span></span>In VB, there are certain functions that you can apply to strings. Below are some examples using them. For the purpose of all the examples below, use the following definition of a string:<br /><br /> <span style="color: rgb(51, 102, 255);">Dim</span> str <span style="color: rgb(51, 102, 255);">As String</span><br /><br /> str.Length()<br /> -will return the length of a string as an integer. Say the string was "Hello". You will get an answer of 5.<br /><br /> str.ToUpper()<br /> -will change the string to all uppercase characters (not numbers or symbols). Say the string is "Hello James 1234!", the function will change this to "HELLO JAMES 1234!"<br /><br /> str.ToLower()<br /> -just the opposite of ToUpper()<br /><br /> str.Trim()<br /> -will eliminate all the white spaces at the front and end of a string. Say the string is " Hello "<br /> Applying this function, you will get "Hello"<br /><br />The following two functions contain something called parameters (explained more in chapter 4). These are data values that are required for the function. Also, these functions require the knowledge of how a string looks in memory. Since I cannot draw with the blog, I will do my best to show it. The pipe symbol | will be used to separate the individual characters:<br /><br />Say you declare a string variable and give it the value, somewhere in the program, "John". Here is how it would look:<br /><br /> J | o | h | n | \0<br /> 0 | 1 | 2 | 3 | 4<br /><br />The numbers appearing below the string are called indices's. They are the "position" of the certain character. The \0 is called the terminating character or the null string. This is basically the end of the string. So let's look at the function:<br /><br /> str.IndexOf( string ) OR str.IndexOf( string, starting position ).<br /><br /> This function will return the index of the FIRST OCCURRENCE of a string you give it to find. If it cannot find it, it will give you -1. So let's use the string "fanatic". Looking at it in memory (this is the best I could do):<br /><br /> f | a | n | a | t | i | c | \0<br /> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7<br /><br />Let's apply the function.<br /><br /> str.IndexOf("ati") will give you an answer of 3 since the first occurrence of this string begins at position, or index of 3.<br /><br /> str.IndexOf("a") will give you an answer of 1 since the first occurrence of this string begins at position, or index, 1.<br /><br /> str.IndexOf("nt") will give you an answer of -1 since this string does not exist!<br /><br /> str.IndexOf("a", 4) will give you an answer of -1 since starting at position 4, there is no matching string.<br /><br />Now let's look at the following function called the sub string:<br /><br /> str.SubString( starting position, how many places to move ) OR str.SubString( starting position )<br /><br />This function will now return a portion of a string instead of the index of a portion of a string. If you do not specify the second parameter, it will go all the way to the end of a string. So now let's use the "fanatic" string again.<br /><br /> str.SubString( 0, 3 ) will return "fan" since beginning at position 0 and going 3 places you get "fan"<br /><br /> str.SubString( 4, 2) will return "ti" since beginning at position 4 and going 2 places, you get "ti"<br /><br /> str.SubString( 4 ) will return "tic" since beginning at position 4 and going to the end you get "tic"<br /><br />(See example 5 in 3.4 for a great example of the previous string functions).<br /><br />For what we covered on Input Boxes and Message Boxes, please refer to the end of that chapter since I cannot draw on the blog. If there are any concerns about them, please let me know next week.<br /></span></span></span></span></span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-2411163667583315866?l=csci80.blogspot.com'/></div>Alexhttp://www.blogger.com/profile/18345997568670314403noreply@blogger.com0tag:blogger.com,1999:blog-3203751836604428014.post-69715021262420096742008-02-04T09:10:00.000-08:002008-02-04T08:38:59.597-08:00Chapter 3.3: NumbersWe went over numbers in the second to last class. We also learned about <b><i>listboxes</i></b>. A listbox is an object in which we can add things (like a list) one line at a time. There are two actions that we learned with listboxes:<br /><pre><br />listbox1<font color="blue">.Items.Add( ... )</font><br />listbox1<font color="blue">.Items.Clear()</font><br /></pre><br /><b><u>Note:</u></b> With the <font color="blue">.Add()</font> feature, the item that is to be added to the listbox is in the parentheses. Therefore we can add numbers to the listbox by placing them in the parentheses of <font color="blue">.Add()</font>.<br /><br /><font color="blue">.Clear()</font> simply erases whatever is in the listbox.<br /><br />Example:<br /><pre><br />lstResults<font color="blue">.Clear()</font><br />lstResults<font color="blue">.Items.Add(8)</font><br />lstResults<font color="blue">.Items.Add(4+3)</font><br />lstResults<font color="blue">.Items.Add(3*9)</font><br /></pre><br /><br />First the listbox is cleared (erased) with the <font color="blue">Clear()</font> statement. Then we will see the following:<br /><pre><br /> 8<br /> 7<br /> 27<br /></pre><br />Notice that the mathematic expressions are evaluated before the answer is printed.<br /><br /><font size="4">Built-in Functions</font><br /><br />We learned about some mathematical functions that we can use to make calculations easier:<br /><br /><ul><li><font color="blue">Math.Sqrt()</font></li><li><font color="blue">Int()</font></li><li><font color="blue">Math.Round()</font></li></ul><br /><br /><font color="blue">Math.Sqrt()</font> returns the square root of the mathematical expression placed in the parentheses.<br /><br /><font color="blue">Int()</font> returns the lower bound integer value of the mathematical expression in placed in the parentheses. <br /><br /><font color="blue">Math.Round()</font> returns the upper bound real value of the mathematical expression in placed in the parentheses. We are able to specify the number of decimal places we would like to round the value to as well.<br /><br />So the following code:<br /><br /> listbox1.Items.Add( <font color="blue">Math.Sqrt</font>( 16 ) )<br /> listbox1.Items.Add( <font color="blue">Math.Round</font>( 9.2675 ) )<br /> listbox1.Items.Add( <font color="blue">Int</font>( -9.2675 ) )<br /><br />would yield the following results:<br /><br /> 4<br /> 9.268<br /> -10<br /><br />Also remember that we can place <u>any</u> mathematical expression in the parentheses so we could even evaluate things as complicated as:<br /><br /> <font color="blue">Math.Round</font>( <font color="blue">Math.Sqrt</font>(19*20)/50+79, 3 )<br /><br /><font size="4">Variables</font><br />A variable is just a name for a place in memory that we can use as temporary storage. We learned that we could use variables to store numerical values in order to make our arithmetic operations more flexible. <br /><br />First we must declare the variable:<br /><br /> <font color="blue">Dim</font> varname <font color="blue">As</font> Type<br /><br /><b>varname</b> can be any name we choose as long as it:<br /><ol><li>begins with a letter</li><li>uses legal characters such as letters, numbers and underscores</li><li>is not used somewhere else in the program. For example the name of a form or listbox</li></ol><br /><br />The <b>Type</b> must also be given when the variable is declared. There are two types for numbers: <b><i>Integer</i></b> and <b><i>Double</i></b>. The type must be given when a variable is declared so that the compiler will know how much memory to allocate for the variable, and every type has a different amount of memory that it needs.<br /><br />So here is an example declaration:<br /><br /> <font color="blue">Dim</font> num <font color="blue">As Integer</font><br /><br />You will notice that the intellisense menu will pop up as you are declaring variables.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-6971502126242009674?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-46582854248618489662008-02-03T08:40:00.000-08:002008-02-04T08:49:00.437-08:00Installation of VB StudioYou can go to:<br /><br /><a href="http://msdn2.microsoft.com/en-us/vstudio/aa718407.aspx">http://msdn2.microsoft.com/en-us/vstudio/aa718407.aspx</a><br /><br />and click on <span style="font-weight:bold;">Download</span> and save the executable. After it finishes downloading double click the executable file to install Visual Studio 2005.<br /><br />The system requirements are listed on the right-hand-side of the page. Installation should work with default choices. Make sure that you register your installation so that you can keep using it for the semester. You can use fake information if you like, though I think you have to use a real email address to get the registration key. <br /><br />Feel free to email me if you have problems.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-4658285424861848966?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0tag:blogger.com,1999:blog-3203751836604428014.post-754644150848796242008-01-28T09:34:00.000-08:002008-01-28T12:46:14.350-08:00Welcome to CS 80!This blog will be used for class notes to help clarify some of the harder concepts of the class. You are welcomed to comment any post you like and ask questions. <br /><br />We also have a listserve that you can join where I will make announcements and you are encouraged to ask questions. The tutors and other lab instructors are also members of the listserve and may make their own announcements and answer questions as needed. I am hoping that this will make things easier for us to keep in touch.<br /><br />You can join by using the below:<table border=0 style="background-color: #fff; padding: 5px;" cellspacing=0><tr><td><img src="http://groups.google.com/groups/img/3nb/groups_bar.gif" height=26 width=132 alt="Google Groups"> </td></tr> <tr><td style="padding-left: 5px"><b>Subscribe to QC CS 80</b> </td></tr><form action="http://groups.google.com/group/qc-cs80/boxsubscribe"> <tr><td style="padding-left: 5px;"> Email: <input type=text name=email><input type=submit name="sub" value="Subscribe"></td></tr></form><tr><td align=right><br /><a href="http://groups.google.com/group/qc-cs80"><font color="blue">Visit this group</font></a></td></tr></table><br /><br />We have one tutor for CS 80 this semester: <span style="font-style:italic;"><span style="font-weight:bold;">Alex Maureau</span></span>. Emails can be found under the ``Contacts'' section of the class website. The tutoring schedule will be up later. There are also two other lab instructors for the evening section: <span style="font-style:italic;"><span style="font-weight:bold;">Daniel Gutlove</span></span> (T/Th eve) and <span style="font-weight:bold;"><span style="font-style:italic;">Smitha Kakkuzhi</span></span> (M/W eve) whose contact information is also found on the class website. Office hours will be posted later, as will the schedule.<br /><br />The first lab is: <a href="http://venus.cs.qc.edu/~jlevy/cs80/intro.html">http://venus.cs.qc.edu/~jlevy/cs80/intro.html</a>. See you on Tuesday!<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3203751836604428014-75464415084879624?l=csci80.blogspot.com'/></div>JLjamie.levy@gmail.com0