Download presentation
Presentation is loading. Please wait.
1
Lesson A4 – Object Behavior
References from A+ Computer Science PowerPoints
2
Objects © A+ Computer Science -
3
Object Instantiation Chicken yeller = new Chicken();
yeller is a Chicken reference. new Chicken() creates a new Chicken Object out in memory. yeller stores the location of that new Chicken Object. © A+ Computer Science -
4
Object Instantiation Chicken yeller = new Chicken(); yeller
0x234 0x234 Chicken yeller is a Chicken reference. new Chicken() creates a new Chicken Object out in memory. yeller stores the location of that new Chicken Object. yeller is a reference variable that refers to a Chicken object. © A+ Computer Science -
5
Methods © A+ Computer Science -
6
What is a method? A method is a storage location
for related program statements. When called, a method usually performs a specific task. System.out.println( ) Methods store commands / program statements. When called, the code inside the method is activated. © A+ Computer Science -
7
What methods have we used?
dude.goHome() keyboard.nextInt( ) System.out.println( ) © A+ Computer Science -
8
out.println("cluck-cluck"); }
methods public void speak() { out.println("cluck-cluck"); } The speak method shown above contains a single println command. The speak method would print out cluck-cluck on the console window. OUTPUT cluck-cluck © A+ Computer Science -
9
methods public void speak( ) { System.out.println("cluck-cluck"); }
access return type name params code public void speak( ) { System.out.println("cluck-cluck"); } A method has a signature. The signature provides information about the method. The name is most used and recognizable part of the signature. The method shown above is named print. The return type states what the method will return. Method print has a return type of void which means the method does not return a value. The access of method print is public. This states that the method print can be called from any location. © A+ Computer Science -
10
All members with public access can be accessed or
What does public mean? All members with public access can be accessed or modified inside and outside of the class where they are defined. Public access simply means the member can be used anywhere inside or outside of the class. © A+ Computer Science -
11
chicken OUTPUT public class Chicken { public void speak()
out.println("cluck-cluck"); } public static void main(String[] args) Chicken red = new Chicken(); red.speak(); OUTPUT cluck-cluck In the Chicken example, method speak() prints out cluck-cluck each time it is called. Method speak() is called three times; thus, it prints out cluck-cluck three times. OUTPUT cluck-cluck © A+ Computer Science -
12
Variables © A+ Computer Science -
13
Instance Variables When you need many methods
to have access to the same variable, make the variable an instance variable / instance field. The scope of an instance variable is the entire class where the variable is defined. An instance variable is a variable tied to an instance of a class. Each time an Object is instantiated, it is given its own set of instance variables. Instance variables are also commonly called instance fields. Monster x = new Monster(); x refers to a unique Monster that contains its own set of Monster instance variables. © A+ Computer Science -
14
Instance Variables OUTPUT 11 public class InstanceVars {
private int one = 8, two = 3; //instance variables / fields private int answer = 0; //exist throughout the class public void add(){ answer = one + two; } public void print(){ System.out.println(answer); public static void main(String args[]) InstanceVars test = new InstanceVars(); test.add(); test.print(); OUTPUT 11 Class InstanceVars contains three instance variables : one, two, and total. Each time class InstanceVars is instantiated, a new set of instance variables is created inside of the new Object. InstanceVars test = new InstanceVars(); test refers to an InstanceVars Object that contains one, two, and three. InstanceVars diff = new InstanceVars(); diff refers to an InstanceVars Object that contains one, two, and three. © A+ Computer Science -
15
InstanceVars one two answer 8 3 0
add print All methods in class InstanceVars can access instance vars one, two, and answer. © A+ Computer Science -
16
All members with private access can be accessed
What does private mean? All members with private access can be accessed or modified only inside the class where they are defined. All members of a class with private access can be accessed or modified within the class where they are defined only. Private members cannot be accessed outside of the class. © A+ Computer Science -
17
All data members should have private access. A set of public
Encapsulation All data members should have private access. A set of public methods should be provided to manipulate the private data. Data should be declared with private access and public methods should be provided to manipulate the private data. © A+ Computer Science -
18
defining parameters © A+ Computer Science -
19
defining parameters public void times( int num1, int num2 ) {
out.println(num1*num2); } There will be times that we define parameters when we define a method. The parameters allow us to specify the type of data the method will receive. Methods are often defined with a parameter list. Parameters are defined within the parenthesis following the method name. public void method( parameter list ) When defining parameters, a data type and name must be provided for each parameter. public void method(int one, double two) public void go(String word, int num) © A+ Computer Science -
20
passing parameters OUTPUT15 //code in main in another class
Fun test = new Fun(); test.times(3 , 5); OUTPUT15 public class Fun { public void times( int num1, int num2 ) out.println(num1*num2); } When calling a method with parameters, the data types and number of parameters are very important. public void method(int one, double two) A call to method would have to have 2 parameters. A call to method would require passing in an integer and a double in that order. method(6, 9.3); method(562, ); © A+ Computer Science -
21
passing parameters OUTPUT15 //code in main in another class
public class Fun { public static void times( int one, int two ) out.println(one*two); } When calling a method with parameters, the data types and number of parameters are very important. public static void times(int one, double two) times is defined as static. A static method exists without the need for an instantiation of the class. Notice that there is no Fun x = new Fun() line in the main example. Fun.times(6, 9); Fun.times(11,22); //code in main in another class Fun.times(3 , 5); © A+ Computer Science -
22
Variable Scope © A+ Computer Science -
23
Scope { int fun = 99; } Any variable defined inside of braces,
only exists within those braces. That variable has a scope limited to those braces. When a variable is defined within a set of braces, that variable can only be accessed inside those braces. © A+ Computer Science -
24
Instance Variables When you need many methods
to have access to the same variable, you make that variable an instance variable. The scope of an instance variable is the entire class where that variable is defined. An instance variable is a variable tied to an instance of a class. Each time an Object is instantiated, it is given its own set of instance variables. Monster x = new Monster(); x would refer to a new Monster that contains its own set of Monster instance variables. © A+ Computer Science -
25
Instance Variables OUTPUT 11 public class InstanceVars {
private int one = 8, two = 3; //instance variables private int total = 0; //exist throughout the class public void add(){ total = one + two; } public void print(){ System.out.println(total); public static void main(String args[]) InstanceVars test = new InstanceVars(); test.add(); test.print(); OUTPUT 11 Class InstanceVars contains three instance variables : one, two, and total. Each time class InstanceVars is instantiated, a new set of instance variables is created. InstanceVars test = new InstanceVars(); test refers to an InstanceVars Object that contains its own set of one, two, and three. InstanceVars diff = new InstanceVars(); diff refers to an InstanceVars Object that contains its own set of one, two, and three. © A+ Computer Science -
26
Defining VS. Assigning int num; int num = 99; num = 56;
definition only definition and assignment When defining a variable, the type must be listed and the name. When defining and assigning a variable, the type, name, and value must be listed. When assigning a variable only, the name and value must be listed. assignment only © A+ Computer Science -
27
Local Variables When you need only one method
to have access to a variable, you should make that variable a local variable. The scope of a local variable is limited to the method where it is defined. © A+ Computer Science -
28
Local Vars OUTPUT public class LocalVars {
private int fun; //instance variable public void change() { int fun = 99; //local variable } public void print() { System.out.println(fun); public static void main(String args[]) LocalVars test = new LocalVars(); test.change(); test.print(); OUTPUT Class LocalVars has an one instance variable named fun. Method change contains a local variable named fun. Local variables take precedence over instance variables. Local variables can only be used inside the method in which they are defined. © A+ Computer Science -
29
fun = 99 LocalVars fun 0 change print
Class LocalVars has an one instance variable named fun. Instance variable fun can be used anywhere in class LocalVars. Method change contains a local variable named fun. Method change does not make any changes to the instance variable fun. Local variable fun can only be used in method change. © A+ Computer Science -
30
Constructors © A+ Computer Science -
31
very similar to methods have the same name as the class
Constructors very similar to methods have the same name as the class have no return type – no void,int,etc. initialize all instance variables Constructors are used to initialize all of the data. Typically, all variables are assigned a value in the constructor. A constructor is very similar to a method, but it is technically not a method. Constructors have no return type and are named the same as the class. This is slightly different from a method. © A+ Computer Science -
32
Constructors VS. Methods
access name params code Constructors and methods are more similar than different and sometimes constructors are referred to as constructor methods. Most of the differences are not visible. The only real visible differences exist in that the constructor has to be named the same name as the class and that the constructor has no return type. Methods and constructors both have a name, a list of parameters, and a block of code to execute. When you call a method or a constructor, the block of code associated with the name will be executed. method access return type name params code © A+ Computer Science -
33
Triangle triangle = new Triangle();
class Triangle { private int sideA, sideB, sideC; public Triangle() sideA=0; sideB=0; sideC=0; } Default Constructor Default constructors have no parameter list. When a default constructor is called, all instance variables / data fields are set to a zero value. If no constructors are provided for a class, Java will provide a default constructor that will initialize all data to a zero value. Triangle triangle = new Triangle(); © A+ Computer Science -
34
Initialization Constructor
public Triangle(int a, int b, int c) { sideA=a; sideB=b; sideC=c; } Constructors often have parameters. The parameters allow data to be passed into the class so that it can be assigned to the instance variables / data fields. Initialization constructors have a parameter list and will receive parameters when called. The number of parameter and types passed in must match up with the parameter list following the method name. © A+ Computer Science -
35
Initialization Constructor Triangle triangle = new Triangle(3,4,5);
class Triangle { private int sideA, sideB, sideC; public Triangle(int a, int b, int c) sideA=a; sideB=b; sideC=c; } Initialization constructors have a parameter list and will receive parameters when called. The number of parameter and types passed in must match up with the parameter list following the method name. Triangle triangle = new Triangle(3,4,5); © A+ Computer Science -
36
return methods © A+ Computer Science -
37
Return Methods int num = keyboard.nextInt();
Return methods perform some action and return a result back to the calling location. int num = keyboard.nextInt(); Return methods typically take in some type of data, do something to the data, and then send back a result. nextInt() is a Scanner return method. nextInt() retrieves the next integer value from the keyboard and sends it back to num. nextInt() returns an int back to the calling location. The value returned is assigned to num. © A+ Computer Science -
38
Return Methods INPUT 1 OUTPUT 1 return method num 1 Scanner keyboard =
new Scanner(System.in); int num = keyboard.nextInt(); out.println(num); INPUT 1 OUTPUT 1 nextInt() gets the next int entered on the keyboard and returns it. The int returned by nextInt() is placed in num. return method num 1 © A+ Computer Science -
39
Return Method access return type name params code
Return methods have a return type specified before the method name. public int getNum() getNum() returns an integer value. public double getStuff() getStuff() returns a double/decimal value. © A+ Computer Science -
40
Math return methods © A+ Computer Science -
41
frequently used methods
Math frequently used methods Name Use floor(x) rounds x down ceil(x) rounds x up pow(x,y) returns x to the power of y abs(x) returns the absolute value of x sqrt(x) returns the square root of x round(x) rounds x to the nearest whole number min(x,y) returns smallest of x and y max(x,y) returns biggest of x and y random() returns a double >=0.0 and < 1.0 The Math class contains many useful math related methods. © A+ Computer Science -
42
Math Methods INPUT 3.45 OUTPUT 4.0 num 3.45 return methods
Scanner keyboard = new Scanner(System.in); double num = keyboard.nextDouble(); out.println(Math.ceil(num)); INPUT 3.45 OUTPUT 4.0 Math methods are return methods. First, a value is passed to a math method. Second, the math method performs some action. Finally, the math method returns a result. num 3.45 return methods © A+ Computer Science -
43
Math Methods OUTPUT out.println(Math.floor(3.254)); out.println(Math.ceil(2.45)); out.println(Math.pow(2,7)); out.println(Math.abs(-9)); out.println(Math.sqrt(256)); out.println(Math.sqrt(144)); out.println(Math.round(3.6)); out.println(Math.max(5,7)); out.println(Math.max(5,-7)); out.println(Math.min(5,7)); out.println(Math.min(5,-7)); floor(val) returns val decreased to the nearest interger. ceil(val) returns val increased to the nearest interger. pow(x,y) reutrns x raised to the power of y abs(val) returns the absolute value of val sqrt(val) returns the square root of val round(val) returns val rounede to the nearest integer max(one, two) returns the largest of one and two min(one, two) returns the smallest of one and two © A+ Computer Science -
44
Math Methods out.println(Math.random()*10); int num = (int)(Math.random()*10); out.println(num); OUTPUT random() returns a random number between 0.0 and 1.0 not including 1.0. random() returns a double in the range 0.0 to 1.0, not including 1.0. © A+ Computer Science -
45
Modifier methods are methods that change the properties of an object.
Modifier methods make changes to the instance variables of the class. © A+ Computer Science -
46
modifier methods OUTPUT 13 public class Calc { private int one, two;
private int answer; public void setNums( int n1, int n2 ){ one=n1; two=n2; } public void add(){ answer = one + two; public void print(){ System.out.println(answer); test.setNums(4,9); test.add(); test.print(); Modifier methods make changes to the instance variables of the class. Method setNums() assigns parameter n1 to instance variable one and parameter n2 to instance variable two. The purpose of method setNums() is to modify the instance variables / instance fields. OUTPUT 13 © A+ Computer Science -
47
Pieces of the OOP Puzzle Part Two
© A+ Computer Science -
48
constructors Default Constructor public Triangle() { sideA=0; sideB=0;
sideC=0; } Default Constructor Constructors are similar to methods. Constructors set the properties of an object to an initial state. © A+ Computer Science -
49
constructors Initialization Constructor
public Triangle(int a, int b, int c) { sideA=a; sideB=b; sideC=c; } Initialization Constructor Constructors are similar to methods. Constructors set the properties of an object to an initial state. © A+ Computer Science -
50
modifier methods public void setSides(int a, int b, int c) { sideA=a;
sideB=b; sideC=c; } Modifier methods are methods that change the properties of an object. © A+ Computer Science -
51
accessor methods public void print() {
out.println(sideA + " " + sideB + " " + sideC); } Accessor methods are methods that retrieve or grant access to the properties of an object, but do not make any changes. © A+ Computer Science -
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.