Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Object-Oriented Concepts in Java

Similar presentations


Presentation on theme: "Introduction to Object-Oriented Concepts in Java"— Presentation transcript:

1 Introduction to Object-Oriented Concepts in Java
Overview of Java Introduction to Object-Oriented Concepts in Java

2 Outline The For-Each Loop Statement Blocks Local Variable Declarations
Method Declarations Modifiers Creating & Initializing Objects

3 The For-Each Loop Finding the sum of elements in an array
int[] numbers = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31}; int sum = 0; for(int i=0; i<numbers.length; i++){ sum += numbers[i]; }

4 The For-Each Loop For each value in numbers ... Execute loop
int[] numbers = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31}; int sum = 0; for(int value:numbers){ sum += value; } For each value in numbers ... Execute loop For-Each operates on one array at a time

5 The For-Each Loop Self-Test
Write a for-each loop to find the average of two arrays defined as follows. double[] fuji = new double[MAX]; double[] gala = new double[MAX]; Assume that the arrays have ben populated with valid data.

6 Statement Blocks In Python statement blocks are tabbed
if n > 6:#assuming n and sum are properly defined sum += n print(n) n +=1 In Java statement blocks are enclosed in opening and closing braces. if(n>6){ //assuming n and sum are properly defined sum += n; print(n); n +=1; }

7 Local Variable Declaration
Local variable declarations can occur anywhere in a statement block, typically in the body of a method. Local variable have local scope, i.e., they are visible only within the block. The scope of a local variable begins at its declaration and extends to its enclosing statement block. Local variable must be initialized before use. All variables must be.

8 Local Variable Declaration
public class LocalVariable{ public void methodA(int num){ int sum = 0;//visible anywhere in the method for(int i=0; i<7; i++){//i is visible in block int val = 3;//visible only in this block sum += i*val; } System.out.println("Sum is: " + sum); System.out.println(“Error!!" val + “ is not visible”); public static void main(String[] args) { }//end of main }//end of class

9 Local Variable Declaration

10 Class Declarations [ClassModifiers] class ClassName
[extends SuperClass] [implements Interface1, Interface2, …] { ClassMemberDeclarations } ClassModifiers = public/abstract/final ClassModifiers is never private except for inner classes.

11 Method Declarations [MethodModifiers] ReturnType MethodNameName ([ParameterList]){ Statements } Declare a method action that take an integer and a string parameter and returns the number of characters in the string added to the integer parameter.

12 Modifiers That can be applied to method, field/variable, and inner class declarations: <none> default to package public any class can access public members protected the class and its subclasses private the class itself static shared by all class instances. A static method accesses only static members. A static nested class does not have implicit reference to enclosing class. final a final field has a constant value which cannot be changed. A final method cannot be overridden in subclasses.

13 Other Modifiers For methods:
abstract defers implementation to subclasses synchronized used in multithreading environment native implemented in C/C++

14 Other Modifiers For fields/variables:
volatile may be modified by non-synchronized methods in a multi-threading environment transient is not part of the persistent state of instances

15 Creating & Initializing Objects
Write the class declaration that correspond with the following pieces of code final int MAX = 100; double[] values = {4.4, 5.5, 6.6, 7.7, 8, 9, 12.8}; String name = “James Truly”; Account obj = new Account(MAX, values, name); double[] values = obj.getValues(MAX);//returns an array with MAX numbers. 15

16 Creating & Initializing Objects
Write code to initialize the class defined below: public class SomeClass { double num; int val; String[] names; public SomeClass(double num, int val){ this.num = num; this.val = val; } public SomeClass(String[] names, double num, int val){ this.names = names;//shallow copy 16

17 Class (Static) Fields and Methods

18 Object Reference this

19 Wrapper Classes

20 Abstract Classes

21 Packages Using packages Directory structure Java Class Library

22 Exceptions Sources of Exceptions Hierarchy of Exceptions
Throwing exceptions Catching and Handling Exceptions

23 The End Qu es ti ons? ______________________ Devon M. Simmonds
Computer Science Department University of North Carolina Wilmington _____________________________________________________________

24 Arithmetic Operators General Assignment operators Increment/Decrement
+ addition - subtraction * Multiplication / division % remainder/modulus Assignment operators +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |= Increment/Decrement ++, --

25 Bitwise Operators ~x x & y x | y x ^ y Complement of x
bitwise and of x and y x | y bitwise inclusive or of x and y x ^ y bitwise exclusive or of x and y

26 Shift Operators x << k = x*2k x >> k = x/2k
Shifts the bits in x k places to the left x >> k = x/2k Shifts the bits in x, k places to the right, filling in with the highest bit (the sign bit) on the left-hand side. x >>> k Shifts the bits in x k places to the right, filling in with 0 bits on the left-hand side.

27 Logical Operators Translating logic into Java AND && OR || XOR ^ NOT !

28 Relational Operators == != < > <= >=

29 Selection Statements if statements

30 Loop Statements if statements

31 The break & continue Statements
if statements

32 The End Qu es ti ons? ______________________ Devon M. Simmonds
Computer Science Department University of North Carolina Wilmington _____________________________________________________________

33 Ss

34

35 Example of a Java Class public class Account implements IAccount {
private String accName; private int accNumber; private double balance; public boolean deposit(double amount){ if(amount > 0){ this.balance += amount; return true; } return false; public boolean withdraw(double amount){ if(this.balance >= amount){ this.balance -= amount; }//end of class Example of a Java Class

36 How Java Works See interpreted-or-compiled-programming-language.html interpreted-What-is-the-difference-What-is-the-JIT-compiler


Download ppt "Introduction to Object-Oriented Concepts in Java"

Similar presentations


Ads by Google