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

Lecture 1 - Java Programming-Introduction.docx

Uploaded by

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

Lecture 1 - Java Programming-Introduction.docx

Uploaded by

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

Java Programming 1st Lecture

Introduction

Topics

 Java, the World Wide Web and beyond


 The Java Language Specification, API, JDK, and IDE
 A Simple Java Program
 Syntax Errors
 Beginning to Write Java Program
 Reading Input from the Console
 Identifiers
 Named Constants
 Displaying Text with printf
 Decision Making: Equality and Relational Operators
1- Java, the World Wide Web and beyond

Java is a powerful and versatile programming language for developing software running on
mobile devices, desktop computers, and servers.

Brief historical survey:


 Java was developed by a team led by James Gosling at Sun Microsystems. Sun
Microsystems was purchased by Oracle in 2010. It was Originally called Oak, Java was
designed in 1991 for use in embedded chips in consumer electronic appliances.

In 1995, renamed Java, it was redesigned for developing Web applications. After that it
becomes enormously popular. Its rapid rise and wide acceptance can be traced to its design
characteristics. Java is simple, object oriented, distributed, interpreted, robust, secure,
architecture neutral, portable, high performance, multithreaded, and dynamic.

Java initially became attractive because Java programs can be run from a Web browser. Such
programs are called applets

Applets employ a modern graphical interface with buttons, text fields, text areas, radio buttons,
and so on, to interact with users on the Web and process their requests. Applets make the Web
responsive, interactive, and fun to use.

Applets are embedded in an HTML file. HTML (Hypertext Markup Language) is a simple
scripting language for laying out documents, linking documents on the Internet, and bringing
images, sound, and video alive on the Web.

Java can be used to develop rich Internet applications. A rich Internet application (RIA) is a
Web application designed to deliver the same features and functions normally associated with
desktop applications.

Java is now very popular for developing applications on Web servers. These applications
process data, perform computations, and generate dynamic Web pages. Many commercial
Websites are developed using Java on the backend.
Java is a versatile programming language: it is used to develop applications for desktop
computers, servers, and small handheld devices. The software for Android cell phones is
developed using Java.

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

The Java language specification is a technical definition of the Java programming language’s
syntax and semantics. The application program interface (API), also known as library, contains
predefined classes and interfaces for developing Java 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 applications. The applications
can run standalone or as applets running from a Web browser.

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

3- A Simple Java Program

This java program displays the message “Welcome to Java!” on the console. The word
“console” is a computer term that refers to the text entry and display device of a computer. The
program is shown in Listing 1.

LISTING 1 Welcome.java

1 public class Welcome


{
2 public static void main(String[] args)
{
3 // Display message Welcome to Java! on the console
4 System.out.println("Welcome to Java!");
5 }
6}
Note: The line numbers are for reference purposes only; they are not part of the program.

Line 1 defines a class. Every Java program must have at least one class. Each class has a name.
By convention, class names start with an uppercase letter. In this example, the class name is
Welcome.

Line 2 defines the main method. The program is executed from the main method. A class may
contain several methods. The main method is the entry point where the program begins execution.
The main method in this program contains the System.out.println statement. This statement
displays the string Welcome to Java! on the console (line 4).

Note1: Every statement in Java ends with a semicolon (;), known as the statement terminator.

Note2: A pair of curly braces in a program forms a block that groups the program’s components. In
Java, each block begins with an opening brace ({) and ends with a closing brace (}).

Note3: Every class has a class block that groups the data and methods of the class. Similarly, every
method has a method block that groups the statements in the method.
Note3: Blocks can be nested, meaning that one block can be placed within another, as shown in the
following code.

Caution: Java source programs are case sensitive. It would be wrong, for example, to replace
main in the program with Main.
Table 1 below shows some special characters (e.g., { }, //, ;), They are almost used in every
program.

Syntax Errors: Like any programming language, Java has its own syntax, and it is compulsory
to write code that conforms to the syntax rules. If a program violates a rule for example, if the
semicolon is missing, a brace is missing, a quotation mark is missing, or a word is misspelled,
the Java compiler will report syntax errors.
4- Beginning to Write Java Program

Now, by writing a program i.e. with Java and like any other language, it is possible to solve
problems. Through these problems a beginner can learn elementary programming using
primitive data types, variables, constants, operators, expressions, and input and output.

Writing a program involves designing algorithms and translating algorithms into programming
instructions, or code.
An algorithm describes how a problem is solved by listing the actions that need to be taken and
the order of their execution. Algorithms can help the programmer plan a program before writing
it in a programming language. Algorithms can be described in natural languages or in pseudo
code (natural language mixed with some programming code).
The following example computes the area of circle. Hence, the program needs to read the radius
entered by the user from the keyboard. This raises two important issues:
 Reading the radius.
 Storing the radius in the program.
At first, the second point “storing” is considered and then implementing program by reading
input from keyboard will be learned.
public class ComputeArea
{
public static void main(String[ ] args)
{
double radius;
double area;

// Step 1: Read in radius


// Step 2: Compute area
// Step 3: Display the area
}
}
The program declares radius and area as variables. The reserved word double indicates that
radius and area are floating-point values stored in the computer.

1 public class ComputeArea {


2 public static void main (String[ ] args) {
3 double radius; // Declare radius
4 double area; // Declare area
5
6 // Assign a radius
7 radius = 20; // radius is now 20
8
9 // Compute area
10 area = radius * radius * 3.14159;
11
12 // Display results
13 System.out.println("The area for the circle of radius " +
14 radius + " is " + area);
15 }
16 }

Note: The plus sign (+) in lines 13–14 is called a string concatenation operator. It combines
two strings into one. If a string is combined with a number, the number is converted into a string
and concatenated with the other string. Therefore, the plus signs (+) in lines 13–14 concatenate
strings into a longer string, which is then displayed in the output.

5- Reading Input from the Console


Java uses System.out to refer to the standard output device and System.in to the standard input
device. By default, the output device is the display monitor and the input device is the keyboard.

To perform console output, it just requires using the println method to display a primitive value
or a string to the console.
Console input is not directly supported in Java, but it is possible to use the Scanner class to
create an object to read input from System.in, as follows:

Scanner input = new Scanner (System.in);

 The syntax new Scanner(System.in) creates an object of the Scanner type.


 The syntax “Scanner input” declares that input is a variable whose type is Scanner.

 The whole line Scanner input = new Scanner(System.in) creates a Scanner object and
assigns its reference to the variable input. An object may invoke its methods.

 To invoke a method on an object is to ask the object to perform a task. For example the
invoke of the nextDouble( ) method to read a double value as follows:
double radius = input.nextDouble( );
ComputeAreaWithConsoleInput.java
1 import java.util.Scanner; // Scanner is in the java.util package
2
3 public class ComputeAreaWithConsoleInput {
4 public static void main(String[] args) {
5 // Create a Scanner object
6 Scanner input = new Scanner(System.in);
7
8 // Prompt the user to enter a radius
9 System.out.print("Enter a number for radius: ");
10 double radius = input.nextDouble( );
11
12 // Compute area
13 double area = radius * radius * 3.14159;
14
15 // Display results
16 System.out.println("The area for the circle of radius " + radius + " is " + area);
17 }
18 }
Note :
The Scanner class is in the java.util package. It is imported in line 1. There are two types of
import statements: specific import and wildcard import. The specific import specifies a single
class in the import statement. For example, the following statement imports. Scanner from the
package java.util.
import java.util.Scanner;
The “wildcard import ” imports all the classes in a package by using the asterisk as the
wildcard. For example, the following statement imports all the classes from the package
java.util.
import java.uitl.*;
6- Identifiers
Identifiers are the names that identify the elements such as classes, methods, and variables in a
program. All identifiers must obey the following rules:

 An identifier is a sequence of characters that consists of letters, digits, underscores (_),


and dollar signs ($).
 An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot
start with a digit.
 An identifier cannot be a reserved word.
 An identifier cannot be true, false, or null.
 An identifier can be of any length.

7- Named Constants
A named constant is an identifier that represents a permanent value. The value of a variable may
change during the execution of a program, but a named constant, or simply constant, represents
permanent data that never changes. The syntax for declaring constant is shown in the following:

final datatype CONSTANTNAME = value;

A constant must be declared and initialized in the same statement. The word final is a Java
keyword for declaring a constant.
ComputeAreaWithConstant.java
1 import java.util.Scanner; // Scanner is in the java.util package
2
3 public class ComputeAreaWithConstant {
4 public static void main(String[] args) {
5 final double PI = 3.14159; // Declare a constant
6
7 // Create a Scanner object
8 Scanner input = new Scanner(System.in);
9
10 // Prompt the user to enter a radius
11 System.out.print("Enter a number for radius: ");
12 double radius = input.nextDouble( );
13 // Compute area
14 double area = radius * radius * PI;
15 // Display result
16 System.out.println("The area for the circle of radius " + radius + " is " + area);
17 }
18 }

8- Displaying Text with printf


The System.out.printf method (f means “formatted”) displays formatted data. The code below
uses this method to output the strings "Welcome to" and "Java Programming!".
System.out.printf( "%s\n%s\n",
"Welcome to", "Java Programming!" );
 Method printf’s first argument is a format string that may consist of fixed text and
format specifiers.
 Fixed text is output by printf just as it would be by print or println.
 Each format specifier is a placeholder for a value and specifies the type of data to output.

 Format specifiers begin with a percent sign (%) followed by a character that represents
the data type. For example, the format specifier %s is a placeholder for a string.

 The format string in the previous code specifies that printf should output two strings,
each followed by a newline character.

9- Decision Making: Equality and Relational Operators


A condition is an expression that can be true or false. This section introduces Java’s if
selection statement, which allows a program to make a decision based on a condition’s value.
For example, the condition “grade is greater than or equal to 60” determines whether a student
passed a test. If the condition in an if statement is true, the body of the if statement executes. If
the condition is false, the body does not execute.

Conditions in if statements can be formed by using the equality operators (== and !=) and
relational operators (>, <, >= and <=) summarized in the figure below. Both equality operators
have the same level of precedence, which is lower than that of the relational operators. The
equality operators associate from left to right. The relational operators all have the same level of
precedence and also associate from left to right.

You might also like