0% found this document useful (0 votes)
22 views26 pages

Ch. 1 & 2 - Lecture3 - OO Program & Character Strings

Object Oriented Programming involves modeling real-world entities as objects that can send and receive messages. The key concepts include: - Classes define common attributes and behaviors of objects as blueprints. For example, the Dog class defines traits of dogs. - Objects are instances of classes that define specific attribute values. For example, Lassie is an object instance of the Dog class. - Encapsulation conceals an object's internal state and manages access to it through methods. For example, a bank account object hides its balance attribute.

Uploaded by

elio saliba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views26 pages

Ch. 1 & 2 - Lecture3 - OO Program & Character Strings

Object Oriented Programming involves modeling real-world entities as objects that can send and receive messages. The key concepts include: - Classes define common attributes and behaviors of objects as blueprints. For example, the Dog class defines traits of dogs. - Objects are instances of classes that define specific attribute values. For example, Lassie is an object instance of the Dog class. - Encapsulation conceals an object's internal state and manages access to it through methods. For example, a bank account object hides its balance attribute.

Uploaded by

elio saliba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

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


A class defines
Balance: 5678 $
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 */


10
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

12
JAVA reserved words
abstract default goto package this
assert do if private throw
boolean double implements protected throws
break else import public transient
byte enum instanceof return true
case extends int short try
catch false interface static void
char final long strictfp volatile
class finally native super while
const float new switch
continue for null synchronized
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) 15
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.println ("Whatever you are, be a good one.");

object method
information provided to the method
name
(parameters)
 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
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 This is


wrong
question is the one that is not asked.”);

 See Facts.java
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

System.out.println ("I said "Hello" to you.");

 Solution
 An escape sequence
 Begins with a backslash character (\)

 the character that follows should be interpreted in a special way


System.out.println ("I
said \"Hello\" to you.");
Escape Sequences
Example: (See Roses.java)

Escape Meaning public class Roses {


sequence
public static void main (String[ ] args) {
\t Tab
System.out.println(“Roses are red, \n” +
\n Newline “Violets \t are blue\n” +
“Sugar is \“sweet\” ”);
\” Double quote }
}
\’ Single quote
Output:
Roses are red
\\ backslash
Violets are blue
Sugar is “sweet”

You might also like