Title: Nelson Series Talk
1Security, Liberties and Trade-offs in the War on
Terrorism
Nelson Series Talk
Since 9/11, we have enacted the Patriot Act,
tighter screening at airports, a proposed
national I.D. card system, a color-coded national
alert system, irradiated mail, and a Department
of Homeland Security, but do all of these things
really make us any less vulnerable to another
terrorist attack? Security expert Bruce Schneier
evaluates the systems that we have in place
post-9/11, revealing which of them actually work
and which ones are simply "security theater."
Learn why most security measures don't work and
never will, why bad security is worse than none
at all, and why strong security means learning
how to fail well. Most of all, learn how you can
take charge of your own security - personal,
family, corporate, and national.
Wed, 11/10 700 pm
http//www.counterpane.com/ http//www.schneier.co
m/
Bruce Schneier is an internationally renowned
security expert. He is the author of eight
books--including the best sellers "Beyond Fear
Thinking Sensibly about Security in an Uncertain
World," "Secrets and Lies," and "Applied
Cryptography"--as well as the Blowfish and
Twofish encryption algorithms. His influential
newsletter, Crypto-Gram, is read by over 100,000
people.
Bruce Schneier
2Week 10 in CS 5
Midterm 2 next Friday, 11/12
Wolfgang Puck not an object-oriented chef!
Object-oriented programming
Were breaking out of the CS5App class
Lab A-L
class CS5App public static void
main(String args)
due Sunday, 11/7 at midnight
M/T sections
due Monday, 11/8 at midnight
W/Th sections
Hw10 Pr1 Connect Four
Hw10 Pr2 Virtual Art!
PAIR
Caution This is very new stuff Come to
recitation (Fri _at_ 8) for help!
3Gaussian Elimination
public static void solve(double A) for
(int c0 cltA0.length-1 c) // columns
multRow( A, c, 1/Acc ) //
diag 1.0 for (int r0 rltA.length r)
// rows if (c ! r) //
off-diagonal elements become 0.0
addMxRaToRb( A, -Arc, c, r )
dest row
multiplier
dest row
source row
multiplier
4Arrays the good and bad
lots of computer work for little programmer work!
you have to use the arrays naming convention
Arc
-
only one built-in capability length
Not everything is a bunch of identical boxes!
int
int
int
int
A
A2
A0
A1
Classes and Objects take care of all 3
drawbacks...
5Classes Objects
An object-oriented programming language allows
you to build your own customized types of
variables.
(1) A class is a type of variable.
(2) An object is one such variable.
6But Why ?
create-your-own
write once, take anywhere
worry once, use anywhere
ordinary data structures
7Javas Library of Classes
details on the data and methods that each class
offers
3000 classes available
java.sun.com/j2se/1.4.2/docs/api/index.html
8Objects
An object is a data structure (like an array),
except
(1) Its data elements need not be the same
type. (2) Its data elements have names chosen by
the programmer. (3) Its data can be protected
from unauthorized changes! (4) An object can have
behaviors built-in by the programmer.
int
double
hours
npeople
String
Course c
name
String
String
String
clist
void addStudent(String s)
int numStudents()
Names!
9Youve done all this before...
String s Im an object! String s2 Im
another object!
if (s.equals(Im an object!)) s2 Ask
me to do something! int L
s2.length() char c s2.charAt(22)
the dot indicates selection or containment
the equals method is a built-in capability of
String objects
0 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20 21 22
nonstatic methods are called by an object
static methods are called by a class, e.g.
Math.sqrt(d)
another nonstatic method
10Classes v Objects some examples
(1) A class is a type of variable.
(2) An object is such a variable.
the classes are in blue
the objects are in green
String s Im an object!
Strings are used so much new is not required
Course c new Course(cs5)
Course c2 new Course(chem)
Board b new Board(6,7)
special constructor methods used with new
Date d new Date(11,13,2004)
Date d2 new Date(11,25,2004)
11Coding classes and objects in main
int
double
hours
npeople
data
String
name
Course c
String
String
String
clist
Course(String name)
constructor
void addStudent(String s)
methods
int numEnrolled()
public static void main(String args)
Course c new Course(cs5) // construct
c c.addStudent(Michael) // add a
student H.pl(c.numEnrolled()) //
print enrollment 1
12Coding classes and objects in Course
class Course private String name private
int npeople private double hours private
String clist public Course(String n)
this.name n this.clist new String42
this.npeople 0 this.hours 3.0
public void addStudent(String s)
this.clistnpeople s public int
numEnrolled() return this.npeople
data members
constructor
methods
13Coding classes and objects in Course
data protection !
class Course private String name private
int npeople private double hours private
String clist public Course(String n)
this.name n this.clist new String42
this.npeople 0 this.hours 3.0
public void addStudent(String s)
this.clistnpeople s public int
numEnrolled() return this.npeople
data members
constructor
the object being constructed
methods
refers to the object calling the method
14Two (or more) objects of the same class
Course c
Course c2
public static void main(String args)
Course c new Course(cs5) // construct
c c.addStudent(Michael) // add a
student to c Course c2 new
Course(chem) // construct c2
c2.addStudent(Marie) // add a
student to c2
15Coding classes and objects in Course
class Course private String name private
int npeople private double hours private
String clist public Course(String n)
this.name n this.clist new String42
this.npeople 0 this.hours 3.0
public void addStudent(String s)
this.clistnpeople s public int
numEnrolled() return this.npeople
data members
constructor
methods
this is sort of a pain
16this is optional !
but it is always there
class Course private String name private
int npeople private double hours private
String clist public Course(String n)
name n clist new String42 npeople
0 hours 3.0 public void
addStudent(String s) clistnpeople s
public int numEnrolled() return
npeople
data members
constructor
methods
17Connect Four
For your convenience, the creators of Javas
library have included a Board class that can
represent any size Connect Four board... !
0
0
X X XO XOOOX
O --------------- 0 1 2 3 4 5 6
1
1
2
2
3
3
4
4
5
5
0 1 2 3 4 5 6
18Connect Four class Board , object b
This is true for sufficiently broad definitions
of the creators of Javas library ...
char
char
char
char
char
nrows
data
Board b
char
char
char
char
char
char
char
char
void addMove(int c, char player)
boolean allowsMove(int c)
19Board
class Board private int nrows private
int ncols private char data public
Board(int R, int C) this.nrows
R this.ncols C this.data
new charnrowsncols for (int r0
rltthis.nrows r) for (int c0
cltthis.ncols c)
this.datarc public void
addMove(int c, char player) public boolean
allowsMove(int c)
3 data members
constructor
methods
Starting code for a Board class and CS5App class
is in Hw10Pr1.zip
20Quiz
class CS5App public static void
main(String args) H.pl(Hi!
Welcome to Connect 4) int R
H.ni(How many rows?(4-15)) int C
H.ni(How many columns? (4-15)) Board
b new Board(R,C) char player
'X' while (true)
b.print() int
uc H.ni(player 's move)
while ( !b.allowsMove(uc) ) uc
H.ni(player 's move)
b.addMove(uc,player) if (player
'X') player '0'
else player 'X'
// end of while // end of main //
end of class CS5App
What is each portion of main doing?
1
2
3
4
What still needs to be done?
21Quiz, part 2
class Board private int nRows private
int nCols private char data //
constructor and other methods here public
void addMove(int c, char player)
for (int rthis.nRows-1 rgt0 --r)
if (this.datarc )
this.datarc player
break
public boolean allowsMove(int c)
What is each line of addMove doing?
Write allowsMove . It should return true if c is
OK for a move false otherwise.
22Problem 1
Hw10 Pr1 Connect Four
Similar to Lights Out and Life, but using a
Board object
class CS5App
class Board
sets things up and runs a large while loop
Board
the constructor
main
addMove
places a checker
- Get the of rows and cols (R, C)
- Create an object, b, of type Board
-
- (3) Big while loop...
- Ask for the next players move
- Check the move then make it
- See if the game is over
print
outputs to screen
Board b new Board(R,C)
allowsMove
checks if allowed
isFull
checks if space left
winsFor
checks if a player has won
23Problem 1
Hw10 Pr1 Connect Four
Similar to Lights Out and Life, but using a
Board object
class CS5App
class Board
sets things up and runs a large while loop
Board
the constructor
QUIZ
main
addMove
places a checker
- Get the of rows and cols (R, C)
- Create an object, b, of type Board
-
- (3) Big while loop...
- Ask for the next players move
- Check the move then make it
- See if the game is over
QUIZ
print
outputs to screen
Board b new Board(R,C)
allowsMove
checks if allowed
isFull
checks if space left
winsFor
checks if a player has won
still to do
24Problem 1
Still to write in the Board class
public boolean isFull()
1
public boolean winsFor(char pl)
2
Extra Credit Mouse input
25Problem 2
Hw10 Pr2 The Date Calculator
Similar to previous menu problems, but using
Date objects
class CS5App
Pair Programming Problem
Methods
sets things up and runs a large while loop
main
printMenu
prints
(0) Enter a new date of interest (1) Print the
current date of interest (2) Move one day forward
in time (3) Move one day backward in time (4) Day
difference finder (5) Find the day of the
week (9) Quit
26Problem 2
Hw10 Pr2 The Date Calculator
PAIR
Similar to previous menu problems, but using
Date objects
class CS5App
Methods
sets things up and runs a large while loop
main
printMenu
prints
(0) Enter a new date of interest (1) Print the
current date of interest (2) Move one day forward
in time (3) Move one day backward in time (4) Day
difference finder (5) Find the day of the
week (9) Quit
no computer required
Prof. Art Benjamin
27Problem 2
Hw10 Pr2 The Date Calculator
PAIR
Similar to previous menu problems, but using
Date objects
class CS5App
class Date
Methods
Methods
sets things up and runs a large while loop
Date
the constructor
main
print
prints
printMenu
prints
tomorrow
forward 1 day
(0) Enter a new date of interest (1) Print the
current date of interest (2) Move one day forward
in time (3) Move one day backward in time (4) Day
difference finder (5) Find the day of the
week (9) Quit
yesterday
backward 1 day
isBefore
helper method
of days difference between 2 Dates
diff
diffNoPrint
same w/ no printing
dayOfWeek
returns a String
28Two Date objects d and d2
int day
int year
int month
Date d
void print()
void tomorrow()
int day
int year
int month
Date d2
void print()
void tomorrow()
29The Date class
class Date private int month private int
day private int year public Date(int m,
int d, int y) this.month m
this.day d this.year y public
void print() H.p(this.month /)
H.p(this.day /) H.p(this.year)
data members
constructor
method
30Making a new Date
class CS5App public static void main(String
args) Date d new Date(11,13,2004)
Date d2 new Date(11,25,2004) H.p(The
ACM contest is on ) d.print()
d.tomorrow() H.p(CS 5s midterm is due on
) d.print() H.p(And Thanksgiving
will be ) d2.print()
31Making a new Date
class CS5App public static void main(String
args) Date d new Date(11,13,2004)
Date d2 new Date(11,25,2004) H.p(The
ACM contest is on ) d.print()
d.tomorrow() H.p(CS 5s midterm is due on
) d.print() H.p(And Thanksgiving
will be ) d2.print()
Output
11/13/2004
32Making a new Date
class CS5App public static void main(String
args) Date d new Date(11,13,2004)
Date d2 new Date(11,25,2004) H.p(The
ACM contest is on ) d.print()
d.tomorrow() H.p(CS 5s midterm is due on
) d.print() H.p(And Thanksgiving
will be ) d2.print()
Output
11/13/2004
11/14/2004
33Making a new Date
class CS5App public static void main(String
args) Date d new Date(11,13,2004)
Date d2 new Date(11,25,2004) H.p(The
ACM contest is on ) d.print()
d.tomorrow() H.p(CS 5s midterm is due on
) d.print() H.p(And Thanksgiving
will be ) d2.print()
Output
11/13/2004
11/14/2004
11/25/2004
34tomorrow()
int
month
class Date public void tomorrow()
int
int
day
year
data members in every Date
35Checking a Date
class CS5App public static void main(String
args) // prompt the user for mo, dy, yr
int mo H.ni() int dy H.ni() int
yr H.ni() Date d new Date(mo,dy,yr)
if (d.isLeapYear()) H.pl(d has 366
days) else H.pl(d has 365 days)
36Leap years
int
month
int
int
class Date public boolean isLeapYear()
if (this.year 400 0) return
true if (this.year 100 0) return
false if (this.year 4 0) return
true
day
year
only one of these is needed here
37Comparing Dates
class CS5App public static void main(String
args) Date d new Date(11,13,2004)
Date d2 new Date(11,25,2004) if
(d.isBefore(d2)) H.pl(d is earlier than
d2) else H.pl(d is not earlier than
d2)
38Comparing Dates
class Date public boolean isBefore(Date d2)
What two dates are being compared here?
d2
is only one of them!
39Problem 2 Tricks
public int diff(Date d)
- positive if d is after this
- negative if d is before this
returns the of days between this and d
- print every day in between!
public String dayOfWeek()
returns the day of the week on which this falls
40Summary/Examples
An object is a variable. Objects are created via
new and a constructor. A class is a type of
variable. A class can include both data members
and methods. The object that calls a method is
named this inside that method.
Date d new Date(11,10,2003)
class Date private int month private int
day private int year public Date(int m,
int d, int y) this.month m
this.day d this.year y public
void print() H.p(this.month /
this.day / this.year)
41Lab this week
Last Names A-L
Date print tomorrow yesterday isBefore diff diffNo
Print dayOfWeek
- Problem 2 The Date Calculator (Pairs)
Youll need to write (and use)
X X XO XOOOX
--------------- 0 1 2 3 4 5 6
isFull winsFor allowsMove
- Extra Credit Mouse input-handling for Connect
Four
42Example contest problem
Factorial Factors - Southern California Regional
ACM Programming Contest The factorial function,
n! 1 2 ... n, has many interesting
properties. In this problem, we want to determine
the maximum number of integer terms (excluding 1)
that can be used to express n!. For example 8!
1 2 3 4 5 6 7 8 2 3 2 2
5 3 2 7 2 2 2 27 32 5 7 By
inspection, it is clear that the maximum number
of terms (excluding 1) that can be multiplied
together to produce 8! is 11. The input for
your program consists of a series of test cases
on separate lines. Each line contains one number,
n, 2 lt n lt 1000000. For each test case, print
the maximum number of factors (excluding 1) that
can be multiplied together to produce n!. Put the
output from each test case on a separate line,
starting in the first column.
Sample Input 2 1000000 1996 5 8 123456
Sample Output 1 3626619 5957 5 11 426566
43Objects
An object is a data structure (like an array),
except
(1) Its data elements need not be the same
type. (2) Its data elements have names chosen by
the programmer. (3) Its data can be protected
from unauthorized changes! (4) An object can have
behaviors built-in by the programmer.
int
double
hours
number
String
Course c
name
String
String
String
students
the dot is used to get at parts of an object
(data or actions)
void addStudent(String s)
int numStudents()
Names!
44slides to print following this
45Gaussian Elimination
public static void solve(double A) for
(int c0 cltA0.length-1 c) // columns
multRow( A, c, 1/Acc ) //
diag 1.0 for (int r0 rltA.length r)
// rows if (c ! r) //
off-diagonal elements become 0.0
addMxRaToRb( A, -Arc, c, r )
dest row
multiplier
dest row
source row
multiplier
46Objects
An object is a data structure (like an array),
except
(1) Its data elements need not be the same
type. (2) Its data elements have names chosen by
the programmer. (3) Its data can be protected
from unauthorized changes! (4) An object can have
behaviors built-in by the programmer.
int
double
hours
npeople
String
Course c
name
String
String
String
clist
void addStudent(String s)
int numStudents()
Names!
47Coding classes and objects in main
int
double
hours
npeople
data
String
name
Course c
String
String
String
clist
Course(String name)
constructor
void addStudent(String s)
methods
int numEnrolled()
public static void main(String args)
Course c new Course(cs5) // construct
c c.addStudent(Michael) // add a
student H.pl(c.numEnrolled()) //
print enrollment 1
48Connect Four
For your convenience, the creators of Javas
library have included a Board class that can
represents any size of Connect Four board... !
0
0
X X XO XOOOX
O --------------- 0 1 2 3 4 5 6
1
1
2
2
3
3
4
4
5
5
0 1 2 3 4 5 6
49Quiz
class CS5App public static void
main(String args) H.pl(Hi!
Welcome to Connect 4) int R
H.ni(How many rows?(4-15)) int C
H.ni(How many columns? (4-15)) Board
b new Board(R,C) char player
'X' while (true)
b.print() int
uc H.ni(player 's move)
while ( !b.allowsMove(uc) ) uc
H.ni(player 's move)
b.addMove(uc,player) if (player
'X') player '0'
else player 'X'
// end of while // end of main //
end of class CS5App
What is each portion of main doing?
1
2
3
4
What still needs to be done?
50Quiz, part 2
class Board private int nRows private
int nCols private char data //
constructor and other methods here public
void addMove(int c, char player)
for (int rthis.nRows-1 rgt0 --r)
if (this.datarc )
this.datarc player
break
public boolean allowsMove(int c)
What is each line of addMove doing?
Write allowsMove . It should return true if c is
OK for a move false otherwise.
51Problem 1
Still to write in the Board class
public boolean isFull()
1
public boolean winsFor(char pl)
2
Extra Credit Mouse input
52Two Date objects d and d2
int day
int year
int month
Date d
void print()
void tomorrow()
int day
int year
int month
Date d2
void print()
void tomorrow()
53tomorrow()
int
month
class Date public void tomorrow()
int
int
day
year
data members in every Date
54Leap years
int
month
int
int
class Date public boolean isLeapYear()
if (this.year 400 0) return
true if (this.year 100 0) return
false if (this.year 4 0) return
true
day
year
only one of these is needed here
55Comparing Dates
class Date public boolean isBefore(Date d2)
What two dates are being compared here?
d2
is only one of them!
56Problem 2 Tricks
public int diff(Date d)
returns the of days between this and d
- positive if d is after this
- negative if d is before this
- print every day in between!
public String dayOfWeek()
returns the day of the week on which this falls
57Lab this week
Last Names A-L
Date print tomorrow yesterday isBefore diff diffNo
Print dayOfWeek
- Problem 2 The Date Calculator (Pairs)
Youll need to write (and use)
X X XO XOOOX
--------------- 0 1 2 3 4 5 6
isFull winsFor allowsMove
- Extra Credit Mouse input-handling for Connect
Four
58Coding classes and objects in main
1
3.0
int
double
cs5
hours
npeople
data
String
name
Michael
Course c
String
String
String
clist
Course(String name)
constructor
void addStudent(String s)
methods
int numEnrolled()
public static void main(String args)
Course c new Course(cs5) // construct
c c.addStudent(Michael) // add a
student H.pl(c.numEnrolled()) //
print enrollment 1