Chapter 1 - Introduction
Chapter 1 - Introduction
Chapter 1 - Introduction
Introduction
Objectives
Provide background in data structures (arrangements of data
in memory) and algorithms (methods for manipulating this
data)
Why is this important? The answer is there is far more to
programming than finding a solution that will work:
Execution time
Memory consumption
So by the end of the course, you should be more equipped to
not just develop an accurate solution to a problem, but the
best solution possible.
1
9/30/2018
Definition of an Algorithm
An algorithm provides a set of instructions for manipulating
data in structures.
Questions to ponder:
What’s an example of an algorithm?
2
9/30/2018
Sort
Search
Stack
Vector
3
9/30/2018
Example applications:
Grocery store lines
Traffic (Queues are actually used when determining timing of
traffic lights!! How? Let’s think about it)
4
9/30/2018
Idea of Objects
A programming unit which has associated:
Variables (data), and
Methods (functions to manipulate this data).
How does this address the two problems on the previous
slide?
Real World Modeling
Organization
Inheritance
Creation of one class, called the base class
Creation of another class, called the derived class
Has all features of the base, plus some additional features.
Example:
A base class Animal may have associated methods eat() and
run() and variable name, and a derived class Dog can inherit
from Animal, gaining these methods plus a new method
bark().
If name is private, does Dog have the attribute?
How do we enforce Dog to also have attribute name?
10
5
9/30/2018
Polymorphism
Idea: Treat objects of different classes in the same way.
What’s the requirement?
11
12
6
9/30/2018
Java Assignments
What must be noted about the following code snippet:
13
Code like this would leak memory in C++, but does not in Java
because of the garbage collector:
while (true) {
Integer tmp = new Integer();
…
}
14
7
9/30/2018
15
== vs. equals()
carPart cp1 = new carPart(“fender”);
carPart cp2 = cp1;
// What’s the difference between this:
if (cp1 == cp2)
System.out.println(“Same”);
// And this:
if (cp1. equals(cp2)
System.out.println(“Same”);
Does “Same” print twice, once, or not at all?
16
8
9/30/2018
17 Source: roseindia.net
Screen Output
System.out is an output stream which corresponds to
standard output, which is the screen:
18
9
9/30/2018
Keyboard Input
Package: java.io
Read a string: InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
10