Docsity

Why ADTS - Lecture Slides - Data Structures | CS 230, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
CS230: Data Structures - Spring 2009
http://cs.wellesley .edu/~cs230
P. Takis Metaxas
Objectives of CS 230
Teach main ideas o f programming
Data abstraction
Modularity
Performance analysis
Basic abstract data types (ADTs)
Make you a more c ompetent programmer…
designer, tester, analyzer, debugger
Help you develop a project worth showing off
Have fun in the process
Why ADTs?
Allow you to w rite
complex programs easier
To keep mental track of
complex data interaction
To reuse code (yes!)
To improve code
performance
Allows modularity of large
projects
Easier to understand
large chunks of code
Easier to collaborate with
large teams
To combat Sergea nt
Spaghetti Code
Some basic ADTs:
Array
Linked list
Stack
Queue
Table
Priority queue
Not so basic:
Tree
Set
Graph
4
1.1 – Do you speak Java?
//********************************************************************
// Lincoln.java Java Foundations
//
// Demonstrates the basic structure of a Java application.
//********************************************************************
public class Lincoln
{
//-----------------------------------------------------------------
// Prints a presidential quote.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.println ("A quote by Abraham Lincoln:");
System.out.println ("Whatever you are, be a good one.");
}
}
Java “portability”
Java Virtual Machine
(Interpreter)
Java source
code Java Compiler Byte Code
Binary code
Unix
Mac OS
Windows
Compiler
C source
code
Unix Compiler
Mac OS Compiler
Windows Compiler
6
1.1 – Java Reserved Words
abstract
assert
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum
extends
false
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
true
try
void
volatile
while
pf3
pf4
pf5

Partial preview of the text

Download Why ADTS - Lecture Slides - Data Structures | CS 230 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS 230 : Data Structures - Spring 2009

http://cs.wellesley.edu/~cs 230

P. Takis Metaxas

Objectives of CS 230

 Teach main ideas of programming

 Data abstraction

 Modularity

 Performance analysis

 Basic abstract data types (ADTs)

 Make you a more competent programmer…

 designer, tester, analyzer, debugger

 Help you develop a project worth showing off

 Have fun in the process

Why ADTs?

 Allow you to write

complex programs easier

 To keep mental track of

complex data interaction

 To reuse code (yes!)

 To improve code

performance

 Allows modularity of large

projects

 Easier to understand

large chunks of code

 large teamsEasier to collaborate with

 To combat Sergeant

Spaghetti Code

 Some basic ADTs:

 Array

 Linked list

 Stack

 Queue

 Table

 Priority queue

 Not so basic:

 Tree

 Set

 Graph 4

1.1 – Do you speak Java?

//******************************************************************** // Lincoln.java Java Foundations // // Demonstrates the basic structure of a Java application. //******************************************************************** public { class Lincoln //----------------------------------------------------------------- // Prints a presidential quote. //----------------------------------------------------------------- public static void main (String[] args) { System.out.println ("A quote by Abraham Lincoln:"); }^ System.out.println^ ("Whatever^ you^ are,^ be^ a^ good^ one."); }

Java “portability”

Java source^ Java Virtual Machine (Interpreter)

code Java Compiler^ Byte Code

Binary code

Unix

Mac OS

Windows

Compiler

C source

code

Unix Compiler

Mac OS Compiler

Windows Compiler

1.1 – Java Reserved Words

abstract

assert

boolean

break

byte

case

catch

char

class

const

continue

default

do

double

else

enum

extends

false

final

finally

float

for

goto

if

implements

import

instanceof

int

interface

long

native

new

null

package

private

protected

public

return

short

static

strictfp

super

switch

synchronized

this

throw

throws

transient

true

try

void

volatile

while

1.5 – Object-Oriented Programming

 Java is an object-oriented programming language

 As the term implies,

an object is a fundamental entity in a Java program

 Objects can be used effectively to represent real-world

entities

 For instance,

an object might represent a particular photo in a photo

management program (e.g., iPhoto)

 Each photo object handles the processing and data

management related to that photograph

1.5 – Classes

 An object is defined by a class

 A class is the blueprint of an object

 The class uses methods to define the behaviors of the

object

 The class that contains the main method of a Java

program represents the entire program

 A class represents a concept ,

and an object represents the embodiment of that

concept

 Multiple objects can be created from the same class

1.5 – Objects and Classes

Bank Account

A class

(the concept)

John’s Bank Account

Balance: $5,

An object

(the realization)

Jason’s Bank Account

Balance: $1,245,

Mary’s Bank Account

Balance: $16,

Multiple objects

from the same class

1.5 – Inheritance

 One class can be used to derive another via inheritance

 Classes can be organized into hierarchies

Bank Account

Account

Charge

Account

Savings

Account

Checking

Account

2.1 – Addition.java

//******************************************************************** // Addition.java Java Foundations // // Demonstrates the difference between the addition and string // //******************************************************************** concatenation operators. public { class Addition //----------------------------------------------------------------- // Concatenates and adds two numbers and prints the results. //----------------------------------------------------------------- public static void main (String[] args) { System.out.println ("24 and 45 concatenated: " + 24 + 45); }^ System.out.println^ ("24^ and^45 added:^ "^ +^ (24^ +^ 45)); }

2.1 – Character Strings

 A string of characters can be represented

as a string literal

by putting double quotes around the text

 "Hello, World! "

"123 Main Street"

"X"

 Every character string is an object in Java,

defined by the String class

 Every string literal represents a String object

 A string literal

cannot be broken across two lines

2.3 – Characters

 A char variable stores a single character

 Character literals are delimited by single quotes:

'a' 'X' '7' '$' ',' '\n'

 Example declarations

char topGrade = 'A';

char terminator = ';', separator = ' ';

 Note the distinction between a character variable, which holds only one character,

and a String object,

which can hold multiple characters

2.3 – Character Sets

 A character set is an ordered list of characters,

with each character corresponding to a unique number

 A char variable in Java can store any character

from the Unicode character set

 The Unicode character set uses 16 bits per character,

allowing for 65,536 unique characters

 It is an international character set, containing symbols

and characters from many world languages

2.3 – Boolean

 A boolean value represents a true or false

condition

 The reserved words true and false are

the only valid values for a boolean type

boolean done = false;

 A boolean variable can also be used to

represent any two states, such as a light bulb

being on or off

2.4 – Division and Remainder

 If both operands to the division operator (/) are

integers, the result is an integer (the fractional part is

discarded)

The remainder operator (%) returns the remainder

after dividing the second operand into the first

14 / 3 equals

8 / 12 equals

14 % 3 equals

8 % 12 equals

2.4 – Operator Precedence

 Operators can be combined into complex expressions

result = total + count / max - offset;

 Operators have a well-defined precedence which determines

the order in which they are evaluated

 Multiplication , division , and remainder are evaluated

prior to addition, subtraction, and string concatenation

 Arithmetic operators with the same precedence

are evaluated from left to right ,

but parentheses can be used to force the evaluation order

2.4 – Assignment Precedence

 The assignment operator has a lower precedence than

the arithmetic operators

 The right and left hand sides of an assignment

statement can contain the same variable

First, one is added to the original value of count

Then the result is stored back into count

(overwriting the original value)

count = count + 1;

2.4 – Increment and Decrement

 The increment and decrement operators can be applied

in postfix form

count++

 or prefix form

++count

 When used as part of a larger expression,

the two forms can have different effects

 Because of their subtleties, the increment and

decrement operators should be used with care

2.4 – Assignment Operators

 Java provides assignment operators

to simplify the assignment process

Operator

Example

x += y

x -= y

x *= y

x /= y

x %= y

Equivalent To

x = x + y

x = x - y

x = x * y

x = x / y

x = x % y

2.5 – Data Conversion

 Sometimes it is convenient to convert data from one type to another

 For example, we may want to treat an integer as a floating point value

 These conversions do not change the type of a variable or the

value that's stored in it they only convert a value as part of a computation

 Conversions must be handled carefully to avoid losing information

 Widening conversions small data type to a larger one (such as a are safest because they tend to go from a short to an int)

 Narrowing conversions can lose information because they tend to

go from a large data type to a smaller one.

 In Java, data conversions can occur in three ways  assignment conversion

  promotioncasting

2.5 – Assignment Conversion

 Assignment conversion occurs when a value of one type

is assigned to a variable of another

 float money;

int dollars;

 The following converts the value in dollars to a float

money = dollars

 Only widening conversions can happen via assignment

 Note that the value or type of dollars did not change

2.5 – Promotion

 Promotion happens automatically

when operators in expressions convert their

operands

 float sum;

int count;

 The value of count is converted

to a floating point value

to perform the following calculation

result = sum / count;

2.5 – Casting

 Casting is the most powerful,

and dangerous, technique for conversion

 Both widening and narrowing conversions

can be accomplished by explicitly casting a value

 To cast, put type in parentheses

in front of the value being converted

 For example, if total and count are integers,

but we want a floating point result when dividing them,

we can cast total

result = (float) total / count;