LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud...
-
Upload
kelly-james -
Category
Documents
-
view
225 -
download
1
Transcript of LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud...
LESSON 2CREATING A JAVA APPLICATION
JAVA PROGRAMMING
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Outlinei. Create Java source file in DOS
environment.ii. Compile the Java source file at the DOS
prompt.iii. Run the Java byte code file at the DOS
prompt.iv. Analyze the Java program.v. Determine the structure of the Java
program.
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Steps to Creating a Java Application
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Create/ Modify the Source Code using Text Editor
Compile Source Code using Java
Compiler
Run Byte Code using Java Virtual
Machine
Source Code (.java)
Byte Code (.class)
Program Execution
Saves on Disk
Is Read By
Is Interpreted
By
Produces
Results
If Error
If Run Time Error or Incorrect results
Create a Source File
Compiled By: Edwin O. Okech [Tutor, Amoud University]
1. Create a new directory in drive C to place all your Java source files.
2. Open a text editor i.e. Notepad.3. Type the Java code.4. Save the code to a file. The name of the file must be the same as
the class name and with the extension name .java.
Demonstrate how to open a Java IDE e.g. Netbeans
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Start - Programs – NetbeansExample Code
// This is my first Java program.public class HelloApp{
public static void main(String [] args){
System.out.println("Hello there");System.out.println("Welcome to Java");
}}
Analyzing the Example HelloApp.java
Compiled By: Edwin O. Okech [Tutor, Amoud University]
// This is my first Java program.public class HelloApp{
}
This is a Java comment, it is ignored by the Java compiler
This is the class header for the
HelloApp classThis are is the body of the class
HelloApp. All of the data and methods for this class will be
between the curly braces
Analyzing the Example HelloApp.java
Compiled By: Edwin O. Okech [Tutor, Amoud University]
// This is my first Java program.public class HelloApp{
public static void main (String args[]){
}}
This is Java main method. Every
application must have a main method.
This area is the body of the main method. All of the actions to be completed
during the main method will be placed between these
curly braces
Analyzing the Example HelloApp.java
Compiled By: Edwin O. Okech [Tutor, Amoud University]
// This is my first Java program.public class HelloApp{
public static void main (String args[]){
System.out.println(“Hello there”);System.out.println(“Welcome to
Java”);}
}This is the Java statement that is executed when the
program runs
Structure of Java programme
Compiled By: Edwin O. Okech [Tutor, Amoud University]
CommentsKeywordsModifiersStatementsBlocksClassesMethodsThe main methodPackage
Comments
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Help the programmers to communicate and understand the program.
Not a programming statement, thus ignored by the compiler.
Three Types of Comments:1. Multiline comments2. Single line comments3. Javadoc comments
Multi line Comments
Compiled By: Edwin O. Okech [Tutor, Amoud University]
/*This is a comment withfour lines oftext.Make sure you have the pair.*/
Single line comments
Compiled By: Edwin O. Okech [Tutor, Amoud University]
// This is a single line comment// Each line must have the double back
slash
Javadoc comments
Compiled By: Edwin O. Okech [Tutor, Amoud University]
/*** This program converts the temperature
value in* Celsius into Fahrenheit.*/
Java keywords
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Words that have a specific meaning to the Java compiler.
Key words are lower case (Java is a case sensitive language).
Key words cannot be used as a programmer‐defined identifier.
Java keywords
Compiled By: Edwin O. Okech [Tutor, Amoud University]
abstract else long synchronized boolean break byte case extends final finally nativenew package private this throw throws transient catch char class float for goto if protected public return try void volatile const continue default do implements import instanceof int short static strictfp super while double interface switch
Modifiers
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Specify the properties of the data, methods, and classes and how they can be used.
Example of modifiers:- public: data, method or class can be
accessed by other classes.- private: data, method or class cannot be
accessed by other classes.- protected- final- static- abstract
Modifiers: examples
Compiled By: Edwin O. Okech [Tutor, Amoud University]
public class ClassA{
public static void main (String[] args) {
System.out.println ("Try your best");}
}
Statements
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Represents an action or a sequence of actions.
• Example of statement:System.out.println("Welcome to Java!")is a statement to display the greeting:"Welcome to Java!"• Every statement in Java ends with a
semicolon (;).
Statement blocks
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Groups the components of the program using the braces { and } in the program.
Every class has a class block that groups the data and the methods of the class.
Every method has a method block that groups the data and the methods of the class.
Block may be nested, meaning that one block can be placed within another.
Classes
Compiled By: Edwin O. Okech [Tutor, Amoud University]
A class is the blue of an object.A class is the template of an object.Class is the essential Java construct.Classes are central to Java.Programming in Java consists of defining a
number of classes:– Every program is a class (a program is
defined by using one or more classes).– All programmer‐defined types are classes.
Example of a class
Compiled By: Edwin O. Okech [Tutor, Amoud University]
public class ClassA {
public static void main (String[] args) {
System.out.println ("Try your best");}
}
Methods
Compiled By: Edwin O. Okech [Tutor, Amoud University]
A collection of statements that performs a sequence of operations.
Contained in a class.If a method is intended to be used to
communicate with or pass information to an object, it should be declared public.
Example: Method println()is an instance method that belongs to an object instance and is applied to an object (System.out).
main Method
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Every Java application must have a main method that is declared in the following way:
public class ClassName{
public static void main(String[] args) {
// Statements;}
}
main Method
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Every Java application must have main a method that is declared in the following way:
public class ClassName{
public static void main(String[] args) {
// Statements;}
}
This method is public i.e. visible from anywhere that
can see this class.
main Method
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Every Java application must have main a method that is declared in the following way:
public class ClassName{
public static void main(String[] args) {
// Statements;}
}
The main method is always static, meaning that this
method can be run without creating an instance of the
class
main Method
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Every Java application must have main a method that is declared in the following way:
public class ClassName{
public static void main(String[] args) {
// Statements;}
}
The void keyword indicates that the data returned from this method is nothing or no value i.e. it does not return a
value
main Method
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Every Java application must have main a method that is declared in the following way:
public class ClassName{
public static void main(String[] args) {
// Statements;}
}
This is the parameter of the main method. It takes arguments of an array of Strings.
The data type String starts with an uppercase S. the square brackets indicate
an array.
Libraries
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Java programs are usually not written from scratch.
There are hundreds of library classes for all occasions.
Library classes are organized into packages. For example:
java.util — miscellaneous utility classesjava.awt — windowing and graphics toolkitjavax.swing — GUI development package
Packages
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Java is a package‐centric language; for good organization and name scoping, put all classes into packages.
A class with default access can be seen only by classes within the same package.
If class A and class B are in different packages, and class A has default access, class B won't be able to create an instance of class A, or even declare a variable or return type of class A.
import statement
Compiled By: Edwin O. Okech [Tutor, Amoud University]
Full library class names include the package name. For example:java.awt.Colorjavax.swing.JButton
import statements at the top of the source file let you refer to library classes by their short names:import javax.swing.JButton; Fully-qualified
...JButton go = new JButton("Go");
Import statements
Compiled By: Edwin O. Okech [Tutor, Amoud University]
You can import names for all the classes in a package by using a wildcard .*:import java.awt.*;import java.awt.event.*;import javax.swing.*;
The above Imports all classes from awt, awt.event and swing packages.
java.lang is imported automatically into all classes; defines System, Math, Object, String, and other commonly used classes.