### CS30 - Spring 2015 - Class 25

#### Lecture notes

- assignment 11

• Nim tournament!

• halting problem
- Could we write a Turing machine that simulates the running of another Turing machine?
- take as input two things:
1) some representation of the Turing machine
2) some input to run on the Turing machine input (i.e. the input Turing machine from 1)
- would then "run" the Turing machine it was simulating on the input
- this is called a "universal Turing machine"
- Consider a TM H that does the following:
H(M,w) =
- accept if M finishes (accepts or rejects) with input w
- reject if M never finishes with input w, i.e. runs forever

- this is called the halting problem
- this program comes up a lot in CS
- it doesn't finish
- will it or is it stuck in an infinite loop?
- for particular types of problems we can definitely answer this problem
- can we in general, though?

- Could we ever write this Turing machine?

• To prove that we *cannot* we'll do a proof by contradiction
- We assume that we can, i.e. that we could construct H above
- And show that this results in a contradiction, meaning it couldn't be the case

- Let's construct the following Turing machine
D(M) =
Run H(M, [M]) where [M] is the string representation of M
- if it accepts (i.e. H(M, [M]) will finish), then loop infinitely
- if it rejects (i.e. H(M, [M] will loop forever), then stop

- What happens when we run D(D)?
- Run H(D, [D])
- if it accepts, that implies that D(D) finishes
- however, if H(D, [D]) accepts, then we know that D(D) loops infinitely
- if it rejects, that implies that D(D) never finishes
- however, if H(D, [D]) rejects, then we know that D(D) halts

- If H says D finishes, then it loops infinitely
- If H says D loops, then it halts

- take a look at the first four functions in SimpleFunctions code
- what do they do?
- what are some of the similarities with python you notice (there are lots!)?
- what are some of the differences from python you notice (there are lots!)?

• some high-level syntactic differences
- all statements in Java are terminated with a semi-colon. This (not an end of line) indicates to java that the statement is done
- Java uses braces and parenthesis to identify blocks of code (not indentation, like python)
- For example, the following are *all* equivalent to the add function
int num2
){return num1+num2;
}

int add(int num1, int num2){return num1+num2;}

int
(
int num1,
int num2
)
{
return
num1
+
num2
;
}

- Java doesn't care!
- We follow certain conventions to make the code more readable, though!
- Curly braces, {}, denote a block of code
- parentheses, (), denote parameters or arguments
- variable and function name conventions
- like python, all functions and variable names will start with a lowercase letter
- unlike python, for multi-word variables and functions the convention for java is to use "camel-casing", i.e. capitalize the first letter of each new word
- For example:
firstAndLastSame NOT first_and_last_same
sumUpTo NOT sum_up_to
- single line comments: //, everything following this to the end of the line is a comment and is ignored
- Everything between /* and */ is a comment and is ignored
- by convention, we will often put a * on each line in the comments to indicate that this is still part of the comment, but it is NOT required
- this can be very useful during testing (or other situations) when you want to temporarily comment out a large section of code

• statically typed vs. dynamically typed languages
- What is a type?
- the type of an object define the types of operations you can perform on that object
- in strongly-typed languages anything that represents a value has a type (e.g. objects)
- in dynamically-typed languages each object is checked on the fly when the program is done to make sure
- For example, is there any problem with the following python statements:
x = 10
y = "banana"
x/y

- Yes! You can't divide a number by a string
- When would python recognize that this was an error?
- Not until you run the program and it gets to that particular line
- it would check to see what type x was
- and check to see what type y was
- and only then realize that you can't calculate x/y
- in statically-type languages the type of objects can be determined *without* running the code
- often this means is that the programmer is required to declare the types of things in the languages
- For example, the above statements would be written in Java as follows:
int x = 10
String y = "banana"
x/y

- When we declare variables we *have* to specify their type
- Because of this, Java can easily figure out before running that x/y will cause an error
- why statically typed?
- static typing allows errors to be caught much earlier and, often, automatically

• typed variables
- every variable and parameter must have a type
- what are some of the types Java might have?
- int (integers)
- float (decimal numbers)
- double (decimal numbers with greater precision, more on this later)
- boolean
- String (strings)
- many more
- to declare a new variable, you first specify the type then the name then assign to it
<type> <name> = <value>;

- Java will enforce that for the lifetime of the variable, it can only be assigned to something of that type (there are actually a few caveats to this, but more later)
- For example:
int x = 10
x = "banana"

- Java will complain!

- when you declare parameters in addition to giving them a name, you also have to specify their type!
- Java checks these when a function is called
- For example:

- would result in an error, even before the function is called
- notice this avoids a lot of issues/errors that could come up either by accident or maliciously

• type functions
- in addition to having to specify the types of all variables and parameters, you also need to specify the type of value that a function returns
- firstAndLastSame returns a boolean
- what is void?
- void is used when a function doesn't return a value

• Strings
- Strings are not a built-in data type, like they are in python (and other languages)
- because of this, there is no special functionality for indexing using []

• for loops
- for loops in Java are a bit more general than for loops in python
- for loops consist of three parts:
1) a beginning statement that is only run once (initialization)
2) the condition or test to see if the loop should keep going (condition)
3) a statement that is run after each loop iteration (increment)

- all three parts are inclosed in parentheses
- each of the parts is separated by a semi-colon
- the main body of the for loop is denoted by braces
- like other block of code, we will indent to make it more readable
- When the program is running and it encounters a for loop:
- first, the initialization statement is run
- this is only done once for a for loop!
- often this is a variable declaration, but anything could go here
- if a variable is declared, the scope of that variable is just the for loop block
- next, the condition is checked. If it's false, the loop finishes immediately.
- if the condition is true, the statements in the for loop block are executed
- after each iteration, the increment condition is run once
- the condition is then checked again and the process is repeated
- really just syntactic sugar for a while loop

• printing
- not as nice as in python :(
- A couple of different ways right now:
- print something and then end the line:
System.out.println(...)
- print something, but DON'T end the line:
System.out.print(...)
- Java is pretty smart about converting things to strings
- all built-in data types (ints, float, double, etc.) get converted in a straightforward way
- most other types of objects also do the right thing

• ++
- Java has a few extra short-hands that are not in Pyhon
- ++ is equivalent to += 1, for example:
i++;

does the same thing as

i += 1

or

i = i + 1
- -- also is available

• Look at the isPrime and isPalindrome functions in SimpleFunctions.java in SimpleFunctions code

• if/else statement
- Java supports:
- stand-alone if statements
- if/else statements
- "if/else if" statements
- like other constructs, the condition needs to be in parentheses and the code inside the block surrounded by {}
- else statement is optional
- the "else-if" statement is just an else followed by another if

• recursion
- Java supports recursive functions

• while loop
- works just like in python or other languages
- like other constructs, the condition needs to be in parentheses and the code inside the block surrounded by {}