Lecture 5:
description
Transcript of Lecture 5:
![Page 1: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/1.jpg)
Lecture 5:
Structured Variables & File Systems
![Page 2: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/2.jpg)
![Page 3: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/3.jpg)
![Page 4: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/4.jpg)
![Page 5: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/5.jpg)
![Page 6: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/6.jpg)
![Page 7: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/7.jpg)
![Page 8: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/8.jpg)
Checking Variable Range
![Page 9: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/9.jpg)
![Page 10: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/10.jpg)
![Page 11: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/11.jpg)
![Page 12: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/12.jpg)
Enumerations
Structs
Arrays
![Page 13: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/13.jpg)
Enumerations
![Page 14: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/14.jpg)
Structs
![Page 15: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/15.jpg)
Arrays
![Page 16: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/16.jpg)
Specifying an Array
![Page 17: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/17.jpg)
![Page 18: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/18.jpg)
![Page 19: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/19.jpg)
![Page 20: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/20.jpg)
Declaring Jagged Arrays
![Page 21: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/21.jpg)
![Page 22: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/22.jpg)
![Page 23: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/23.jpg)
![Page 24: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/24.jpg)
![Page 25: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/25.jpg)
![Page 26: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/26.jpg)
![Page 27: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/27.jpg)
![Page 28: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/28.jpg)
![Page 29: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/29.jpg)
![Page 30: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/30.jpg)
The Generic List<T>loading data from a file
using System.IO; : :namespace EmpListDemo{ static class Program { static void Main(string[] args) { string fname = "empstable.txt"; string txtline;
List<Employee> Emps = new List<Employee>(); // a generic list of Employee List<Employee> Emps2 = new List<Employee>(); // we will make a copy of Emps // reading employee data from a text file TextReader tr = new StreamReader(fname); do { txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split(','); Emps.Add(new Employee(field[0].Trim(),field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4])));
} while (true); tr.Close(); : :
![Page 31: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/31.jpg)
// display the contents of the list Empsforeach (Employee emp in Emps){ Console.WriteLine("{0} {1} {2} {3} {4}", emp.FirstName, emp.LastName, emp.Age, emp.YrsEmp, emp.Wage);}
Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9
Displaying the Contents of the List Emps
![Page 32: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/32.jpg)
// we are making a copy of Emps called Emps2foreach (Employee emp in Emps){ Employee emp2 = new Employee(); emp2 = emp; Emps2.Add(emp2);}// so why not just assign one list to the other?// Emps2 = Emps;//// because this would not make a separate copy but// rather point both Emps2 and Emps to the same records!
Making a Copy of a List
![Page 33: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/33.jpg)
// we "tag" each record that passes our criteriaforeach (Employee emp in Emps2){ if (emp.Age > 39 & emp.YrsEmp >= 10) emp.Tag = true;}
// now we remove all records from Emps2 that HAVE NOT // been "tagged" i.e. remove those with emp.Tag = false// this construct is implemented using a delegateEmps2.RemoveAll(delegate(Employee emp){ return !emp.Tag;});
The RemoveAll Delegate Method
age>39 and yrsemp >= 10Wade Boggs 45 20 12.5Amanda Rekonwith 55 25 22.5
Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9
![Page 34: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/34.jpg)
5 6 4 3 2 7 7 8 6 54 3 2 5 3 2 3 8 8 81 1 2 2 3 3 6 4 5 33 5 5 6 4 9 7 5 2 04 3 8 7 0 4 3 2 5 41 3 2 4 3 5 4 6 5 77 5 6 8 7 7 5 4 7 91 3 2 4 3 6 5 4 3 28 8 9 6 5 5 3 0 0 11 1 1 6 6 8 8 7 6 5
using System.IO;using System.Collections.Generic;using System.Linq;using System.Text;
namespace LoadArrayFromTextfile{ class Program { static void Main(string[] args) { int[,] mat = new int[10,10]; string textline; int k;
TextReader tr = new StreamReader("sample_01.txt"); for (int i = 0; i < 10; i++) { textline = tr.ReadLine(); k = 0; foreach (string str in textline.Split(' ')) { if (str != "") { mat[i, k] = Convert.ToInt32(str); k += 1; } } } tr.Close();
Loading an Array from a Text File
for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.Write("{0} ", mat[i, j]); } Console.WriteLine(); } Console.ReadKey(); } }}
![Page 35: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/35.jpg)
List<Employee> Emps = new List<Employee>();string txtline;
TextReader tr = new StreamReader("employees.txt");
do{ txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split('\t'); Emps.Add(new Employee(field[0].Trim(), field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4])));
} while (true);tr.Close();
Reading and Writing Textfiles
![Page 36: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/36.jpg)
foreach (Employee emp in Emps){ Console.WriteLine("{0} {1}", emp.FirstName, emp.LastName);}
TextWriter tw = new StreamWriter("employees.txt");foreach (Employee emp in Emps){ tw.WriteLine("{0} \t {1} \t {2} \t {3} \t {4}", emp.FirstName, emp.LastName, emp.Age, emp.YrsEmp, emp.Wage);}tw.WriteLine("xxx");tw.Close();
Reading and Writing Text Filescontinued
![Page 37: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/37.jpg)
Dealing with Datacut & paste
![Page 38: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/38.jpg)
Pasting into Word or PPT Preserves Cells
![Page 39: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/39.jpg)
Pasting to a Text Editor Creates Separators e.g. Tabs
![Page 40: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/40.jpg)
![Page 41: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/41.jpg)
![Page 42: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/42.jpg)
An Example ProjectEmployee Records Manager
![Page 43: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/43.jpg)
![Page 44: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/44.jpg)
![Page 45: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/45.jpg)
Main Form Concept
![Page 46: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/46.jpg)
Defining Test Sets
![Page 47: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/47.jpg)
Incremental Software Development
![Page 48: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/48.jpg)
![Page 49: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/49.jpg)
![Page 50: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/50.jpg)
![Page 51: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/51.jpg)
![Page 52: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/52.jpg)
![Page 53: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/53.jpg)
![Page 54: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/54.jpg)
![Page 55: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/55.jpg)
![Page 56: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/56.jpg)
![Page 57: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/57.jpg)
![Page 58: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/58.jpg)
Messages to User
changed is used to indicate that there is a current edit
alerted is used to indicate that user has been alerted to an issue
saved is used to indicate that the current employess list has been saved to the hard drive
![Page 59: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/59.jpg)
![Page 60: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/60.jpg)
![Page 61: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/61.jpg)
![Page 62: Lecture 5:](https://reader035.fdocuments.in/reader035/viewer/2022062805/56814e28550346895dbb8f6f/html5/thumbnails/62.jpg)
Summary
Type Conversions
Enumerations
Structs
Arrays
String Manipulation
Generic Type List<T>