FP 301 OBJECT ORIENTED PROGRAMMING FP 301 Object
FP 301 OBJECT ORIENTED PROGRAMMING FP 301 Object Oriented Programming 1. 0 • Introduction To OOP 2. 0 • Fundamentals Of Java Programming Language 3. 0 • Exception Handling 4. 0 • Classes, Inheritance And Polymorphism 1
2. 0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE 2. 2 Declaring, initializing and use variable 2
FP 301 OBJECT ORIENTED PROGRAMMING Learning Outcome Subtopic 2. 2 1 • Identify the use of variables 2 3 • Describe the four categories of Primitive Data Types • Choose an appropriate data types to represent variables 4 • Illustrate local variables and constant stored in memory 5 • Declare variables and assign value to variable 6 • Identify the variable naming convention 7 • Assign a value to a variable 8 • Declare a constant 3
FP 301 OBJECT ORIENTED PROGRAMMING VARIABLES • Is the name given to the memory location where a value is stored. • The name given to the variable is called as identifier. 4
FP 301 OBJECT ORIENTED PROGRAMMING 4 CATEGORIES OF PRIMITIVE DATA TYPES Integral Floating Point • These are positive and negative whole numbers. • Integers hold number values that cannot have a fractional part. There are four different types: • byte • short • int • long • Any number that has a fractional part. • Unlike integers, floating point numbers like fractional parts. There are two different types: • float • double 5
FP 301 OBJECT ORIENTED PROGRAMMING 4 CATEGORIES OF PRIMITIVE DATA TYPES Textual Logical • a single character. • There is only one primitive data type that deals with individual characters – char • either true or false. • As Java programs deal in logic there needs to be a way to determine when a condition is true and when it is false. The boolean data type can hold those two values 6
FP 301 OBJECT ORIENTED PROGRAMMING Choose an appropriate data type to represent variable ------------------ no. Of. Goals; ------------------ price. Of. Ball; ------------------ average. Age; ------------------ received. Status; 7
FP 301 OBJECT ORIENTED PROGRAMMING Illustrate local variables and constant stored in memory Local variable: • Variables that are defined inside a method and are called local, automatic, temporary, or stack variables • Variables that are created when the method is executed are destroyed when the method is exited • only visible to the methods in which they are declared • they are not accessible from the rest of the class Constant: • A constant is a name for a memory location that stores a value that cannot be changed from its initial assignment. Syntax : final <type> <identifier> = <value>; 8
FP 301 OBJECT ORIENTED PROGRAMMING Illustrate local variables and constant stored in memory • • Represent with a rectangle. Place value (if present) inside the rectangle. Place type and name above or beside rectangle. Example : int sum=23; int sum int 23 sum 23 • If the variable is a named constant (that is, declared with the modifier final), then the border of the rectangle is made thicker. • This thicker border is meant to suggest that the value of the constant cannot be changed. • Example : final double PI=14. 5; double 14. 5 PI 9
FP 301 OBJECT ORIENTED PROGRAMMING Cont. . • When you declare a variable, Java reserves memory locations of sufficient size to store the variable type. • The actual data values will be stored in these memory locations. • a variable can store only one value at any one time int x; x = 5; x = 10; x x 5 x 10 the value of x is 10 because this was the last value assigned to x. 10
FP 301 OBJECT ORIENTED PROGRAMMING Declare variables and assign value to variable • To initialize a variable we use an assignment statement. • To give a variable a value, the left side is the name of the variable and the right side is the value: Example : num. Players = 12; num. Players = num. Players + 2; //num. Players is assigned 12 //num. Players is now 14 11
FP 301 OBJECT ORIENTED PROGRAMMING Identify the variable naming convention Rules: • Variable identifiers must start with either an uppercase or lowercase letter, an underscore (_), or a dollar sign ($). • Variable identifiers cannot contain punctuation , spaces, or dashes. • Java technology keywords cannot be used. • Begin each variable with a lowercase letter; subsequent words should be capitalized, such as my. Variable. • Choose names that are mnemonic and that indicate to the casual observer the intent of the variable. 12
FP 301 OBJECT ORIENTED PROGRAMMING Assign a value to a variable • Example: double price = 12. 99; • Example (boolean): boolean is. Open = false; • Assigning literal values: int ID = 0; float pi = 3. 14 F; char my. Char = ’G’; boolean is. Open = false; • Assigning the value of one variable to another variable: int ID = 0; int sale. ID = ID; 13
FP 301 OBJECT ORIENTED PROGRAMMING Declare a constant • Variable (can change): double sales. Tax = 6. 25; • Constant (cannot change): final double SALES_TAX = 6. 25; • Constants should be capitalized with words separated by an underscore (_). 14
- Slides: 14