Week 11 - Friday
What did we talk about last time? Object methods Accessors Mutators Constructors Defining classes
An object is the actual data that you can use in your code A class is a template whereby you can create objects of a certain kind Class =Car Object=Mitsubishi Lancer Evolution X Just like int is a type and 34 is an instance of that type A key difference is that you can define new classes
When designing classes, they contain two kinds of elements: Members Methods
public class Name {private int member1; private double member2; private Hedgehog member3; public Name() { … } public int method1( double x ) { … } Class definition Member declarations Constructor definition Method definition
Let’s create a Student class Each Student will contain First Name: String Last Name: String GPA: double ID: int We need a constructor specifying each of these things, accessors to read them later, and mutators to change GPA and ID
Using the Student class, we can create a roster of students The roster is just an array of Student objects We’ll read in some student information from a file and create lots of Student objects Then, with our array, we can do some operations: Print out a nicely formatted roster Find the average GPA of all students Determine the valedictorian
We can make a Ball class Each Ball will have: Color: Color x Location: double y Location: double Radius: double x Velocity: double y Velocity: double We will need a constructor specifying these values and a method to update the position of the ball over time
With the Ball class created, it’s easy to make a program that shows balls bouncing all over the screen We simply create an array of Ball objects and update them repeatedly
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
Finish object examples Class variables and constants Big Oh notation
Keep working on Project 4 Due next Friday