Object Oriented Programming Idea Computer program may be seen as comprising a collection of objects Object Fundamental entity in a JAVA program Used to.

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 1 “ Introduction to Java and OOP”
Outline Java program structure Basic program elements
Chapter 1 Introduction. © 2004 Pearson Addison-Wesley. All rights reserved1-2 Outline Computer Processing Hardware Components Networks The Java Programming.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Chapter 1 Introduction.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2004 Pearson Addison-Wesley. All rights reserved1-1 Intermediate Java Programming Lory Al Moakar.
Chapter 1 Introduction.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 1: Introduction.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2006 Pearson Education Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Java Language and SW Dev’t
© 2006 Pearson Education 1 Obj: cont 1.3 and 1.4, to become familiar with identifiers and to understand how programming languages work HW: p.51 #1.8 –
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
The Java Programming Language
Introduction. Objectives An overview of object-oriented concepts. Programming and programming languages An introduction to Java 1-2.
Program Statements Primitive Data Types and Strings.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Data and Expressions Part One. © 2004 Pearson Addison-Wesley. All rights reserved2-2/29 Data and Expressions Let's explore some other fundamental.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
By Mr. Muhammad Pervez Akhtar
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 02 Data and Expressions.
Web Programming Chapter 1 : Introduction to Java Programming
Working with Java.
Intermediate Java Programming
Lecture 1 Introduction Richard Gesick.
Chapter 3 Syntax, Errors, and Debugging
Console Output, Variables, Literals, and Introduction to Type
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Creating Objects & String Class
1.3 Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: Understand the problem Dissect the.
Introduction to Objects
Escape Sequences What if we wanted to print the quote character?
Data and Expressions Part One
Introduction to C++ Programming
Chapter 1: Computer Systems
Fundamentals of Programming
Java Software Solutions Foundations of Program Design 9th Edition
Focus of the Course Object-Oriented Software Development
CSC 1051 – Data Structures and Algorithms I
Instructor: Alexander Stoytchev
Java Programming Presented by Dr. K. SATISH KUMAR,
Introduction to Objects
Presentation transcript:

Object Oriented Programming Idea Computer program may be seen as comprising a collection of objects Object Fundamental entity in a JAVA program Used to represent real world entities Example: employee in a company may be an object Sends messages to other objects Receives messages from other objects Can be viewed as an independent actor with distinct role

Object oriented software principles Class Abstract characterization or blueprint of an object Defines the state and behaviors of an object State => attributes; set of behaviors => methods Example: Dog consists of traits shared by all dogs  (fur color, and ability to bark members of a class).

Object oriented software principles (cont’d) Object Particular instance of a class Defines the set of values of attributes (states) Example: Lassie is one particular dog whose fur is brown and white Lassie is an instance of the Dog class

Object oriented software principles (cont’d) Bank account A class defines a concept John’s Bank Account Balance: 5678 $ Bill’s Bank Account Balance: $ Mary’s Bank Account Balance: $ Multiple encapsulated objects can be created from one class

Object oriented software principles (cont’d) Encapsulation Conceals the state of an object The object protects and manages its own information Objects should be designed so that other objects cannot reach in and change its state Example: It is important to hide the balance attribute of a bank account

Object oriented software principles (cont’d) Inheritance The definition of one class is based on another One class is used to derive several new classes Derived classes can be used to derive more classes Create a hierarchy of classes Attributes and methods are inherited by children Bank account Savings accountChecking account

A simple Java program Consider A simple but complete Java program This program Prints two sentences on the screen  A quote by Abraham Lincoln Sample output:  A quote by Abraham Lincoln Whatever you are, be a good one

First JAVA program //Linclon.JAVA //demonstrates the basic structure of a JAVA application public class Lincoln { public static void main (String[ ]) { System.out.println(“A quote by Lincoln”); System.out.println(“Whatever you are, be a good one.”); } Comments Class definition Refer to Lincoln.java

Dissecting the first Java program All Java applications Start with a class definition In this case Lincoln preceded by public class Have a main method which is where processing begins is always preceded by public, static, and void The previous example invokes another method (execute) Println that prints a character string to the screen  Enclosed in double quote characters (”)

10 Comments Comments in a program are called inline documentation included to explain the purpose of the program do not affect how a program works Java comments can take three forms: // this comment runs to the end of the line /* this comment runs to the terminating symbol, even across line breaks */ /** this is a javadoc comment */

Identifiers and reserved words These fall into 3 categories Words made up when writing a program Example: Lincoln Words that another programmer chose Example: String, System, out, and main Words reserved for special purposes in the language Example: class, public, static, and void

12 Identifiers An identifier can be made up of letters, digits, and special characters. Identifiers cannot begin with a digit Java is case sensitive Total, total, and TOTAL are different identifiers

JAVA reserved words abstractdefaultgotopackagethis assertdoifprivatethrow booleandoubleimplementsprotectedthrows breakelseimportpublictransient byteenuminstanceofreturntrue caseextendsintshorttry catchfalseinterfacestaticvoid charfinallongstrictfpvolatile classfinallynativesuperwhile constfloatnewswitch continuefornullsynchronized

White Space The way a programmer uses white space is important To make a program easier to read Except when used to separate words The computer ignores white space It does not affect the execution of a program you should adopt and use a set of guidelines that increase the readability of your code Refer to Lincoln3.java

15 Errors A program can have three types of errors The compiler will find syntax errors If compile-time errors exist, an executable version of the program is not created A problem can occur during program execution which causes a program to terminate abnormally (run- time errors) A program may run, but produce incorrect results perhaps using an incorrect formula (logical errors)

Chapter 2: Data and Expressions

Outline Chapter 2 focuses on Character strings Primitive data The declaration and use of variables Expressions and operator precedence Data conversions Accepting input from user Java applets

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

Character strings A string of characters is an object in JAVA, defined by the class String Can be represented as a string literal by putting double quotes around the text Examples: "This is a string literal." "123 Main Street" "X"

The print and println Methods In Lincoln.java we invoked the println System.out.println(“Whatever you are”); To print a character string System.out is an object Represents a destination to which we can send output Which by default is the monitor screen Provides a service thru println Takes only 1 parameter: the string of characters to be printed

The print and println Methods System.out Provides another service print method Difference between print and println println: prints information and move to beginning of next line Print Does not advance to the next line when completed See Countdown.java System.out.println ("Whatever you are, be a good one."); object method name information provided to the method (parameters)

String concatenation The string concatenation operator (+) is used to append one string to the end of another "Peanut butter " + "and jelly“ It can also be used to append a number to a string “Speed of airplane: “ “ km per s” A string literal not fitting on one line Cannot be broken across two lines in a program System.out.println (“the only stupid question is the one that is not asked.”); See Facts.java This is wrong

String concatenation (cont’d) //Linclon.JAVA //demonstrates the basic structure of a JAVA application public class Lincoln { public static void main (String[ ] args) { System.out.println(“A quote by Lincoln”); System.out.print(“Whatever you are” + “ be a good one.”); } + String concatenation operator

String concatenation (cont’d) The + operator is also used For arithmetic addition The function that it performs depends on The types of data on which it operates, If either of the operands are strings String concatenation is performed If both operands are numeric, it adds them The addition is evaluated left to right See Addition.java

Escape sequences What if we wanted to print the quote character? The following line would confuse the compiler Because it would interpret the 2 nd quote as end of string Solution An escape sequence Begins with a backslash character ( \ ) the character that follow should be interpreted in a special way System.out.println ("I said "Hello" to you."); System.out.println ("I said \"Hello\" to you.");

Escape Sequences Escape sequence Meaning \tTab \nNewline \”Double quote \’Single quote \\backslash Example: (See Roses.java) public class Roses { public static void main (String[ ] args) { System.out.println(“Roses are red, \n” + “Violets \t are blue\n” + “Sugar is \“sweet\” ”); } Output: Roses are red Violetsare blue Sugar is “sweet”