Title: Creating Java Applications
1Creating Java Applications
(Software Development Life Cycle)
1. specify the problem requirements -
clarify 2. analyze the problem - Input?
Processes? Output expected? 3. design the
classes - top down What are the major
subtasks for which -
Classes exist that are
appropriate? Classes exist that can be
modified? New classes must be
written? (Write algorithms where needed.) 4.
implement the classes - write Java code from
design 5. test and verify the program - desk
check with examples from all cases of
data, then run program to verify. 6. maintain /
update the program
2A Java application is a collection of classes.
1. An application (or client) class containing
main() is required. This tells the reader and
the JVM where to start execution. (The order in
which all other classes and the methods in them
are stored doesnt matter.)
2. All other classes are supporting classes
which do all the work.
3. Each class contains two types of
members data fields and
methods. (methods contain the instructions to
process the data as needed)
3Overview Of A Class Definition
class name
class header
public class PieceOfFabric extends Simple GUI
private double sqMeter public double
toSqYards() public void readSqMeters()
public void displayFabric()
data declaration
method definition
Is this a client or support class?
(Support - there is no main() method.
4Sample Java Application
class name
required for applications
HelloWorld.java
public class HelloWorld public static void
main (String args) System.out.println(Hello
World )
predefined method
predefined class
5Method Body
return type
scope
name of method
public void readSquareMeters()
. . .
start of block
Instructions to access, modify and/or return data.
body / block
end of block
6Two types of Statements 1.Declaration - tells
the compiler the name (or identifier) of a
variable (a storage location) the scope (private,
protected or public) and the type of data it is
permitted to store.
7Scope
(Visibility Modifiers)
Public - accessible to all clients desiring
access. Classes are usually public and methods
are often public, but not always, but data fields
are usually not public.
Private - prevents classs clients from accessing
variables directly, thus preventing data fields
from being processed in any unpredictable way.
8public class SomeClass long x, y10, z
//declare
three longs at once
- variables x, y, and z are accessible ONLY by
classes within the same package because public,
private, or protected is omitted
- package is a group of related classes stored in
directory of same name. - if public, private, or protected is omitted, the
variable (data field, methods, or class) is
accessible ONLY by classes within the same package
9Importing a Package
An import statement must appear at the top of a
file to allow a package of classes defined
elsewhere in storage to be used by the program.
(If this is not done, each class used must be
referenced by the long name packageName.classNam
e)
Example import javax.swing. (this allows use
of classes in that package including, JOptionPane
which has methods that create windows for user
interface)
10Extending a class
A hierarchy of classes is created by this
process, by which code can be reused easily. When
a class is extended it is called the superclass
(or parent) and the subclass in which the word
extend appears in the heading has direct access
to its members, while being able to declare its
own.
11Creating An Object / Instantiation
- instantiation - creating an object from a class
- objects are created from classes
Shoe object
class definition
Shoe class
12Creating An Object
reserve word
data type
identifier
constructor
PieceOfFabric aPiece new PieceOfFabric()
- new allocates memory for the object (aPiece)
- aPiece - has the address of the object created on
the right hand side of the assignment statement - memory allocated has the values of the objects
instance variables