Introduction To Java Programming

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 40

Introduction to

Java Programming
Introduction
 Course Objectives

2
Course Objectives
 Upon completing the course, you will understand
– Create, compile, and run Java programs
– Primitive data types
– Java control flow
– Methods
– Arrays (for teaching Java in two semesters, this could be the end)
– Object-oriented programming
– Core Java classes (Swing, exception, internationalization,
multithreading, multimedia, I/O, networking, Java
Collections Framework)

3
Course Objectives, cont.
 You will be able to
– Develop programs
– Write simple programs using primitive data
types, control statements, methods, and arrays.
– Create and use methods
– Develop a GUI interface and Java applets
– Write interesting projects
– Establish a firm foundation on Java concepts

4
Programming Concepts

 Programming in the Big Picture?

 What is JAVA?

5
Programming in the Big Picture?
 What are Computers Used For?
- Word Processing   (i.e., typing up some documents and
printing them)
– Business Applications   (i.e., accounting, spreadsheets,
presentations,...)
– Engineering Applications   (i.e., scientific analysis,
simulations)
– Database Management   (i.e., police records, stock market, ...)
– Entertainment   (i.e., games, multimedia applications, ...)
– Manufacturing   (i.e., CAD/CAM, robotics, assembly, ...)
– Many more things
6
Who is Involved With Computers ?
 System/Hardware Designers   (The people that design
computers and related products)
 Manufacturers   (The people that actually build and
assemble computers)
 Software Designers   (The people that design
applications to be used with the computers)
 Programmers   (The people that write computer
programs to achieve working applications, games and
other software packages).
 User   (The people that buy and use the software)

7
– We are going to play the part of the Programmer
in this course, although we will also be designing
our programs (software) and hence we will also be
the Software Designer.  When we test our
programs, we'll pretend that we are the User. 
Testing is an important part of programming to
ensure that the User is happy with software that is
not full of Bugs!

8
What is program?
 A program is traditionally known as
– a sequence of instructions that can be executed by a
computer to solve some problem.
Here, we will learn to write our own programs to
solve some very simple problems.
There are different styles (types) of programming:
– procedural (structured)
– logical (specifying constraints and conditions)
– object-oriented

9
What is Object Oriented Programming ?

 Object Oriented programming is


– a method of programming that allows a programmer
to build a more natural model of a "real world"
problem within the computer.
– based on the creation of objects which often
represent their real world equivalent, but not always.

10
 For example, consider the following
programming scenario and try to come up with
some objects that might be used in an object
oriented program.  Also, try to think of what
operations might be needed to interact with the
objects.  That is, what information do we need to
know about the objects and how do we get the
information ?

11
Example: An Automated Bank Teller
Machine (ATM)
OBJECTS OPERATIONS
Money Withdraw or Deposit
Buttons Is a button being pressed ? In what Order ?
Display Window Display greeting, main menu, withdrawal message,...
Deposit Envelope What's in it: cash or cheque ?

Bank Card Has one been inserted properly ? What account is it for ?

Transaction Slip Is one required ? What transaction Information ?

Bank Account Who's account ? What type ? How much funds ?

Cheque From who ? To who ? How much ?

Customer Name, address, account number ?


PIN What's the special "secret" password ?
12
Important Issues When Developing
Software:
 There are several important issues when developing any type of computer
software.  Any "good" software MUST adhere to these standards:
 C orrectness - Make sure that your program does what it is supposed to
do.
 R obustness - Make sure that the program does not crash.  Exhaustive
testing helps prevent this.
 I nterface Usability - Make sure that the interface is easy to use and
intuitive.
 S implicity - Keep the code as simple as possible, while still meeting
requirements.  Modifications are often required and hence code
maintenance is a big issue.
 P resentation and Documentation - Make sure that software is
thoroughly documented for maintenance purposes.  (Don't forget about
the TA's that need to mark your assignments)
 E fficiency - Make sure that the software is time and space efficient.

13
What Is Java?
 History
 Characteristics of Java

 JAVA is:
– An programming language from SUN Microsystems.
– Object-Oriented
– Interpreted

14
History
 James Gosling and Sun Microsystems
 Oak
 Java, May 20, 1995, Sun World
 HotJava
– The first Java-enabled Web browser
 JDK Evolutions
 J2SE, J2ME, and J2EE (not mentioned in the
book, but could discuss here optionally)
15
Characteristics of Java
 Java is simple
 Java is object-oriented
 Java is distributed
 Java is interpreted
 Java is robust
 Java is secure
 Java is architecture-neutral
 Java is portable
 Java’s performance
 Java is multithreaded
 Java is dynamic

16
JDK Versions
 JDK 1.02 (1995)
 JDK 1.1 (1996)
 Java 2 SDK v 1.2 (a.k.a JDK 1.2, 1998)
 Java 2 SDK v 1.3 (a.k.a JDK 1.3, 2000)
 Java 2 SDK v 1.4 (a.k.a JDK 1.4, 2002)

17
JDK Editions
 Java Standard Edition (J2SE)
– J2SE can be used to develop client-side standalone
applications or applets.
 Java Enterprise Edition (J2EE)
– J2EE can be used to develop server-side applications such
as Java servlets and Java ServerPages.
 Java Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile
devices such as cell phones.

This courses uses J2SE to introduce Java


programming.
18
Java IDE Tools
 Forte by Sun MicroSystems
 Borland JBuilder
 Microsoft Visual J++
 WebGain Café
 IBM Visual Age for Java
 JCreator,
 Etc

19
 When programming in JAVA, you will usually use the
following building blocks:
 classes from the JAVA class libraries
 classes you create yourself
 classes that other people make available to you

 Most of the classes that you make will be using other


classes by creating and using objects belonging to that
class.
 Using the JAVA class libraries whenever possible is a
good idea since
– the classes are carefully written and are efficient.
– it would be silly to write code that is already available to you.

20
How does JAVA really work ?

21
Getting Started with Java
Programming
 A Simple Java Application
 Compiling Programs
 Executing Applications

22
A Simple Application
Example 1.1

//This application program prints Welcome


//to Java!

public class Welcome {


public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

23
Creating and Compiling Programs
 On command line

– javac file.java

24
Executing Applications
 On command line
– java classname

Bytecode

Java Java Java


Interpreter Interpreter Interpreter
...
on Windows on Linux on Sun Solaris

25
Example
javac Welcome.java

java Welcome

output:...

26
Compiling and Running a Program
Where are the files
Welcome.java
stored in the
c:\example directory?
chapter1 Welcome.class

Welcome.java~

chapter2 Java source files and class files for Chapter 2

.
.
.
chapter19 Java source files and class files for Chapter 19

27
Anatomy of a Java Program
 Comments
 Package
 Reserved words
 Modifiers
 Statements
 Blocks
 Classes
 Methods
 The main method
28
Comments

In Java, comments are preceded by two


slashes (//) in a line, or enclosed
between /* and */ in one or multiple lines.
When the compiler sees //, it ignores all
text after // in the same line. When it
sees /*, it scans for the next */ and ignores
any text between /* and */.
29
Package

The second line in the program


(package chapter1;) specifies a package
name, chapter1, for the class Welcome.
Forte compiles the source code in
Welcome.java, generates
Welcome.class, and stores
Welcome.class in the chapter1 folder.
30
Reserved Words
Reserved words or keywords are words that
have a specific meaning to the compiler and
cannot be used for other purposes in the
program. For example, when the compiler sees
the word class, it understands that the word
after class is the name for the class. Other
reserved words in Example 1.1 are public,
static, and void. Their use will be introduced
later in the courses.
31
Modifiers
Java uses certain reserved words called
modifiers that specify the properties of the
data, methods, and classes and how they can
be used. Examples of modifiers are public
and static. Other modifiers are private, final,
abstract, and protected. A public datum,
method, or class can be accessed by other
programs. A private datum or method cannot
be accessed by other programs. Modifiers are
discussed in Chapter 6, "Objects and Classes."
32
Statements
A statement represents an action or a
sequence of actions. The statement
System.out.println("Welcome to Java!") in
the program in Example 1.1 is a statement
to display the greeting "Welcome to Java!"
Every statement in Java ends with a
semicolon (;).

33
Blocks
A pair of braces in a program forms a block
that groups components of a program.

public class Test {


Class block
public static void main(String[] args) {
System.out.println("Welcome to Java!"); Method block
}
}

34
Classes
The class is the essential Java construct. A class
is a template or blueprint for objects. To
program in Java, you must understand classes
and be able to write and use them. The mystery
of the class will continue to be unveiled
throughout this book. For now, though,
understand that a program is defined by using
one or more classes.

35
Methods
What is System.out.println? It is a method: a
collection of statements that performs a sequence of
operations to display a message on the console. It can
be used even without fully understanding the details
of how it works. It is used by invoking a statement
with a string argument. The string argument is
enclosed within parentheses. In this case, the
argument is "Welcome to Java!" You can call the
same println method with a different argument to
print a different message.

36
main Method
The main method provides the control of
program flow. The Java interpreter executes the
application by invoking the main method.
 
The main method looks like this:
 public static void main(String[] args) {
// Statements;
}

37
Displaying Text in a Message
Dialog Box
you can use the showMessageDialog
method in the JOptionPane class.
JOptionPane is one of the many
predefined classes in the Java system,
which can be reused rather than
“reinventing the wheel.”

38
The showMessageDialog Method
JOptionPane.showMessageDialog(null, "Welcome
to Java!","Example 1.2",
JOptionPane.INFORMATION_MESSAGE));

39
The exit Method
Use Exit to terminate the program and stop
all threads.

NOTE: When your program starts, a thread


is spawned to run the program. When the
showMessageDialog is invoked, a separate
thread is spawned to run this method. The
thread is not terminated even you close the
dialog box. To terminate the thread, you
have to invoke the exit method.
40

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy