Download Java Programming, Second edition

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
IT258 Foundation of
Programming Using Java
Unit 2 Seminar :
(Chapter 1 )
Instructor : Vladimir Gubanov, PhD
Email : [email protected]
A reminder:
1.
2.
3.
Our Seminars :
they will be each Thursday , from 9
PM to 10 PM EST
My Office Hours :
Mondays, 7PM to 8PM
Saturdays, 9AM to 10 AM
My email :
[email protected]
Java Programming,
Fifth Edition
Chapter One:
Creating Your First Java Classes
Objectives
•
•
•
•
•
Object-oriented programming
concepts
Learn about Java
Analyze a Java application that uses
console output
Save, compile, run, and modify a
Java application
Create a Java application using GUI
output
Java Programming, Fifth
Edition
4
Object-Oriented Programming
Concepts
•
•
Procedural programming
– Sets of operations executed in sequence
– Variables hold values
– Procedures group operations in logical units
Object-oriented programs
– Create classes first
– Create objects from classes when a
functionality of the class needs to be used
– Object-oriented programming includes:
– Encapsulation
– Inheritance
– Polymorphism
Java Programming, Fifth
5
Edition
Understanding Objects and
Classes
•
Objects
– Made up of attributes, methods, events
•
Attributes
– Characteristics that define object
– Value of attributes is object’s state
•
Class
– Describes objects with common properties
– Definition – template for objects
– Instance of a class – an object to use
Java Programming, Fifth
Edition
6
OBJECT
•
Represents something you would find in
the “real-world”
Object: Car
•
•
Objects can be described by their
attributes(properties) and
behaviors(methods)
In Java, an object is represented by a
software module, which contains a collection
of related data and procedures
PROPERTY (or field)
•
Attribute which describes the object:
Property: Color
Make
Year
VIN
•
In Java, a property is represented by
a variable associated with the object
METHOD
•
Behavior of the object or what the
object can do
Method: Start
Accelerate
Turn Left
Stop
•
In Java, a method is represented by a
public subroutine or function related
to the object
STATE
•
•
The collection of an object’s
property values determine its state
The object’s state can affect its
behavior
Object: Car
Property: GasolineLevel
OilLevel
Speed
State
CLASS
•
•
•
•
A category of objects – “abstraction”
Template for the object, also called “factory”
Allows multiple objects to have common
methods and properties
An object is an “instance” of a class
Objects:
Class: Car
VIN: 323422344
VIN: 323422345
VIN: 323422346
INTERFACE
•
The set of public methods and
properties which are available to
access or manipulate the object
An object may have many interfaces
•
Object: Car
Properties: Color
GasolineLevel
Make
OilLevel
Model
Speed
Year
Methods: Stop
Turn Left
Start
Turn Right
Accelerate
Interface
CLASS : elaborate
room
window
A blueprint is the specification of a
House. This blueprint specifies
a house with room, door, and window.
door
class
object
A class is the
specification
from which a
developer
generates objects
A blueprint is the
specification from
which a builder
generates houses
ENCAPSULATION
•
•
•
Hides the internal workings of an object from
the rest of the application.
You may change Implementation while
interface remains constant
In Java properties and methods are used to
get information and perform actions in an
application that are hidden within classes:
you do not know how they are
implemented , but can use then as “black
box”
INHERITANCE: Very Important
•
•
Ability to reuse functionality in a class.
A base class defines the common
functionality:
Subclasses inherit that functionality
and may add new functionality
– i.e.: A general class of Sport Car is used to
create sub-classes of Camaro and
FireBird. They have many similar (reused) features but are different cars,
because they have other properties
(methods) different.
Learning About Java
•
Java
– Developed by Sun Microsystems
– Object-oriented language
– Advantages :
• Simple, Distributed , Interpreted, Secure
• Architecture neutral, Portable, Multithreaded
– Can be run on wide variety of computers
– Runs on hypothetical computer known as
Java virtual machine (JVM)
Java Programming, Fifth
Edition
17
Learning About Java (continued)
•
Java (continued)
– Can be run on wide variety of computers
– Does not execute instructions on
computer directly
– Runs on hypothetical computer known
as Java virtual machine (JVM)
•
Source code
– Programming statements written in highlevel programming language
Java Programming, Fifth
Edition
18
The Java Virtual Machine – special
“basic computer”
Java is platform independent. What this means is that a program written in
Java should be executable in any computer with any chipset.
This is done by means of the Java Virtual Machine (JVM). The JVM interacts with
a computer through bytecode.
Computer
JVM
Program
Java Program Types
•
Applets
– Programs embedded in Web page
•
Java applications
– Called Java stand-alone programs
– Console applications
• Support character output
– GUI based applications
• Menus
• Toolbars
• Dialog boxes
Java Programming, Fifth
Edition
20
Java Programming Process:
•
•
•
•
•
•
•
•
Write a specification for the program (what the
program is supposed to do)
Design the program
Choose algorithms and decide how data will be
stored , develop pseudocode and/or flowchart
Write the program (Java language, indeed!)
Compile the program (to lower-level language)
Execute the program
Debug the program
Maintenan the program in a workable condition
Analyzing a Java Application
That Uses Console Output
•
Even simplest Java application
– Involves fair amount of confusing syntax
•
Goal of the first application :
– Print “First Java application” on screen
Java Programming, Fifth
Edition
22
Analyzing a Java Application
That Uses Console Output :
Java Programming, Fifth
Edition
23
Understanding the First Class
•
•
•
Everything used within Java program
must be part of a class
Define Java class using any name or
identifier
Requirements for identifiers
– Must begin with:
• Letter of English alphabet
• Or non-English letter (such as α or π)
– Cannot begin with digit
Java Programming, Fifth
Edition
24
Understanding the First Class
•
Requirements for identifiers
– Can only contain:
•
•
•
•
Letters
Digits
Underscores
Dollar signs
– Cannot be Java reserved keyword
– Cannot be true, false, or null
•
Access modifier
– Defines how class can be accessed
Java Programming, Fifth
Edition
25
Understanding the First Class
Java Programming, Fifth
Edition
26
Understanding the Statement
That Prints the Output
Java Programming, Fifth
Edition
27
Some Illigal in Java Class names
Java Programming, Fifth
Edition
28
Understanding the First Class
(continued)
Java Programming, Fifth
Edition
29
Understanding the main()
Method
•
static
– Means method accessible and usable
• Even though no objects of class exist
•
void
– Use in main() method header
– Indicates main() method does not return
value when called
– Doesn’t mean main() doesn’t produce
output
Java Programming, Fifth
Edition
30
Another Java Program: Example.java
/*
This is a simple Java program.
Change .println to
Println and compile
Call this file Example.java
*/
class Example
{
//A Java program begins with a call to main().
public static void main(String[] args)
{
System.out.println("Java drives the Web."); // System.out : console output
}
// println() displays the string
}
// passed to it
Here: Java is case sensitive language!!
• a source file : the same name as class with .java extension
• comments(ignored by the compiler): single line and multiline
• a subroutine is called a method; main() method is the entry point
• public: access specifier
;
static – can be called before an object is
created
from a class
;
void - does not return a value
Shell Code – create and use as
a template for any class
Do not forget to same in the file
AnyClassName.java
Java Programming, Fifth
Edition
32
Adding Comments to a Java
Class
•
Types of Java comments
– Line comments
• Start with two forward slashes (//)
• Continue to end of current line
• Do not require ending symbol
– Block comments
• Start with forward slash and asterisk (/*)
• End with asterisk and forward slash (*/)
Java Programming, Fifth
Edition
33
Saving, Compiling, and Running
and Modifying a Java Application
•
Saving a Java class
– Save class in file with exactly same name
and.java extension
• For public classes
• Class name and filename must match exactly
•
Compiling a Java class
– Compile source code into bytecode
– Translate bytecode into executable statements
• Using Java interpreter
– Type javac First.java
Java Programming, Fifth
Edition
34
Running a Java Application
•
Run application from command line
– Type java First
•
•
Shows application’s output in
command window
Class stored in folder named Java on
C drive
Java Programming, Fifth
Edition
35
Running a Java Application
In order javac and java programs be able to
find First.java and First.class files - set up
Environmental Path variable
Java Programming, Fifth
Edition
36
Modifying a Java Class
•
•
Modify text file that contains existing
class
Save file with changes
– Using same filename
•
Compile class with javac command
•
Interpret class bytecode and execute
class using java command
Java Programming, Fifth
Edition
37
Creating a Java Application
Using GUI Output
•
JOptionPane
– Produce dialog boxes
•
Dialog box
– GUI object resembling window
– Messages placed for display
•
Package
– Group of classes
•
import statement
– Use to access built-in Java class
Java Programming, Fifth
Edition
38
Creating a Java Application
Using GUI Output
Java Programming, Fifth
Edition
39
Correcting Errors
•
•
•
First line of error message displays:
– Name of file where error found
Do not expect miracles :
– Line number
an error message will
only approximately
– Nature of error
indicate where an error
Next lines identify:
is and what it could be –
no guarantee that these
– Symbol
are 100% correct
– Location
Compile-time error
– Compiler detects violation of language rules
– Refuses to translate class to machine code
Java Programming, Fifth
Edition
40
Correcting Errors and
Finding Help (continued)
•
Parsing
– Process compiler uses to divide source
code into meaningful portions
•
Logic error
– Syntax correct but produces incorrect
results when executed
– Usually more difficult to find and resolve
• Java API
– Also called the Java class library
– Prewritten Java classes
Java Programming, Fifth
41
Edition
You Do It
•
•
•
•
Your first application
Adding comments to a class
Modifying a class
Creating a dialog box
Java Programming, Fifth
Edition
42
Don’t Do It
•
•
•
•
•
•
File’s name must match name of class
Don’t confuse names parentheses, braces,
brackets, curly braces, square brackets, and angle
brackets
Don’t forget to end a block comment
Don’t forget that Java is case sensitive
End every statement with semicolon
– Do not end class or method headers with
semicolon
Recompile when making changes
Java Programming, Fifth
Edition
43
Summary
•
•
•
•
Computer program
– Set of instructions that tells a computer what to
do
Object-oriented programs
– Classes
– Objects
Java virtual machine (JVM)
– Standardized hypothetical computer
Everything in a Java program must be part of a
class
Java Programming, Fifth
Edition
44
Summary (continued)
•
•
•
•
Access modifier
– Word that defines circumstances under which
class can be accessed
All Java applications must have method named
main()
Program comments
– Nonexecuting statements
– Add to file for documentation
javac
– Compile command
Java Programming, Fifth
Edition
45
Summary (continued)
•
java
– Execute command
•
JOptionPane
– GUI
– Provides methods for creating dialogs
Java Programming, Fifth
Edition
46
Quiz
• True or False: Object-oriented programming is a style of programming
in which sets of operations are executed one after another in sequence.
• Answer: False
• Writing ____ programs involves creating classes, creating objects from
those classes, and creating applications.
• Answer: object-oriented
Answer: False
• A(n) ____ of a class is an existing object of a class.
• Answer: instance
• Programming statements written in a high-level programming
language are called ____.
• Answer: source code
47
Quiz (cont.)
•
•
•
•
•
Stand-alone programs are called Java ____.
Answer: applications
Answer: True
True or False: Not all classes have a main() method.
Answer: True
• Line comments start with ____.
• Answer: two forward slashes (//)
48
Quiz (cont.)
• True or False: In Java, if a class is public (that is, if you
use the public access modifier before the class name), you
must save the class in a file with exactly the same name
and a .class extension.
• Answer: False and *=
• To compile a file named First.java, you type ____
First.java and then press Enter.
• Answer: javac
• To run the First application from the command line, you
type ____ First.
• Answer: java
49
Quiz (cont.)
• A(n) ____ is a GUI object resembling a window in
which you can place messages you want to
display.
• Answer: dialog box
50
This is the end of our Unit 2
Seminar
•Any questions ?
51