Java Day-1 2
Java Day-1 2
Concepts - Day 1
2
TCS Internal
CLASSES AND OBJECTS
3
TCS Internal
INTRODUCTION
4
INTRODUCTION
5
WHY JAVA ?
6
CLASSES
7
SCENARIO
8
CLASSES
Circle(class)
centre(field)
radius(field)
circumference()(method)
area()(method)
▪ Add fields
Classes
public class Circle {
public double x, y; // centre coordinate
public double r; // radius of the circle
10
ADDING METHODS
11
ADDING METHODS
public class Circle {
13
TCS Internal
OBJECTS
Objects have-
1. State
2. Behavior
3. Identity- java uniquely identifies all objects
14
OBJECTS
circle1 circle2
null null
17
ACCESSING OBJECT DATA
Similar to C syntax for accessing data defined in a
structure.
ObjectName.VariableName
ObjectName.MethodName(parameter-list)
18
ACCESSING METHODS IN CLASS
double area;
circle1.r = 1.0;
area = circle1.area();
19
USING CLASS
class MyMain
{
public static void main(String args[])
{
Circle circle1; // creating reference
circle1 = new Circle(); // creating object
circle1.x = 10; // assigning value to data field
circle1.y = 20;
circle1.r = 5;
double area = circle1.area(); // invoking method
double circumf = circle1.circumference();
System.out.println("Radius="+circle1.r+"
Area="+area);
System.out.println("Radius="+circle1.r+"
Circumference ="+circumf);
}
}
20
STATIC
21
THIS
22
CONSTRUCTOR
Every class has a constructor.
If we do not explicitly write a constructor for a class the
Java compiler builds a default constructor for that class.
Each time a new object is created, at least one constructor
will be invoked.
A constructor should have the same name as the class.
A class can have more than one constructor.
23
ACCESS MODIFIER
The access modifiers in java specifies accessibility (scope) of a
data member, method, constructor or class.
There are 4 types of java access modifiers:
1. Public - The public access modifier is accessible
everywhere. It has the widest scope among all other
modifiers.
2. Default – When the access modifier is not mentioned,
default is the modifier by default. The default modifier is
accessible only within package.
3. Protected - The protected access modifier is accessible
within package and outside the package but through
inheritance only.
4. Private - The private access modifier is accessible only
within class. It is the most restrictive access level.
Variables that are declared private can be accessed
outside the class using getter methods. 24
ACCESS MODIFIER
The scope of access modifiers in java
25
GETTER AND SETTER
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
} 26
ASSIGNMENT
QUE – 1: Constructor
Create a class Customer with below attributes:
id - int
name - String
dob - String
salary - double
email - String
age – int
Following Constructor needs to be created for the class
Customer:-
Create a default constructor which sets the default value as
the value of the attributes.
Create a MAIN Method to test the constructors mentioned
above.
27
ASSIGNMENT
QUE – 2: Constructor
28
ASSIGNMENT
QUE – 3: Static Members
number - int
acType - String
balance - doubnoOfAccountHoldersle
- int
numberCounter - int which is a static field and initialize to 0
29
ASSIGNMENT
30
CONDITIONAL OPERATIONS
31
TCS Internal
CONDITIONAL OPERATIONS
There are many scenarios in our everyday life, where we
need to take some decisions based on some criteria or
condition.
Decision making is one of the major part of Java also as it
is a programming language.
34
TCS Internal
IF-ELSE STATEMENT
if ... else –This statement provides a secondary path of
execution when the condition becomes false.
There can be nested if then else statement also.
OUTPUT:
Rohan is younger
35
TCS Internal
IF-ELSE STATEMENT
36
TCS Internal
SWITCH STATEMENT
Switch –A switch statement allows a variable to be tested
for equality against a list of values. Each value is called a
case, and the variable being switched on is checked for each
case.
public class SwitchExample{
public static void main(String[] args) {
Customer c1 = new Customer(102,"Nisha",31,'F');
Customer c2 = new Customer(102,"Rohan",11,'M');
char gender = 'F';
switch(gender) {
case 'F' :
System.out.println("Female");
break;
case 'M' :
System.out.println("Male");
break;
default :
System.out.println("Not specified");}} }
OUTPUT:
Female
37
TCS Internal
SWITCH STATEMENT
38
TCS Internal
TERNARY OPERATOR
The conditional operator(or ?) is a ternary operator as it has
three operands and is used to evaluate boolean expressions,
much like an if statement except instead of executing a
block of code if the test is true, a conditional operator will
assign a value to a variable.
The first value (the one to the left of the colon) is assigned
if the conditional (boolean) test is true, and the second
value is assigned if the conditional test is false. In below
example, if variable a is less than b then the variable x
value would be 50 else x =60.
39
TCS Internal
TERNARY OPERATOR
class TernaryOperatorExample{
public static void main(String[] args){
String younger = c1.getAge()<c2.getAge() ? c1.getName() :
c2.getName();
System.out.println(younger + " is younger");
}}
OUTPUT:
Rohan is younger
40
TCS Internal
ASSIGNMENT
41
ARRAYS AND ITERATIONS
42
TCS Internal
CONTENT
▪ Data Types
▪ Loops
▪ Array
43
TCS Internal
DATA TYPES
44
DATA TYPES
45
PRIMITIVE DATA TYPES
▪ A primitive type is predefined and is named by a reserved
keyword.
▪ Primitive data types in java are:
47
LOOPS
48
WHILE LOOP
The while statement continually executes a block of
statements while a particular condition is true.
The syntax can be expressed as:
49
DO-WHILE LOOP
The difference between do-while and while is that do-
while evaluates its expression at the bottom of the loop
instead of the top. Therefore, the statements within the
do block are always executed at least once.
The syntax can be expressed as:
OUTPUT:
12345
51
FOR-EACH LOOP
Its simple structure allows one to simplify code by
presenting for-loops that visit each element of an
array/collection without explicitly expressing how one
goes from element to element.
The syntax can be expressed as:
▪ Advantages of Array:
55
DISADVANTAGES OF ARRAY
▪ Disadvantages of Arrays:
56
ASSIGNMENT
QUE – 1: Array
int - id
String - name
String - dob
double - salary
String - email
int – age
searchCustomerById(Customer[] objArray)
58
ASSIGNMENT
QUE – 2: Array
int - id
String - name
double - price
int – quantity
59
ASSIGNMENT
This method will take array of Item objects and name as input
and returns new array of Item objects for all values found with
the given name else return null if not found.
60
Thank You