data types and expressions - sabraz · pdf fileusing scanner class • the scanner class is...
TRANSCRIPT
Lesson 02
Data Types and Statements
MIT 11053, Fundamentals of Programming
By: S. Sabraz Nawaz
Senior Lecturer in MIT
Department of MIT
FMC, SEUSL
Exercise
• A university pays its Academic Staff
oAcademic Allowance 39% of Basic Salary
o Research Allowance 45% of Basic Salary
o Cost of Living Allowance 7,550/=.
• And deducts UPF 8% of the Basic Salary.
• Write a Java program to input the Basic Salary.
Calculate the above and display them all with
Net Salary
Fundamentals of Programming by
SaNa@seu
Topics Covered
• Statements
• Variables
• Data Types
• Arithmetic Calculations
• Pre and Post increment operators
• Taking Input from User
Fundamentals of Programming by
SaNa@seu
Statements
• A Statement is the simplest task you can accomplish in Java.
Fundamentals of Programming by
SaNa@seu
int othrs=5;
System.out.println("netsalary= "+netsal);
You need to put a semi colon ; at theend of a statement.
Variables
• Variables are locations in memory where values can be
stored
Fundamentals of Programming by
SaNa@seu
Variable Name
• Variable is a location in memory
• Each location in memory has a memory address, which is a number
o This long number is inconvenient to use when we want to access the memory location
• We give a human understandable name to refer to this number
o e.g. age, quantity
• The compiler and the interpreter maps this name to the memory address number
Fundamentals of Programming by
SaNa@seu
Value of a Variable
• At a given time one value can be stored under the variable
Fundamentals of Programming by
SaNa@seu
quantity
Variable Type
• You need to specify what type of data is to be stored. e.g.
int, char
• This is because we must instruct how much memory
should be reserved by the program to store the value of a
variable
• The amount of memory needed depends on the maximum
of the value we need to store in the variable.
Fundamentals of Programming by
SaNa@seu
Variable Type…
Fundamentals of Programming by
SaNa@seu
Java Data Types
• Java supports eight primitive data types.
• These are built into the language itself.
• Consists of Numeric Types, char type and Boolean type.
• Remember String is not a primitive data type in Java
o String is a class in Java, thus it is handled as a data type derived from a class.
• In Java we write classes and class can be a data type
o Eg: If you write a class called Student you can use it as the Student
data type
Fundamentals of Programming by
SaNa@seu
Data Types
Fundamentals of Programming by
SaNa@seu
Name Range Storage Size
byte –2
7 (-128) to 2
7–1 (127) 8-bit signed
short –2
15 (-32768) to 2
15–1 (32767) 16-bit signed
int –2
31 (-2147483648) to 2
31–1 (2147483647) 32-bit signed
long –2
63 to 2
63–1 64-bit signed
(i.e., -9223372036854775808
to 9223372036854775807)
float Negative range: 32-bit IEEE 754
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to
-4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308
Declaring Variables
int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;
Fundamentals of Programming by
SaNa@seu
Declaring Variables
Fundamentals of Programming by
SaNa@seu
public static void main (String args[]) {int count;String title;boolean isAsleep;...
}
Variables are usually defined at thebeginning. However this need not always
be the case.
Declaring Variables
Fundamentals of Programming by
SaNa@seu
int x, y, z;
String firstName, lastName;
Multiple variables can be defined under one type
Declaring Variables
• Once declared the variable need to be initializedo Initialization – Specify the value we want to store in the variable
Fundamentals of Programming by
SaNa@seu
int myAge;
myAge = 32;
String myName = “SaNa";
boolean isTired = true;
int a = 4, b = 5, c = 6;
You can also initialize variables asthe declaration is done.
Declaring and Initializing
in One Step
• int x = 1;
• double d = 1.4;
Fundamentals of Programming by
SaNa@seu
int age=19;
The above statements are identical
int age;
…
age = 19;
Variable Names
Fundamentals of Programming by
SaNa@seu
int age;
float $money;
char my_char;
long _no;
String name7;
A Variable Name should start with an Alphabetical letter or $, or _ symbol
The other characters can include numbersBut you cannot use symbols like @, #, etc
Variable Names
Fundamentals of Programming by
SaNa@seu
int my age;
float @money;
char 6my_char;
long no*;
The above names are incorrect.You cannot have spaces and other
special symbols.
Variable Names
Fundamentals of Programming by
SaNa@seu
int qty;
String firstName;
float basicSal, netSal;
It’s best if you can give suitable (short but meaningful) variable names.
Numeric Operators
Fundamentals of Programming by
SaNa@seu
Name Meaning Example Result
+ Addition 34 + 1 35
- Subtraction 34.0 – 0.1 33.9
* Multiplication 300 * 30 9000
/ Division 1.0 / 2.0 0.5
% Remainder 20 % 3 2
Arithmetic Expressions
Fundamentals of Programming by
SaNa@seu
)94
(9))(5(10
5
43
y
x
xx
cbayx
is translated to
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
How to Evaluate an Expression
Fundamentals of Programming by
SaNa@seu
• Though Java has its own way to evaluate an expression behind the
scene, the result of a Java expression and its corresponding
arithmetic expression are the same. Therefore, you can safely apply
the arithmetic rule for evaluating a Java expression.
3 + 4 * 4 + 5 * (4 + 3) - 1
3 + 4 * 4 + 5 * 7 – 1
3 + 16 + 5 * 7 – 1
3 + 16 + 35 – 1
19 + 35 – 1
54 - 1
53
(1) inside parentheses first
(2) multiplication
(3) multiplication
(4) addition
(6) subtraction
(5) addition
Shortcut Assignment Operators
Fundamentals of Programming by
SaNa@seu
Operator Example Equivalent
+= i += 8 i = i + 8
-= f -= 8.0 f = f - 8.0
*= i *= 8 i = i * 8
/= i /= 8 i = i / 8
%= i %= 8 i = i % 8
Numeric Type Conversion
Consider the following statements:
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;
Fundamentals of Programming by
SaNa@seu
Conversion Rules
When performing a binary operation involving two operands
of different types, Java automatically converts the operand
based on the following rules:
1. If one of the operands is double, the other is converted into
double.
2. Otherwise, if one of the operands is float, the other is converted
into float.
3. Otherwise, if one of the operands is long, the other is converted
into long.
4. Otherwise, both operands are converted into int.
Fundamentals of Programming by
SaNa@seu
Type Casting
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is truncated)
Fundamentals of Programming by
SaNa@seu
byte, short, int, long, float, double
range increases
The String Type
The char type only represents one character. To represent a string
of characters, use the data type called String. For example,
String message = "Welcome to Java";
String is actually a predefined class in the Java library just like the
System class. The String type is not a primitive type. It is known
as a reference type. Any Java class can be used as a reference type
for a variable.
Fundamentals of Programming by
SaNa@seu
String Concatenation
// Three strings are concatenated
String message = "Welcome " + "to " + "Java";
// String Chapter is concatenated with number 2
String s = "Chapter" + 2; // s becomes Chapter2
// String Supplement is concatenated with character B
String s1 = "Supplement" + 'B'; // s1 becomes SupplementB
Fundamentals of Programming by
SaNa@seu
Exercises
1. Write a Java program that takes three marks of an exam and displays the
total and the average.
2. Write a program to input how many notes, coins of denominations of
1000/=, 500/=, 200/=, 100/= 50/=,20/=,10/=,5/=, 2/= and 1/= are
available. Print the total amount
Fundamentals of Programming by
SaNa@seu
Taking User Inputs
Fundamentals of Programming by
SaNa@seu
Using Scanner Class
• The Scanner class is a class in java.util, which allows the user
to read values of various types.
• The Scanner looks for tokens in the input. A token is a series of
characters that ends with what Java calls whitespace. A
whitespace character can be a blank, a tab character, a carriage
return, or the end of the file.
Fundamentals of Programming by
SaNa@seu
Using Scanner Class
Fundamentals of Programming by
SaNa@seu
Method Returns
int nextInt() Returns the next token as an int.
long nextLong() Returns the next token as a long.
float nextFloat() Returns the next token as a float.
double nextDouble()
Returns the next token as a double.
String next()Finds and returns the next complete token from this scanner andreturns it as a string; a token is usually ended by whitespace such asa blank or line break.
String nextLine() Returns the rest of the current line, excluding any line separator atthe end.
Using Scanner Class
Fundamentals of Programming by
SaNa@seu
01
02
03
Using Scanner Class…
Fundamentals of Programming by
SaNa@seu
Using Scanner Class…
Fundamentals of Programming by
SaNa@seu
Using Scanner Class…
Fundamentals of Programming by
SaNa@seu
Solution for Restaurant Bill – Exam - Answer
Fundamentals of Programming by
SaNa@seu
Solution for Restaurant Bill – Classroom Exam
Fundamentals of Programming by
SaNa@seu
End of Lecture
Fundamentals of Programming by
SaNa@seu