chapter2

chapter2 - Chapter 2 Using Objects Overview This chapter...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 2 – Using Objects
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Overview • 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 requirement
Background image of page 2
Basic Computer Program What is the basis of computing? Mathematics Program has three generic steps 1. Get input data 2. Perform computation on that data 3. Output the results of that computation For example, perhaps we want to be able to compute the sum of two terms x and y . How do we do that?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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: variables
Background image of page 4
Variables • 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.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Variable Declaration typeName variableName = value ; or typeName variableName ; Example: String greeting = "Hello, Bob!"; Purpose: – To define a new variable of a particular type and optionally supply an initial value
Background image of page 6
Variables • Variables have four key ingredients: 1.The type of data stored 2.A name (also called an identifier ) 3.A memory location 4.A value • To declare a variable, you need to decide – What type you should use for the variable (ie what kind of data am I going to store) – What name you should give to the variable
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Data 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 type
Background image of page 8
Data Types • Primitive: (language defined) –Numer ic ( int , double ) – Non-numeric ( boolean ) • Reference: (programmer defined) –Ob ject • stores several variables that collectively represent a single entity Color blue 51 green 153 red 255
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Identifiers • Identifier: name of a variable, method, or class • Must follow Naming Standards – rules for creating identifiers • Should follow Naming Conventions make identifiers useful and readable
Background image of page 10
Naming Standards 1. Use only: a) letters b) digits c) _ d) $ 2. Start with a letter 3. Cannot be a reserved word 4. Case sensitive 5. No spaces, punctuation
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Naming 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)
Background image of page 12
Memory Location • When you declare a variable, the
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/08/2008 for the course CS 302 taught by Professor Willbenton during the Spring '07 term at Wisconsin.

Page1 / 82

chapter2 - Chapter 2 Using Objects Overview This chapter...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online