0% found this document useful (0 votes)
17 views8 pages

Soft Cons 2

software cons 2 discussed

Uploaded by

qamarmemon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

Soft Cons 2

software cons 2 discussed

Uploaded by

qamarmemon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Today’s Topics

getting up to speed with Java


¾ note that programming experience is a prerequisite for 6.005
¾ we assume you’ve used Python
¾ these initial lectures will show the Java way to do things you should already
be able to do in Python (or some other language)
what makes software “good”
¾ whether it works isn’t the only consideration

Why We Use Java in 6.005 Hailstone Sequences


safety start with some positive integer n
¾ static typing catches errors before you even run (unlike Python) ¾ if n is even, then next number is n/2
¾ strong typing and memory safety catch errors at run time (unlike C/C++) ¾ if n is odd, then next number is 3n+1
ubiquity ¾ repeat these moves until you reach 1
¾ Java is widely used in industry and education examples
libraries 2, 1 7, 22, 11, 34, 17, 52, 26, 13, 40, ...?
¾ Java has libraries and frameworks for many things 3, 10, 5, 16, 8, 4, 2, 1 2n, 2n-1 , ... , 4, 2, 1
4, 2, 1
tools
5, 16, 8, 4, 2, 1
¾ excellent, free tools exist for Java development (like Eclipse)
¾ why “hailstone”?
hailstone ? because hailstones in clouds also bounce up and down
it’s good to be multilingual chaotically before finally falling to the ground
¾ knowing two languages paves the way to learning more (which you should)
let’s explore this sequence
why we regret using Java... ¾ open question: does every positive integer n eventually reach 1?
¾ wordy, inconsistent, freighted with legacy baggage from older languages,
no interpreter, no lambda expressions, no continuations, no tail recursion,
...

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

Computing a Hailstone Sequence Declarations and Types


variables must be declared before being used
int n = 3;
declares the integer ¾ a declaration includes the type of the variable
while (n != 1) {
variable n ¾ two kinds of types, primitive and object
System.out.println(n);
if (n % 2 == 0) {
¾ primitive types include
n = n / 2; • int (integers up to +/- 2 billion)
} else { prints a value to the console • long (integers up to +/- 263)
n = 3 * n + 1; (useful for debugging) • boolean (true or false)
} • double (floating-point numbers)
} • char (characters)
System.out.println(n); ¾ objject types
yp include
• String (a sequence of characters, i.e. text)
¾ you can define new object types (using classes), but you can’t define new
primitive types

© 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

© Robert Miller 2007 © Robert Miller 2007

Length of a Hailstone Sequence More Method Definitions


/* /*
* Returns the number of moves of the hailstone sequence * Returns true if and only if n is even.
* needed to get from n to 1. */
*/ type of value returned public static boolean isEven(int n) {
by
y the
themethod
method
public static int hailstoneLength(int n) {by the method return n % 2 == 0;
int moves = 0; }
while (n != 1) {
argument(s) of the method
if (isEven(n)) { /*
n = n / 2; * Start of the program.
} else { */
n = 3 * n + 1; public static void main(String[] args) { ... }
}
++moves;
common operator, equivalent to ¾ void means the method has no return type (so no return statement is
}
moves = moves + 1 required)
return moves; ¾ String [ ] is an array of String objects (in this case, these strings are the
} arguments given to the program on the Unix/Windows/Mac command
© Robert Miller 2007
line) © Robert Miller 2007

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);
}
}

© Robert Miller 2007 © Robert Miller 2007

Strings Hailstone Sequence as a String


¾ a String is an object representing a sequence of characters /*
* Returns the hailstone sequence from n to 1
• returning a List of integers would be better, but we need more
* as a comma-separated string.
machinery for Java Lists, so we’ll defer it
* e.g. if n=5, then returns "5,16,8,4,2,1".
¾ stringgs can be concatenated usingg+ *//
• “8” + “4” Î “84” public static String hailstoneSequence(int n) {
• String objects are immutable (never change), so concatenation String seq = n;
creates a new string, it doesn’t change the original string objects String seq = String.valueOf(n);
while (n != 1) { Type error! Java requires
¾ String objects have various methods you to convert the integer
if (isEven(n)) {
String seq = “4,2,1”; n = n / 2; into a String object. This is
seq.length() Î5 } else { a compile‐time error.
seq.ch
harAt(0)
A (0) Î ‘4’ n = 3 * n + 1;
}
seq.substr(0, 2) Î “4,”
seq += "," + n; But the + operator converts
¾ use Google to find the Java documentation for String } numbers to strings automatically
• learn how to read the Java docs, and get familiar with them return seq;
}
common shorthand for s = s + “,” + n
© Robert Miller 2007 © Robert Miller 2007

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

Hailstone Sequence as an Array Maximum Value of an Array


/** /**
* Returns the hailstone sequence starting from n as an * Returns the maximum value of an array of
* array of integers, e.g. hailstoneArray(8) returns * positive integers.
* the length-4 array [8,4,2,1].
* Returns 0 if the array is empty.
/
*/
*/
public static int[] hailstoneArray(int n) {
int[] array = new int[hailstoneLength(n)+1]; public static int maxValue(int[] array) {
int i = 0; int max = 0;
while (n != 1) { for (int i = 0; i < array.length; ++i) {
array[i] = n; if (array[i] > max) max = array[i];
++i; }
if (isEven(n)) { The for loop is commonly used for
Wha
Whatt happens
pp
happens ifif you
yyou omit
omit return max;
n = n / 2; iterating
ierat ing through a collection.
collection
this
this “+1”?
“+1”? The
The array
array isis too
too }
} else { for (init; test; update) {... }
n = 3 * n + 1; short,
short, and
and Java
Java produces
produces aa is roughly equivalent to
} runtime
runtime error
error when
when you you try
try init; while (test) { ... ; update }
} to write the
to write the last
last number.
number.
array[i] = n;
return array;
© 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

Summary About 6.005


basic Java lecturers
¾ control statements, expressions, operators ¾ Daniel Jackson and Rob Miller
¾ types and declarations teaching assistants
¾ methods ¾ Harold
H ld Cooper,
C Max Goldman,
M G ld E
Eunsuk
k Kang,
K Clayton
Cl Si
Sims, Kuat
K Yessenov
Y
¾ strings
lab assistants
¾ arrays
¾ TBD
properties of good software
¾ easy to understand
¾ ready for change
¾ safe from bugs

© 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

© Robert Miller 2007 © Robert Miller 2007

Your Responsibilities Grading Policy


assignments collaboration
¾ three 1-week explorations ¾ projects in teams of 3: must have different teams for each project
• writing a program we’ll use as a lecture example ¾ problem sets and explorations are done individually
¾ three 2-week
2 week problem sets • discussion permitted but writing or code may not be shared
• both written and programming components use of available resources
¾ three 2-week projects ¾ can use publicly available code, designs, specs
• in rotating teams of 3 people ¾ cannot reuse work done in 6.005 by another student (in this or past term)
¾ three 3-hour project labs, one for each project ¾ cannot make your work available to other 6.005 students
• project labs prepare you to get started on the project
grade breakdown
meetings ¾ project
j ts40%
40%
¾ two lectures each week (Mon,Wed, sometimes Fri) ¾ problem sets 30%
¾ one recitation each week ¾ explorations 20%
¾ project meetings with your team members and teaching staff ¾ participation 10%
• lecture time will often be made available for these meetings
© Robert Miller 2007 © Robert Miller 2007

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

© Robert Miller 2007

You might also like