Methods SWE 344 Internet Protocols & Client Server Programming.

Slides:



Advertisements
Similar presentations
Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
Advertisements

Section 2 - Selection and Repetition. Equality and Relational Operators.
Getting Started with C# 1 SWE 344 Internet Protocols & Client Server Programming.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
C# Programming: From Problem Analysis to Program Design1 Methods and Behaviors C# Programming: From Problem Analysis to Program Design 3rd Edition 3.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
תכנות ב C#. דוגמא לפלט using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
4. Statements and Methods. 2 Microsoft Objectives “With regards to programming statements and methods, C# offers what you would come to expect from a.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
1. 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class.
C#: Statements and Methods Based on slides by Joe Hummel.
Reading and Writing to the Console Svetlin Nakov Telerik Corporation
Introduction to Classes SWE 344 Internet Protocols & Client Server Programming.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Advanced Programming LOOP.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Computer Programming 1.  Editor Console Application Notepad Notepad++ Edit plus etc.  Compiler & Interpreter Microsoft.NET Framework  Microsoft visual.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn.
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.
CPS120: Introduction to Computer Science Lecture 14 Functions.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Exercise 2 Introduction to C# CIS Create a class called Employee that contains the following private instance variables: Social Securitystring.
CIS 3301 C# Lesson 3 Control Statements - Selection.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool, string.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
MT262A Review.
C# — Console Application
Repeating Code Multiple Times
COMP 170 – Introduction to Object Oriented Programming
Chapter Topics 11.1 Introduction to Menu-Driven Programs
Chapter 4 – Control Structures Part 1
Control Statement Examples
Advanced Programming Lecture 02: Introduction to C# Apps
Conditional Statements
عرض اجمالي المهام الشرطية في سي شارب (الأمر if)
Social Media And Global Computing Managing MVC with Custom Models
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Module5 Looping Techniques: Part 2
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Looping III (do … while statement)
Lecture 5: Functions and Parameters
Lecture 1 Review of 1301/1321 CSE /26/2018.
Chapter 5 – Control Structures: Part 2
Arrays Arrays A few types Structures of related data items
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Lecture 1 Review of 1301/1321 CSE /26/2018.
Presentation transcript:

Methods SWE 344 Internet Protocols & Client Server Programming

 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 } Method Structure 2

using System; class OneMethod { public static void Main() { string myChoice; OneMethod om = new OneMethod(); 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; Example #1: Method Structure 3

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 } Example #1: Method Structure 4

string getChoice() { string myChoice; // 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(); return myChoice; } Example #1: Method Structure 5

using System; class Address { public string name; public string address; } class MethodParams { public static void Main() { string myChoice; MethodParams mp = new MethodParams(); 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 } Example #2: Method Structure 6

// show menu and get user's choice string getChoice() { string myChoice; // 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(); return myChoice; } // make decision void makeDecision(string myChoice) { Address addr = new Address(); switch (myChoice) { case "A": case "a": addr.name = "Joe"; addr.address = “Hail"; this.addAddress(ref addr); break; Example #2: Method Structure 7

case "D": case "d": addr.name = "Robert"; this.deleteAddress(addr.name); break; case "M": case "m": addr.name = "Matt"; this.modifyAddress(out addr); 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; } Example #2: Method Structure 8

// insert an address void addAddress(ref Address addr) { Console.WriteLine("Name: {0}, Address: {1} added.", addr.name, addr.address); } // remove an address void deleteAddress(string name) { Console.WriteLine("You wish to delete {0}'s address.", name); } // change an address void modifyAddress(out Address addr) { //Console.WriteLine("Name: {0}.", addr.name); // causes error! addr = new Address(); addr.name = "Joe"; addr.address = “Hail"; } // show addresses void viewAddresses(params string[] names) { foreach (string name in names) { Console.WriteLine("Name: {0}", name); } Example #2: Method Structure 9

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 int mult(int a, int b) { return a * b; } }}

END 13