1 / 26

Object Oriented Programming

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

breck
Télécharger la présentation

Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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) Multiple encapsulated objects can be created from one class John’s Bank Account Balance: 5678 $ A class defines a concept Bank account Bill’s Bank Account Balance: 12789 $ Mary’s Bank Account Balance: 16833 $

  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 account Checking 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 Comments //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.”); } } 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. 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. 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

  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. 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. object method name information provided to the method (parameters) The print and println Methods System.out.println ("Whatever you are, be a good one."); • 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

  22. This is wrong 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

  23. + String concatenation operator 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.”); } }

  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 2nd 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 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 Violets are blue Sugar is “sweet”

More Related