Download presentation
Presentation is loading. Please wait.
Published byCody Campbell Modified over 9 years ago
2
Introduction to Objects A way to create our own types
3
Type and Variables Until this point we were only able to create simple types, actually call “primitive” types –integer –double –float –char –String (actually not primitive)
4
We want to do better Bank Account Math Calculator Stock Car Vending machine Telephone Model “whatever” as software Make a computer act like one
5
Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();
6
Consider methods Take a known process Package it for reuse if ((a >= b) && (a>=c)) max = a else if ((b >= a) && (b>=c)) max = b else max = c; int maxof3(int a, int b, int c){ int max; if ((a >= b) && (a>=c)) max = a else if ((b >= a) && (b>=c)) max = b else max = c; return max; }
7
How are object like this? Take a group of methods and data and package those for reuse. a( ) b( ) c( ) int i int k a( ) b( ) c( ) int i int k class Testclass
8
int i; int j; void a() {…} void b() {…} void c() {…} class TestClass{ int i; int j; void a() {…} void b() {…} void c() {…} }
9
How do we decide what goes inside? The problem will guide us. The things we put inside will define –What the object will do – How we can interact with it These things will be the “Bank Account”s, “Student”s, etc
10
Let’s start simple A circle
11
What are some of the attributes of a circle? Radius (most obvious) Color Border Position
12
How do we interact with a circle? Change its size Move it Ask it for its area … depending on the problem’s needs
13
Let’s start with a simple Circle class Just a radius –No borders or colors A means of asking it for its area. This will serve as the basis (a type or class) for creating lots of circles
14
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; }
15
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } Heading for the class
16
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } A property of each circle
17
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } A method named Area that will calculate the area of that specific circle
18
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } A constructor Used to initialize the circle Let’s see how in the next slide
19
Creating circles Circle circle1 = new Circle(10); Radius:10 circle1
20
Creating circles Circle circle1 = new Circle(10); radius:10 circle1 Circle circle2 = new Circle(15); radius:15 circle2
21
Asking about area Circle circle1 = new Circle(10); System.out.println(circle1.Area()); radius:10 circle1 Circle circle2 = new Circle(15); System.out.println(circle2.Area()); radius:15 circle2 Each circle will return it’s own area
22
What’s the difference? Circle circle1 = new Circle(10); Circle circle1; Creates a REFERENCE Like having a telephone number for a friend.. a means to find them. But this one is a contact without a number.
23
What’s the difference? Circle circle1 = new Circle(10); Circle circle1; Creates the object and defines the reference to the object In this case, circle1 actually refers to a real Circle.
24
Only a reference.. No circle Circle circle1; circle1
25
A reference with a circle Circle circle1 = new Circle(10); Radius:10 circle1
26
Put it Together! public class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String[ ] args) { Circle circle1 = new Circle(10); System.out.println(circle1.Area()); Circle circle2 = new Circle(15); System.out.println(circle2.Area()); } 314.1562.. 706.8583..
27
Do I need new() ? Can I define a variable and just reference from the main? How about this? … TRY IT… public class Circle() { double radius=5; public static void main(String args[]) { System.out.println(radius); } Error: non-static variable radius cannot be referenced from a static context You never “new()”ed one. No radius exists.
28
Only a reference.. No circle Circle circle1; circle1
29
Do I need new() ? Here there is no Area() or radius defined. Because NO new() has occurred! public class Circle() { double radius=5; double Area() { double this area = radius*radius*Math.PI; } public static void main(String args[]) { System.out.println(Area()); } Error : nonstatic method can not be referenced from static method
30
This one creates the object. Then a radius and Area() exists to use… no errors. public class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { Circle circle1 = new Circle(10); System.out.println(circle1.Area()); Circle circle2 = new Circle(15); System.out.println(circle2.Area()); } These are created when you new()
31
public class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { int i = 5; System.out.println(i); Circle circle1 = new Circle(10); System.out.println(circle1.Area()); } Why can I declare “i” like this in the main, but not radius in the previous example? This is legal!
32
public class Circle { double radius; Circle(double r) { radius = r; } double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { int i = 5; System.out.println(i); Circle circle1 = new Circle(10); System.out.println(circle1.Area()); } Because main is static. Static needs more explanation.
33
Conclusion: “ new ” before using static is coming!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.