0% found this document useful (0 votes)
6 views9 pages

OOP Lab Manual 2

Download as doc, pdf, or txt
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 9

DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH

OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH

Name: _____________________________ Roll No: __________________________________

Signature of Lab Tutor: ____________________ Date: ________________________________

LAB 2: INTRODUCTION TO DIFFERENT DATA TYPES AND OPERATORS USED IN


JAVA

OBJECTIVES

 To get conversant with various data types used in Java


 To get conversant with different operators used in Java.

Data Types and Variables


Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float, double,
and boolean.
Integers
Java defines four integer types: byte, short, int, and long. All of these are signed, positive and
negative values. Java does not support unsigned, positive-only integers. The width and ranges of
these integer types vary widely, as shown in this table:
Name Width Range
long 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
int 32 –2,147,483,648 to 2,147,483,647
short 16 –32,768 to 32,767
byte 8 –128 to 127
Byte
The smallest integer type is byte. This is a signed 8-bit type that has a range from –128 to 127.
Variables of type byte are especially useful when you’re working with a stream of data from a
network or file. The following declares two byte variables called b and c:
byte b, c;
Short
Short is a signed 16-bit type. It has a range from –32,768 to 32,767. It is probably the least-used
Java type. Here are some examples of short variable declarations:
short s;
short t;

Int
The most commonly used integer type is int. It is a signed 32-bit type that has a range from -
2,147,483,648 to 2,147,483,647. In addition to other uses, variables of type int are commonly
employed to control loops and to index arrays. Any time you have an integer expression
involving bytes, shorts, ints, and literal numbers, the entire expression is promoted to int before
the calculation is done. Remember, type determines behavior, not size.
1
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
Long
Long is a signed 64-bit type and is useful for those occasions where an int type is not large
enough to hold the desired value. The range of a long is quite large. This makes it useful when
big, whole numbers are needed.
Floating-Point Types
Floating-point numbers, also known as real numbers, are used when evaluating expressions that
require fractional precision. There are two kinds of floating-point types, float and double, which
represent single- and double-precision numbers, respectively. Their width and ranges are shown
here:
Name Width in Bits Approximate Range
Double 64 4.9e–324 to 1.8e+308
Float 32 1.4e−045 to 3.4e+038
Float
The type float specifies a single-precision value that uses 32 bits of storage. Single precision is
faster on some processors and takes half as much space as double precision, but will become
imprecise when the values are either very large or very small. Here are some example float
variable declarations:
float hightemp, lowtemp;
Double
Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double
precision is actually faster than single precision on some modern processors that have been
optimized for high-speed mathematical calculations.
Characters
Variables of type char store a single character code. They each occupy 16 bits, or 2 bytes, in
memory because all characters in Java are stored as Unicode. Unicode defines a fully
international character set that can represent all of the characters found in all human languages.
Booleans
Variables of type boolean can have only one of two values, true or false. The values true and
false are boolean literals. The boolean type is named after the mathematician George Boole, who
invented Boolean algebra, and variables of this type are described as boolean variables. You can
define a variable of type boolean called state with the following statement:
boolean state = true;
Java Operators

Java operators can be classified as unary, binary, or ternary—meaning taking one, two, or three
operands, respectively. A unary operator may appear before (prefix) its argument or after
(postfix) its argument. A binary or ternary operator appears between its arguments.
Arithmetic Operators
Arithmetic operators are used in mathematical expressions. The following table lists the
arithmetic operators:

Operator Result
+ Addition
– Subtraction (also unary minus)
* Multiplication

2
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
/ Division
% Modulus
++ Increment
+= Addition assignment
–= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
–– Decrement

The operands of the arithmetic operators must be of a numeric type. You cannot use them on
boolean types, but you can use them on char types, since the char type in Java is, essentially, a
subset of int.
The Basic Arithmetic Operators
Java provides eight Arithmetic operators. They are for addition, subtraction, multiplication,
division, modulo (or remainder), increment (or add 1), decrement (or subtract 1), and negation.
The minus operator also has a unary form which negates its single operand.
The Modulus Operator
The modulus operator, %, returns the remainder of a division operation. It can be applied to
floating-point types as well as integer types.

Arithmetic Assignment Operators


Java provides special operators that can be used to combine an arithmetic operation with an
assignment. As you probably know, statements like the following are quite common in
programming:
a = a + 4;
In Java, you can rewrite this statement as
shown here: a += 4;

Increment and Decrement


The ++ and the – – are Java’s increment and decrement operators. The increment operator
increases its operand by one. The decrement operator decreases its operand by one. For example,
this statement:
x = x + 1;
can be rewritten like this by use of the increment
operator: x++;
These operators are unique in that they can appear both in postfix form, where they follow the
operand as just shown, and prefix form, where they precede the operand. In the prefix form, the
operand is incremented or decremented before the value is obtained for use in the expression. In
postfix form, the previous value is obtained for use in the expression, and then the operand is
modified.
The Bitwise Operators
Java's bitwise operators operate on individual bits of integer (int and long) values. If an operand
is shorter than an int, it is promoted to int before doing the operations. It helps to know how

3
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
integers are represented in binary.
They are summarized in the following table:

Operator Result
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
>> Shift right
>>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
^= Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment

The Left Shift


The left shift operator, <<, shifts all of the bits in a value to the left a specified number of times.
It has this general form:
value << num

Here, num specifies the number of positions to left-shift the value in value. That is, the << moves
all of the bits in the specified value to the left by the number of bit positions specified by num.
For each shift left, the high-order bit is shifted out (and lost), and a zero is brought in on the
right.
The Right Shift
The right shift operator, >>, shifts all of the bits in a value to the right a specified number of
times. Its general form is shown here:
value >> num

Here, num specifies the number of positions to right shift the value in value. That is, the >>
moves all of the bits in the specified value to the right the number of bit positions specified by
num.

The Unsigned Right Shift

As you have just seen, the >> operator automatically fills the high-order bit with its previous
contents each time a shift occurs. This preserves the sign of the value. However, sometimes this
is undesirable. For example, if you are shifting something that does not represent a numeric
value, you may not want sign extension to take place. This situation is common when you are
working with pixel-based values and graphics. In these cases you will generally want to shift a
zero into the high-order bit no matter what its initial value was. This is known as an unsigned
shift.
Relational Operators
The relational operators determine the relationship that one operand has to the other.
4
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
Specifically, they determine equality and ordering. The relational operators are shown here:

Operator Result
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Boolean Logical Operators


The Boolean logical operators shown here operate only on boolean operands. All of the binary
logical operators combine two boolean values to form a resultant boolean value.

Operator Result
& Logical AND
| Logical OR
^ Logical XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND assignment
|= OR assignment
^= XOR assignment
== Equal to
!= Not equal to
?: Ternary if-then-else

The Assignment Operator


The assignment operator is the single equal sign, =. It has this
general form: var = expression;
Here, the type of var must be compatible with the type of expression. The assignment operator
does have one interesting attribute that you may not be familiar with: it allows you to create a
chain of assignments. For example, consider this fragment:
int x, y, z;
x = y = z = 100; // set x, y, and z to 100
The ? Operator
Java includes a special ternary (three-way) operator that can replace certain types of if-then-else
statements. This operator is the ?. The ? has this general form:
expression1 ? expression2 : expression3
Here, expression1 can be any expression that evaluates to a boolean value. If expression1 is true,
then expression2 is evaluated; otherwise, expression3 is evaluated.
max = (a > b) ? a : b;

(a > b) ? a : b; is an expression which returns one of two values, a or b. The condition, (a > b), is
tested. If it is true the first value, a, is returned. If it is false, the second value, b, is returned.

5
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
Whichever value is returned is dependent on the conditional test, a > b. The condition can be any
expression which returns a boolean value.

6
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH

Task 1: Write a java program to create 2 integers and display their sum, difference, product and
division on the screen.

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Task 2: Assuming that int a = 3, b = 5, c = 1, x = 2, y = -2, z = 6; and following precedence


rules. Write a program that evaluates all the expressions and prints the result on the screen
● (a / c) + z / a

______________________________________________________________________________
●c++ + b / x - y

______________________________________________________________________________
●x + ++x * x--

______________________________________________________________________________
●--a * ++a - (z++ * z++)

______________________________________________________________________________

7
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH

● ((true && (5 > 7)) || ((3 <= 10) && !false))

______________________________________________________________________________
● (3 < 6) || (c++ == x)
______________________________________________________________________________

Task 3: Write an application that asks the user to enter two integers, obtains them from the user
and displays the larger number followed by the words "is larger". If the numbers are equal, print
the message "These numbers are equal".

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Task 4: Write an application that inputs one number consisting of five digits from the user,
separates the number into its individual digits and prints the digits separated from one another by
three spaces each. For example, if the user types in the number 42339, the program should print
4 2 3 3 9.

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Task 5: Using only the programming techniques you learned so far, write an application that
calculates the squares and cubes of the numbers from 0 to 10 and prints the resulting values in
table format, as shown below.
num sq cube
0 0 0
1 1 1
2 4 8
3 9 27

8
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

You might also like