01b: Algorithms
ICS211, Spring 2013
Dr. Zach
(switch view)
Status
- Latest news on course website
- Quiz 01A is in Laulima.
- Check out EC1 (warmups don't count)
- Questions about course website or syllabus?
A00
- Hello World. Purpose:
- install/configure JDK on your home machine
- use Tamarin
- Need a Tamarin account first (first step of A00)
- TAs will tell you more in lab
- (Tamarin demo)
Tamarin Reminders
- No GUIs (JOptionPane, etc.)
- Don't use packages
- Class containing main method must be public
- 1 file per upload.
- For now: your .java source code, named with your username and the assignment
- Can add additional non-public classes to same file... (bad practice)
- Later in the term we'll use jar files to bundle multiple classes together
- Tamarin runs Java 7 now
- CGI pages are a bit slow. :(
By "package", I mean you have the line package somename;
at the top of your file.
Submission Policies
- Submission has to compile to get additional points
- Grade is tentative until verified by your TA
- Can resubmit as much as you want before verified (but -1 each time)
- Grade and lateness is determined by your last submission
Some big words
- loquacious
- cerumen
- abecedarian
- sesquipedalian
- sesquipedaliophobia
- hippopotomonstrosesquippedaliophobia
- algorithm
The word algorithm is a corruption of name of Muhammed ibn Musa Al-Khwarizmi.
Some algorithm definitions
- a step-by-step procedure for solving a problem or accomplishing some end (Mirriam-Webster, broad def.)
- a process or set of rules to be followed in calculations or other problem-solving operations, esp. by a computer (Google)
- a set of rules for solving a problem in a finite number of steps (Dictionary.com)
- a step-by-step procedure for calculations; a set of rules that precisely defines a sequence of operations (Wikipedia)
- a logical arithmetical or computational procedure that if correctly applied ensures the solution of a problem (Collin's World English Dictionary)
Characteristics (of informal definition)
- how to do something (instructions for a procedure)
- step-by-step
- to solve a problem or accomplish some end
- Examples:
- recipe, directions, instruction manual, etc.
More characteristics (of formal CS definition)
- precisely defined
- includes all the steps
- correctly ordered steps
- unambiguous
- definitions/complete information: no interpretation or thought required to execute it
- sufficient level of detail
- covers all possible states or required decision points
- reaches an end (in finite time/space)
- each step is possible
- involves computation or calculation (symbol manipulation or number crunching)
- assumes: input, intermediate steps, output.
Three levels here
- Problem (to solve)
- Algorithm (how to solve it; abstract)
- Implementation (actually doing it; concrete)
Even when you have the same algorithm, the implementation can vary. Examples:
- cooking: same recipe, different quality outcome
- directions: one person runs, one walks
- program: different code details (variable names, for loop vs while loop, switch vs if/else, etc)
Exercise: How to put on shoes
- Write an algorithm for putting on a pair of shoes
- Assume a robot, alien, or obstinate child will be executing it
- Compare your algorithm with your neighbor(s)
- ordered steps? sufficiently precise and well-defined? possible to reach the end?
- are your algorithms identical? how do they differ?
- Execution
- Further reading
Algorithm selection
- Why select one algorithm over another?
- Example: travel directions from A to B
- Example: mac and cheese recipe
- (Continue the discussion in Laulima: selecting an algorithm for a program)
- Either/or posting requirement.
Java Review
Basics II: Loops, exceptions, input, etc
Nature of this Review
- Again, assuming you already know this stuff from 111
- But different 111 backgrounds
- And different amount of time since 111
- Read the Java Tutorial: Language basics or textbook's Appendix A.
- Practice with CodingBat
Using Objects (eg. Strings)
- Constructor, with
new
String name = new String("Zach");
Scanner keybd = new Scanner(System.in);
Object obj = new Object();
- Calling instance methods
String word = "Hello";
String end = word.substring(word.lastIndexOf('l'));
System.out.println(end);
- Calling class (static) methods
String number = "13";
int num = Integer.parseInt(number);
User Input with Scanner (1/3)
- With Scanner (other techniques possible):
java.util.Scanner keybd = new java.util.Scanner(System.in);
System.out.print("Enter your name: ");
String name = keybd.nextLine();
System.out.print("Enter your age: ");
int age = keybd.nextInt();
- Can use
import java.util.Scanner
and then you only need:
Scanner keybd = new Scanner(System.in);
User Input with Scanner (2/3)
- Most useful Scanner methods (see API for more)
nextLine()
- read in everything user types (as a String)
next()
- skips any whitespace and reads in only first/next token (word) the user types (as a String)
nextInt()
- as next(), but then convert token to an int
nextDouble()
- as next(), then converts token to a double
- If nextInt or nextDouble can't convert the token, throws a
java.util.InputMismatchException
.
Catching Exceptions
try
, catch
, and finally
(optional) blocks.
try
- attempts to complete the code here. If an exception occurs, jumps to corresponding catch
catch
- need to name the exception type you want to catch
finally
- always executes, whether try block completed normally, one of the catch blocks caught an exception, or if an exception was thrown that was not caught by any of the catch blocks
- A contrived example (wouldn't normally catch AIOOBE or divide by 0, but avoid it in the first place):
int n = ... //try: 2 or 0 or 4
int[] nums = {0, 2, 4, 6};
try {
int x = 15 / nums[n];
System.out.println(x);
}catch (ArithmeticException e) {
System.out.println("Can't divide by 0.");
}finally {
System.out.println("Done.");
}
System.out.println("Goodbye.");
User Input with Scanner (3/3)
//gets a positive integer from the user
Scanner keybd = new Scanner(System.in);
int input = 0;
while (input <= 0) {
try {
System.out.print("Enter a positive integer: ");
input = keybd.nextInt();
if (input <= 0) {
System.out.println("That is not a positive integer.");
}
}catch (InputMismatchException e) {
System.out.println("That is not even an integer!");
keybd.nextLine(); //important: clears the bad input from the input stream
}
}
System.out.println("You entered: " + input);
Summary
- Tamarin basics
- What an algorithm is
- Java review: loops, try/catch, Scanner for user input
Next time...
- Go to lab (twice)
- Do A00: Get a Tamarin account, setup your JDK, submit Hello World to Tamarin
- Contribute to the algorithm discussion in Laulima: Forums (sooner is better)
- Take Quiz 01A and 01B
- Start on A01
- Next: Software development process; Java methods