Java 1234319 CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: 1990. Inspired by C++ programming language.

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

Data Structures Lecture 2 Fang Yu Department of Management Information Systems National Chengchi University Fall 2011.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Inheritance Inheritance Reserved word protected Reserved word super
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Java and C++, The Difference An introduction Unit - 00.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Pascal Course Spring Introduction Designed: 1968/9 by Niklaus Wirth Published: 1970 Imperative, structural, procedural Static and strong.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming: Java Edition By: Samuel Robinson.
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.
Chapter 2: Everything is an Object ● C++ has many non object oriented features inherited from C. It is a hybrid language meaning that it support different.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Pascal CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing.
Programming in Java CSCI-2220 Object Oriented Programming.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
Object-Oriented Programming Chapter Chapter
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
A Quick Java Course part 1 Michael McDougall CIS 573 September 27 th, 1999.
Interfaces and Inner Classes
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Topics Instance variables, set and get methods Encapsulation
OOP Basics Classes & Methods (c) IDMS/SQL News
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
ISBN Chapter 12 Support for Object-Oriented Programming.
Object Oriented Programming Lecture 2: BallWorld.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Examples of Classes & Objects
Inheritance and Polymorphism
Agenda Warmup AP Exam Review: Litvin A2
Pascal Winter 2010/ Course.
CS360 Windows Programming
Interface.
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Java Programming Language
Classes & Objects: Examples
Chap 1. Getting Started Objectives
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Java CourseWinter 2009/10

Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language. Improvements: –Syntax –General purpose libraries –Garbage collection –Platform independence with ‘Java Virtual Machine’ Performance? 2Winter 2009/ Course

OOP Basics Java programs define classes, and objects that are instantiated from them. –All methods (functions) are class members. –Variables are class members, or local function variables. –No multiple inheritance But can implement multiple interfaces (learn in OOP course). –Classes can be nested. These definitions allow us to hide away the technical details inherent to imperative programming. –set value to X, read value from Y, etc… 3Winter 2009/ Course

No Pointers in Java Another way to hide away the technical details –No pointers arithmetic, no pointers to functions, no memory management, etc. Things you could do in C++ int a[10], b[10]; for (int i = 0; i < 20; i++) { a[i] = 0; } But also int a[10]; for (int i = 0; i <= 10; i++) a[i] = some_function(i); 4Winter 2009/ Course When will the problem be detected?

Java has a safe array access Yet another way to hide away the gory details Every line like a[i] = 7; is compiled into: if (i 9) BOOM! else a[i] = 7; BOOM is actually an IndexOutOfBoundsException instance being thrown. C/C++ compilers cannot add such checks to generated code. 5Winter 2009/ Course

Java performance Historically, Java's performance is considered slow. Two reasons: –Code runs on Virtual Machine –Garbage collector In reality, modern JVMs are very powerful, and can optimize code aggressively. Java programs often have slow start-up times, but after that, performance is often on-par with C++ code. 6Winter 2009/ Course

Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } 7Winter 2009/ Course

Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } The class keyword is used to define classes By convention, class names begin with capital letter All code and variables in Java reside in classes If no parent was explicitly specified, the new class is derived from the java.lang.Object class 8Winter 2009/ Course

Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } The method is public –Same meaning as in C++ Unlike C++, in Java the access level is specified explicitly before each and every member variable and method 9Winter 2009/ Course

Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } The method is static –Same meaning as in C++ –Can be accessed without a class instance static can be applied both to methods and member variables 10Winter 2009/ Course

Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } We are defining a void method –Meaning it does not return a value –Like void functions in C++, or procedures in Pascal 11Winter 2009/ Course

Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } The method is called “main” Unlike in C++, there’s nothing special about the name “main” by itself It is only in combination with the other properties of this method that it gets some special meaning –As the program’s starting point, of course 12Winter 2009/ Course

Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } Function accepts a single formal argument The argument’s name is “args” The argument’s type is an array of String s 13Winter 2009/ Course

Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } System is the name of a class –From the java.lang package This class contains many basic system-related objects –Such as the console I/O objects 14Winter 2009/ Course

Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } The class System has a member field called out –of type PrintStream –It is static, so we can access it without an instance of the System class –It is also public, otherwise we would have been unable to access it 15Winter 2009/ Course

Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } The out object, just like any instance of PrintStream, has a method called println Can you guess what this method does? 16Winter 2009/ Course

Remember simple problem? Given a range of positive numbers: –Summarize all numbers in range that divide by 3 or 5. –Print the result. 17Winter 2009/ Course

Version 1 public class Sum { private int sum; public Sum() { sum = 0; } private void sumOfMatching(int begin, int end) { for ( int i = begin ; i < end ; ++i ) if ( i % 3 == 0 || i % 5 == 0 ) sum += i; } private int getSum() { return sum; } public static void main(String[] args) { Sum s = new Sum(); s.sumOfMatching(1, 100); System.out.println( s.getSum() ); } 18Winter 2009/ Course

Version 1 public class Sum { private int sum; public Sum() { sum = 0; } private void sumOfMatching(int begin, int end) { for ( int i = begin ; i < end ; ++i ) if ( i % 3 == 0 || i % 5 == 0 ) sum += i; } private int getSum() { return sum; } public static void main(String[] args) { Sum s = new Sum(); s.sumOfMatching(1, 100); System.out.println( s.getSum() ); } 19Winter 2009/10 Private field Course

Version 1 public class Sum { private int sum; public Sum() { sum = 0; } private void sumOfMatching(int begin, int end) { for ( int i = begin ; i < end ; ++i ) if ( i % 3 == 0 || i % 5 == 0 ) sum += i; } private int getSum() { return sum; } public static void main(String[] args) { Sum s = new Sum(); s.sumOfMatching(1, 100); System.out.println( s.getSum() ); } 20Winter 2009/10 Constructor Course

Version 1 public class Sum { private int sum; public Sum() { sum = 0; } private void sumOfMatching(int begin, int end) { for ( int i = begin ; i < end ; ++i ) if ( i % 3 == 0 || i % 5 == 0 ) sum += i; } private int getSum() { return sum; } public static void main(String[] args) { Sum s = new Sum(); s.sumOfMatching(1, 100); System.out.println( s.getSum() ); } 21Winter 2009/10 Private methods Course

Version 1 public class Sum { private int sum; public Sum() { sum = 0; } private void sumOfMatching(int begin, int end) { for ( int i = begin ; i < end ; ++i ) if ( i % 3 == 0 || i % 5 == 0 ) sum += i; } private int getSum() { return sum; } public static void main(String[] args) { Sum s = new Sum(); s.sumOfMatching(1, 100); System.out.println( s.getSum() ); } 22Winter 2009/10 No unsigned int  Course

Version 1 public class Sum { private int sum; public Sum() { sum = 0; } private void sumOfMatching(int begin, int end) { for ( int i = begin ; i < end ; ++i ) if ( i % 3 == 0 || i % 5 == 0 ) sum += i; } private int getSum() { return sum; } public static void main(String[] args) { Sum s = new Sum(); s.sumOfMatching(1, 100); System.out.println( s.getSum() ); } 23Winter 2009/10 Main function Can ‘main’ be located in a different class? Course

Version 2 public class Sum { private int sum = 0; private boolean isMatching(int i) { return i % 3 == 0 || i % 5 == 0; } private void sumOfMatching(int begin, int end) throws IllegalArgumentException { if ( begin < 0 || end < begin ) throw new IllegalArgumentException(); for ( int i = begin ; i < end ; ++i ) if ( isMatching(i) ) sum += i; } private int getSum() { … } public static void main(String[] args) { … } } 24Winter 2009/ Course

Version 2 public class Sum { private int sum = 0; private boolean isMatching(int i) { return i % 3 == 0 || i % 5 == 0; } private void sumOfMatching(int begin, int end) throws IllegalArgumentException { if ( begin < 0 || end < begin ) throw new IllegalArgumentException(); for ( int i = begin ; i < end ; ++i ) if ( isMatching(i) ) sum += i; } private int getSum() { … } public static void main(String[] args) { … } } 25Winter 2009/10 Initializing field w/o constructor Course

Version 2 public class Sum { private int sum = 0; private boolean isMatching(int i) { return i % 3 == 0 || i % 5 == 0; } private void sumOfMatching(int begin, int end) throws IllegalArgumentException { if ( begin < 0 || end < begin ) throw new IllegalArgumentException(); for ( int i = begin ; i < end ; ++i ) if ( isMatching(i) ) sum += i; } private int getSum() { … } public static void main(String[] args) { … } } 26Winter 2009/10 Extract method Course

Version 2 public class Sum { private int sum = 0; private boolean isMatching(int i) { return i % 3 == 0 || i % 5 == 0; } private void sumOfMatching(int begin, int end) throws IllegalArgumentException { if ( begin < 0 || end < begin ) throw new IllegalArgumentException(); for ( int i = begin ; i < end ; ++i ) if ( isMatching(i) ) sum += i; } private int getSum() { … } public static void main(String[] args) { … } } 27Winter 2009/10 Exception declaration Course

Version 2 public class Sum { private int sum = 0; private boolean isMatching(int i) { return i % 3 == 0 || i % 5 == 0; } private void sumOfMatching(int begin, int end) throws IllegalArgumentException { if ( begin < 0 || end < begin ) throw new IllegalArgumentException(); for ( int i = begin ; i < end ; ++i ) if ( isMatching(i) ) sum += i; } private int getSum() { … } public static void main(String[] args) { … } } 28Winter 2009/10 Throwing exception Course

More improvements Like in the Pascal implementation we can change ‘3’ and ‘5’ to be given as parameters: –To sumOfMatching. –To class itself (via constructor). But let’s try something a bit more general and OO… –Define abstract class Matcher with abstract method isMatching. –Subclass Matcher and implement isMatching the way we want to. 29Winter 2009/ Course

Version 3 public abstract class Matcher { public abstract boolean isMatching( int i ); } public class TwoNumDivMatcher extends Matcher { private int div1; private int div2; public TwoNumDivMatcher( int div1, int div2 ) { this.div1 = div1; this.div2 = div2; } public boolean isMatching( int i ) { return i % div1 == 0 || i % div2 == 0; } 30Winter 2009/ Course

Version 3 public abstract class Matcher { public abstract boolean isMatching( int i ); } public class TwoNumDivMatcher extends Matcher { private int div1; private int div2; public TwoNumDivMatcher( int div1, int div2 ) { this.div1 = div1; this.div2 = div2; } public boolean isMatching( int i ) { return i % div1 == 0 || i % div2 == 0; } 31Winter 2009/10 We must override ‘ isMatching ’ (Why?) Course

Version 3 – cont. public class Sum { private int sum = 0; private void sumOfMatching(int begin, int end, Matcher m) throws IllegalArgumentException { if ( begin < 0 || end < begin ) throw new IllegalArgumentException(); for ( int i = begin ; i < end ; ++i ) if ( m.isMatching(i) ) sum += i; } public static void main(String[] args) { Sum s = new Sum(); s.sumOfMatching( 1, 100, new TwoNumDivMatcher(3, 5) ); System.out.println( s.getSum() ); } … 32Winter 2009/ Course

Version 3 – cont. public class Sum { private int sum = 0; private void sumOfMatching(int begin, int end, Matcher m) throws IllegalArgumentException { if ( begin < 0 || end < begin ) throw new IllegalArgumentException(); for ( int i = begin ; i < end ; ++i ) if ( m.isMatching(i) ) sum += i; } public static void main(String[] args) { Sum s = new Sum(); s.sumOfMatching( 1, 100, new TwoNumDivMatcher(3, 5) ); System.out.println( s.getSum() ); } … 33Winter 2009/ Course