CS 340 Data Structures - Jacksonville University

Download Report

Transcript CS 340 Data Structures - Jacksonville University

CS 340 DATA STRUCTURES
Instructor: Xenia Mountrouidou
Course Objectives
2

At the end of this class you will be able to:
 Make
design decisions on which data structure is best to
use regarding
 performance,
 memory,
and
 implementation efficiency.
 Devise
or use the most efficient algorithm in your
projects.
 Understand algorithmic complexity.
 Think analytically and identify complexity of a
program.
Course Objectives (cont.)
3

At the end of this class you will be able to:
 Apply
object oriented programming principles when
you develop software.
 Use and understand third party code.
 Detect inefficiency of data structures and algorithms of
third party code.
 Develop projects using agile test driven approach
(Junit).
 Employ the Java API.
CS 340
Why do you need CS 340?
4

Scenario:
 You
are a senior developer for e-bay.
 You are working on their e-commerce application
server!
 You drop code, you are a java guru, OO programming
is second nature to you… but you do not understand
data structures and algorithms.
 BIG DEAL! Everything runs perfectly. Until one day…
 You need to use a sorting algorithm to sort all potential
sellers of a product based in price or ranking.
CS 340
Why do you need CS 340?
5

Scenario (cont.):
 On
every click for a product search, your sorting
algorithm will be used.
 You choose bubble sort. After all, it has a cool name!
 Let’s see what happens:
 http://www.cs.bu.edu/teaching/alg/sort/demo
Software is not just coding…
It is design, performance, memory consumption
It is an art, a riddle to be solved with every project
CS 340
Lectures
6


We meet at 15:00-16:45, every Tues/Thurs, at
Merritt Penticoff Science Bld, Room 130
Attendance will have a part in your grade.
 Attendance



means: active participation!
Check the schedule in our webpage
Reading and examples will be posted online.
Check the webpage for news frequently.
CS 340
How to get help
7




Join my office hours!
Join the conversation on Piazza.
Check our website frequently.
Use the textbook:
 “Data
Structures: Abstraction and Design
Using Java”, Second Edition
by Elliot B. Koffman, Paul A. T. Wolfgang

Experiment with code. It’s fun…
CS 340
Grading
8
Midterm exam
Final exam
Homeworks
Programming projects
Class participation
Total
 Homework
online
CS 340
20%
20%
25%
25%
10%
100%
and Programming projects will be posted
Programming Projects
9

They involve
 Design
 Coding
 Testing
 Debugging



Some of these can be done in pairs
Both team members will need to answer detailed
questions about the implementation
Each team member will evaluate his/her team mate
CS 340
Principles of Pair Programming
10
CS 340
Principles of Pair Programming
12

All I Really Need to Know about pair programming I
Learned in Kindergarten
 Share
everything.
 Play fair.
 Don’t hit people.
 Put things back where you found them.
 Clean up your own mess.
 Don’t take things that aren’t yours.
 Say you’re sorry when you hurt somebody.
CS 340
Principles of Pair Programming
13
 Wash
your hands before you eat.
 Flush.
 Warm
cookies and cold milk are good for you.
 Live a balanced life – learn some and think some and
draw and paint and sing and
 Dance and play and work every day some.
 Take a nap every afternoon.
 When you go out into the world, watch out for traffic,
hold hands and stick together.
 Be aware of wonder.
CS 340
Policies
14


Read the collaboration policy carefully.
Late policy:
 1st
day late: 10% off
 10% is reduced by every day the homework is late
CS 340
15
Java… a bit of history
CS 340
Java timeline
16

http://www.youtube.com/watch?v=WAy9mgEYb6o
CS 340
Java Design Goals
17





Simple, object oriented, and familiar
Robust and secure
Architecture neutral and portable
High performance
Interpreted, threaded, and dynamic
CS 340
Java abbreviations
17




JDK: Java Development Kit
JSDK: Java Servlet Development Kit
JVM: Java Virtual Machine
J2EE: Java Platform, Enterprise Edition. A widely
used platform for server programming.
CS 340
18
OOP or… Object Oriented Programming
CS 340
Object-Oriented Programming
19

Object-oriented programming (OOP) is popular
because:
 enables
reuse of previous code
 saves time


If a new class is similar to an existing class, the
existing class can be extended
This is called inheritance
CS 340
Java is object oriented
20

Old programming languages:


code was executed line by line and accessed variables
or records
Java
 objects

that come with their own methods
When coding in Java one is always thinking about
“which object is running this code?”
CS 340
Inheritance
21





Meat is a Food
Meat has all the data
fields and methods
defined by Food
Food is the superclass of
Meat
Meat is a subclass of
Food
Meat may define other
variables and methods
that are not contained in
Food
CS 340
Food
expirationDate()
Meat
percentageOfProtein()
A Superclass and Subclass Example
22


Robot
A robot has a
 manufacturer
 processor
 disk
 parts
 processor
speed
Write the robot
class
CS 340
A Superclass and Subclass Example (cont.)
23


Robot
A robot has a
 manufacturer
 processor
 disk
Robot
 parts
 processor
CS 340
speed
String manufacturer
String processor
int diskSize
int numberOfParts
double processorSpeed
A Superclass and Subclass Example (cont.)
24
Robot
String manufacturer
String processor
int diskSize
int numberOfParts
double processorSpeed
int getDiskSize()
double getProcessorSpeed()
int getParts()
String toString()
CS 340
A Superclass and Subclass Example (cont.)
254


Cylon
A Cylon has all the properties of
Robot,
 manufacturer
 processor
 disk
 parts
 processor

speed
plus,
 vision
(pixels)
 hands (battle speed)
What is a Cylon class?
CS 340
A Superclass and Subclass Example (cont.)
26
Robot
String manufacturer
String processor
int diskSize
int numberOfParts
double processorSpeed
int getDiskSize()
double
getProcessorSpeed()
int getParts()
String toString()
Cylon
double pixels
double battleSpeed
CS 340
A Superclass and Subclass Example (cont.)
27

The constructor of a subclass begins by initializing
the data fields inherited from the superclass(es)
super(man, proc, parts, disk, procSpeed);
which invokes the superclass constructor with the
signature
Robot(String man, String processor, int parts,
int disk, double procSpeed)
CS 340
A Superclass and Subclass Example (cont.)
28
/** Class that represents a robot */
public class Robot {
private String manufacturer;
private String processor;
private int numParts;
private int diskSize;
private double processorSpeed;
public Robot(String man, String processor, int numParts, int
disk, double procSpeed) { //constructor
manufactuer = man;
this.processor = processor;
this.numParts = numParts;
diskSize = disk;
processorSpeed = procSpeed;
}
}
Write the
methods’ code
A Superclass and Subclass Example (cont.)
29
public double getProcessorSpeed() { return processorSpeed; }
public int getDiskSize() { return diskSize; }
public int getParts() { return numParts; }
public String toString() {
String result = "Manufacturer: " + manufacturer +
"\nCPU: " + processor +
"\nBody parts: " + numParts +
"\nDisk: " + diskSize + " gigabytes" +
"\nProcessor speed: " + processorSpeed +
" gigahertz";
return result;
}
}
CS 340
A Superclass and Subclass Example (cont.)
30
public class Cylon extends Robot
{
private double pixels;
private double battleSpeed;
public Cylon(String man, String processor, int parts, int
disk,
double procSpeed, double pix, double bSpeed) {
super(man, proc, parts, disk, procSpeed);
pixels = pix;
battleSpeed = bSpeed;
}
}
CS 340
Protected Visibility for Superclass Data
Fields
31



Variables with private visibility (defined by the
keyword private) cannot be accessed by a
subclass
Variables with protected visibility (defined by the
keyword protected) are accessible by any
subclass or any class in the same package
By default variables are public, i.e., they can
be accessed by any package
CS 340
Is-a versus Has-a Relationships
32


In an is-a or inheritance relationship, one class is a
subclass of the other class
In a has-a or aggregation relationship, one class has
the other class as an attribute
CS 340
Is-a versus Has-a Relationships (cont.)
33
public class Robot {
private Memory mem;
...
}
public class Memory {
private int size;
private int speed;
private String kind;
...
}
A Robot has only one
Memory
But a Robot is not a
Memory (i.e. not an is-a
relationship)
If a Cylon extends
Robot,then the Cylon isa Robot
CS 340