Download presentation
Presentation is loading. Please wait.
Published byJade Smith Modified over 9 years ago
1
Week 12 - Wednesday
2
What did we talk about last time? Hunters and prey
6
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
7
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
8
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
10
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?
11
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; }
12
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
14
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
15
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! }
16
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
17
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; } }
19
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
20
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
21
147n 3 + 2n 2 + 5n + 12083 is O(n3)O(n3) n 1000 + 2 n is O(2 n ) 15n 2 + 6n + 7log n + 145 is O(n2)O(n2) 659n + nlog n + 87829 is O(n log n) Note: In CS, we use log 2 unless stated otherwise
23
How long does it take to do multiplication by hand? 123 x 456 738 615 492__ 56088 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 )
24
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]);
25
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; }
27
Sorting Lab 12
28
Finish Project 4 Due Friday before midnight
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.