Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.

Slides:



Advertisements
Similar presentations
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Advertisements

CS0007: Introduction to Computer Programming
MATH 224 – Discrete Mathematics
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey  Class variables  Big Oh notation.
Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Week 11: Class Design 1.  Most classes are meant to be used more than once  This means that you have to think about what will be helpful for future.
Week 12: Running Time and Performance 1.  Most of the problems you have written in this class run in a few seconds or less  Some kinds of programs can.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
The Efficiency of Algorithms
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Elementary Data Structures and Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Java Unit 9: Arrays Declaring and Processing Arrays.
Analysis of Performance
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Mathematical Calculations in Java Mrs. G. Chapman.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
1 CS 177 Week 12 Recitation Slides Running Time and Performance.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
CS 2601 Runtime Analysis Big O, Θ, some simple sums. (see section 1.2 for motivation) Notes, examples and code adapted from Data Structures and Other Objects.
Mathematical Calculations in Java Mrs. C. Furman.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Algorithm Analysis (Big O)
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
Copyright 2010 by Pearson Education Homework 8: Critters reading: HW8 spec.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Introduction to Analysis of Algorithms
Week 4 - Friday CS221.
Loops BIS1523 – Lecture 10.
Introduction To Repetition The for loop
Introduction to Algorithms
Week 14 - Monday CS 121.
Algorithm Analysis (not included in any exams!)
Introduction to Data Structures
CS 201 Fundamental Structures of Computer Science
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Revision of C++.
Homework 8: Critters (cont.)
Building Java Programs
Software Development Techniques
Presentation transcript:

Week 12 - Monday

 What did we talk about last time?  Defining classes  Class practice  Lab 11

 We can extend the idea of moving balls further  Let’s make some balls hunters and some balls prey  Hunters search for the nearest prey  If they reach a particular prey, they eat it  Prey balls always head away from the nearest hunter

 The Prey class has:  x Location: double  y Location: double  Aliveness: boolean  Speed: double  It also has accessors for x, y, and aliveness and a method we can use to kill the prey  Finally, it has an update method where it decides what it will do next

 The Hunter class has:  x Location: double  y Location: double  Speed: double  It also has accessors for x and y  Finally, it also has an update method where it decides what it will do next

 Members are the data inside objects  But, sometimes, wouldn’t it be nice if some data were linked to the class as a whole, rather than just the object?  What if you wanted to keep track of the total number of a particular kind of object you create?

 Static members are stored with the class, not with the object public class Item { private static int count = 0;// one copy total private String name;// one copy per object public Item( String s ) { name = s; count++;// updates global counter } public String getName() { return name; } public static int getItemsInUniverse() { return count; } public class Item { private static int count = 0;// one copy total private String name;// one copy per object public Item( String s ) { name = s; count++;// updates global counter } public String getName() { return name; } public static int getItemsInUniverse() { return count; }

 Static members are also called class variables  Static members can be accessed by either static methods or regular methods (unlike normal members which cannot be accessed by static methods)  Static members can be either public or private  In general, static variables should not be used

 Sometimes a value will not change after an object has been created:  Example: A ball has a single color after it is created  You can enforce the fact that the value will not change with the final keyword  A member declared final can only be assigned a value once  Afterwards, it will never change

 Using final, we can fix the value of a member  It will only be set once, usually when the constructor is called  It is customary to use all caps for constant names public class Animal { private String name; private final boolean MAMMAL; // never changes public Animal( String s, boolean mammal ) { name = s; MALE = mammal; } public void evolve() { MAMMAL = !MAMMAL;// compile-time error! } public class Animal { private String name; private final boolean MAMMAL; // never changes public Animal( String s, boolean mammal ) { name = s; MALE = mammal; } public void evolve() { MAMMAL = !MAMMAL;// compile-time error! }

 It is possible to set a static member to be constant using the final keyword  Usually, this is used for global constants that will never ever change  Making these values public is reasonable  Since they never change, we never have to worry about a user corrupting the data inside of the object

 The number of sides of a pentagon is always 5  Other code can access this information by using the value Pentagon.SIDES  Exactly like Math.PI or Math.E public class Pentagon { private double x; private double y; public static final int SIDES = 5; // never changes public Pentagon( double newX, double newY ) { x = newX; y = newY; } public double getX() { return x; } public double getY() { return y; } } public class Pentagon { private double x; private double y; public static final int SIDES = 5; // never changes public Pentagon( double newX, double newY ) { x = newX; y = newY; } public double getX() { return x; } public double getY() { return y; } }

 We want to compare the running time of one program to another  We want a mathematical description with the following characteristics:  Worst case We care mostly about how bad things could be  Asymptotic We focus on the behavior as the input size gets larger and larger

 First, let’s compute an expression that gives us running time for a program  Assume that any single line of code that doesn’t call a method or execute a loop takes 1 unit of time  Then, we sum up the number of lines that are executed in a method or in a loop

 Add up the operations done by the following code:  Initialization: 1 operation  Loop: 1 initialization + n checks + n increments + n additions to sum = 3n + 1  Output: 1 operation  Total: 3n + 3 int sum = 0; for( int i = 0; i < n; i++ ) sum += i; System.out.println("Sum: " + sum); int sum = 0; for( int i = 0; i < n; i++ ) sum += i; System.out.println("Sum: " + sum);

 We could express the time taken by the code on the previous slide as a function of n: f(n) = 3n + 3  This approach has a number of problems:  We assumed that each line takes 1 time unit to accomplish, but the output line takes much longer than an integer operation  This program is 4 lines long, a longer program is going to have a very messy running time function  We can get nit picky: Does sum += i; take one operation or two if we count the addition and the store separately?

 In short, this way of getting a running time function is almost useless because:  It cannot be used to give us an idea of how long the program really runs in seconds  It is complex and unwieldy  The most important thing about the analysis of the code that we did is learning that the growth of the function should be linear  A general description of how the running time grows as the input grows would be useful

 Enter Big Oh notation  Big Oh simplifies a complicated running time function into a simple statement about its worst case growth rate  All constant coefficients are ignored  All low order terms are ignored  3n + 3 is O(n)  Big Oh is a statement that a particular running time is no worse than some function, with appropriate constants

 147n 3 + 2n 2 + 5n is O(n3)O(n3)  n n is O(2 n )  15n 2 + 6n + 7log n is O(n2)O(n2)  659n + nlog n is O(n log n)  Note: In CS, we use log 2 unless stated otherwise

 More on Big Oh notation  Searching  Sorting

 Keep working on Project 4  Due this Friday