java sawaluddin bahan ujian


Introduction to Java Programming
Sawaluddin
Introduction
Course Objectives

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)
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
Programming Concepts

Programming in the Big Picture?

What is JAVA?

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

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)

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

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.

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 ?
Example: An Automated Bank Teller Machine (ATM)
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.

What Is Java?
History
Characteristics of Java

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

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

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)
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.
Java IDE Tools
Forte by Sun MicroSystems
Borland JBuilder
Microsoft Visual J++
WebGain Café
IBM Visual Age for Java
JCreator,
Etc

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.

Getting Started with Java Programming
A Simple Java Application
Compiling Programs
Executing Applications
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!”);
}
}
Creating and Compiling Programs
On command line

javac file.java
Executing Applications
On command line
java classname
Example
javac Welcome.java

java Welcome

output:…
Compiling and Running a Program
Anatomy of a Java Program
Comments
Package
Reserved words
Modifiers
Statements
Blocks
Classes
Methods
The main method
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 */.
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.
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.
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.”
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 (;).
Blocks
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.
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.
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;
}
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.”
The showMessageDialog Method
JOptionPane.showMessageDialog(null, “Welcome to Java!”,”Example 1.2″, JOptionPane.INFORMATION_MESSAGE));

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.

Tinggalkan Balasan

Please log in using one of these methods to post your comment:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s