DOC PREVIEW
UW-Madison CS 302 - Using Objects

This preview shows page 1-2-3-4-5-39-40-41-42-43-44-78-79-80-81-82 out of 82 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 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 82 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 82 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 82 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 82 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 82 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 82 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 82 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 82 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 82 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 82 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 82 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 82 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 82 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 82 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 82 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 2 – Using ObjectsOverviewBasic Computer ProgramTypes and VariablesVariablesVariable DeclarationSlide 7Data ValuesData TypesIdentifiersNaming StandardsNaming ConventionsMemory LocationDeclaration ExamplesAssignmentSlide 16IMPORTANTSlide 18InitializationSlide 20Slide 21Slide 22Uninitialized VariablesQuestionsNumerical Data TypesIntegersSlide 27RealsNumbersWhy have integers?ArithmeticIncorrect InitializationsStringsString MethodsObjects, Classes, and MethodsExampleSlide 37MethodsObjectsClassReference Data (Objects)Slide 42Slide 43Constructing ObjectsRectangleCreating ObjectsCreating objectsSlide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 54Method ParametersSlide 56Return ValuesPassing Return ValuesSlide 59More complex method callsSlide 61Method definitionsAccessors and MutatorsExamplesObject ReferencesSlide 66Primitive in memoryReference in memorySlide 69Why is this important?Slide 71Writing ProgramsImporting PackagesSlide 74Slide 75Advantages of PackagesAdvantages of Importjava.langSlide 79OutputAPI DocumentationAPIChapter 2 – Using ObjectsChapter 2 – Using ObjectsOverviewOverviewThis chapter touches briefly on topics we This chapter touches briefly on topics we will discuss as the course progresseswill discuss as the course progressesObjects – main point of courseObjects – main point of courseFirst, learn about objectsFirst, learn about objectsHow to use objects we already know aboutHow to use objects we already know aboutCreate objectsCreate objectsDesign classes to complete a task requirementDesign classes to complete a task requirementBasic Computer ProgramBasic Computer ProgramWhat is the basis of computing?What is the basis of computing?MathematicsMathematicsProgram has three generic stepsProgram has three generic steps1.1.Get input dataGet input data2.2.Perform computation on that dataPerform computation on that data3.3.Output the results of that computationOutput the results of that computationFor example, perhaps we want to be able to For example, perhaps we want to be able to compute the sum of two terms compute the sum of two terms xx and and yy. . How do we do that?How do we do that?Types and VariablesTypes and VariablesEvery value (piece of data) has a Every value (piece of data) has a typetypeA type describes what category a certain A type describes what category a certain piece of information falls in (number, string, piece of information falls in (number, string, bank account, etc)bank account, etc)13 is an integer (13 is an integer (intint in java) in java)““Hello, World” is a Hello, World” is a StringString13.3 is a real (13.3 is a real (doubledouble in Java) in Java)How do we store a value for later use?How do we store a value for later use?Answer: variablesAnswer: variablesVariablesVariablesIn math, a variable represents an unknown In math, a variable represents an unknown value.value. y = 3x + zy = 3x + zIn computer science, a In computer science, a variablevariable is a is a named space (in memory) that stores a named space (in memory) that stores a value. Like mathematics, a variable can value. Like mathematics, a variable can store any value in a given range.store any value in a given range.Variable DeclarationVariable DeclarationtypeName variableNametypeName variableName = = valuevalue;;orortypeName variableNametypeName variableName;;Example:Example:String greeting = "Hello, Bob!";String greeting = "Hello, Bob!";Purpose:Purpose:To define a new variable of a particular type To define a new variable of a particular type and optionally supply an initial valueand optionally supply an initial valueVariablesVariablesVariables have four key ingredients:Variables have four key ingredients:1.1.The The typetype of data stored of data stored2.2.A A namename (also called an (also called an identifieridentifier))3.3.A A memory locationmemory location4.4.A A valuevalueTo declare a variable, you need to decideTo declare a variable, you need to decideWhat type you should use for the variable (ie What type you should use for the variable (ie what kind of data am I going to store)what kind of data am I going to store)What name you should give to the variableWhat name you should give to the variableData ValuesData ValuesExamples of values:Examples of values:3.1415923.1415922727"The Grapes of Wrath""The Grapes of Wrath"Color.ORANGEColor.ORANGEAll of the above are All of the above are literalsliteralsYou cannot store values in variables of the You cannot store values in variables of the wrong type…wrong type…All of these values have an associated typeData TypesData TypesPrimitive: (language defined)Primitive: (language defined)Numeric (Numeric (intint, , doubledouble))Non-numeric (Non-numeric (booleanboolean))Reference: Reference: (programmer defined)(programmer defined)ObjectObjectstores several variables stores several variables that collectively that collectively represent a single entityrepresent a single entityColorblue51green153red255Color myFavColorIdentifiersIdentifiersIdentifier: name of a variable, method, or Identifier: name of a variable, method, or class class Must follow Must follow Naming StandardsNaming Standards – rules for – rules for creating identifierscreating identifiersShould follow Should follow Naming ConventionsNaming Conventions – – make identifiers useful and readablemake identifiers useful and readableNaming StandardsNaming Standards1.1.Use only:Use only:a)a) letterslettersb)b) digitsdigitsc)c) __d)d) $$2.2.Start with a Start with a letterletter3.3.Cannot be a Cannot be a reservedreserved word word4.4.Case sensitiveCase sensitive5.5.No spaces, punctuationNo spaces, punctuationNaming ConventionsNaming ConventionsUseful to follow, make program easier to readUseful to follow, make program easier to readVariables and methods start with lowercase letterVariables and methods start with lowercase lettervariableNamevariableName and and methodName( )methodName( )Class names should start with an uppercase letterClass names should start with an uppercase letterClassNameClassName CONSTANTS_ALL_CAPSCONSTANTS_ALL_CAPSIf two words are joined, the first letter of the second If two words are joined, the first letter of the second word should be capitalized (camel case)word should be capitalized (camel case)Memory LocationMemory LocationWhen you declare a variable, the When you declare a variable, the computer will you a memory


View Full Document

UW-Madison CS 302 - Using Objects

Download Using Objects
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 Using Objects 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 Using Objects 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?