Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 9 Using Objects.

Similar presentations


Presentation on theme: "Lecture 9 Using Objects."— Presentation transcript:

1 Lecture 9 Using Objects

2 Remember: 3 Different Kinds of Classes
Application Class – what we've been doing Has public static void main ( String [] args) Instantiable Class – use to make objects from String, Scanner Classes with only static utility methods like Math class, Coin class, WordUtil class

3 Today—Instantiable Classes
Instantiable Class – used to make objects from SO FAR ONLY: String, Scanner To use an Instantiable class we must Declare a variable of that class type String message; Scanner myUserInput; Instantiate a new object message = new String("hello"); // message="hello“ is OK myUserInput = new Scanner(System.in); Use associated methods (name of object '.' method) message.trim(); myUserInput.nextInt();

4 Instantiable Classes make Objects
Objects are more complex than primitives Primitive values are just pieces of data (numbers, characters, or true/false) For example, 2.3 is a value of type double Instances of classes (objects), on the other hand, contain both data and ways of acting on that data For example, a Point object has an x and y coordinate, but also comes with a way to translate the point by giving the amount of change desired in the x and y coordinates

5

6

7 Review: Primitives int x; x = 3; int y=x; x = y+2; y = x
Review: Primitives int x; x = 3; int y=x; x = y+2; y = x*3; double z; z = 3.45; z = z + x;

8

9 Object (reference) variables

10

11

12 Demo: Point Class Represents a point in 2D
Used in lots of computer graphics Stores the x and y value Has methods to modify point values

13 Example: Objects of class Point
(download demo file) Point myPoint = new Point(2,-3); //This declares the variable origin, and sets it to (0,0) Point origin = new Point(); //At this point in time, origin is (0,0) //The next line causes origin to change itself to 7,12 origin.translate(7, 12); //The next line causes origin to change itself to 10,17 origin.translate(3, 5); //Prints out: java.awt.Point[x=10,y=17] System.out.println(origin);

14 Memory Maps – Primitives vs Objects
int x = 24 double y = 34.4 Point p; p= new Point(14,35); how to change p to (20,40)? Point q = new Point(-10,29); (p contains "null")

15 Recap An instance of a class is called an object
To make a new object, use the new operator The actions you can invoke on an object are called its methods To use a method, we use dot notation as in the example To find out the details of how to use a particular class, we use the javadoc documentation

16 Lab: Particle Class Represents a rock or ball subject to gravity
Used in the golf game coming next week Helps us find out how high ball is after so many seconds have elapsed The rock started meters off the ground. After 2.63 seconds, the rock was meters off the ground.


Download ppt "Lecture 9 Using Objects."

Similar presentations


Ads by Google