1 Java Programming From Flowcharts To Java (CSUS, Fall 2015) Object Oriented Programming Programming with "class" This means that your program consists of a series of objects that will interact with each other An object is very abstract can be anything 12/5/2015 Sacramento State - CSc 10A 3 Object Oriented Programming Many objects are related to each other – having the same abilities and attributes These objects belong to the same class – which is a classification of related objects 12/5/2015 Sacramento State - CSc 10A 4 Classes A class describes what an object will store, how it behaves, etc… Properties contain data about the object Methods describe how the treats data (its and others) 12/5/2015 Sacramento State - CSc 10A 5 Classes "Cat" Class properties can include: • name • fur color • breed "Student" Class properties can include: • name • major • academic level – freshman, junior, … 12/5/2015 Sacramento State - CSc 10A 6 Example Properties
22
Embed
Object Java Programming - California State University ...athena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Java_Fall...Java Programming From Flowcharts To Java ... CSc 10A 6 Example
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Java
Programming
From Flowcharts To Java
(CSUS, Fall 2015)
Object
Oriented
Programming
Programming with "class"
This means that your
program consists of a series
of objects that will interact
with each other
An object is very abstract can
be anything
12/5/2015 Sacramento State - CSc 10A 3
Object Oriented Programming
Many objects are related to
each other – having the same
abilities and attributes
These objects belong to the
same class – which is a
classification of related
objects
12/5/2015 Sacramento State - CSc 10A 4
Classes
A class describes what an
object will store, how it
behaves, etc…
Properties contain data about
the object
Methods describe how the
treats data (its and others)
12/5/2015 Sacramento State - CSc 10A 5
Classes
"Cat" Class properties can include:
• name
• fur color
• breed
"Student" Class properties can include:
• name
• major
• academic level – freshman, junior, …
12/5/2015 Sacramento State - CSc 10A 6
Example Properties
2
"Cat" Class methods:
• eat
• purr
• sleep
"Student" Class methods:
• study
• play on smart phone
• sleep
12/5/2015 Sacramento State - CSc 10A 7
Example Methods
Classes can also "inherit" from classes
When a class inherits another:
• gets all the features of the original class
• but can extend its functionality
• allows work, created previously to become the
foundation of a more advanced class
12/5/2015 Sacramento State - CSc 10A 8
Class Inheritance
Classes just describe the
behavior of some "object"
They don't do anything
In object-oriented
programming, you will create
instances of these classes –
i.e. objects
12/5/2015 Sacramento State - CSc 10A 9
Instances / Objects
Instances will have all the
features of its class
So, different instances of the
same class share the same
features
But, each instance is a
different and unique
12/5/2015 Sacramento State - CSc 10A 10
Instances / Objects
"Game" Class can have instances of:
• Pac-Man
• Call of Duty
• Fallout 4
"Food" Class can have instances of:
• ice cream
• pizza
• top ramen
12/5/2015 Sacramento State - CSc 10A 11
Example Classes & Instances
Introduction to
Java
Start the coffee maker – seriously...
3
The Java Programming is
currently one of the most
popular languages in the
World
Java followed a long
evolutionary chain that
started with C (later C++)
12/5/2015 Sacramento State - CSc 10A 13
What is Java?
When Java was developed,
C/C++ had been in use for
over 20 years
So, to aid programmers...
• Java syntax very close to C++
• Java has most of the same
semantics as C++
12/5/2015 Sacramento State - CSc 10A 14
And Along Comes Java
However, Java is not
compatible with C++
It removed the low-level
features of C++
But, it will still work on
snippets of code
12/5/2015 Sacramento State - CSc 10A 15
However, it is different
Java contains many advanced features
But, has a very symbolic syntax
• contains very few "words" - not English-like
• so, programs are not easy to read at first
It is not a beginners language
• syntax it can be intimidating
• you must type of bunch of "weird" stuff you
won't understand at first
12/5/2015 Sacramento State - CSc 10A 16
The Result...
Structure of
Java
Programs
What the heck am I looking at?
Java programs consist of
series of class definitions
Each class contains local
variables (properties) and
functions
Each function contains its
own local variables as well as
statements
12/5/2015 Sacramento State - CSc 10A 18
Structure of Java Programs
4
A statement will carry out a specific task
Statements are executed in order from the
first listed to the last
In Java, you can create your own and use
ones created for you
12/5/2015 Sacramento State - CSc 10A 19
What are Statements?
Statements can be grouped together into a
block
Some types of statements…
• calls to other functions
• control – looping, etc…
• create variables
12/5/2015 Sacramento State - CSc 10A 20
What are Statements?
12/5/2015 Sacramento State - CSc 10A 21
Structure of a Java Program
Data about class
Used by Method
Java Data
Types
What information Java can hold
Java classes are made of
other classes or some
primitive types
Primitive types are not really
classes, but data that the
processor understands
12/5/2015 Sacramento State - CSc 10A 23
Data in Java
Used to store whole numbers
Java has three data types
that store integers
Why three?
• more bytes you use to store a
value, the larger can be
• however, it will take more
memory
12/5/2015 Sacramento State - CSc 10A 24
Integers
5
1
5
-100
1846
1947
-12345
12/5/2015 Sacramento State - CSc 10A 25
Integer Examples
Data Type Range of values Bytes
byte -128 .. 127 1
short -32,768 .. 32,767 2
int-2,147,483,648 ..
2,147,483,647 4
long-9,223,372,036,854,775,808 ..
9,223,372,036,854,775,8078
12/5/2015 Sacramento State - CSc 10A 26
Integer Data Types
Real numbers in Java are
called floating-point
Why?
• the name is based on how it is
actually stored
• the decimal place "floats
around" like it does in scientific
notation
12/5/2015 Sacramento State - CSc 10A 27
Real Numbers
Java has two data types for
storing real numbers
Why?
• again, you might need to use
more bytes to store larger
values
• but, it will cost more memory
12/5/2015 Sacramento State - CSc 10A 28
Real Numbers
-6.78
3.1415
1.618
2.71828
-355.1234
1234.0
12/5/2015 Sacramento State - CSc 10A 29
Floating-Point Examples
Note the zero!
Data Type Range of values Bytes
float
10-38 to 10+38
Both positive and negative
About 6 digits precision
4
double
10-308 to 10+308
Both positive and negative
About 15 digits precision
8
12/5/2015 Sacramento State - CSc 10A 30
Floating Point Data Types
6
Used to store letter individual
letters, digits, symbols, etc…
These are the keys you have
on your keyboard
In Java, chars are delimited
by single quotes (also called
apostrophes)
12/5/2015 Sacramento State - CSc 10A 31
Character Data Type
'A'
'4'
' '
'$'
'&'
'^'
12/5/2015 Sacramento State - CSc 10A 32
Character Examples
Space
Characters are combined
(into arrays) to form strings
Java denotes a string literal
with double quotes
These are stored using a
class – so a String is not a
primitive data type
12/5/2015 Sacramento State - CSc 10A 33
Strings
"Sac State"
"Computer Science"
"Joe Gunchy"
"Hornet"
"1947"
"Cheese"
12/5/2015 Sacramento State - CSc 10A 34
Examples of Strings
Often you want to add a control character do your program
Java has help escape sequences start with a backslash
This is followed by another character that represents the control character
12/5/2015 Sacramento State - CSc 10A 35
Java Escape Sequences
NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI
DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US