1 / 13

Methods

SWE 344 Internet Protocols & Client Server Programming. Methods. Method Structure. Methods are extremely useful because they allow you to separate your logic into different units . You can pass information to methods, have it perform one or more statements, and retrieve a return value.

vern
Download Presentation

Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SWE 344 Internet Protocols & Client Server Programming Methods

  2. Method Structure • Methods are extremely useful because they allow you to separate your logic into different units. • You can pass information to methods, have it perform one or more statements, and retrieve a return value. • The capability to pass parameters and return values is optional and depends on what you want the method to do. • The syntax required for creating a method: • attributes modifiers return-type method-name(parameters ) • { • statements • }

  3. Example #1: Method Structure using System; classOneMethod { publicstaticvoid Main() { stringmyChoice; OneMethodom = newOneMethod(); do { myChoice = om.getChoice(); // Make a decision based on the user's choice switch (myChoice) { case"A": case"a": Console.WriteLine("You wish to add an address."); break; case"D": case"d": Console.WriteLine("You wish to delete an address."); break; case"M": case"m": Console.WriteLine("You wish to modify an address."); break;

  4. Example #1: Method Structure • case"V": • case"v": • Console.WriteLine("You wish to view the address list."); • break; • case"Q": • case"q": • Console.WriteLine("Bye."); • break; • default: • Console.WriteLine("{0} is not a valid choice",myChoice); • break; • } • // Pause to allow the user to see the results • Console.WriteLine(); • Console.Write("press Enter key to continue..."); • Console.ReadLine(); • Console.WriteLine(); • } while (myChoice != "Q" || myChoice != "q"); // Keep going until the user wants to quit • }

  5. Example #1: Method Structure • stringgetChoice() • { • stringmyChoice; • // Print A Menu • Console.WriteLine("My Address Book\n"); • Console.WriteLine("A - Add New Address"); • Console.WriteLine("D - Delete Address"); • Console.WriteLine("M - Modify Address"); • Console.WriteLine("V - View Addresses"); • Console.WriteLine("Q - Quit\n"); • Console.Write("Choice (A,D,M,V,or Q): "); • // Retrieve the user's choice • myChoice = Console.ReadLine(); • Console.WriteLine(); • returnmyChoice; • } • }

  6. Example #2: Method Structure using System; classAddress { publicstring name; publicstring address; } classMethodParams { publicstaticvoid Main() { stringmyChoice; MethodParams mp = newMethodParams(); do { // show menu and get input from user myChoice = mp.getChoice(); // Make a decision based on the user's choice mp.makeDecision(myChoice); // Pause to allow the user to see the results Console.Write("press Enter key to continue..."); Console.ReadLine(); Console.WriteLine(); } while (myChoice != "Q"||myChoice != "q"); // Keep going until the // user wants to quit }

  7. Example #2: Method Structure • // show menu and get user's choice • stringgetChoice() • { • stringmyChoice; • // Print A Menu • Console.WriteLine("My Address Book\n"); • Console.WriteLine("A - Add New Address"); • Console.WriteLine("D - Delete Address"); • Console.WriteLine("M - Modify Address"); • Console.WriteLine("V - View Addresses"); • Console.WriteLine("Q - Quit\n"); • Console.WriteLine("Choice (A,D,M,V,or Q): "); • // Retrieve the user's choice • myChoice = Console.ReadLine(); • returnmyChoice; • } • // make decision • voidmakeDecision(stringmyChoice) • { • Addressaddr = newAddress(); • switch (myChoice) • { • case"A": • case"a": • addr.name = "Joe"; • addr.address = “Hail"; • this.addAddress(refaddr); • break;

  8. Example #2: Method Structure • case"D": • case"d": • addr.name = "Robert"; • this.deleteAddress(addr.name); • break; • case"M": • case"m": • addr.name = "Matt"; • this.modifyAddress(outaddr); • Console.WriteLine("Name is now {0}.", addr.name); • break; • case"V": • case"v": • this.viewAddresses("Cheryl", "Joe", "Matt", "Robert"); • break; • case"Q": • case"q": • Console.WriteLine("Bye."); • break; • default: • Console.WriteLine("{0} is not a valid choice", myChoice); • break; • } • }

  9. Example #2: Method Structure • // insert an address • voidaddAddress(refAddressaddr) • { • Console.WriteLine("Name: {0}, Address: {1} added.", addr.name, addr.address); • } • // remove an address • voiddeleteAddress(string name) • { • Console.WriteLine("You wish to delete {0}'s address.", name); • } • // change an address • voidmodifyAddress(outAddressaddr) • { • //Console.WriteLine("Name: {0}.", addr.name); // causes error! • addr = newAddress(); • addr.name = "Joe"; • addr.address = “Hail"; • } • // show addresses • voidviewAddresses(paramsstring[] names) • { • foreach (string name in names) • { • Console.WriteLine("Name: {0}", name); • } • } • }

  10. ref tells the compiler that the object is initialized before entering the function, while out tells the compiler that the object will be initialized inside the function.

  11. Question 2: Find the sum and multiplication of two numbers .Read the numbers from the user. Write the methods for sum and multiplication and return the value. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lab3 { class Class1 { public static void Main() { int x, y,sum,mult; Console.Write("Enter first number:"); x = Convert.ToInt16(Console.ReadLine()); Console.Write("Enter second number:");

  12. y = Convert.ToInt16(Console.ReadLine()); math m1 = new math(); sum = m1.sum(x, y); mult = m1.mult(x, y); Console.WriteLine("Sum={0}", sum); Console.WriteLine("Multiplication={0}", mult); Console.ReadLine(); } } class math { public int sum(int a, int b) { return a + b; } public intmult(int a, int b) { return a * b; } }}

  13. END

More Related