Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CIS3023: Programming Fundamentals for CIS Majors II Introduction to Problem Solving, Algorithms and Pseudocode, Programming in JAVA - I Course Lecture Slides Summer 2010 Ganesh Viswanathan Announcements • CISE systems are being upgraded. CISE webpages were down yesterday! • So, course slides will also be posted on E-Learning (http://lss.at.ufl.edu) from now on. • E-Learning contains course schedule and weekly schedule. • Midterm: June 14 Final Exam: Aug 5 Location: TBA Problem Solving Using a step-by-step programmatic approach to find a solution for complex problems Image credits: coventry.gov.uk Problem-Solving Model Iterative Process! Image credits: usf.edu Problem 1 Give a rough estimate of the number of cellphones in the University of Florida Gainesville campus. How to become a better problem solver - Break the problem into parts - Actively work to check your understanding - Recognize limitations and pitfalls - Be open, Engage confusing problems, Brainstorm ideas, and Learn techniques that you can apply in a wide range of scenarios. Problem 2 If the triangle below is shorter than the square and the circle is taller than the square, then label the triangle “1”. However, if this is not the case, label the second tallest figure as “2”. Problem-Solving Model 1. Define the problem 2. Analyze and decide course of action (design algorithm) 3. Plan solution and verify goals 4. Generate solutions 5. Testing and Evaluation Iterative Process! Algorithm What is an algorithm? An algorithm is an effective procedure for solving a problem expressed as a finite sequence of instructions. “effective procedure” = program Problem = set of instances of a question Solution = set of answers to these instances Algorithm What is an algorithm? An algorithm is an effective procedure for solving a problem expressed as a finite sequence of instructions. e.g., PRIME numbers Is 2 a prime? Is 3 a prime? Is 10500 a prime? Is 6 24 810 a prime? Algorithm What is an algorithm? An algorithm is an effective procedure for solving a problem expressed as a finite sequence of instructions. Problem = question + input parameter(s) The size of a problem can be characterized by an integer n PRIME: #digits of input parameter SORTING: #inputs Algorithm Is there an algorithm for each problem? NO! 1. The problem (question) needs to be effectively specified. “How many angels can dance on the head of a pin” is not an effectively specified problem. 2. Even it effectively specified, there is not necessarily an algorithm to solve every problem. Algorithm ULAM’s Problem: Given f(n) = if n=1 then stop else if n is even then f(n/2) else f(3n+1) Question: Will f(n) stop ? f(1) Stop f(2) → 1 → stop f(3) → 10 → 5 → 16 → 8 → 4 → 2 → 1 → stop f(4) → 2 → 1 → stop f(5) → 16 → 8 → 4 → 2 → 1 → stop f(6) → 3 → (etc., as for 3 above) → stop f(7) → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → … → stop • Nobody has ever found an n for which f doesn’t stop !! • Nobody has ever found a proof that f stops for all natural numbers (n>0) !! Algorithm Q: Search for a value in an array of numbers. i 1 2 3 4 5 6 A 100 20 50 80 10 90 LINEAR-SEARCH [A, n, x]: Searches the array using basic linear search to find a match for x. INPUT: Unsorted array A of n natural numbers. Search key x. OUTPUT: If search key is found, returns its location in the array, else “x not found” 1 for i ← 1 to n 2 do if A[i] = x 3 then return i 4 else i ← i + 1 5 return “x not found” Pseudocode • A compact and informal description of algorithms using the structural conventions of a programming language and meant for human reading. • A text-based design tool that helps programmers to develop algorithms. Q: Write a pesudocode to print pass/ fail for each student based on grade. Pass is 60 points or higher. A: //Pseudocode to print student pass/fail status. If student grade > 60 print “pass” Else print “fail” Pseudocode Q: Write a pesudocode to compute the class average for upto n students in Exam1, from user input. A: //Pseudocode to compute class average for n students Set average = 0, total = 0, grade = 0 Set grade counter = 0 While user has entered a grade While (grade counter) < n Input next grade Add grade to total Increment grade counter If grade counter != 0 Set average = ( total / n ) Print “class average is “ average Always remember to test-run your pesudocode to check for correctness. JAVA Who is this guy? James Gosling - “invented” JAVA in 1994 !! JAVA Java 1.0 was first released by Sun Microsystems in 1995. Java became popular for web programming. "Write Once, Run Anywhere" (WORA) philosophy. Key design principles: - Platform independence - Security - Stability Duke, the JAVA mascot How Java works! Programming in JAVA – I Basic concepts in Java • • • • • • • Values Variables Complex data/objects Functions/methods Expressions Conditionals Loops Programming in JAVA English to JAVA translation: • • • • • • • Piece of data: value Kind of data: type Place to store a value: variable Structured group of variables: object Kind of object: class Object of a particular class: class instance Variable belonging to an object: instance variable Programming in JAVA Values and Types: • All data values in Java have a type • The type of a value determines: • Allowed operations • Memory representation • Allowed operations • Conversion to other types • Allowed operations • Types are program invariants • Help to catch errors Default initial values (if omitted) are based on type Programming in JAVA Primitive Types: • EIGHT basic pre-defined types: values on which Java can operate directly. • byte, short, int, long, float, double, boolean, char • Plus, special support for character strings - using java.lang.String class