OOP Lab Manual 2
OOP Lab Manual 2
OOP Lab Manual 2
OBJECTIVES
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.
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
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.
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
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
(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.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
●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
______________________________________________________________________________
● (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
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________