Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
JAVA PROGRAMMING LESSON 2 CREATING A JAVA APPLICATION Compiled By: Edwin O. Okech [Tutor, Amoud University] Outline 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. i. Compiled By: Edwin O. Okech [Tutor, Amoud University] Steps to Creating a Java Application Create/ Modify the Source Code using Text Editor If Error Compile Source Code using Java Compiler Saves on Disk Produces If Run Time Error or Incorrect results Run Byte Code using Java Virtual Machine Compiled By: Edwin O. Okech [Tutor, Amoud University] Results Source Code (.java) Byte Code (.class) Program Execution Create a Source File 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. 1. Compiled By: Edwin O. Okech [Tutor, Amoud University] Demonstrate how to open a Java IDE e.g. Netbeans Start - Programs – Netbeans Example 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"); } } Compiled By: Edwin O. Okech [Tutor, Amoud University] Analyzing the Example HelloApp.java // 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 class This are is the body of the class HelloApp. All of the data and methods for this class will be between the curly braces } Compiled By: Edwin O. Okech [Tutor, Amoud University] Analyzing the Example HelloApp.java // This is my first Java program. public class HelloApp { This is Java main method. Every application must have a main method. public static void main (String args[]) 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 } } Compiled By: Edwin O. Okech [Tutor, Amoud University] Analyzing the Example HelloApp.java // 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 Compiled By: Edwin O. Okech [Tutor, Amoud University] Structure of Java programme Comments Keywords Modifiers Statements Blocks Classes Methods The main method Package Compiled By: Edwin O. Okech [Tutor, Amoud University] Comments Help the programmers to communicate and understand the program. Not a programming statement, thus ignored by the compiler. Three Types of Comments: 1. 2. 3. Multiline comments Single line comments Javadoc comments Compiled By: Edwin O. Okech [Tutor, Amoud University] Multi line Comments /* This is a comment with four lines of text. Make sure you have the pair. */ Compiled By: Edwin O. Okech [Tutor, Amoud University] Single line comments // This is a single line comment // Each line must have the double back slash Compiled By: Edwin O. Okech [Tutor, Amoud University] Javadoc comments /** * This program converts the temperature value in * Celsius into Fahrenheit. */ Compiled By: Edwin O. Okech [Tutor, Amoud University] Java keywords 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. Compiled By: Edwin O. Okech [Tutor, Amoud University] Java keywords abstract boolean extends new throw char goto return const implements short while else break final package throws class if try continue import static double long byte finally private transient float protected void default instanceof strictfp interface Compiled By: Edwin O. Okech [Tutor, Amoud University] synchronized case native this catch for public volatile do int super switch Modifiers 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 Compiled By: Edwin O. Okech [Tutor, Amoud University] Modifiers: examples public class ClassA { public static void main (String[] args) { System.out.println ("Try your best"); } } Compiled By: Edwin O. Okech [Tutor, Amoud University] Statements 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 (;). Compiled By: Edwin O. Okech [Tutor, Amoud University] Statement blocks 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. Compiled By: Edwin O. Okech [Tutor, Amoud University] Classes 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. Compiled By: Edwin O. Okech [Tutor, Amoud University] Example of a class public class ClassA { public static void main (String[] args) { System.out.println ("Try your best"); } } Compiled By: Edwin O. Okech [Tutor, Amoud University] Methods 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). Compiled By: Edwin O. Okech [Tutor, Amoud University] main Method 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; } } Compiled By: Edwin O. Okech [Tutor, Amoud University] main Method 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. Compiled By: Edwin O. Okech [Tutor, Amoud University] main Method 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 Compiled By: Edwin O. Okech [Tutor, Amoud University] main Method 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 Compiled By: Edwin O. Okech [Tutor, Amoud University] main Method 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. Compiled By: Edwin O. Okech [Tutor, Amoud University] Libraries 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 classes java.awt — windowing and graphics toolkit javax.swing — GUI development package Compiled By: Edwin O. Okech [Tutor, Amoud University] Packages 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. Compiled By: Edwin O. Okech [Tutor, Amoud University] import statement Full library class names include the package name. For example: java.awt.Color javax.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"); Compiled By: Edwin O. Okech [Tutor, Amoud University] Import statements 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. Compiled By: Edwin O. Okech [Tutor, Amoud University] THE END END JAVA PROGRAMMING - LESSON 2 (CREATING A JAVA APPLICATION) Compiled By: Edwin O. Okech [Tutor, Amoud University]