Ch. 1 & 2 - Lecture3 - OO Program & Character Strings
Ch. 1 & 2 - Lecture3 - OO Program & Character Strings
Idea
Computer program may be seen
as comprising a collection of objects
Object
Fundamental entity in a JAVA program
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
Example:
Lassie is one particular dog whose fur is brown and white
Bank account
Bill’s Bank Account
Balance: 12789 $
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
This program
Prints two sentences on the screen
A quote by Abraham Lincoln
Sample output:
A quote by Abraham Lincoln
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
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
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
Primitive data
Data conversions
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
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”);
System.out is an object
Represents a destination to which we can send output
Which by default is the monitor screen
object method
information provided to the method
name
(parameters)
System.out
Provides another service
print method
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“
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”);
See Addition.java
Escape sequences
What if we wanted to print the quote character?
Solution
An escape sequence
Begins with a backslash character (\)