Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.

Similar presentations


Presentation on theme: "Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes."— Presentation transcript:

1 Week 11 - Friday

2  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes

3

4

5

6

7  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

8  When designing classes, they contain two kinds of elements:  Members  Methods

9 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

10

11  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

12  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

13  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

14  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

15  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

16  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

17  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

18

19

20  Finish object examples  Class variables and constants  Big Oh notation

21  Keep working on Project 4  Due next Friday


Download ppt "Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes."

Similar presentations


Ads by Google