Lab 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

DEPARTMENT OF TELECOMMUNICATION ENGINEERING

BACHELOR OF SCIENCE IN CYBER SECURITY


MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
OBJECT OREINTED PROGRAMMING
(2nd SEMESTER, 1st Year) LAB EXPERIMENT # 2

Name: _____________________________________________ Roll No: _____________

Score: ____________Signature of the Lab Tutor: _______________ Date: ____________

To practice programs using variable, data types and operators

LAB SUBJECT DATA ANALYSIS ABILITY TO SCORE


PERFORMANCE KNOWLEDGE AND CONDUCT
INDICATOR INTERPRETATION EXPERIMENT

OBJECTIVE NO:

Objective

• To practice programs using Variable, data types and operators


Tools
• Eclipse IDE/IntelliJ Idea / Notepad++

Keywords: Variable , data types, and operators. Duration:03 hours

1. Overview of lab [20 minutes]

1.1 Introduction

1.1.1 Variables: It is basically a name or label assigned to location of memory address

• It is used to store and retrieve the data.


• Variables always declare and initialize first before use.
• In order to create a variable, we need to specify three things:
Data type, name(identifier) and value(optional).
1.1.1 a Naming Rules:
Variable names are case-sensitive. A variable's name can be any legal identifier — an
unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar
sign "$", or the underscore character "_".

A keyword or reserved can never use as variable name.

1|Page
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
BACHELOR OF SCIENCE IN CYBER SECURITY
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
OBJECT OREINTED PROGRAMMING
(2nd SEMESTER, 1st Year) LAB EXPERIMENT # 2

Space is not allowed within the names of variables.

1. 1.1b Naming Convention: The convention, however, is to always begin your variable
names with a letter, not "$" or "_". If the name you choose consists of only one word, spell
that word in all lowercase letters. If it consists of more than one word, capitalize the first
letter of each subsequent word.
• Example. int average=0; float temperature=1.8f; int currentGear=1; and e.t.c

1.1.2 Data Types:

Java supports eight primitive data types.


Primitive Data
Reserved Memory Type of Data store Example
types

Byte One byte memory Store integer value byte b =1;

Short Two byte memory Store integer value short s =2;

Int Four byte memory Store integer value int i=3;

Long Eight byte memory Store integer value long l=4l;

Float Four byte memory Store decimal value float f=5.5f;

Double Eight byte memory Store decimal value double d=4.5;


Store alphabets,
Character Two byte memory characters & char c=’A’;
symbols.
Store either true or
Boolean One byte memory boolean x=true;
false.
1.1.3 Casting:

It is conversion of real number value to integer value and integer to real number value.

There are two types of casting.


1) Implicit casting. It is conversion of integer value into real number value and it is
directly performed by Compiler. Example: double temperature = 2;

2|Page
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
BACHELOR OF SCIENCE IN CYBER SECURITY
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
OBJECT OREINTED PROGRAMMING
(2nd SEMESTER, 1st Year) LAB EXPERIMENT # 2

2) Explicit casting also known as forceful conversion. It is conversion of real value into
integer number value, it is forcefully implemented.
Example: int Age = (int) 2.314;

1.1.4 Operators:

These are some symbols or special characters which specify some specific operation.

They can be classify on the basis of two parameters.

1. Type of operation.
2. Numbers of operands.
Number of
Operators Symbols Operands Uses

All are binary Used for arithmetic


Arithmetic +, - , * , / , % operators operations
Use for assign the
Assignment = Binary operator value

Arithmetic All are binary Used for summarize


+=, -=, *=, /=, %=
Assignment operators equations
All are binary Used to compare
Relational <, >, <=, >=, ==, != operators values
&& and | | are
Used to compare
Logical && , | | , ! binary. ! is unary
expressions
operators
Increment & Used for increment
Decrement ++, -- Unary operators and decrement
All are binary
Used to perform bit
Bitwise &, |, ^, !, >>, <<, >>> operators except !
by bit operation
that is unary

2. Demonstration of practice examples. [20 minutes for each problem]

2.1 Default values of all data types (Example).

3|Page
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
BACHELOR OF SCIENCE IN CYBER SECURITY
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
OBJECT OREINTED PROGRAMMING
(2nd SEMESTER, 1st Year) LAB EXPERIMENT # 2

Output

2.2 Casting (Example).

4|Page
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
BACHELOR OF SCIENCE IN CYBER SECURITY
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
OBJECT OREINTED PROGRAMMING
(2nd SEMESTER, 1st Year) LAB EXPERIMENT # 2

Output

2.3 computes the roots of quadratic equation by using quadratic formula.

5|Page
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
BACHELOR OF SCIENCE IN CYBER SECURITY
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
OBJECT OREINTED PROGRAMMING
(2nd SEMESTER, 1st Year) LAB EXPERIMENT # 2

Output

2. 4 Operators (Example).

Output

6|Page
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
BACHELOR OF SCIENCE IN CYBER SECURITY
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
OBJECT OREINTED PROGRAMMING
(2nd SEMESTER, 1st Year) LAB EXPERIMENT # 2

3. Exercise solving [40 minutes]

*Example attached at the end of lab

*Maximum tasks should be included to engage students.

4. Presentation and Discussion: [25 minutes]

1 to 2 minutes for every student

Question & Answer Session [15 minutes]

7|Page
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
BACHELOR OF SCIENCE IN CYBER SECURITY
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
OBJECT OREINTED PROGRAMMING
(2nd SEMESTER, 1st Year) LAB EXPERIMENT # 2

Exercise
[60 minutes]
Q.1 Write a java program that asks the user to enter two numbers and print the largest one.

Q. 2 Write a Java program to convert temperature from Fahrenheit to Celsius degrees.

Q.3 Average acceleration is defined as the change of velocity divided by the time taken to
make the change, as shown in the following formula:

𝑣1−𝑣0
𝑎=
𝑡

Write a program that prompts the user to enter the starting velocity v0 in meters/ second, the ending
velocity v1 in meters/second, and the time span t in seconds, and displays the average acceleration.

Q.4 Write a program that prompts the user to enter his/her monthly salary and computes
the bonus.
The bonus is 1000/= PKR plus 2% of the amount above 7000/=PKR of the employee’s annual
salary. Assume that every employee has annual salary above 7000/= PKR

Q. 5 Write a program that prompts the user to enter some amount in PKR, determines the number
of 5000 Rs. notes, 1000 Rs. notes, 500 Rs. notes, 100 Rs. notes, and 1 Rs. Coins for the given
amount.

8|Page

You might also like