No Slide Title

Download Report

Transcript No Slide Title

Creating 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
A 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 doesn’t 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)
Overview Of A Class Definition
class name
class header
public class PieceOfFabric extends Simple GUI{
data declaration
private double sqMeter;
method
definition
public double toSqYards() { …}
public void readSqMeters() { …}
public void displayFabric() { …}
}
Is this a client or support class?
(Support - there is no main() method.
Sample Java Application
HelloWorld.java
class name
required for
applications
public class HelloWorld {
public static void main (String args[]){
System.out.println(“Hello World …”);
}
}
predefined class
predefined method
Method Body
scope
return type
name of method
public void readSquareMeters()
start of block
body / block
{
.
.
.
}
Instructions to access, modify
and/or return data.
end of block
Two 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.
scope
type
name of identifier
private double sqMeters;
•private - identifier is only accessible by methods in the class
Scope
(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 class’s clients from accessing
variables directly, thus preventing data fields
from being processed in any unpredictable way.
public class SomeClass {
long x, y=10, 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
(If no package is declared for a class, the Java compiler will put it in
the default package - just keep all classes in the same folder.)
Importing 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.className)
Example:
import javax.swing.* ;
(this allows use of classes in that package including,
JOptionPane which has methods that create windows for
user interface)
Extending 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.
Creating An Object / Instantiation
• instantiation - creating an object from a
class
• objects are created from classes
class definition
Shoe class {…}
Shoe object
Creating An Object
data type
identifier
reserve word
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 object’s instance
variables