Introduction to Object-Oriented Concepts in Java

Slides:



Advertisements
Similar presentations
COSC 2006 Data Structures I Instructor: S. Xu URL:
Advertisements

Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Java Language and SW Dev’t
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
The Java Programming Language
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Methods. 2 A sequence of statements can be packaged together as a unit and re-used. A method is a named unit of re-usable code. modifier returnType methodName(
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Information and Computer Sciences University of Hawaii, Manoa
Modern Programming Tools And Techniques-I
JAVA MULTIPLE CHOICE QUESTION.
Chapter 7 User-Defined Methods.
Working with Java.
Exceptions, Interfaces & Generics
Multiple variables can be created in one declaration
University of Central Florida COP 3330 Object Oriented Programming
Programming Language Concepts (CIS 635)
Starting JavaProgramming
null, true, and false are also reserved.
Java Programming Language
Introduction to Java Programming
An Introduction to Java – Part I, language basics
An overview of Java, Data types and variables
Classes & Objects: Examples
The Building Blocks Classes: Java class library, over 1,800 classes:
Group Status Project Status.
Chapter 1: Computer Systems
Units with – James tedder
Units with – James tedder
Focus of the Course Object-Oriented Software Development
Instructor: Alexander Stoytchev
Introduction to Object-Oriented Concepts in Java
The Object-Oriented Thought Process, Chapter 1
Chap 2. Identifiers, Keywords, and Types
Final and Abstract Classes
Introduction to java Part I By Shenglan Zhang.
Introduction to Object-Oriented Concepts
Presentation transcript:

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

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

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]; }

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

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.

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; }

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.

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

Local Variable Declaration

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

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.

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.

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

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

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

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

Class (Static) Fields and Methods

Object Reference this

Wrapper Classes

Abstract Classes

Packages Using packages Directory structure Java Class Library

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

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

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

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

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.

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

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

Selection Statements if statements

Loop Statements if statements

The break & continue Statements if statements

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

Ss

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

How Java Works See https://javarevisited.blogspot.com/2014/06/is-java- interpreted-or-compiled-programming-language.html https://www.quora.com/Is-Java-a-compiled-language-or- interpreted-What-is-the-difference-What-is-the-JIT-compiler