chapter2

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

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

Chapter 2 – Using Objects

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

View Full Document
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
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?

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

View Full Document
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
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.

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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)
Memory Location • When you declare a variable, the

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

View Full Document
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
Ask a homework question - tutors are online