1. Trang chủ
  2. » Giáo Dục - Đào Tạo

C lab07

5 95 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

PROGRAMMING IN C# Module 13: Collections Lab Guide for Lab7 Session Objectives In this session, you will be practicing with  Use Collections classes to work with set of objects Part – Getting started (30 minutes) Add User defined Objects to an ArrayList Step 1: Open Visual Studio Step 2: Select the menu File->New->Project to create console based project named ‘ArrayListClass’ and Step 3: Create Product class and write code in Program class using System; using System.Collections; class Product {     string name;     double cost;     int onhand;     public Product(string n, double c, int h)     {         name = n;         cost = c;         onhand = h;     }     public override string ToString()     {         return String.Format("{0,­10}Cost: {1,6:C} On hand: {2}", name, cost,  onhand);    } } class Program {     public static void Main()     {         ArrayList inv = new ArrayList();         // Add elements to the list         inv.Add(new Product("A", 5.9, 3));         inv.Add(new Product("B", 8.2, 2));         inv.Add(new Product("C", 3.5, 4));         inv.Add(new Product("D", 1.8, 8));         Console.WriteLine("Product list:");         foreach (Product i in inv)         {             Console.WriteLine(" " + i);         }     } } Step 4: Select menu File -> Save to save the file Step 5: Select Build -> Build ‘ArrayListClass’ option to build the project Step 6: Select Debug -> Start without Debuging to execute the program The output of the program as following SortedList class Step 1: Add a console based project ‘SortedListClass’ to the solution Step 2: Right click on project SortedListClass -> set as Startup project Step 3: Replace the code in ‘Program.cs’ with the given code using System; using System.Collections; public class Program {     public static void Main()     {         SortedList objSL = new SortedList();         objSL.Add("4", "!");         objSL.Add("3", "Brilliant");         objSL.Add("2", "am");         objSL.Add("1", "I");         Console.WriteLine("Number of Elements in objSL : {0}", objSL.Count);         Console.WriteLine("\t­KEY­\t­VALUE­");         for (int i = 0; i  Save to save the file Step 5: Select Build -> Build ‘SortedListClass’ option to build the project Step 6: Select Debug -> Start without Debuging to execute the program The output of program as following Add key-value pair to Hashtable by using the indexer Step 1: Add a console based project ‘HashtableTest’ to the solution Step 2: Right click on project HashtableTest -> set as Startup project Step 3: Replace the code in ‘Program.cs’ with the given code using System; using System.Collections; class Program {     public static void Main()     {         Hashtable ht = new Hashtable();         ht.Add("a", "A");         ht.Add("b", "B");         ht.Add("c", "C");         ht.Add("e", "E");         ht["f"] = "F";         // Get a collection of the keys         ICollection c = ht.Keys;         foreach (string str in c)             Console.WriteLine(str + ": " + ht[str]);      } } Step 4: Select menu File -> Save to save the file Step 5: Select Build -> Build ‘HashtableTest’ option to build the project Step 6: Select Debug -> Start without Debuging to execute the program The output of program as following Part – Workshops (30 minutes)   Quickly look at Module 13’s workshops for reviewing basic steps for using ArrayList, Hashtable, SortedList and Dictionary types Try to compile, run and observe the output of sample code provided for related workshop Discuss with your class-mate and your instructor if needed Part – Lab Assignment (60 minutes) Do the assignment for Module 13 carefully Discuss with your class-mates and your instructor if needed See ACTCSharp_Module13_Assignment.pdf file Part – Do it your self Write a program to manager a list of Employees of a department First, create Employee class with name, age, salary properties Next, create EmployeeManager class with a member ArrayList to contain all Employee objects This class has Add, Modify, Remove and Display methods to manage list of Employees, create parameters appropriately In the main method, create a menu to test all the methods of EmployeeManager class Change the member ArrayList, replace it by Hashtable and Dictionary type and change all the methods of EmployeeManager class to control the new member

Ngày đăng: 27/10/2019, 09:28

Xem thêm:

Mục lục

    Part 1 – Getting started (30 minutes)

    Part 3 – Lab Assignment (60 minutes)

    Part 4 – Do it your self

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w