Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.

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

MATH 224 – Discrete Mathematics
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
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.
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 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
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.
Introduction to Analysis of Algorithms
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Complexity Analysis (Part I)
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
Loops We have been using loops since week 2, our void draw(){ } is a loop A few drawbacks of draw() –It is endless –There is only one draw() –It updates.
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.
CSE 1301 J Lecture 13 Sorting Richard Gesick. CSE 1301 J 2 of 30 Sorting an Array When an array's elements are in random order, our Sequential Search.
Java Unit 9: Arrays Declaring and Processing Arrays.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
One Dimensional Array. Introduction to Arrays Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
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.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Week 15 – Friday.  Student Questions  Review up to Exam 2  Loops  Arrays  StdDraw  StdAudio  Static methods.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Week 12 - Friday.  What did we talk about last time?  Finished hunters and prey  Class variables  Constants  Class constants  Started Big Oh notation.
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.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
COP 3540 Data Structures with OOP
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Copyright 2010 by Pearson Education Homework 8: Critters reading: HW8 spec.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
CS 116 Object Oriented Programming II Lecture 4 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Week 2 - Wednesday CS221.
Introduction to Analysis of Algorithms
Week 4 - Friday CS221.
Week 13: Searching and Sorting
Recitation 13 Searching and Sorting.
Introduction to Algorithms
Week 14 - Monday CS 121.
CS 177 Week 15 Recitation Slides
Introduction to Data Structures
Homework 8: Critters (cont.)
Arrays in Java What, why and how Copyright Curt Hill.
CS 201 Fundamental Structures of Computer Science
2 code samples int [] array; int i, j, temp; for(i = 0; i < array.length/2; i++) { j = array.length-1-i; temp = array[i]; array[i] = array[j]; array[j]
Homework 8: Critters (cont.)
Building Java Programs
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Week 12 - Wednesday

 What did we talk about last time?  Hunters and prey

 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

 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

 How long does it take to do multiplication by hand? 123 x __  Let’s assume that the length of the numbers is n digits  (n multiplications + n carries) x n digits + (n + 1 digits) x n additions  Running time: O(n 2 )

 How do we find the largest element in an array?  Running time: O(n) if n is the length of the array  What if the array is sorted in ascending order?  Running time: O(1) int largest = array[0]; for( int i = 1; i < array.length; i++ ) if( array[i] > largest ) largest = array[i]; System.out.println("Largest: " + largest); int largest = array[0]; for( int i = 1; i < array.length; i++ ) if( array[i] > largest ) largest = array[i]; System.out.println("Largest: " + largest); System.out.println("Largest: " + array[array.length-1]);

 Here is some code that sorts an array in ascending order  What is its running time?  Running time: O(n 2 ) for( int i = 0; i < array.length; i++ ) for( int j = 0; j < array.length - 1; j++ ) if( array[j] > array[j + 1] ) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } for( int i = 0; i < array.length; i++ ) for( int j = 0; j < array.length - 1; j++ ) if( array[j] > array[j + 1] ) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; }

 Sorting  Lab 12

 Finish Project 4  Due Friday before midnight