Avalue 200 line 2 int bvalue 896 line 3 int result

This preview shows page 36 - 44 out of 70 pages.

aValue = 200; Line 2 int bValue = 896; Line 3 int result; Line 4 result = Math.Max(aValue, bValue); // result = 896 Line 5 result += bValue * Line 6 Math.Max(aValue, bValue) – aValue; // result = 896 + (896 * 896 - 200) (result = 803512) Line 7 Console.WriteLine(“Largest value between {0} ” Line 8 + “and {1} is {2}”, aValue, bValue, Line 9 Math.Max(aValue, bValue)); In an assignment statement Part of arithmetic expression Argument to another method call
Image of page 36

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 37 Writing Your Own Class Methods [modifier(s)] returnType MethodName ( parameterList ) { // body of method - consisting of executable statements } void Methods Simplest to write No return statement
Image of page 37
C# Programming: From Problem Analysis to Program Design 38 Writing Your Own Class Methods – void Types public static void DisplayInstructions( ) { Console.WriteLine(“This program will determine how ” + “much carpet to purchase.”); Console.WriteLine( ); Console.WriteLine(“You will be asked to enter the ” + “ size of the room and ”); Console.WriteLine(“the price of the carpet, ” + ”in price per square yards.”); Console.WriteLine( ); } class method A call to this method looks like: DisplayInstructions( );
Image of page 38

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 39 Writing Your Own Class Methods – void Types ( continued ) public static void DisplayResults( double squareYards, double pricePerSquareYard) { Console.Write(“Total Square Yards needed: ”); Console.WriteLine(“{0:N2}”, squareYards); Console.Write(“Total Cost at {0:C} “, pricePerSquareYard); Console.WriteLine(“ per Square Yard: {0:C}”, (squareYards * pricePerSquareYard)); } static method called from within the class where it resides To invoke method – DisplayResults(16.5, 18.95);
Image of page 39
C# Programming: From Problem Analysis to Program Design 40 Value-Returning Method Has a return type other than void Must have a return statement Compatible value Zero, one, or more data items may be passed as arguments Calls can be placed: In assignment statements In output statements In arithmetic expressions Or anywhere a value can be used
Image of page 40

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 41 Value-Returning Method ( continued ) public static double GetLength( ) { string inputValue; int feet, inches; Console.Write(“Enter the Length in feet: ”); inputValue = Console.ReadLine( ); feet = int .Parse(inputValue); Console.Write(“Enter the Length in inches: “); inputValue = Console.ReadLine( ); inches = int .Parse(inputValue); return (feet + ( double ) inches / 12); } Return type→ double double returned
Image of page 41
C# Programming: From Problem Analysis to Program Design 42 CarpetExampleWithClassMethods /* CarpetExampleWithClassMethods.cs */ using System; namespace CarpetExampleWithClassMethods { public class CarpetWithClassMethods { public static void Main( ) { double roomWidth, roomLength, pricePerSqYard, noOfSquareYards; DisplayInstructions( ); // Call getDimension( ) to get length roomLength = GetDimension(“Length”);
Image of page 42

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 43 CarpetExampleWithClassMethods ( continued ) /* CarpetExampleWithClassMethods.cs */ using System; namespace CarpetExampleWithClassMethods { public class CarpetWithClassMethods {
Image of page 43
Image of page 44
  • Fall '15
  • Program design, Method, Subroutine, Mutator method, noOfSquareYards

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern