Laboratory Manual EE 121 Computer Fundamental: Experiment 3
Laboratory Manual EE 121 Computer Fundamental: Experiment 3
Laboratory Manual
EE 121 Computer Fundamental
Experiment 3
Kausar Abbas
Arslan Shahid
Department of Electrical Engineering
University of Engineering & Technology Lahore
Name : _____________________
Section : ____________________
Reg. No. : ___________________
This material is for the exclusive use of the students of EE 121 Computer Fundamental at the Department of
Electrical Engineering, University of Engineering & Technology, Lahore
New Computer Laboratory Expt 3:2/9
EXPERIMENT 3
Variables
Programs manipulate data that are stored in memory. In machine language, data can only be
referred to by giving the numerical address of the location in memory where it is stored. In a
high-level language such as Java, names are used instead of numbers to refer to data. It is the
job of the computer to keep track of where in memory the data is actually stored; the
programmer only has to remember the name. A name used in this way -- to refer to data
stored in memory -- is called a variable. In Java, the only way to get data into a variable --
that is, into the box that the variable names -- is with an assignment statement. An
assignment statement takes the form:
variable = expression;
2
where expression represents anything that refers to or computes a data value. When the
computer comes to an assignment statement in the course of executing a program, it evaluates
the expression and puts the resulting data value into the variable. For example, consider the
simple assignment statement
rate = 0.07;
The variable in this assignment statement is rate, and the expression is the number 0.07. The
computer executes this assignment statement by putting the number 0.07 in the variable rate,
replacing whatever was there before. Now, consider the following more complicated
assignment statement, which might come later in the same program:
Here, the value of the expression "rate * principal" is being assigned to the variable interest.
In the expression, the * is a "multiplication operator" that tells the computer to multiply rate
times principal. The names rate and principal are themselves variables, and it is really the
This material is for the exclusive use of the students of EE 121 Computer Fundamental at the Department of
Electrical Engineering, University of Engineering & Technology, Lahore
New Computer Laboratory Expt 3:3/9
values stored in those variables that are to be multiplied. We see that when a variable is used
in an expression, it is the value stored in the variable that matters; in this case, the variable
seems to refer to the data in the box, rather than to the box itself. When the computer
executes this assignment statement, it takes the value of rate, multiplies it by the value of
principal, and stores the answer in the box referred to by interest. When a variable is used on
the left-hand side of an assignment statement, it refers to the box that is named by the
variable.
A variable in Java is designed to hold only one particular type of data; it can legally hold that
type of data and no other. The compiler will consider it to be a syntax error if you try to
violate this rule. We say that Java is a strongly typed language because it enforces this rule.
There are eight so-called primitive types built into Java. The primitive types are named byte,
short, int, long, float, double, char, and boolean.
The first four types hold integers (whole numbers such as 17, -38477, and 0). The four
integer types are distinguished by the ranges of integers they can hold.
The float and double types hold real numbers (such as 3.6 and -145.99). Again, the two real
types are distinguished by their range and accuracy.
A variable of type char holds a single character from the Unicode character set. And a
variable of type boolean holds one of the two logical values true or false.
Any data value stored in the computer's memory must be represented as a binary number, that
is as a string of zeros and ones.
A single zero or one is called a bit.
3
A string of eight bits is called a byte. Memory is usually measured in terms of bytes. Not
surprisingly, the byte data type refers to a single byte of memory.
A variable of type byte holds a string of eight bits, which can represent any of the integers
between -128 and 127, inclusive. (There are 256 integers in that range; eight bits can
represent 256 -- two raised to the power eight -- different values.)
As for the other integer types, short corresponds to two bytes (16 bits). Variables of type
short have values in the range -32768 to 32767.
int corresponds to four bytes (32 bits). Variables of type int have values in the range -
147483648 to 2147483647.
long corresponds to eight bytes (64 bits). Variables of type long have values in the range -
9223372036854775808 to 9223372036854775807.
You don't have to remember these numbers, but they do give you some idea of the size of
integers that you can work with. Usually, you should just stick to the int data type, which is
good enough for most purposes.
The float data type is represented in four bytes of memory, using a standard method for
encoding real numbers.
This material is for the exclusive use of the students of EE 121 Computer Fundamental at the Department of
Electrical Engineering, University of Engineering & Technology, Lahore
New Computer Laboratory Expt 3:4/9
A variable of type char occupies two bytes in memory. The value of a char variable is a
single character such as A, *, x, or a space character.
Hexadecimal numbers are also used in character literals to represent arbitrary Unicode
characters. A Unicode literal consists of \u followed by four hexadecimal digits. For example,
the character literal '\u00E9' represents the Unicode character that is an "e" with an acute
accent.
Java has other types in addition to the primitive types, but all the other types represent objects
rather than "primitive" data values. For the most part, we are not concerned with objects for
the time being.
However, there is one predefined object type that is very important: the type String. A
String is a sequence of characters.
You've already seen a string literal: "Hello World!". The double quotes are part of the literal;
they have to be typed in the program. However, they are not part of the actual string value,
which consists of just the characters between the quotes. Within a string, special characters
can be represented using the backslash notation. Within this context, the double quote is itself
a special character. For example, to represent the string value:
I said, "Are you listening!" with a linefeed at the end, you would have to type the string
literal:
"I said, \"Are you listening!\"\n"
Our next application reads (or inputs) two integers typed by a user at the keyboard, compute
the sum of the values and displays the result.
// Addition.java
// Addition program that displays the sum of two numbers.
import java.util.Scanner; // program uses class Scanner
Output
Line 3
is an import declaration that helps the compiler locate a class that is used in this program. A
great strength of Java is its rich set of predefined classes that programmers can reuse rather
than "reinventing the wheel." These classes are grouped into packages named collections of
classes. Collectively, Java's packages are referred to as the Java class library, or the Java
Application Programming Interface. Programmers use import declarations to identify the
predefined classes used in a Java program. The import declaration in line 3 indicates that this
example uses Java's predefined Scanner class (discussed shortly) from package java.util.
Then the compiler attempts to ensure that you use class Scanner correctly.
Line 5
begins the declaration of class Addition. The file name for this public class must be
Addition.java. Remember that the body of each class declaration starts with an opening left
brace (line 6), {, and ends with a closing right brace (line 29), }. The application begins
execution with method main (lines 8). The left brace (line 9) marks the beginning of main's
body, and the corresponding right brace (line 27) marks the end of main's body.
Line 11
is a variable declaration statement (also called a declaration) that specifies the name and type
of a variable (input) that is used in this program. A variable is a location in the computer's
memory where a value can be stored for use later in a program. All variables must be
declared with a name and a type before they can be used. A variable's name enables the
This material is for the exclusive use of the students of EE 121 Computer Fundamental at the Department of
Electrical Engineering, University of Engineering & Technology, Lahore
New Computer Laboratory Expt 3:6/9
program to access the value of the variable in memory. A variable's type specifies what kind
of information is stored at that location in memory. Like other statements, declaration
statements end with a semicolon (;).
The declaration in line 11 specifies that the variable named input is of type Scanner.
A Scanner enables a program to read data (e.g., numbers) for use in a program. The data
can come from many sources, such as a file on disk or the user at the keyboard. Before using
a Scanner, the program must create it and specify the source of the data.
The equal sign (=) in line 11 indicates that Scanner variable input should be initialized
(i.e., prepared for use in the program) in its declaration with the result of the expression new
Scanner(System.in) to the right of the equal sign. This expression creates a Scanner object
that reads data typed by the user at the keyboard. Recall that the standard output object,
System.out, allows Java applications to display characters in the command window.
Similarly, the standard input object, System.in, enables Java applications to read information
typed by the user. So, line 11 creates a Scanner that enables the application to read
information typed by the user at the keyboard.
6
The variable declaration statements at lines 13 - 15
declare that variables number1, number2 and sum are data of type int.
Line 17
Uses System.out.print to display the message "Enter first integer:". This message is called
a prompt because it directs the user to take a specific action. So, System is a class. Class
System is part of package java.lang. Notice that class System is not imported with an
import declaration at the beginning of the program.
Line 18
Uses Scanner object input's nextInt method to obtain an integer from the user at the
keyboard. Technically, the user can type anything as the input value. Our program assumes
that the user enters a valid integer value as requested. In this program, if the user types a non-
integer value, a runtime logic error will occur and the program will terminate.
Line 23
is an assignment statement that calculates the sum of the variables number1 and number2
and assigns the result to variable sum by using the assignment operator, =. The statement is
read as "sum gets the value of number1 + number2." Most calculations are performed in
This material is for the exclusive use of the students of EE 121 Computer Fundamental at the Department of
Electrical Engineering, University of Engineering & Technology, Lahore
New Computer Laboratory Expt 3:7/9
assignment statements. When the program encounters the addition operation, it uses the
values stored in the variables number1 and number2 to perform the calculation. In the
preceding statement, the addition operator is a binary operator its two operands are number1
and number2.
For example, the value of the expression number1 + number2 is the sum of the numbers.
Similarly, the value of the expression input.nextInt() is an integer typed by the user.
uses method System.out.print to display the sum. The String which is passed on to the this
method has two parts. "Sum is \n" is the first part and sum (which is a variable that contains
a numeric value) is the second part. The + operator performs String Concatenation.
Executes, the number typed by the user is placed into a memory location to which the name
number1 has been assigned by the compiler. Suppose that the user enters 45. The computer
places that integer value into location number1, as shown in Fig. 2.1.
Figure 2.1. Memory location showing the name and value of variable number1.
number 1 - 45
executes, suppose that the user enters 72. The computer places that integer value into location
number2. The memory now appears as shown in Fig. 2.2.
Figure 2.2. Memory locations after storing values for number1 and number2.
number 1 - 45
This material is for the exclusive use of the students of EE 121 Computer Fundamental at the Department of
Electrical Engineering, University of Engineering & Technology, Lahore
New Computer Laboratory Expt 3:8/9
number 2 - 72
After the program of Example 1 obtains values for number1 and number2, it adds the values
and places the sum into variable sum. The statement (line 23)
Performs the addition, then replaces sum's previous value. After sum has been calculated,
memory appears as shown in Fig. 2.3. Note that the values of number1 and number2 appear
exactly as they did before they were used in the calculation of sum. These values were used,
but not destroyed, as the computer performed the calculation.
Figure 2.3. Memory locations after calculating and storing the sum of number1 and
number2.
number 1 - 45
number 2 - 72
sum - 117
5.0 Arithmetic
• Usage
* for multiplication
/ for division
% for remainder
+, - for addition and subtraction
This material is for the exclusive use of the students of EE 121 Computer Fundamental at the Department of
Electrical Engineering, University of Engineering & Technology, Lahore
New Computer Laboratory Expt 3:9/9
Arithmetic operators
Arithmetic Operators
Multiplication * bm b*m
This material is for the exclusive use of the students of EE 121 Computer Fundamental at the Department of
Electrical Engineering, University of Engineering & Technology, Lahore