DOC PREVIEW
LETU COSC 2103 - More About Classes: Instance Methods

This preview shows page 1-2-3-4-27-28-29-30-56-57-58-59 out of 59 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 59 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

More About Classes: Instance MethodsObjectivesChapter ObjectivesClassesIntroductory Example: Modeling TemperaturesOperationsAlgorithmCoding6.2 Designing a ClassClass DeclarationExternal and Internal PerspectivesTemperature BehaviorAdditional Behaviors DesiredTemperature AttributesImplementing Class AttributesEncapsulationInformation HidingClass InvariantsSlide 196.4 Implementing Static OperationsInstance Methods CategoriesTemperature Output: a Convert-to-String MethodConstructor MethodsDefault Value ConstructorExplicit-Value ConstructorsMethod Names and OverloadingUtility MethodsA Utility Method: fatal()Static vs. Instance MethodsSlide 30Class Design PointersAccessor MethodsMutator MethodsManaging the InputConversion MethodsRaising/Lowering a TemperatureComparing Temperature ValuesAlternate Comparison StrategyReference-type DeclarationHandlesReference Type CopyingSlide 42Slide 43Class OrganizationClass Interface6.5 Graphical/Internet Java: Raise the FlagInheritancePaintingMethods in DrawingPain()Graphics Class MethodsDutch Flag GUI ApplicationDutch Flag AppletPart of the Picture: Artificial IntelligenceIntelligenceAI TopicsAI Programming TechniquesSlide 57Example: JackDice GameStrategies for JackDiceMore About Classes:Instance MethodsObjectives•Look at how to build classes as types•Study instance methods–contrast with static (class) methods•Place in context of real world object (temperature)•Implement attribute (instance) variables•Explain importance of encapsulation and information hidingChapter Objectives•Build a complete class to model temperatures•Describe, give examples for–constructors, accessor methods, mutator methods, converter methods, utility methods•Investigate graphics programming•Look at artificial intelligence topicsClasses•Generally used to describe a group or category of objects–attributes in common•Java class used as a repository for static methods used by other classes•Now we will create a class that serves as a type –from which objects are created–contains instance methodsIntroductory Example:Modeling Temperatures•Problem–Temperature Conversion–Fahrenheit, Celsius, Kelvin•Preliminary Analysis–Attributes of temperature•number of degrees•scale–We seek a type which will …•hold all attributes and …•provide methods for manipulating those attributesOperations1. Display a string to System.out2. Read a Temperature from System.in3. Determine Fahrenheit equivalent of Temperature4. Determine Celsius equivalent5. Determine Kelvin equivalent6. Display a Temperature on System.outAlgorithm1. Declare required variables temp2. Send System.out message to display prompt3. Send temp a message, ask it to read value from System.in4. Send System.out a message to displayFahrenheit, Celsius, Kelvin equivalentsCoding•Write source code•Assumes existence of the class Temperature•Note calls to Temperature methods.read(theKeyboard).inFahrenheit().inCelsius().inKelvin6.2 Designing a Class•For a class, we must identify –Behavior, operations applied to class objects–Attributes, data stored to characterize a class object•These are "wrapped together" in a class declarationClass Declaration•Syntax:class className{ Method definitions Field Declarations}•Method definitions are as described in earlier chapters•Field declarations are of variables and constantsExternal and Internal Perspectives•External Perspective–observer from outside the program–views internal details•Internal Perspective–object carries within itself ability to perform its operations–object autonomyTemperature Behavior•Define myself implicitly–initialize degrees, scale with default values•Read value from a Keyboard object and store it within me•Compute Fahrenheit, Celsius, Kelvin temperature equivalent to me•Display my degrees and scale using a Screen objectFrom an internal perspectiveAdditional Behaviors Desired•Define myself explicitly with degrees, scale•Identify my number of degrees•Identify my scale•Increase, decrease my degrees by a specified number•Compare myself to another Temperature object•Assign another Temperature value to meTemperature Attributes•Review the operations•Note information each requires•Temperature has two attributes1. my degrees2. my scaleImplementing Class Attributes•Stand alone class declared in a separate file: Temperature.java•Specify variables to hold the attributes double myDegrees; char myScale;–called the instance variables, data members, or fieldsEncapsulation•Wrap the attribute objects in a class declarationclass Temperature{ double myDegrees; char myScale;}•Use the class declaration as a type for declare actual objectsTemperature todaysTemp = new Temperature();The class Temperature encapsulates the double and the char valuesThe class Temperature encapsulates the double and the char valuesInformation Hiding•Attribute variables can be accessed directlytodaysTemp.myScale = 'Q'; // ???•We wish to ensure valid values only•Solution is to "hide" the information to direct outside accessclass Temperature{ private double myDegrees; private char myScale;}It is good programming practice to hide all attribute variables of a class by specifying them as private.It is good programming practice to hide all attribute variables of a class by specifying them as private.Class Invariants•Important to identify restrictions on values of attributes–minimum, maximum temp–myScale limited to F, C, or K•Specify with boolean statements in comments . . . private char myScale; // 'F', 'C', or 'K'Class Invariants•Helpful to specify static (class) constantsclass Temperature{public final static double ABS_ZERO_F = -459.67, ABS_ZERO_C = -273.15, ABS_ZERO_K = 0.0;…All objects of type Temperature share a single instance of these valuesAll objects of type Temperature share a single instance of these values6.4 Implementing Static Operations•Use instance methods•Contrast:Static (Class) MethodsInstance (Object) Methods•Declared with keyword static•Shared by all objects of class•Invoke by sending message to the class•No static modifier used•Each class has its own copy•Invoked by sending message to class objectInstance Methods Categories•Constructors–initialize attribute variables•Accessors–retrieve (but not change) attribute variables•Mutators–change attribute variable values•Converters–provide representation of an object in a different


View Full Document

LETU COSC 2103 - More About Classes: Instance Methods

Documents in this Course
Arrays

Arrays

16 pages

Templates

Templates

17 pages

Methods

Methods

22 pages

Methods

Methods

22 pages

Arrays

Arrays

11 pages

Load more
Download More About Classes: Instance Methods
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view More About Classes: Instance Methods and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view More About Classes: Instance Methods 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?