270 likes | 498 Views
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
E N D
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) 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 $
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 account Checking 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 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
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 (”)
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
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
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
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)
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
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
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
+ 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.”); } }
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 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.");
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”