0% found this document useful (0 votes)
14 views

lec2

Uploaded by

z9mohamed74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

lec2

Uploaded by

z9mohamed74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

1

Computer
Programming l2
Dr: Marwa AL Enany
A Simple Java Program
2
Listing 1.1

Run Welcome to Java


A Simple Java Program
3
• Line 1 defines a class. Every Java program must have at
least one class.
• Each class has a name (Welcome in our example).
• By convention, class names start with an uppercase letter.
• Line 2 defines the main method.
• A class may contain several methods. The main method is
the entry point where the program begins execution.
• A method is a construct that contains statements.
• 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).
A Simple Java Program
4

• String is a programming term meaning a sequence of


characters.
• A string must be enclosed in double quotation marks.
• Every statement in Java ends with a semicolon (;).
• Reserved words, or keywords, 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 this program are public, static, and
void.
A Simple Java Program
5
• Line 3 is a comment that documents what the program is and
how it is constructed.
• Comments help programmers to communicate and understand
the program. They are not programming statements and thus
are ignored by the compiler.
• In Java, comments are preceded by two slashes (//) on a line,
called a line comment, or enclosed between /* and */ on one or
several lines. Here are examples of comments:
A Simple Java Program
6
Listing 1.2: Welcome with three messages

Run
A Simple Java Program
7

𝟏𝟎.𝟓+𝟐𝟑
Listing 1.3: Example of evaluating
𝟒𝟓−𝟑.𝟓

Run
Creating, Compiling, and Running Java
Programs
8
Compiling Java Source Code
9
You can port a source program to any machine with appropriate
compilers.
The source program must be recompiled, however, because the object
program can only run on a specific machine.
Java was designed to run object programs on any platform.
With Java, you write the program once, and compile the source
program into a special type of object code, known as bytecode.
The bytecode can then run on any computer with a Java Virtual
Machine.
Java Virtual Machine is a software that interprets Java bytecode.
Anatomy of a Java Program
10
Class name
Main method
Statements
Statement terminator
Reserved words
Comments
Blocks
Class Name 11
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.

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Main Method
12
In order to run a class, the class must contain a
method named main.
The program is executed from the main method.

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Statement
13
A statement represents an action or a sequence of
actions.
The statement System.out.println("Welcome to
Java!") in the program in Listing 1.1 is a statement to
display the greeting "Welcome to Java!“.

//This program prints Welcome to Java!


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

Every statement in Java ends with a semicolon (;).

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Reserved words 15
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.
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Blocks
16

A pair of braces in a program forms a block that groups


components of a program.
Special Symbols
17
{ …}
// This program prints Welcome to Java! 18
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

) …(
;
// This program prints Welcome to Java! 19
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

// …
"…" 20
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Programming Style and Documentation
21

Appropriate Comments
Naming Conventions
Proper Indentation and Spacing Lines
Block Styles
Appropriate Comments 22

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
23

Choose meaningful and descriptive names.


Class names:
– Capitalize the first letter of each word in the name.
For example, the class name ComputeExpression.
Proper Indentation and
Spacing 24

Indentation
– Indent two spaces.
Spacing
– Use blank line to separate segments of the code.
Block Styles
25

Use end-of-line style for braces.


Programming Errors
26

Syntax Runtime Logical


Errors Errors Errors
Detected Causes the Produces
by the program to incorrect
compiler abort result
27
Syntax Errors

Two errors are reported:


• The keyword void is missing before main in line 2.
• The string Welcome to Java should be closed with a closing
quotation mark in line 3.

Guidelines:
➢ A single error will often display many lines of compile errors
➢ Fix errors from the top line and work downward.
➢ Fixing errors that occur earlier in the program may also fix
additional errors that occur later.
Runtime Errors
28
Runtime errors are errors that cause a program to terminate
abnormally.

An example of runtime errors is division by zero. This


happens when the divisor is zero for integer divisions.
Logical Errors
29
Logical errors occur when a program does not perform the way
it was intended to.

You will get Fahrenheit 67 degrees, which is wrong. It should


be 95.0. In Java, the division for integers is an integer—the
fractional part is truncated—so in Java 9 / 5 is 1. To get the
correct result, you need to use 9.0 / 5, which results in 1.8.

You might also like