0% found this document useful (0 votes)
39 views15 pages

Chapter 2 - Review - Part 1

The document discusses Java variables including: 1) Variables are containers that hold values and are declared with a type and name. 2) Identifiers for variables follow naming rules and cannot be reserved words. 3) Assignment statements initialize variables and assignment expressions can update values. 4) The Scanner class reads user input which can be stored in variables.

Uploaded by

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

Chapter 2 - Review - Part 1

The document discusses Java variables including: 1) Variables are containers that hold values and are declared with a type and name. 2) Identifiers for variables follow naming rules and cannot be reserved words. 3) Assignment statements initialize variables and assignment expressions can update values. 4) The Scanner class reads user input which can be stored in variables.

Uploaded by

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

Chapter 2 - Elementary Programming

2.4 Identifiers
2.5 Variables
2.6 Assignment Statements and Assignment Expressions
2.3 Reading Input from the Console
2.7 Named Constants
2.5 Variables
Declaration Statements
int x; // Declare x to be an integer variable
double radius; // Declare radius to be a double variable
char y; // Declare y to be a character variable

A variable is a container which holds the value while the Java program is executed.
It is like a box in the memory to store a value in it.
To declare (create) a variable, you will specify the type, leave at least one space, then
the name for the variable and end the line with a semicolon ( ; ). For example: int x;
the statement creates a variable named x that can hold an integer value.
double radius; creates a variable named radius that can hold a decimal value. So
Java uses the keyword int for integers, double for a floating point numbers, and char
for characters.
A variable cannot be used in a program unless it has been declared.
Now we haven’t initialized the variables. The declaration gives a name and a data
type for the variable. These containers are empty.

2
2.4 Identifiers

name score age answer

String name = "Hello"; double score = 12.7; int age = 51; boolean answer= true;

dataType identifier value


An identifier (name of variable or method or program) is a sequence (of any length) of
characters that can 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.
Use a meaningful name for the identifier (e.g. radius instead of r).
If the identifier is the name of a variable then use all small letters (e.g. radius instead of Radius).
Caution
int x = 12;
System.out.println("x");  prints the letter x
System.out.println(x); prints the value of the variable x (it prints 12)3
2.6 Assignment Statements and Assignment Expressions
Declaration Statements
int x; // Declare x to be an integer variable
double radius; // Declare radius to be a double variable
char y; // Declare y to be a character variable

Assignment Statements
x = 1; // Assign 1 to x
radius = 1.0; // Assign 1.0 to radius
y = 'A'; // Assign 'A' to y

Declaring and Assignment in One Step

Equivalent to
int x = 1;
int x;
char y = 'A'; x=1;
double radius = 1.0;
4
Assignment expression
int a, b;
int a, b; Is equivalent to
a b int a;
int b;

12
b
a = 12;
a
7
b = 7;
b
3
a = 3; 12
a 3+7=10

int c = a + b; c // a + b is called an assignment expression

Note: You can change the value of a variable (changed a from 12 to 3).
5
Concatenation operator (+)
int a, b;
Output on the Screen:
a = 3; The value of c is 10

b = 7;
b
int c = a + b;
System.out.println("The value of c is " + c );
//System.out.println("The value of c is " c); Syntax error

//System.out.println("The value of c is c"); logic error

Concatenation is the operation of joining together two strings, a


string and a value (of any type), or a string and a variable(of any
type). c
You can concatenate things using the concatenation operator (+) in
the system.out.print() statement.
6
2.3 Reading Input from the Console

 Example 1: Compute an area with radius 1


double radius = 1.0;
double area = radius * radius * 3.14159;
System.out.println("The area is " + area + ", radius "
+ radius);
Output on the Screen:
The area is 3.14159, radius 1.0

 Example 2: Compute an area with radius 10


double radius = 10.0;
double area = radius * radius * 3.14159;
System.out.println("The area is " + area + ", radius "
+ radius);
Output on the Screen:
The area is 314.159, radius 10.0

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

8
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);
}
}

9
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);
}
}

10
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);
}
}

11
Read an input value from the user using Scanner
public class ComputeArea {
public static void main(String[] args) {
double radius;
double area;
// Assign a radius
radius = ??????????;

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

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

Output on the Screen:


Enter the value of radius: 2.0
The area is 6.28318 for radius 2.0

12
Getting Input Using Scanner

Sample Run

13
2.7 Named Constants

final datatype CONSTANTNAME = VALUE;


final double PI = 3.14159;
final int SIZE = 3;

• The value of a variable may change during the execution of the


program, but a constant never changes.

• We use the keyword final before the declaration.

• By convention, constants are named in uppercase.

14
animation

Trace a Program Execution


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

// Compute area
area = radius * radius * PI;

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

15

You might also like