Soft Cons 2
Soft Cons 2
1
Computing a Hailstone Sequence Java Syntax
statement grouping
Java Python
¾ curly braces surround groups of statements
// hailstone sequence from n # hailstone sequence from n
¾ semicolons terminate statements
while (n != 1) { while n != 1:
if (n % 2 == 0) { if n % 2 == 0: ¾ indentation is technically optional but essential for human readers
n = n / 2; n = n / 2 comments
} else { else: ¾ // introduce comment lines
n = 3 * n + 1; n = 3 * n + 1 ¾ /* ... */ surround comment blocks
}
control statements
}
¾ while and if require parentheses around their conditions
operators
¾ mostly common with Python (+, -, *, /, <, >, <=, >=, ==)
¾ != means “not equal to”
¾ ! means “not” , so n!=1 is the same as !(n == 1)
¾ the % operator computes remainder after division
© Robert Miller 2007 © Robert Miller 2007
2
Static Typing A Complete Java Program
static vs. dynamic all Java code must be
public class Hailstone {
¾ static or compile-time means “known or done before the program runs” contained
public static void main(String[] args) {int n = within
3; a class
¾ dynamic or run-time means “known or done while the program runs”
while (n != 1) {
System.out.println(n); a Java program starts by
Java has static typing if (n % 2 == 0) { running the main
n = n / 2; method of a class
¾ expressions are checked for type errors before the program runs
} else {
¾ Eclipse does it while you’re writing, in fact
n = 3 * n + 1;
int n = 1;
}
n = n + “2”; // type error – Eclipse won’t let you run the program we’lltalk
talkabout
aboutwhat
what
} we’ll
¾ Python
P th hh asddynamiicttypiing – it wouldn’t ’t compllaiin abboutt n + “2” until
til it System.out.println(n); publicand
public andstatic
staticmean
mean
reaches that line in the running program } inthe
in thenext
nextlecture;
lecture;for
for
ld
} now,we’ll
now, we’lljust
justuse
usethem
them
onall
on allmethods
methods
3
Recursive Method Hailstone Sequence as a String
/*
* Returns the hailstone sequence from n to 1
public static int hailstoneLength(int n) { * as a comma-separated string.
if (n == 1) { * e.g. if n=5, then returns "5,16,8,4,2,1".
bbase case
return 0; */
} else if (isEven(n)) { public static String hailstoneSequence(int n) {
return 1 + hailstoneLength(n/2); ...
recursive cases
} else { }
return 1 + hailstoneLength(3*n + 1);
}
}
4
Hailstone Sequence as an Array Arrays
/**
array is a fixed-length sequence of values
* Returns the hailstone sequence starting from n as an
* array of integers, e.g. hailstoneArray(8) returns ¾ base type of an array can be any type (primitive, object, another array type)
* the length-4 array [8,4,2,1]. int[] intArray;
/
*/ char[] charArray;
public static int[] hailstoneArray(int n) {
String[] stringArray;
...
} double[][] matrix; // array of arrays of floating-point numbers
¾ fresh arrays are created with new keyword
intArray = new int[5]; // makes array of 5 integers
¾ operations on an array
intArray[y[0]] = 200;; // sets a value
intArray[0] Î 200 // gets a value
intArray.length Î 5 // gets array’s length
¾ unlike a String, an array’s elements can be changed
¾ but once created, an array’s length cannot be changed
• so it’s not like a Python list – a Java array can’t grow or shrink
© Robert Miller 2007 © Robert Miller 2007
5
What Makes “Good” Software A Larger View of Good Software
easy to understand correct
¾ well chosen, descriptive names ¾ gets the right answers
¾ clear, accurate documentation economical
¾ indentation ¾ runsffast, uses miiniimall resources,ddoesn’’t cost muchh to prod
duce
ready for change dependable
¾ nonredundant: complex code or important design decisions appear in only ¾ safe from bugs
one place
maintainable
¾ “decoupled”: changeable parts are isolated from each other
¾ easy to understand and ready for change
safe from bugs
usable
¾ static typing helps find bugs before you run
¾ has an effective user interface
¾ testable in small parts
¾ no hidden assumptions waiting to trap you or another programmer later secure
¾ safe from malicious attacks
... all these properties matter in practice
¾ sometimes supporting each other, sometimes in conflict
© Robert Miller 2007 © Robert Miller 2007
6
Objectives Intellectual Structure
what you should expect to get out of this course three paradigms
¾ state machine programming
¾ symbolic programming
fundamental programming skills
¾ object-based
object based programming
¾ how to specify, design, implement and test a program
¾ proficiency in Java and use of Java APIs
¾ use of standard development tools (Eclipse, SVN, JUnit) pervasive themes
¾ models and abstractions
engineering sensibilities
¾ interfaces and decoupling
¾ capturing the essence of a problem
¾ analysis with invariants
¾ inventing powerful abstractions
¾ appreciating the value of simplicity
¾ awareness of risks and fallibilities incremental approach
cultural literacy ¾ concepts introduced as needed
¾ familiarity with a variety of technologies (http, postscript, sockets, etc) ¾ deepening sophistication as ideas are revisited
7
What You Should Do
today
¾ sign up for a recitation on the 6.005 web site
tomorrow
¾ go to the i you’’ve bbeen assigned
h reciitation i d to
Friday
¾ read Lab 1 before coming to lab
¾ go to your assigned lab location for Lab 1