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

2 - Elementary Programming 1_Syntax, Comments, Variables

Chapter 2 of the document focuses on elementary programming concepts in Java, including syntax, variables, and the main method. It covers how to write simple Java programs, use identifiers for naming, and perform basic computations with operators. The chapter also discusses Java comments, variable declaration, assignment statements, and naming conventions.

Uploaded by

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

2 - Elementary Programming 1_Syntax, Comments, Variables

Chapter 2 of the document focuses on elementary programming concepts in Java, including syntax, variables, and the main method. It covers how to write simple Java programs, use identifiers for naming, and perform basic computations with operators. The chapter also discusses Java comments, variable declaration, assignment statements, and naming conventions.

Uploaded by

3slm3al
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Chapter 2 Elementary Programming

Part (1)

Modified from:
-W3Schools.com
-Introduction to Java Programming, Liang, 10 th Edition

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
1
Introduction

● This lecture will cover how write Java programs


to perform simple computations , how use
identifiers to name variables, constants, methods,
classes and variables to store data , and how
perform operations using operators +, -, *, /, and
%.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
2
Outline
● Java Syntax.
● The main Method.
● Java Comments.
● Java Variables.
● Declaring Variables .
● Assignment Statements.
● Display Variables.
● Java Identifiers.
● Naming Conventions.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
3
Objectives
● To write Java programs to perform simple computations .
● To use identifiers to name variables, constants, methods, and
classes .
● To use variables to store data.
● To program with assignment statements and assignment
expressions .
● To use constants to store permanent data.
● To name classes, methods, variables, and constants by
following their naming conventions.
● To perform operations using operators +, -, *, /, and % .

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
4
Java Syntax
● In the previous chapter, we created a Java file
called Welcome.java, and we used the following code to
print “Welcome to Java" to the screen:

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

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
5
Java Syntax (Cont..)
● Every line of code that runs in Java must be inside a
class. In our example, we named the class Welcome.
A class should always start with an uppercase first
letter.
● The name of the java file must match the class name.
When saving the file, save it using the class name and
add ".java" to the end of the filename.

Note: Java is case-sensitive: "MyClass" and "myclass" has different


meaning.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
6
The main Method
● The main() method is required and you will see it in
every Java program:
public static void main(String[] args)
● Any code inside the main() method will be executed.
You don't have to understand the keywords before
and after main. You will get to know them bit by bit
during the course.
● For now, just remember that every Java program has
a class name which must match the filename, and that
every program must contain the main() method.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
7
System.out.println()
● Inside the main() method, we can use the println()
method to print a line of text to the screen:

public static void main(String[] args) {


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

● Note: The curly braces {} marks the beginning and the end of a
block of code.
● Note: Each code statement must end with a semicolon.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
8
Exercise:

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
9
Java Comments
● Comments can be used to explain Java code, and to make
it more readable. It can also be used to prevent execution
when testing alternative code.
– Single-line Comments
– Multi-line comments

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
10
Single-line Comments
● Single-line comments start with two forward slashes (//).
● Any text between // and the end of the line is ignored by
Java (will not be executed).
● This example uses a single-line comment before a line of
code:
// This is a comment
System.out.println("Hello");

● This example uses a single-line comment at the end


of a line of code:
System.out.println("Hello World"); // This is a comment

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
11
Multi-line comments
● Multi-line comments start with /* and ends with */.
● Any text between /* and */ will be ignored by Java.
● This example uses a multi-line comment (a comment
block) to explain the code:

/* The code below will print the words Hello World to


the screen, and it is amazing */
System.out.println("Hello World");

● Single or multi-line comments?


– It is up to you which you want to use.
– Normally, we use // for short comments, and /* */ for longer.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
12
Exercise:

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
13
Java Variables
● Variables are containers for storing data values.
● In Java, there are different types of variables, for
example:
– String - stores text, such as "Hello". String values are
surrounded by double quotes
– int - stores integers (whole numbers), without decimals, such
as 123 or -123
– float - stores floating point numbers, with decimals, such as
19.99 or -19.99
– char - stores single characters, such as 'a' or 'B'.
Char values are surrounded by single quotes
– boolean - stores values with two states: true or false
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
14
Declaring (Creating) Variables
Syntax:
type variable;

int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
15
Assignment Statements
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
16
Declaring and Initializing in One
Step
● Syntax:
type variable = value;

int x = 1;
double d = 1.4;
float myFloatNum = 5.99f;
char myLetter = ‘S';
boolean myBool = true;
String myName = “Sara";
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
17
Declaring and Initializing
in One Step
● if you assign a new value to an existing
variable, it will overwrite the previous value:

int myNum = 15;


myNum = 20; // myNum is now 20

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
18
Display Variables
String name = “Sara";
System.out.println("Hello " + name);

String firstName = “Sara";


String lastName = “Ali";
String fullName = firstName + lastName;
System.out.println(fullName);
int x = 5;
int y = 6;
System.out.println(x + y);

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
19
Display Variables (Cont.)
● To combine both text and a variable, use
the + character.
● You can also use the + character to add a variable to
another variable.
● For numeric values, the + character works as a
mathematical operator.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
20
Declare Many Variables

int x = 5, y = 6, z = 50;
System.out.println(x + y + z);

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
21
Example: Computing the Area of a Circle
public class ComputeArea {
/* Main method */
public static void main(String[] args) {
double radius;
double area;

// Assign a radius
radius = 20;

// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
22
animation

Trace a Program Execution


allocate memory
public class ComputeArea {
/** Main method */ for radius
public static void main(String[] args) {
double radius; radius no value
double area;

// Assign a radius
radius = 20;

// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
23
animation

Trace a Program Execution


public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
double radius; radius no value
double area; area no value
// Assign a radius
radius = 20;
allocate memory
// Compute area for area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
24
animation

Trace a Program Execution


public class ComputeArea { assign 20 to radius
/** Main method */
public static void main(String[] args) {
double radius; radius 20
double area;
area no value
// Assign a radius
radius = 20;

// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
25
animation

Trace a Program Execution


public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
double radius; radius 20
double area;
area 1256.636
// Assign a radius
radius = 20;

// Compute area compute area and assign it


area = radius * radius * 3.14159; to variable area

// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
26
animation

Trace a Program Execution


public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
double radius; radius 20
double area;
area 1256.636
// Assign a radius
radius = 20;

// Compute area
area = radius * radius * 3.14159; print a message to the
console
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
27
Example
// Compute the first area
radius = 1.0;
area = radius * radius * 3.14159;
System.out.println("The area is “ +
area + " for radius "+radius);

// Compute the second area


radius = 2.0;
area = radius * radius * 3.14159;
System.out.println("The area is “ +
area + " for radius "+radius);

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
28
Final Variables/Named Constants
● However, you can add the final keyword if you don't
want others (or yourself) to overwrite existing values
(this will declare the variable as "final" or "constant",
which means unchangeable and read-only):
final datatype CONSTANTNAME = VALUE;

final double PI = 3.14159;


final int SIZE = 3;

SIZE = 20; // will generate an error:


cannot assign a value to a final variable
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
29
Java Identifiers
● All Java variables must be identified with unique names.
● These unique names are called identifiers.
– An identifier is a sequence of characters that consist 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 (like int, boolean ).
– An identifier can be of any length.
– Identifiers are case sensitive ("myVar" and "myvar"
are different variables)

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
30
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.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
31
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

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
32
Exercise:

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
33
Summary
● Java code must be written inside a class.
● The main() method is required in every Java program and serves as the
entry point of execution.
● System.out.println() is used to print text to the screen.
● Comments can be used to explain code and are either single-line (//) or
multi-line (/* */).
● Class names capitalize the first letter of each word, while constants are
written in all capitalized letters .
● Java supports variables for storing different types of data, such as
strings, integers, floating-point numbers, characters, and boolean values.
● All Java variables must have unique names called identifiers.
● Choosing meaningful and descriptive names for variables and methods
enhances code readability.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
34
References

● Y. Daniel Liang, 2019, Intro to Java Programming, Comprehensive


Version, Student Value Edition 12th Edition. Pearson, ISBN-10 :
0136520154 ISBN-13: 978-0136520153.

● Introduction to Java, https://www.w3schools.com/java/java_intro.asp


, Last Updated 2024, Last Accessed March 2024

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
35

You might also like