1 / 18

Object Oriented Program Terms

***** SWTJC STEM *****. Recall that a class is composed of data declarations, variable declarations, and methods. A method is a group of programming statements given a name. When a method’s name is invoked, its statements are executed.

Download Presentation

Object Oriented Program Terms

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. ***** SWTJC STEM ***** • Recall that a class is composed of data declarations, variable declarations, and methods. • A method is a group of programming statements given a name. • When a method’s name is invoked, its statements are executed. • When the method is done, control returns to the calling program. • A method may be part of the class in which it resides or in a completely different class. • If inside the same class it is invoked by using the method’s name. • If outside the class, it is invoked with the object’s name, then a “dot”, and lastly the method name. Ex. Math.PI ( ) • Let’s look again at the cylinder volume problem. Object Oriented Program Terms Class -- Method Chapter 4-2 cg 42

  2. ***** SWTJC STEM ***** Cylinder Object (UML) Cylinder ---------------------------------------------------- radius: double height: double ---------------------------------------------------- setRadius (double value): void getRadius( ): double setHeight (double value): void getHeight( ): double getVolume( ): double Class Attributes Methods Unified Modeling Language (UML) Diagram. Chapter 4-2 cg 42

  3. ***** SWTJC STEM ***** Cylinder Object (UML) Cylinder ---------------------------------------------------- radius: double height: double ---------------------------------------------------- setRadius (double value): void getRadius( ): double setHeight (double value): void getHeight( ): double getVolume( ): double variable type parameter type return type Unified Modeling Language (UML) Diagram. Chapter 4-2 cg 42

  4. ***** SWTJC STEM ***** Cylinder Objectw/ toString Method Cylinder ---------------------------------------------------- radius: double height: double ---------------------------------------------------- setRadius (double value): double getRadius( ): double setHeight (double value): double getHeight( ): double getVolume( ): double toString (): String Add New Method Cylinder UML modified to include toStringmethod.Note how easy it is to extend the capabilities of the class! Chapter 4-2 cg 42

  5. ***** SWTJC STEM ***** • It is common practice in Java to include a toString method that returns a string that "textually represents" the object. • The result should be a concise but informative representation that is easy for a person to read. • For the Cylinder class, toString returns the objects attributes including radius, height, and volume. • Since it will be visible outside the object, the method declaration begins with the visibility modifier public. • The return type is next. • When a method does not return a value, use the return type “void”. The main method always is void. Why? • When a method returns a value, specify the return type. The “toString” method will return a “String” value, so follow the visibility modifier “public” with the return type“String”. Adding a toString Method Chapter 4-2 cg 42

  6. ***** SWTJC STEM ***** Next is the method name “toString” followed by parentheses for parameters to be passed, and a semicolon. Lastly, we include the pair of braces that define the extent of the method. All statements within this pair of braces belong to the method So far we have:public String toString ( ); { ... Statements } Recalling that “toString” should return a “a concise but informative representation”, let’s use this format:Cylinder:radius = 5.0, height = 10.0, volume = 785.4 “Cylinder:”, “radius”, etc. are string literals, but how do we get the double floating point variables converted to strings? Adding a toString Method Visibility Modifier Name Return Type Chapter 4-2 cg 42

  7. ***** SWTJC STEM ***** • Easy! Numeric data types have a corresponding java.lang class that contains many useful service methods including “toString” that converts the value to a string. • Finally, concatenating the parts together, we have:public String toString ( ); {return "Cylinder: " + "radius = " + Double.toString(radius) + ", height = " + Double.toString(height) + ", volume = " + Double.toString(getVolume());} • Note that any method that returns a value must have a “return” statement, consisting of the reserved word “return” followed by an expressions that dictates the value returned. • The value of the expression must be consistent with the return type specified in the method declaration, in this case “String”. Adding a toString Method Chapter 4-2 cg 42

  8. ***** SWTJC STEM ***** • Let’s create a driver class to test the new “toString” method: public class CylinderToStringTester { public static void main(String[] args) { // First, construct a cylinder object Cylinder testCylinder = new Cylinder(5, 10); // Get object infomation System.out.println(testCylinder.toString()); // Set (mutate) radius to 4 and height to 8 testCylinder.setRadius(4); testCylinder.setHeight(8); // Get new object information System.out.println(testCylinder.toString()); } } Driver Code for Testing Chapter 4-2 cg 42

  9. ***** SWTJC STEM ***** • Running TestCylinderToString:Cylinder: radius = 5.0, height = 10.0, volume = 785.398... Cylinder: radius = 4.0, height = 8.0, volume = 402.1238... Checking The Output Chapter 4-2 cg 42

  10. ***** SWTJC STEM ***** • A void method (one that does not return a value) usually does not contain a return statement; it returns automatically when the method’s closing brace is reached. Such methods can contain a return statement without an expression. Example: // Radius mutator – Sets radius public void setRadius(double value){ radius = value; } // Height mutator – Sets height public void setHeight(double value){ height = value; } Void Methods – No return Chapter 4-2 cg 42

  11. ***** SWTJC STEM ***** It is not generally good practice to use more than one return statement in a method, even though it is possible to do so. In general, a method should have a single return statement as the last line in the method, unless this makes the method overly complex. As time goes by, we will explore more examples to see these practices more clearly. One return Per Method Chapter 4-2 cg 42

  12. ***** SWTJC STEM ***** The parameter list in the method header specifies the types of the values that are passed and the names by which the called method will refer to those values. The names of the parameter in the method header are called formal parameters. The parameters in the invocation (from where the method is called) are designated actual parameters. Actual parameters are called the arguments to the method. Parameters are always enclosed in parentheses. If there are no parameters, parentheses are still required Actual parameters are copied into the formal parameters when the method is invoked. Therefore, actual and formal parametersmust match in order and type. Let’s look at an example from the Cylinder class. Parameters Chapter 4-2 cg 42

  13. ***** SWTJC STEM ***** Actual Parameters2 double numeric literals radius, then height Parameters Examples Calling Statement Cylinder testCylinder = new Cylinder(5.0, 10.0); Matches inorder and type Called (Invoked) Method public Cylinder( double value 1, double value2) { radius = value1;height = value2; } Chapter 4-2 cg 42

  14. ***** SWTJC STEM ***** • Recall the output looks like this:Cylinder: radius = 5.0, height = 10.0, volume = 785.398... Cylinder: radius = 4.0, height = 8.0, volume = 402.1238... • The volume needs to be rounded off. • We could use the DecimalFormat class, but let’s take a different approach to practice adding methods. • Let’s add a method called “roundValue” that rounds a double to one decimal place. • The math is based on this algorithm: • Multiply the value by 10.0. (7853.98...) • Add 0.5 (7854.48...) • Truncate to an integer. (7854) • Divide by 10.0 (785.4) • Done. (785.398... is rounded to 785.4) Cleaning Up the Output Chapter 4-2 cg 42

  15. ***** SWTJC STEM ***** • The code to accomplish this is: private double roundValue(double value){ return (Math.rint(10.0 * value + 0.5) / 10.0); } • Note the use of the visibility modifier private, since, as a support method, it will only be used inside the object. • We need to modify the toString method to incorporate the new roundValue method. public String toString() { return "Cylinder: " + "radius = " + Double.toString(radius) + ", height = " + Double.toString(height) + ", volume = " + Double.toString(roundValue(getVolume())); } Adding A Support Method Note: Class not needed! Method is in class. Chapter 4-2 cg 42

  16. ***** SWTJC STEM ***** • Running the test program: Cylinder: radius = 5.0, height = 10.0, volume = 785.4 Cylinder: radius = 4.0, height = 8.0, volume = 402.1 Checking The Output Chapter 4-2 cg 42

  17. ***** SWTJC STEM ***** Cylinder UML modified to include roundValue method Cylinder Objectw/ roundValue Method Cylinder ---------------------------------------------------- radius: double height: double ---------------------------------------------------- setRadius (double value): void getRadius( ): double setHeight (double value): void getHeight( ): double getVolume( ): double toString (): String -roundValue(double value): double Note: Minus sign denotesprivate (support) method Chapter 4-2 cg 42

  18. ***** SWTJC STEM ***** Class statement - Public Attribute variable declarations - Private Constructor method - Public Setter and getter methods for attributes - Public Getter methods for calculated values - Public toString method - Public Support methods - Private Class Components Chapter 4-2 cg 42

More Related