DOC PREVIEW
UW-Madison COMPSCI 302 - Chapter 2 – 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 DeclarationVariablesData ValuesData TypesIdentifiersNaming StandardsNaming ConventionsMemory LocationDeclaration ExamplesAssignmentAssignmentIMPORTANTAssignmentInitializationInitializationInitializationAssignmentUninitialized VariablesQuestionsNumerical Data TypesIntegersIntegersRealsNumbersWhy have integers?ArithmeticIncorrect InitializationsStringsString MethodsObjects, Classes, and MethodsExampleMethodsObjectsClassReference Data (Objects)ExampleExampleConstructing ObjectsRectangleCreating ObjectsCreating objectsCreating ObjectsCreating ObjectsCreating ObjectsCreating ObjectsCreating ObjectsMethodsMethod ParametersReturn ValuesPassing Return ValuesMore complex method callsMethod definitionsAccessors and MutatorsExamplesObject ReferencesObject ReferencesPrimitive in memoryReference in memoryWhy is this important?Writing ProgramsImporting PackagesImporting PackagesImporting PackagesAdvantages of PackagesAdvantages of Importjava.langOutputAPI DocumentationAPIChapter 2 – Using ObjectsOverview• This chapter touches briefly on topics we will discuss as the course progresses• Objects – main point of course– First, learn about objects– How to use objects we already know about– Create objects– Design classes to complete a task requirementBasic Computer Program• What is the basis of computing?– Mathematics• Program has three generic steps1. Get input data2. Perform computation on that data3. Output the results of that computation• For example, perhaps we want to be able to compute the sum of two terms xand y. How do we do that?Types and Variables• Every value (piece of data) has a type– A type describes what category a certain piece of information falls in (number, string, bank account, etc)– 13 is an integer (int in java)– “Hello, World” is a String– 13.3 is a real (double in Java)• How do we store a value for later use?– Answer: variablesVariables• In math, a variable represents an unknown value.y = 3x + z• In computer science, a variable is a named space (in memory) that stores a value. Like mathematics, a variable can store any value in a given range.Variable DeclarationtypeName variableName = value;ortypeName variableName;Example:– String greeting = "Hello, Bob!";Purpose:– To define a new variable of a particular type and optionally supply an initial valueVariables• Variables have four key ingredients:1.The type of data stored2.A name (also called an identifier)3.A memory location4.A value• To declare a variable, you need to decide– What type you should use for the variable (iewhat kind of data am I going to store)– What name you should give to the variableData Values• Examples of values:– 3.141592– 27– "The Grapes of Wrath"– Color.ORANGE• All of the above are literals• You cannot store values in variables of the wrong type…All of these values have an associated typeData Types• Primitive: (language defined)–Numeric (int, double)– Non-numeric (boolean)• Reference: (programmer defined)–Object• stores several variables that collectively represent a single entityColorblue51green153red255Color myFavColorIdentifiers• Identifier: name of a variable, method, or class • Must follow Naming Standards – rules for creating identifiers• Should follow Naming Conventions –make identifiers useful and readableNaming Standards1. Use only:a) lettersb) digitsc) _d) $2. Start with a letter3. Cannot be a reserved word4. Case sensitive5. No spaces, punctuationNaming Conventions• Useful to follow, make program easier to read• Variables and methods start with lowercase letter– variableName and methodName( )• Class names should start with an uppercase letter– ClassName• CONSTANTS_ALL_CAPS• If two words are joined, the first letter of the second word should be capitalized (camel case)Memory Location• When you declare a variable, the computer will you a memory location for storing the value of that variable• When dealing with objects, you need to use new to create space for the object (we’ll cover this later)Declaration Examples• Declaration:<type> <identifiers>;• Examples:– int month, day, year;– String lastName, firstName;– double accountBalance;AssignmentvariableName = value;Example:– luckyNumber = 12; Purpose:– To assign a new value to a previously defined variable.Assignment• Assignment statement– x = 20; a = 3.5; y = 0.12f;• Don’t confuse with mathematical statementx + y = a + b; 4+10 = x; // Not valid• Can initialize value during declaration– int x = 20;IMPORTANT• Before using a variable, you must declare it and initialize itAssignment• = operator is an action to assign (replace) the a value to a variable• Think about this as ←• Not used as a statement about equality• Used to change the value of a variable int luckyNumber = 13; luckyNumber = 12;Initializationint total = 1;int memory locationInitializationint total = 1;totalname (identifier)Initializationint total = 1;valuetotal1Assignmenttotal = 3;total13Uninitialized Variables• The following code generates an error:int luckyNumber;System.out.println(luckyNumber);// ERROR - uninitialized variableQuestions• How do you change the value of the greeting variable to "Hello, Nina!"?•Is 12 = x a valid Java statement?• How about 12 = x; ?Numerical Data Types• There are six numerical data types•Two sets– Integers – byte, short, int, long– Reals/Floating Point – float, double• Range vs. Precision• What’s the tradeoff with higher precision?Integers• byte– 1 byte of information (8 bits)– Range from -128 to 127• short–2 bytes– Range from -32768 to 32767• int–4 bytes– Range from -2147483648 to 2147483647Integers• long–8 bytes• Which one should we use?– Why not always short?– Why not always use long?• Settle on int–Flexible– EfficientReals• double vs. float– double really stands for double float – twice the memory• Floating point representation– Similar to scientific notation• Default to double, more possible valuesNumbers• Do not use commas– 13,000 Æ 13000• Can represent exponents– 1.3 x 10-4Æ 1.3E-4Why have integers?• Integers are a subset of the reals…• Why can’t we just represent all integers as floating-point?–Memory– Speed– RoundingArithmetic• The data types discussed so far do not have methods• But we can do


View Full Document

UW-Madison COMPSCI 302 - Chapter 2 – Using Objects

Download Chapter 2 – 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 Chapter 2 – 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 Chapter 2 – 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?