0% found this document useful (0 votes)
3 views4 pages

02 Lesson - Basic Input output_

This document is an introduction to elementary programming concepts, focusing on variables, assignment statements, and named constants in Java. It explains how to declare and use variables, the syntax for assignment statements, and the importance of using constants for permanent values. The document also includes exercises for practice and understanding of these concepts.

Uploaded by

j2cvpthc7h
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
3 views4 pages

02 Lesson - Basic Input output_

This document is an introduction to elementary programming concepts, focusing on variables, assignment statements, and named constants in Java. It explains how to declare and use variables, the syntax for assignment statements, and the importance of using constants for permanent values. The document also includes exercises for practice and understanding of these concepts.

Uploaded by

j2cvpthc7h
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

21

Introduction to Computer Science 11

Lesson 2 – Elementary Programming


Goal(s):
● To use variables to store data
● To program using assignment statements and assignment expressions
● To name constants using the keyword final

Minds On…

With only using one System.out.println()Write a program that produces the following
output:

In your program, substitute ??? with your own name. If necessary, adjust the positions and the

number of the stars to produce a rectangle.

Action …

Variables

Key Point

Variables are used to represent values that may be changed in the program.

Variables are used to store values to be used later in a program. They are called variables because
their values can be changed. Variables are used to represent data of a certain type. To use a variable,
you declare it by telling the compiler its name as well as the type of data it can store. The variable
declaration tells the compiler to allocate appropriate memory space for the variable based on its data
type. The syntax for declaring a variable is:

datatype variableName; Examples

int count; // Declare count to be an integer variable


double radius; // Declare radius to be a double variable
String name; // Declare name to be a String variable. Notice that String starts with a capital ‘S.’
This is because unlike int and double, String is not a primitive data type. Primitive vs. non-primitive
data types will be discussed later. You can go to this link to teach yourself a bit about it to get ahead.

If variables are of the same type, they can be declared together, as follows:
dataType variable1, variable2, variable3;
For example,
int i, j, k;

Note

By convention, variable names are in lowercase. If a name consists of several words, concatenate all of
them and capitalize the first letter of each word except the first. Examples of variables are radius,
numberOfStudents, and interestRate.

Variables often have initial values. You can declare a variable and initialize it in one step. Consider, for
instance, the following code:

int count = 1;

This is equivalent to the next two statements:

int count;
count = 1;

You can also use shorthand to declare and initialize multiple variables of the same type together like this:

int i = 1, j=2;

Assignment Statements and Assignment Expressions

Key Point

An assignment statement designates a value for a variable. An assignment statement can be used as
an expression in Java.

After a variable is declared, you can assign a value to it by using an assignment statement. In Java, the
equal sign ( = ) is used as the assignment operator. The syntax for assignment statements is as follows:

variable = expression;

An expression represents a computation involving values, variables, and operators that, taking them
together, evaluates to a value. In an assignment statement, the expression on the right-hand side of
the assignment operator is evaluated, and then the value is assigned to the variable on the left-hand
side of the assignment operator. For example, consider the following code:

int y = 1; //Assign 1 to the variable y

double radius = 1.0; // Assignment 1.0 top the variable radius

int x = 5*(3/2); //Assignment the value of the expression to x

x = y + 1; //Assign the addition of y and 1 to x

double area = Math.pow(radius,2)*Math.PI; //Computer area


Note that something like this will NOT work in Java:
int x = y = z = 1; 4

Named Constants
Key Point
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. Here is the syntax for declaring a
constant using the keyword final:
final dataType CONSTANT_NAME = value;

For example,

final double PI = 3.14159;

There are three benefits of using constants:


1. If you don’t have to repeatedly type the same value;
2. If you have to change the constant value (e.g., from 3.14 to 3.14159 for PI), you need change it
only in a single location in the source code;
3. Descriptive constant names make the program easy to read.

Assessment

1. Identify and fix the errors in the following code.

2. Write a statement to declare a variable named x of the double type with an initial value 4.5.

3. Which of the following are correct names for variables according to the naming conventions
adopted in this course?
4. What are the benefits of using constants? Declare an int constant SIZE with a value of 20.

5. Translate the following algorithm into Java code:

Step 1: Declare a double variable named miles with initial value 100.

Step 2: Declare a double constant named KILOMETERS_PER_MILE with value 1.609.

Step 3: Declare a double variable named kilometers, multiply miles and KILOMETERS_PER_MILE, and
assign the result to kilometers

Step 4: Display kilometers to the console. What is kilometers after Step 4?

6. To declare a constant MAX_LENGTH inside a function with value 99.98, you write:
a. final MAX_LENGTH = 99.98;
b. constant MAX_LENGTH= 99.98;
c. constant double MAX_LENGTH= 99.98;
d. final double MAX_LENGTH= 99.98;

7. Which of the following is a constant according to the convention adapted in this course?

You might also like