Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Introduction to Java_chapter1

Chapter 1 introduces Java, a versatile programming language developed for Internet applications, highlighting its features such as simplicity, security, and portability. It covers the Java language specification, API, JDK, and IDE, as well as the process of creating, compiling, and executing Java programs. The chapter also discusses programming style, documentation, and common programming errors.

Uploaded by

Mohammed Breka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction to Java_chapter1

Chapter 1 introduces Java, a versatile programming language developed for Internet applications, highlighting its features such as simplicity, security, and portability. It covers the Java language specification, API, JDK, and IDE, as well as the process of creating, compiling, and executing Java programs. The chapter also discusses programming style, documentation, and common programming errors.

Uploaded by

Mohammed Breka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 1

INTRODUCTION TO
COMPUTERS,
PROGRAMS, AND JAVA
Lecture notes for computer programming 1
Faculty of Engineering and Information Technology
Prepared by: Iyad Albayouk
Java, World Wide Web, and
Beyond
Java was developed by a team led by James
Gosling at Sun Microsystems.
Originally called Oak, it was designed in 1991 for
use in embedded chips in consumer electronic
appliances.
In 1995, renamed Java, it was redesigned for
developing Internet applications.
Java is simple, object oriented, distributed,
interpreted, robust, secure, architecture neutral,
portable, high performance, multithreaded, and
dynamic.
Java, World Wide Web, and
Beyond, cont.
Java initially became attractive because Java programs can be run
from a Web browser. Such programs are called applets.

FIGURE 1.1 A Java applet for playing TicTacToe is embedded in an HTML page.
Java, World Wide Web, and
Beyond, cont.
Java can also be used to develop applications on the server side.
These applications can be run from a Web server to generate dynamic Web
pages.

FIGURE 1.2 Java was used to develop an automatic grading system to


accompany this book.
Java, World Wide Web, and
Beyond, cont.
Java is a versatile programming language. You can use it to develop
applications on your desktop and on the server.
You can also use it to develop applications for small handheld
devices.
The software for Android cell phones is developed using Java.

Figure 1.3 Java is used in Android phones.


The Java Language Specification, API, JDK,
and IDE
• Java syntax is defined in the Java language specification, and the
Java library is defined in the Java API.
• The JDK is the software for developing and running Java
programs.
• An IDE is an integrated development environment for rapidly
developing programs.

• Java is a full-fledged and powerful language that can be used in


many ways. It comes in three editions:
Java Standard Edition (Java SE) to develop client-side
standalone applications or applets.
Java Enterprise Edition (Java EE) to develop server-side
applications, such as Java servlets, JavaServer Pages (JSP),
and JavaServer Faces (JSF).
Java Micro Edition (Java ME) to develop applications for mobile
devices, such as cell phones.
A Simple Java Program
A Java program is executed from the main method in the class.

public class Welcome {

public static void main(String[] args) {

// Display message Welcome to Java! on the console


System.out.println("Welcome to Java!");
}
}

Welcome to Java!
Creating, Compiling, and Executing a
Java Program
• You save a Java program in a .java file and compile it into a
.class file. The .class file is executed by the Java Virtual Machine.

FIGURE 1.4 The Java program-development process consists of repeatedly


creating/modifying source code, compiling, and executing programs.
Creating, Compiling, and Executing a
Java Program, cont.

FIGURE 1.5 (a) Java source code is translated into bytecode. (b) Java bytecode
can be executed on any computer with a Java Virtual Machine.
Displaying Text in a Message Dialog Box
/* This application program displays Welcome to Java!
in a message dialog box.
*/
import javax.swing.JOptionPane;

public class WelcomeInMessageDialogBox {

public static void main(String[] args) {

// Display Welcome to Java! in a message dialog box


JOptionPane.showMessageDialog(null,"Welcome to Java!");
}
}

FIGURE 1.6 “Welcome to Java!” is displayed in a message box.


Displaying Text in a Message Dialog Box ,
cont.
• There are several ways to use the showMessageDialog method. For the
time being, you need to know only two ways. One is to use a statement,
as shown in the example:

JOptionPane.showMessageDialog(null, x);
where x is a string for the text to be displayed.

The other is to use a statement like this one:


JOptionPane.showMessageDialog(null, x,y, JOptionPane.INFORMATION_MESSAGE);
Programming Style and
Documentation

Appropriate Comments
Naming Conventions
Proper Indentation and Spacing Lines
Block Styles
Appropriate Comments
Include a summary at the beginning of the
program to explain what the program does, its
key features, its supporting data structures,
and any unique techniques it uses.

Include your name, class section, instructor,


date, and a brief description at the beginning
of the program.
Naming Conventions
Choose meaningful and descriptive names.
Variables and method names:
Use lowercase. If the name consists of several
words, concatenate all in one, use lowercase for
the first word, and capitalize the first letter of
each subsequent word in the name. For
example, the variables radius and area, and the
method computeArea.
Naming Conventions, cont.
Class names:
Capitalize the first letter of each word in the
name. For example, the class name
ComputeArea.

Constants:
Capitalize all letters in constants, and use
underscores to connect words. For example,
the constant PI and MAX_VALUE
Proper Indentation and Spacing
Indentation
Indent two spaces.

Spacing
Use blank line to separate segments of the
code.
Block Styles
Use end-of-line style for
braces.
Next-line public class Test
style {
public static void main(String[] args)
{
System.out.println("Block Styles");
}
}

End-of-line
style
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
}
}
Programming Errors
Syntax Errors
Detected by the compiler
Runtime Errors
Causes the program to abort
Logic Errors
Produces incorrect result
Syntax Errors
public class ShowSyntaxErrors {
public static main(String[] args) {
System.out.println("Welcome to Java);
}
}
Runtime Errors
public class ShowRuntimeErrors {
public static void main(String[] args) {
int i = 1 / 0;
}
}
Logic Errors
public class ShowLogicErrors {
public static void main(String[] args) {
System.out.println("Celsius 35 is Fahrenheit degree
");
System.out.println((9 / 5) * 35 + 32);
}
}

You might also like