Presentation is loading. Please wait.

Presentation is loading. Please wait.

© A+ Computer Science - www.apluscompsci.com OOP Pieces © A+ Computer Science - www.apluscompsci.com.

Similar presentations


Presentation on theme: "© A+ Computer Science - www.apluscompsci.com OOP Pieces © A+ Computer Science - www.apluscompsci.com."— Presentation transcript:

1 © A+ Computer Science - www.apluscompsci.com
OOP Pieces © A+ Computer Science -

2 © A+ Computer Science - www.apluscompsci.com
Objects © A+ Computer Science -

3 © A+ Computer Science - www.apluscompsci.com
Object Instantiation new Scanner(System.in); new AplusBug(); When the reserved word new is combined with a constructor call, a new Object is created in memory. This process of creating a new Object in memory is called instantiation. Instantiation creates a spot for an object and sets the initial state of the object. Object state is determined by the objects instance variables. © A+ Computer Science -

4 © A+ Computer Science - www.apluscompsci.com
Instantiation AplusBug dude = new AplusBug(); dude is an AplusBug reference. new AplusBug() creates a new AplusBug Object out in memory. dude stores the location of that new AplusBug Object. new AplusBug() creates a new AplusBug object. © A+ Computer Science -

5 © A+ Computer Science - www.apluscompsci.com
References Scanner keyboard = new Scanner(System.in); AplusBug dude; dude = new AplusBug(); Typically, a reference is used to store the location of the new Object. keyboard is a Scanner reference that is storing the location of the new Scanner. © A+ Computer Science -

6 © A+ Computer Science - www.apluscompsci.com
References AplusBug dude = new AplusBug(); dude 0x234 0x234 AplusBug dude is an AplusBug reference. new AplusBug() creates a new AplusBug Object out in memory. dude stores the location of that new AplusBug Object. dude is a reference variable that refers to an AplusBug object. © A+ Computer Science -

7 © A+ Computer Science - www.apluscompsci.com
Instance Variables © A+ Computer Science -

8 © A+ Computer Science - www.apluscompsci.com
Instance Variables When you need multiple methods to have access to the same variable, make the variable an instance variable. 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 defined at the top of the class. Instance variable can be used by all methods within the class. © A+ Computer Science -

9 © A+ Computer Science - www.apluscompsci.com
Private 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 -

10 © A+ Computer Science - www.apluscompsci.com
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 -

11 © A+ Computer Science - www.apluscompsci.com
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 -

12 © A+ Computer Science - www.apluscompsci.com
Instance Variables public class Calc { private int one, two; private int answer; public void add(){ answer = one + two; } public int getAnswer(){ return answer; Instance variables are shared by all methods in a class. Instance variables are defined at the top of the class. Instance variable can be used by all methods within the class. © A+ Computer Science -

13 © A+ Computer Science - www.apluscompsci.com
Constructors © A+ Computer Science -

14 © A+ Computer Science - www.apluscompsci.com
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 -

15 Constructors vs. Methods © A+ Computer Science - www.apluscompsci.com
access name params code method 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. access return type name params code © A+ Computer Science -

16 © A+ Computer Science - www.apluscompsci.com
Constructors public Triangle() { sideA=0; sideB=0; sideC=0; } Constructors are similar to methods. Constructors set the properties of an object to an initial state. © A+ Computer Science -

17 © A+ Computer Science - www.apluscompsci.com
Default Constructor class Triangle { private int sideA, sideB, sideC; public Triangle() sideA=0; sideB=0; sideC=0; } 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 -

18 © A+ Computer Science - www.apluscompsci.com
Instance Variables class Triangle { private int sideA, sideB, sideC; public Triangle(int a, int b, int c) { sideA=a; sideB=b; sideC=c; } public String toString() return sideA + " " + sideB + " " + sideC; Instance variables are shared by all methods in a class. They are initialized in the constructor. Instance variables are defined at the top of the class. Instance variable can be used by all methods within the class. © A+ Computer Science -

19 © A+ Computer Science - www.apluscompsci.com
Constructors public Triangle(int a, int b, int c) { sideA=a; sideB=b; sideC=c; } Constructors are similar to methods. Constructors set the properties of an object to an initial state. © A+ Computer Science -

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

21 Initialization Constructor
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 -

22 Modifier & Accessor Methods
© A+ Computer Science -

23 © A+ Computer Science - www.apluscompsci.com
Modifier Methods 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 -

24 © A+ Computer Science - www.apluscompsci.com
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 -

25 © A+ Computer Science - www.apluscompsci.com
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 -

26 © A+ Computer Science - www.apluscompsci.com
Accessor Methods class Triangle { private int sideA, sideB, sideC; public Triangle(int a, int b, int c) { sideA=a; sideB=b; sideC=c; } public String toString() return sideA + " " + sideB + " " + sideC; return type return method toString() is used to display an Object. print() and println() automatically call toString() when displaying an Object reference. toString() typically sends back all data/properties from an Object as one String. © A+ Computer Science -

27 © A+ Computer Science - www.apluscompsci.com
Variable Scope © A+ Computer Science -

28 © A+ Computer Science - www.apluscompsci.com
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 -

29 © A+ Computer Science - www.apluscompsci.com
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 -

30 © A+ Computer Science - www.apluscompsci.com
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 -

31 © A+ Computer Science - www.apluscompsci.com
Local Variables 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 -

32 © A+ Computer Science - www.apluscompsci.com
LocalVars fun change fun = 99 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 -

33 Formatting Numbers

34 © A+ Computer Science - www.apluscompsci.com
Formatting Output How to format What to format out.printf( " %.2f " , ); Method printf() is used to format output. printf() is most commonly used to set the number of decimal places when displaying a real number. printf() can also be used to align output to the left or to the right. The % sign is used to indicate that a value needs to be displayed. The value will be found in the comma separated list. %f – real / decimal value %d – integer value %c – character value %s – string value - left aligned OUTPUT9.24 © A+ Computer Science -

35 © A+ Computer Science - www.apluscompsci.com
Formatting Output OUTPUT dec == 9.2 dec == 9.23 dec == 9.231 dec == dec == double dec = ; out.printf("dec == %.1f\n",dec); out.printf("dec == %.2f\n",dec); out.printf("dec == %.3f\n",dec); out.printf("dec == %.4f\n",dec); out.printf("dec == %.5f\n",dec); Method printf() is used to format output. printf() is most commonly used to set the number of decimal places when displaying a real number. printf() can also be used to align output to the left or to the right. The % sign is used to indicate that a value needs to be displayed. The value will be found in the comma separated list. %f – real / decimal value %d – integer value %c – character value %s – string value - left aligned © A+ Computer Science -

36 © A+ Computer Science - www.apluscompsci.com
Formatting Output Column size # of decimals out.printf( " %9.2f " , ); Method format() is used to format output. format() is most commonly used to set the number of decimal places when displaying a real number. format() differs from printf() in that format() is a return method and printf() is a void method. The % sign is used to indicate that a value needs to be displayed. The value will be found in the comma separated list. %f – real / decimal value %d – integer value %c – character value %s – string value - left aligned OUTPUT ? type of data © A+ Computer Science -

37 © A+ Computer Science - www.apluscompsci.com
Formatting Output double dec = ; out.println(String.format("%.3f",dec)); out.println(String.format("%12.3f",dec)); out.println(String.format("%-7.3fx",dec)); OUTPUT ? Method format() is used to format output. format() is most commonly used to set the number of decimal places when displaying a real number. format() differs from printf() in that format() is a return method and printf() is a void method. The % sign is used to indicate that a value needs to be displayed. The value will be found in the comma separated list. %f – real / decimal value %d – integer value %c – character value %s – string value - left aligned © A+ Computer Science -

38 © A+ Computer Science - www.apluscompsci.com
Formatting Output int num = 923; out.printf("%d\n", num); out.printf("%6d\n", num); out.printf("%-6d\n", num); out.printf("%06d\n", num); OUTPUT ? Method printf() is used to format output. printf() is most commonly used to set the number of decimal places when displaying a real number. printf() can also be used to align output to the left or to the right. The % sign is used to indicate that a value needs to be displayed. The value will be found in the comma separated list. %f – real / decimal value %d – integer value %c – character value %s – string value - left aligned © A+ Computer Science -

39 © A+ Computer Science - www.apluscompsci.com
Formatting Output int num = 567; out.println(String.format("%d",num)); out.println(String.format("%6d",num)); out.println(String.format("%-6d",num)); out.println(String.format("%06d",num)); OUTPUT ? Method format() is used to format output. format() is most commonly used to set the number of decimal places when displaying a real number. format() differs from printf() in that format() is a return method and printf() is a void method. The % sign is used to indicate that a value needs to be displayed. The value will be found in the comma separated list. %f – real / decimal value %d – integer value %c – character value %s – string value - left aligned © A+ Computer Science -


Download ppt "© A+ Computer Science - www.apluscompsci.com OOP Pieces © A+ Computer Science - www.apluscompsci.com."

Similar presentations


Ads by Google