Chapter 9 • Using Classes and Objects PDF
Document Details
Uploaded by AdvantageousZeal
Tags
Summary
This document provides a detailed explanation of classes and objects in C# programming. It includes examples of creating a Rectangle class, methods to manipulate rectangles, and demonstrations of how to use these classes. The intended audience is likely undergraduate students.
Full Transcript
Practice Example Chapter 9 Using Classes and Objects 1 Another Example: Building a Rectangle class A Rectangle object will have the following fields: – length - holds the rectangle’s length. – width - holds the rectangle’s width. Build...
Practice Example Chapter 9 Using Classes and Objects 1 Another Example: Building a Rectangle class A Rectangle object will have the following fields: – length - holds the rectangle’s length. – width - holds the rectangle’s width. Building a Rectangle class The Rectangle class will also have the following methods (no Properties) yet: – SetLength - sets a value in an object’s length field. – SetWidth - sets a value in an object’s width field. – GetLength - returns the value in an object’s length field. – GetWidth - returns the value in an object’s width field. – GetArea - returns the area of the rectangle, which is the result of the object’s length multiplied by its width. Writing the Code for the Class Fields public class Rectangle { private double length; private double width; } Header for the SetLength Method Return Notice the word Type static does not Access Method appear in the method specifier Name header designed to work on an instance of a class (instance method). public void SetLength (double len) Parameter variable declaration Writing and Demonstrating the SetLength Method // The method stores a value in the length field. // public void SetLength( double len ) { length = len; } Header for the GetLength Method Return Notice again the word Type static does not Access Method appear in the method specifier Name header designed to work on an instance of a class (instance method). public double GetLength () No Parameters Writing and Demonstrating the GetLength Method // The method returns the value in the length field. // public double GetLength() { return length; } Rectangle.cs (Version 1) public class Rectangle { private double length; private double width; // The SetLength method stores a value in the length field // **param len - The new value to store in length. public void SetLength(double len) { length = len; } // The SetWidth method stores a value in the width field // **param wid - The new value to store in width. public void SetWidth(double wid) { width = wid; } Rectangle.cs (cont’d) // The GetLength method returns a Rectangle object's length // *return The value in the length field. public double GetLength() { return length; } // The GetWidth method returns a Rectangle object's width // **return The value in the width field. public double GetWidth() { return width; } // The GetArea method returns a Rectangle object's area. // **return The product of length times width. public double GetArea() { return length * width; } } RectangleDemo.cs // This program demonstrates the Rectangle class' // SetLength, SetWidth, GgetLength, GetWidth, and getArea methods. using System; public static class RectangleDemo { The box’s length is 10 public static void Main() The box’s width is 20 { The box’s area is 200 Rectangle box = new Rectangle(); box.SetLength(10.0); box.SetWidth(20.0); Console.WriteLine("The box's length is {0}", box.GetLength()); Console.WriteLine("The box's width is {0}", box.GetWidth()); Console.WriteLine("The box's area is {0}", box.GetArea()); Console.ReadLine(); } } Create a Rectangle class object Rectangle box = new Rectangle(); A Rectangle object The box variable holds length: 0.0 the address of address the Rectangle width: 0.0 object. Calling the SetLength Method box.SetLength(10.0); A Rectangle object The box variable holds length: 10.0 the address of address the Rectangle width: 0.0 object. This is the state of the box object after the SetLength method executes. Accessors and Mutators of Rectangle Class For the rectangle example, the accessors and mutators are: – SetLength : Sets the value of the length field. public void SetLength(double len) … – SetWidth : Sets the value of the width field. public void SetWidth(double w) … – GetLength : Returns the value of the length field. public double GetLength() … – GetWidth : Returns the value of the width field. public double GetWidth() … Other names for these methods are getters and setters. Rectangle.cs (Version 2) public class Rectangle { private double length; private double width; // Parameterless constructor public Rectangle() { length = 0; width = 0; } // Parameter Constructor // ** len - The new length of the rectangle. // ** wid - The new width of the rectangle. public Rectangle(double len, double wid) { length = len; width = wid; } Rectangle.cs (cont’d) // The SetLength method stores a value in the length field // **param len - The new value to store in length. public void SetLength(double len) { length = len; } // The SetWidth method stores a value in the width field // **param wid - The new value to store in width. public void SetWidth(double wid) { width = wid; } // The GetLength method returns a Rectangle object's length // *return The value in the length field. public double GetLength() { return length; } Rectangle.cs (cont’d) // The GetWidth method returns a Rectangle object's width // **return The value in the width field. public double GetWidth() { return width; } // The GetArea method returns a Rectangle object's area. // **return The product of length times width. public double GetArea() { return length * width; } } Instance Fields and Methods Instance fields and instance methods require an object to be created in order to be used. Note that each room represented in this example can have different dimensions. Rectangle kitchen = new Rectangle(); Rectangle bedroom = new Rectangle(); Rectangle den = new Rectangle(); RoomArea.cs / This program creates three instances of the Rectangle class. using System; Public static class RoomAreas { public static void Main() { double number, // To hold a number totalArea; // The total area // Create three Rectangle objects. Rectangle kitchen = new Rectangle(); Rectangle bedroom = new Rectangle(); Rectangle den = new Rectangle(); // Get and store the dimensions of the kitchen. Console.Write("What is the kitchen's length?"); number = Convert.ToDouble(Console.ReadLine()); kitchen.SetLength(number); RoomArea.cs (cont’d) Console.Write("What is the kitchen's width?"); number = Convert.ToDouble(Console.ReadLine()); kitchen.SetWidth(number); // Get and store the dimensions of the bedroom. Console.Write("What is the bedroom's length? "); number = Convert.ToDouble(Console.ReadLine()); bedroom.SetLength(number); Console.Write("What is the bedroom's width? "); number = Convert.ToDouble(Console.ReadLine()); bedroom.SetWidth(number); // Get and store the dimensions of the den. Console.Write("What is the den's length? "); number = Convert.ToDouble(Console.ReadLine()); den.SetLength(number); RoomArea.cs (cont’d) Console.Write("What is the den's width?"); number = Convert.ToDouble(Console.ReadLine()); den.SetWidth(number); // Calculate the total area of the rooms. totalArea = kitchen.GetArea() + bedroom.GetArea() + den.GetArea(); // Display the total area of the rooms. Console.WriteLine("\nThe total area of the rooms is {0}", totalArea); Console.ReadLine(); What is the kitchen's length?15 } What is the kitchen's width?10 } What is the bedroom's length? 12 What is the bedroom's width? 9 What is the den's length? 11 What is the den's width?11 The total area of the rooms is 379 States of Three Different Rectangle Objects The kitchen variable length: 15.0 holds the address of a address Rectangle Object. width: 10.0 The bedroom variable length: 12.0 holds the address of a address Rectangle Object. width: 9.0 The den variable length: 11.0 holds the address of a address Rectangle Object. width: 11.0 Rectangle.cs (Version 3) Using Properties public class Rectangle { private double length; private double width; // No-Arg constructor public Rectangle() { length = 0; width = 0; } // Parameter Constructor public Rectangle(double len, double wid) { length = len; width = wid; } Rectangle.cs (cont’d) // the get and set for the Width property // the get and set for the Length property public double Width public double Length { { get get { return length; } { return width; } set set { { if (value < 0) if (value < 0) length = 0; width = 0; else else length = value; width = value; } } } } // GetArea method public double GetArea() { return Length * Width; } } PropertyTest.cs // This program demonstrates Rectangle1.cs. // This tests C# properties. using System; public static class PropertyDemo The box's length is 5 { The box's width is 15 public static void Main() { The box's area is 75 Rectangle box = new Rectangle(); box.Length = 5.0; box.Width = 15.0; Console.WriteLine("The box's length is {0}", box.Length); Console.WriteLine("The box's width is {0}", box.Width); Console.WriteLine("The box's area is {0}", box.GetArea()); Console.ReadLine(); } }