Getting Organized: Chap. 1
Getting Organized: Chap. 1
Chap. 1
Design
high-level
low-level
Testing
Verification
Delivery
Operation
Maintenance
Spiral
LifeCycle
Model
Agile Methods
Customer involvement across life cycle
Incremental development and delivery
Embrace change
Customer satisfaction is primary goal
Pair Programming
and more
// Constructor
public Date(int newMonth,
int newDay,
int newYear)
{
month = newMonth;
day = newDay;
year = newYear;
}
// Observers
public int getYear()
{
return year;
}
}
Within
Everywhere
Subclasses
in Other
Packages
public
protected
package
private
Objects
Applications
An object-oriented application is a set of objects
working together, by sending each other
messages, to solve a problem.
Identifying classes for the problem is important.
DaysBetween Design
display instructions
prompt for and read in info about the first date
create the date1 object
prompt for and read in info about the second date
create the date2 object
if dates entered are too early
print an error message
else
use the date.lilian method to obtain the
Lilian Day Numbers
compute and print the number of days
between the dates
//---------------------------------------------------------------------// DaysBetween.java
by Dale/Joyce/Weems
Chapter 1
//
// Asks the user to enter two "modern" dates and then reports
// the number of days between the two dates.
//---------------------------------------------------------------------import java.util.Scanner;
public class DaysBetween
{
public static void main(String[] args)
{
Scanner conIn = new Scanner(System.in);
int day, month, year;
System.out.println("Enter two 'modern' dates: month day year");
System.out.println("For example January 12, 1954 would be: 1 12 1954");
System.out.println();
System.out.println("Modern dates occur after " + Date.MINYEAR + ".");
System.out.println();
System.out.println("Enter the first date:");
month = conIn.nextInt();
day = conIn.nextInt();
year = conIn.nextInt();
Date date1 = new Date(month, day, year);
Inheritance
Allows programmers
to create a new class
that is a specialization
of an existing class.
The new class is a
subclass of the
existing class, which in
turn is the superclass
of the new class.
Example of Inheritance
public class IncDate extends Date
{
public IncDate(int newMonth, int newDay, int newYear)
{
super(newMonth, newDay, newYear);
}
public void increment()
// Increments this IncDate to represent the next day.
// For example if this = 6/30/2005 then this becomes 7/1/2005.
{
// increment algorithm goes here
}
}
" +
" +
aDate.increment();
System.out.println("the day after is: " +
aDate.getDay());
Packages
Java lets us group related classes together into
a unit called a package. They
let us organize our files.
can be compiled separately and imported into our
programs.
make it easier for programs to use common class
files.
help us avoid naming conflicts (two classes can have
the same name if they are in different packages).
Using Packages
A Java compilation unit can consist of a file with
the keyword package followed by an identifier indicating the
name of the package:
package someName;
import declarations, to make the contents of other packages
available:
import java.util.Scanner;
one or more declarations of classes; exactly one of these
classes must be public.
The name of the file must match the name of the public
class within the unit.
package gamma;
public class Four {}
class Three {}
gamma/
One.java
Four.java
Impart gamma.*;
Implementation Dependent
Structures
Array
Linked List
Implementation Independent
Structures
Stack
Tree
Queue
Sorted List
Graph
Arrays
References
Memory addresses
Sometimes referred to as links, addresses, or
pointers
null to indicate an absence of reference
Assignment Statements
Be aware of aliases
Comparison Statements
Garbage Management
Garbage The set of currently unreachable
objects
Garbage collection The process of finding all
unreachable objects and deallocating their
storage space
Deallocate To return the storage space for an
object to the pool of free memory so that it can
be reallocated to new objects
Dynamic memory management The
allocation and deallocation of storage space as
needed while an application is executing
Counting Operations
To measure the complexity of an algorithm
we attempt to count the number of basic
operations required to complete the
algorithm
We express it as a function of the size of the
problem.
But
too dependent on
programming language
and counting approach
difficult if
problem/algorithm is
more complicated
A further simplification:
Big-O Notation
The complexity of an algorithm as the number of times a
fundamental operation is performed
As a function of the size of the problem.
log2N Nlog2N N2
N3
2N
16
64
16
16
64
256
4,096
65,536
64
384
4,096
262,144
requires
20 digits
128 7
896
16,384 2,097,152
requires
39 digits
256 8
2,048
65,536 16,777,216
requires
78 digits
Algorithm Sum2
sum = ((n + 1) * n) / 2;
Sum2 is O(1)
Lookup1 is O(N)
Algorithm Lookup2
Search area = entire book
Check middle name in search area
While (unsuccessful)
If middle name > target name
Search area = first half of
search area
Else
Search area = second half of
search area
Check middle name in search area
Lookup2 is O(log2N)