You are on page 1of 16

Practical lesson

Data Types
arithmetic Expressions
Dr.Solaf Ali
Variables and Data Types (1)
• Variables are used to store data in a program.
• Variables represents a value stored in computer’s
memory.
• A variable is just a memory location that has been given a
name.
• Variables are for representing data of a certain type.

2
Variables and Data Types (2)
• Type: A category or set of data values.
• The term data type refers to the type of data that can
be stored.

3
Java's Primitive Types and Strings
Type Description Size Examples
byte Integer 1 byte (up to 27 - 1)
short Integer 2 byte (up to 215 - 1)
(up to 231 - 1)
int Integer 4 byte
42, -3, 0, 926394
long Integer 8 byte (up to 263 – 1)
Primitive (up to 10308)
data types double Real number 8 byte
3.1, -0.25, 9.4e3
float Real number 4 byte
2 byte for
char Single text character Unicode 'a‘ , 'X‘ ,'?‘ , ‘4’, '\n'
characters
boolean Logical value 1 bit of memory ? true , false
Non- “program”,
Primitive
type
String
Sequence of
characters ? “Computer”, “science”,
“a”, “4”
4
Declaring Variables
• Every variable must be declared, before it can be used.
• The syntax for declaring a variable is:
datatype variableName;
Memory
• Examples:
o int mark; mark
o double radius;
o double interestRate; radius
o char firstChar;
interestRate

firstChar
5
Expressions
• Expression: A computation involving values, variables,
and operators that together evaluates to a value.
• Examples:
• num = 1 + 4 * 5
• num = (7 + 2) * 6 / 3

6
Mathimatical operations
Operator Description Example – given a is 15 and b is 6

+ Addition a + b, would return 21


- Subtraction a - b, would return 9
* Multiplication a * b, would return 90
/ Division a / b, would return 2
% Modulus a % b, would return 3 (the remainder)
Example 1
• class Example4 {
• public static void main(String args[]) {
• int iresult, irem;
• double dresult, drem;
• iresult = 10 / 3;
• irem = 10 % 3;
• dresult = 10.0 / 3.0;
• drem = 10.0 % 3.0;
• System.out.println("Result and remainder of 10 / 3: " +
• iresult + " " + irem);
• System.out.println("Result and remainder of 10.0 / 3.0: "
• + dresult + " " + drem);
•}
•}
Example 2: Writing a Simple Java Program (1)

• Write a program to calculate result of the following equation:

• The algorithm for calculating the equation can be described as follows:


1. Give value of x.
2. Calculate the equation using value of x.
3. Display f. Start
4. End.
Give value of x

Calculate

Output f

Stop 9
Solve the problems by writing Java Programs
1. Consider your class representative wants to copy “Programming” materials for
the students. The price of copying 1 page is 50 IQD. He wants to know how much
does it cost to copy the materials for the students. Test your solution with the
following data:
• The material is 5 pages for 85 students.
2. An employee’s job in a printing company is to order paper for reports in board
meetings. The paper comes in sets of 500 sheets. He always makes five more
copies than the number of people that will be in the meeting. He wants to know
how many sets of paper he needs for a meeting. (Hint: He can order only whole
not partial sets). Test your solution with the following data:

• The report is 145 pages long and there will be 25 members at the meeting.
• Practical Lesson
 string
 Joptionpane dialog box
Excersices
Write the algorithm and then the java program for each of the
following Problems:
1.Read student name and student degree (2 degrees for one subject)
then find the average of the student. The output should display the
student name and average.(hint: use Joptionpane dialog box).
2.Input two strings by user then ,
a-find the length of the strings.
b-concatenate the strings.
c-check if the strings have the same syntax(value).
d-print the charecters in 3rd and 4th position for each string
Type Casting
• Type Casting and Conversions Casting is the term used when a value is converted
from one data type to another, except for Boolean data types which cannot be
converted to any other type. Usually conversion occurs to a data type which has a
larger range or else there could be loss of precision.
Example1:
public static void main(String args[]) {long L;
double D;
L = 100123285L;
D = L; // L = D is impossible
System.out.println("L and D: " + “\t” L + “\t”+ D);
}

13
• class Example2 { public static void main(String args[])
{ byte b;
int i;
b = 10;
i = b * b; // OK, no cast needed
b = 10;
b = (byte) (b * b); // cast needed!! as cannot assing int to byte
System.out.println("i and b: " + i + " " + b);
}
}
The Math Class
• In order to perform certain mathematical operations
like square root (sqrt), or power (pow); Java has a
built in class containing a number of methods .
• All the methods involving angles use radians and
return a double (excluding the Math.round()).
class Example15 { public static void main(String args[])
{ double x, y, z;
x = 3;
y = 4;
z = Math.sqrt(x*x + y*y);
System.out.println("Hypotenuse is " +z);
}
} Predicted Output: Hypotenuse is 5.0
• Write program for each of the following problems:

• Calculate the following equation:

• Calculate the following equation:

2
b b  4ac
2a

You might also like