CHAPTER SIX: OBJECT-ORIENTED PROGRAMING 6.2: Object- oriented Programming overview Object: a set of related data which identifies the current state of the object + a set of behaviors. An objects state refers to the characteristics that currently define the object Encapsulation: when something is wrapped up inside a protective covering? Means that an objects data are protected by being hidden inside the object. Benefits of OOP- why its preferred over procedural programming They have a more natural organization Makes it easier to develop and maintain large programs Class: a description of all the objects it defines?an abstraction?a concept apart from any particular instances One class can have any number of objects associated with it The description consists of a list of variables + a list of methods Can define two types of variables: class variables and instance variables Can define two types of methods: class methods and instance methods 6.3: First OOP Class Start the implementation process by describing the solution pictorially with a UML class diagram A UML class diagram is a diagrammatic technique for describing classes, objects, and the relationships between them ( a standard in the software industry) Instance variables must be declared outside all methods and its good practice to declare them all at the beginning of the class definition The primary difference between instance variable declarations and variable declarations from the past is the private access modifier Private can only be accessed from within the members class Instance variables are almost always declared with the private access modifier because you almost always wan an objects data to be hidden (for local tasks) Making it private gives you control over how its value can be changed Argument: a method calls parenthetical values ?this. ? is how you tell the java compiler that the variable you are referring to is a instance variable The instance variable and the parameter can have the same name (instance uses the this.) 6.4: Driver Class Driver: a common computer tem that applies to a piece of software that runs or ?drives? something else? a driver class is a class that is in charge of running another class. Can create objects and manipulate them Normally consists entirely of a main method and nothing else ... calls upon the driven class to create and manipulate objects The driven classes are typically longer than driver classes Driver classes are in separate files and must be public Reference variables: contain a value that references and object and holds the address of where an object is stored in memory. Aka object You must always declare a variable before you use it Instantiation and assigning a value to a reference variable Use the new operator Ex. new Mouse() After instantiating an object, youll normally assign it to a reference variable Ex. gus = new Mouse; You can do this all in one statement Ex. Mouse gus = new Mouse(); Calling a method After you instantiate an object and assign its reference to a reference variable, you cal call/invoke an instance method: . (); Ex. gus.setPercentGrowthRate(growthRate); or gus.grow(); or gus.display(); 6.5: Calling Object, this Reference Calling object: whenever and instance method is called, it is associated with a calling object It is to the left of the dot in an instance method call statement Ex. gus.grow(); or stdIn.nextDouble(); The this reference: stands for a calling object but it doesn?t tell you which object that is You have to look at what call that method?may need to look in an above method Provides a standard way to make the distinction and show the relationship at the same time? so it?s no longer necessary to use slightly different names 6.6: Instance Variables Default values for instance variables A default values is the variables value when there is no explicitly assigned initial value?when there?s no initialization Int and long are assigned 0 by default Float and double are assigned 0.0 by default Boolean are assigned false by default Reference type variables are assigned null by default Instance variable persistence: refers to how long a variables values survives before it?s wiped out Instance variables persist for the duration of a particular object If an object makes tow method calls, the second called method does not reset the calling objects instance variable to their initialized values 6.7: Tracing an OOP Program You need to keep track of which class and which method you?re in and which object called that method 6.8: UML Class Diagrams (SKIM) 6.9: Local Variables Local variable: a variable that declared and used locally inside a method (different from an instance variable which is declared at the top of a class, outside all methods) Scope- a local variable can be used only from the point at which the variable is declared to the end of the variables block A variable block is established by the closest pair of braces that enclose the variables declaration Most of the time you should declare a methods local variable at the top of the methods body (then the scope is the whole body of the method) 6.10: The return Statement If the method header has a void modifier the method doesn?t return any value Returning a value In some cases allows you to pass a value from the method back to the place from which the method was called The type of value you want returned must match the type specified in the method heading Empty return statement Looks like this: return; Only allowed with a void return type?terminates the current method and causes control to be passed back to the calling module at the point that immediately follows the method call It would be illegal to have an empty return stmt and a non-empty return stmt in the same method because they have different return types in the method header Return stmt within a loop You should use restraint when considering the use of a return stmt inside a loop 6.11: Argument Passing When a method is called, the JVM effectively assigns the value of each argument in the calling stmt to the corresponding parameter in the called method Within a method, parameters are treated like local variables Pass-By-Value Java uses this for its argument passing scheme?it passes a copy of the arguments value to the parameter? changing the copy doesn?t change the original Same name versus different names for argument- parameter pairs Most of the time you?ll want to use the same name?but using different names is legal 6.12: Specialized Methods?Accessors, Mutators, Boolean Methods Accessor methods Accessor: a method that retrieves part of an objects stored data?typically primitive data Ex. getAge and getWeight methods Should be name with a ?get? prefix?often called get methods A method should be written such that it accomplishes one thing that its name implies Mutator method Mutator: a method that changes or mutates an objects stat by changing some or all of that objects stored data?typically private data Ex. setPercentGrowthRate Should be named with a ?set? prefix?often called set methods Boolean methods: checks to see whether some condition is true or false Must always specify a Boolean return type A Boolean method name should normally start with ?is? Ex. isAdolescent() 6.13: Problem Solving with Simulation (SKIM) CHAPTER SIX SUMMARY: An objevt is a group of related data which identifies the current condition or state of the object plus the methods that describe the behavior of that object Objects are instances of the classes which define them. A class definition specifies the instance variables and object of that class contained, and it defines the method an object of the class may call. Each object contains its own copy of the instance variables its class defines, and a given instance variable generally has different values in different objects Use he private access modifier to specify that a particular variable is encapsulated or hidden. Use the public access modifier to make method accessible to the outside world To meake a class as general as possible, drive it from a main method in a separate driver class. In the drivers main method, declare a reference variable of the drive class type. Then, use java's keyword new to instantiate an object of the driven class, and initialize the reference variable with the object reference returned by new. Use javas keyword this to refer to the calling object from within one of that objects methods. Use this ro distinguish an instance variable from a same-named parameter or local variable. When you trace an object-oriented program, you need to keep track of which class tour in, which method you?re in, which object called that method, parameter and local variable names, and the name of all instance variables in each object. A UML class diagram has separate boxes for the class name, a description of the class?s variables, and heading for the class?s methods. Use a ?+? prefix for public and a ?-? prefix form private. Specify variable and method return types and non-default initial values. Instance variable default values are zero for numbers, false for Boolean values, and null for reference values. Instance variable values persist for the life of their object. Local variable default values are undefined garbage. Local variables and parameters persist for as long as their method is being executed, and after that, their values are undefined. Unless a methods return type is void, every path thorough the method must end with a statement that returns a values of the methods type. A methods parameter must have the same types as the method calls argument. What the method gets is a copy of what is in the calling program?s value. Use setX and getX method to modify and retrieve private instance variable values. Include filtering in setX methods to protect your program from bad input. Use Boolean isX methods to return true or false depending on the value of some condition. Optionally improve simulation speed and accuracy by computing the next increment with values determined half way between that increments starting and ending points.
Want to see the other 4 page(s) in CS complete ch 6 Notes?JOIN TODAY FOR FREE!