Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Object Oriented Programming Idea Computer program may be seen as comprising a collection of objects Object Fundamental entity in a JAVA program Used to."— Presentation transcript:

1 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

2 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).

3 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

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

5 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

6 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

7 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

8 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

9 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 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 */

11 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 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

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

14 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 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)

16 Chapter 2: Data and Expressions

17 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

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

19 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"

20 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

21 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)

22 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: “ + 40 + “ 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

23 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

24 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

25 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.");

26 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”


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

Similar presentations


Ads by Google