LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Your First Java Program: HelloWorld.java
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
How to Create a Java program CS115 Fall George Koutsogiannakis.
Excerpts from Introduction to Java Programming, 4E Author: Y. Daniel Liang (Copyright by Prentice Hall)
Introduction to Java Programming, 4E
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
Outline Java program structure Basic program elements
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
S.W. Ma/CIM/LWL41211/2 Prog. IIA Page 1 HKIVE (Lee Wai Lee Campus) Department of CIM Course : Year 2 Module : Programming IIA Textbook : Introduction.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
Java 程序设计 Java Programming Fall, Contents for Today Java Program Structure  How to Compile a Java Program  How to Run a Java Program Environment.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Java: Chapter 1 Computer Systems Computer Programming II.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Java Programming. History F James Gosling and Sun Microsystems F Oak F Java, May 20, 1995, Sun World F HotJava –The first Java-enabled.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
POS 406 Java Technology And Beginning Java Code
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Introduction to Java Programming with Forte Y. Daniel Liang.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 (part 2) Khalid Siddiqui.
Chapter 2: Java Fundamentals
Introduction To JAVA By Ihtesham Ul Haq. Course Objectives Upon completing the course, you will understand Upon completing the course, you will understand.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Introduction to programming in the Java programming language.
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
Inheritance A Review of Objects, Classes, and Subclasses.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Introduction to Java Programming. 2 Chapter 1 Introduction to Java and Forte F What Is Java? F Getting Started With Java Programming –Create, Compile.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
OOP Basics Classes & Methods (c) IDMS/SQL News
Object Oriented Programming Object and Classes Lecture 3 MBY.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Console Input and Output JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Introduction to Java Programming, 4E Y. Daniel Liang.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
JAVA MULTIPLE CHOICE QUESTION.
Working with Java.
3 Introduction to Classes and Objects.
Chapter 1: Computer Systems
Java Intro.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Anatomy of a Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Presentation transcript:

LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]

Outline i. 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 – 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"); }

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 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

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] Comments Keywords Modifiers Statements Blocks Classes Methods The main method Package

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 comments 2. Single line comments 3. Javadoc comments

Multi line Comments Compiled By: Edwin O. Okech [Tutor, Amoud University] /* This is a comment with four lines of text. 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 booleanbreak byte case extendsfinal finally native new package private this throw throws transientcatch char class floatfor goto if protectedpublic return tryvoidvolatile const continuedefaultdo implements import instanceofint short static strictfpsuper while double interfaceswitch

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 classes  java.awt — windowing and graphics toolkit  javax.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.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");

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.

THE END Compiled By: Edwin O. Okech [Tutor, Amoud University] END JAVA PROGRAMMING - LESSON 2 (CREATING A JAVA APPLICATION)