Classes/Methods C# Framework Class Library (FCL) defines many classes, e.g.,
CSc 330 C# Programming
Console MessageBox Int32 Math
The definition of a class includes both methods and data properties:
methods, e.g., • Console.Write( ) • Console.WriteLine( ) • Int32.Parse( )
Methods, Classes, and Objects
properties, e.g., • • • •
Int32.MinValue Int32.MaxValue Math.PI Math.E
2
Example: Math Class Methods
Methods
Method Abs( x )
A method should provide a well-defined, easy-to-understand
functionality
Ceiling( x ) rounds x to the smallest integer not less than x Cos( x ) trigonometric cosine of x (x in radians) Exp( x ) exponential method ex
A method takes input (parameters), performs some actions, and (sometime) returns a value
Floor( x )
Example: invoking the WriteLine method of the Console
Log( x )
class: Console.WriteLine ( "Whatever you are, be a good one.“ );
Max( x, y )
Min( x, y )
class
method dot
Description absolute value of x
Pow( x, y )
Information provided to the method (parameters)
Sin( x ) Sqrt( x ) Tan( x )
3
Methods Provide Abstraction and Reuse
Example Abs( 23.7 ) is 23.7 Abs( 0 ) is 0 Abs( -23.7 ) is 23.7 Ceiling( 9.2 ) is 10.0 Ceiling( -9.8 ) is -9.0 Cos( 0.0 ) is 1.0
Exp( 1.0 ) is approximately 2.7182818284590451 Exp( 2.0 ) is approximately 7.3890560989306504 rounds x to the largest integer Floor( 9.2 ) is 9.0 not greater than x Floor( -9.8 ) is -10.0 natural logarithm of x (base e) Log( 2.7182818284590451 ) is approximately 1.0 Log( 7.3890560989306504 ) is approximately 2.0 larger value of x and y Max( 2.3, 12.7 ) is 12.7 (also has versions for float, Max( -2.3, -12.7 ) is -2.3 int and long values) smaller value of x and y Min( 2.3, 12.7 ) is 2.3 (also has versions for float, Min( -2.3, -12.7 ) is -12.7 int and long values) x raised to power y (xy) Pow( 2.0, 7.0 ) is 128.0 Pow( 9.0, .5 ) is 3.0 trigonometric sine of x Sin( 0.0 ) is 0.0 (x in radians) square root of x Sqrt( 900.0 ) is 30.0 Sqrt( 9.0 ) is 3.0 trigonometric tangent of x Tan( 0.0 ) is 0.0 (x in radians)
Commonly used Math class methods.
4
More Examples of Classes/Methods
An abstraction hides (or ignores) the right details
at the right time
The Console class The Write and WriteLine methods
A method is abstract in that we don't really have to
think about its internal details in order to use it
e.g., we don't have to know how the WriteLine method works in order to invoke it
The String class The Format method: e.g.,
Why abstraction? Divide and conquer Reuse Easier to understand
String.Format( “{0:C}”, 2.0 )
5
6
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
More Examples of Classes/Methods The MessageBox class The Show method we will use has four parameters • text to be displayed, caption, buttons, icon
Argument 4: MessageBox Icon (Optional)
Argument 2: Title bar string (Optional)
Outline
// Sum.cs // Summation with the for structure. using System; using System.Windows.Forms;
Sum.cs
class Sum { static void Main( string[] args ) { int sum = 0;
The caption of the message box
for ( int number = 2; number = 0 && value < 24 ) ? value : 0 ); } } // end property Hour
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
// property Minute public int Minute { get { return minute; }
Outline Property Second Time3.cs
set { Property Minute minute = ( ( value >= 0 && value < 60 ) ? value : 0 ); } } // end property Minute // property Second public int Second { get { return second; } set { second = ( ( value >= 0 && value < 60 ) ? value : 0 ); } } // end property Second
10
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
// convert time to universal-time (24 hour) format string public string ToUniversalString() { return String.Format( "{0:D2}:{1:D2}:{2:D2}", Hour, Minute, Second ); }
Outline Time3.cs
// convert time to standard-time (12 hour) format string public string ToStandardString() { return String.Format( "{0}:{1:D2}:{2:D2} {3}", ( ( Hour == 12 || Hour == 0 ) ? 12 : Hour % 12 ), Minute, Second, ( Hour < 12 ? "AM" : "PM" ) ); } } // end class Time3
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
public TimeTest3() { // Required for Windows Form Designer support InitializeComponent();
98 99 100 101 102 103 104 105 106 107 108 109
if ( time.Second == 0 ) { time.Minute = ( time.Minute + 1 ) % 60;
Outline TimeTest3.cs
time = new Time3(); UpdateDisplay(); } // Visual Studio .NET generated code // main entry point for application [STAThread] static void Main() { Application.Run( new TimeTest3() ); } // update display labels public void UpdateDisplay() { displayLabel1.Text = "Hour: " + time.Hour + "; Minute: " + time.Minute + "; Second: " + time.Second; displayLabel2.Text = "Standard time: " + time.ToStandardString() + "\nUniversal time: " + time.ToUniversalString(); }
if ( time.Minute == 0 ) time.Hour = ( time.Hour + 1 ) % 24;
Outline TimeTest3.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
Outline
// TimeTest3.cs // Demonstrating Time3 properties Hour, Minute and Second. using using using using using using
System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data;
TimeTest3.cs
// TimeTest3 class definition public class TimeTest3 : System.Windows.Forms.Form { private System.Windows.Forms.Label hourLabel; private System.Windows.Forms.TextBox hourTextBox; private System.Windows.Forms.Button hourButton; private System.Windows.Forms.Label minuteLabel; private System.Windows.Forms.TextBox minuteTextBox; private System.Windows.Forms.Button minuteButton; private System.Windows.Forms.Label secondLabel; private System.Windows.Forms.TextBox secondTextBox; private System.Windows.Forms.Button secondButton; private System.Windows.Forms.Button addButton; private System.Windows.Forms.Label displayLabel1; private System.Windows.Forms.Label displayLabel2; // required designer variable private System.ComponentModel.Container components = null; private Time3 time;
// set Hour property when hourButton pressed private void hourButton_Click( Set Minute property object sender, System.EventArgs e ) Time3 object { time.Hour = Int32.Parse( hourTextBox.Text ); hourTextBox.Text = ""; Set Second property UpdateDisplay(); } of Time3 object // set Minute property when minuteButton pressed private void minuteButton_Click( object sender, System.EventArgs e ) { time.Minute = Int32.Parse( minuteTextBox.Text ); minuteTextBox.Text = ""; UpdateDisplay(); Add 1 second to Time3 }
Outline of TimeTest3.cs
Set Hour property of Time3 object
object
// set Second property when secondButton pressed private void secondButton_Click( object sender, System.EventArgs e ) { time.Second = Int32.Parse( secondTextBox.Text ); secondTextBox.Text = ""; UpdateDisplay(); } // add one to Second when addButton pressed private void addButton_Click( object sender, System.EventArgs e ) { time.Second = ( time.Second + 1 ) % 60;
Using Access Modifiers to Implement Encapsulation: Methods
}
Public methods present to the class’s clients a view of the
UpdateDisplay(); }
services that the class provides
} // end class TimeTest3
• public methods are also called service methods Program Output
A method created simply to assist service methods
is called a support or helper method • since a support method is not intended to be called by a client, it should not be declared with public accessibility
66
11
The Effects of Public and Private Accessibility public
private
variables
violate Encapsulation Unless properties
enforce encapsulation
methods
provide services to clients
support other methods in the class
67
12