Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views120 pages

Chapter 3

Uploaded by

chaya02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views120 pages

Chapter 3

Uploaded by

chaya02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 120

Established as per the Section 2(f) of the UGC Act, 1956

Approved by AICTE, COA and BCI, New Delhi

SLA –Cloud Computing

Dr.Chaya Ravindra
Associate Professor
TYPES OF SLA
1. Customer-level SLA
A customer-based SLA is an agreement that covers all of the
services used by a customer. A customer service level agreement
covers specific details of services, provisions of service availability,
an outline of responsibilities, escalation procedures, and terms for
cancellation.
2.Service-level SLA
A service-level SLA is a contract that details an identical service
offered to multiple customers. For example, if a service provider had
multiple clients using its virtual help desk, the same service-based
SLA would be issued to all clients.
TYPES OF SLA

3.Multi-level SLA
This type of agreement is split into multiple levels that integrate
several conditions into the same system. This approach is suitable
for providers that have many customers using their product at
different price ranges or service levels. These differing service levels
can be built into a multi-level SLA
COMMON ELEMENTS OF A
SERVICE LEVEL AGREEMENT
Agreement overview
An agreement overview includes the start and end dates of an SLA, details of the
parties involved, and an overview of the services included.
Description of services
A description of services outlines all services provided within an SLA. It details
information such as turnaround times, technologies and applications, maintenance
schedules, and processes and procedures.
Exclusions
This section describes all exclusions and exemptions that are agreed upon by both
parties.
Service level objective
A service level objective (SLO) is an agreement within an SLA about a specific metric
like response time or uptime. Both parties agree to key service performance metrics
backed by data.
COMMON ELEMENTS OF A
SERVICE LEVEL AGREEMENT
Security standards
Both the service provider and the client use security standards to demonstrate the security
measures and protocols in place. This section also commonly includes non-disclosure agreements
(NDAs) and anti-poaching agreements.
Disaster recovery process
An SLA will often detail the process of disaster recovery and outline the mechanisms and processes
to follow in case of service failure of the vendor. This section also includes information on the
restarting process, including restart times and alerts.
Service tracking and reporting agreement
In this section, performance metrics are agreed upon by both parties. Most customers closely track
their service performance. A reasonable baseline for this tracking would be before and after using a
new service provider.
Penalties
This section clearly states the penalties, financial or otherwise, that either side incurs if they fail to
live up to their SLA obligations.
Termination processes
There may come a time when you want to bring your agreement to an
end. In addition to requiring a notice period from either party, the SLA
also clearly outlines the circumstances that permit termination or
expiration.
Review and change processes
It is important that you regularly review your SLA and any key
performance indicators (KPIs) that you are using to measure
performance. Any large-scale changes in your requirements need to be
recorded in the agreement.
Signatures
The agreement to each item contained in the document is reviewed
and signed by authorized individuals and pertinent stakeholders from
both sides. As long as the agreement is in effect, both parties are
bound by it.
LIFE CYCLE OF SLA
JAVA !
The Java programming is claimed as

Simple Robust

Object-
Portable
Oriented

Secure
Java Architecture
Neutral

High
Dynamic
Performance
Platform
Multithreaded
Independent
Interpreted
CURRENT POPULARITY
Java has Platform
Language Score worldwide Independence
popularity

Java offers
Parallel and
Distributed
backward
development compatibility

Base for Reliability,


Android speed and
mobile Java has consistently been more performanc
platform popular than any other programming e
language.
VON NEUMANN ARCHITECTURE OF
COMPUTING
PROGRAMMING LANGUAGES
Program

Machine Level Assembly Level High Level


ADD X Y #include<…>
11010111 main(){
MOV Z A printf(“…”)
10100101
… }

Assembler

Interpreter

Compiler
THIRD GENERATION PROGRAMMING
LANGUAGES

• A third generation (programming) language (3GL) is a grouping of programming languages that introduced significant enhancements to second
generation languages, primarily intended to make the programming language more programmer-friendly.

• English words are used to denote variables, programming structures and commands, and Structured Programming is supported by most 3GLs.

• Commonly known 3GLs are FORTRAN, BASIC, Pascal, JAVA and the C-family (C, C+, C++, C#, Objective-C) of languages. Also known as a
high-level programming language.
FUNCTION-ORIENTED
PROGRAMMING
OBJECT-ORIENTED PROGRAMMING
TOKEN

1. A token is defined as each individual component of Java program


that carries some meaning and takes active part in program
execution. The different types of tokens in Java are:
a) Identifiers
b) Literals
c) Operators
d) Punctuators
e)Separators
f)Keywords
DEFINITION OF JAVA
IDENTIFIERS

Identifiers in Java are a sequence of characters to identify


something in a program.
They are names given to a class, variable, package, method, or
interface and allow the programmer to refer to the specific item
from any place in the program
JAVA CODE SNIPPET AND TAKE NOTE OF THE
IDENTIFIERS.
TOKEN VS IDENTIFIER
Function Oriented Programming (FOP) Object Oriented Programming (OOP)

Program organization Program is divided into small parts called functions. Program is divided into parts called objects.

Importance Importance is not given to data but to functions Importance is given to the data rather than procedures

Approach FOP follows top down approach OOP follows bottom up approach

Access Specifiers Does not have any access specifier Has three access specifiers, namely Public, Private, Protected

Data Moving Data can move freely from function to function in the system Objects can move and communicate with each other

Maintainability To add new data and function is not so easy Provides an easy way to add new data and function

Function uses global data for sharing that can be accessed freely from function to function in the
Data Access system. Object uses local data and can be accessed in a control manner

Data Hiding No data hiding is possible, hence security is not possible Provides data hiding, hence secured programming is possible

Overloading Polymorphism is not possible Polymorphism is possible

Examples C, Visual Basic, FORTRAN, Pascal. C++, JAVA, VB.NET, C#.NET.


JVM

1. Java compiler compiles Java source code to


Bytecode. Bytecode cannot run on the processor
directly as processor only understands Machine
Code.
2. Java Virtual Machine (JVM) takes this Bytecode as
input and converts it into Machine Code line by line.
3. So, JVM acts as an interpreter for converting
Bytecode to Machine Code. In this way, a Java
program uses both a Compiler as well as an
Interpreter to get executed on the processor.
JAVA PROGRAMMING PARADIGMS
Java is based on the concept of object-oriented programming. As the name suggests, at
the center of it all is an object. Objects contain both data and the functionality that
operates on that data. This is controlled by the following four paradigms

• Encapsulation

• Inheritance

• Information hiding

• Polymorphism
FEATURES OF JAVA PROGRAMMING
Interfaces Multithreading Java multimedia Java script

I/O Handler JSP

Java Java Java JDBC


Java bean
Core Applet Internet
Distributed
Packages programming

Exception handling Windows toolkit Java swing Networking


PROGRAM IN C AND JAVA

A program in C to display message A program in Java to display message

#include <stdio.h> import java.lang.*;


class HelloWorldApp
int main()
{
{
public static void main(String args[])
printf("Hello, World!");
{ System.out.println("Hello,
return 0;
World!");
}
}
}

Note: Both the languages are case sensitive


PGMM

1. class StudentInfo {
2. public static void main(String args[]) {
3. System.out.println("Name: Akshay Anand");
4. System.out.println("Class: 10");
5. System.out.println("Roll No.: 5");
6.
7. }
8. }
C VERSUS JAVA
Aspects C Java Aspects C Java
Paradigms Procedural Object-oriented Supported
Inheritance No inheritance (Simple
Platform inheritance)
Dependency Dependent Independent
Pointers Supported No Pointers
Datatypes : union,
structure Supported Not supported Code translation Compiled Interpreted

Pre-processor Supported Multi-threading


Not supported and Interfaces Not supported Supported
directives (#include, #define)
Use packages Exception No exception
Header files Supported Handling handling Supported
(import)
Database
Storage class Supported Not supported Connectivity Not supported Supported
JAVA PROGRAM EDITING

• Any text editor can be used to write Java programs. For


example,
– In Windows
• Notepad, Edit, Wordpad, MS-Word, etc.
– In Unix
• vi, emacs, gedit etc.

• Save the program


– Save the program in a file with the name
HelloWorldApp.java
JAVA PROGRAM EDITING
JAVA PROGRAM EDITING
JAVA PROGRAM EDITING
JAVA PROGRAM EDITING
JAVA PROGRAM COMPILATION

The Java compiler ( javac ) converts a Java program into Java byte
code

– Open a DOS shell (in Windows) or Terminal (in Unix)

– Move to the directory where your Java program has been saved

– Enter the following command to compile:

javac HelloWorldApp.java
JAVA PROGRAM COMPILATION
JAVA PROGRAM COMPILATION
JAVA PROGRAM EXECUTION

To execute the Java program, type the command java (from


the command prompt).

– For example, the current program HelloWorldApp.class can be


executed as

java HelloWorldApp
JAVA PROGRAM EXECUTION
JAVA PROGRAM EXECUTION
C/C++ versus Java execution
C

JAVA
C++ VERSUS JAVA

Areas of applications
– C++ is best suitable for developing large software.
• Library management system, Employee management
system, Passenger reservation system, etc.

– Java is best suitable for developing communication/ Internet


application software.
• Network protocols, Internet programs, web page, web
browser, etc.
C++ VERSUS JAVA : PROGRAMMING
FEATURES
Features in C++ in Java
Data abstraction and encapsulation √ √
Polymorphism √ √
Static √ √
Binding
Dynamic √ √
Single Inheritance √ √
Inheritance
Multiple Inheritance √ ×
Operator overloading √ ×
Template classes √ ×
Global variables √ ×
Header files √ ×
Pointers √ ×
Interface and packages × √
API (Application Programming Interface) × √
C++ VERSUS JAVA : PROGRAMMING
ENVIRONMENTS Java source
code

Java
compiler

Java BYTE
code
JVM

C++
Complier Java Java Java
interprter interprter interprter
C++ source C++ object WIN32 Solaris Macintosh
code code

Intel Pentium Sun Solaris Apple Macintosh

C++ provides platform dependent programming Java provides platform independent programming
TOOLS AVAILABLE FOR JAVA
PROGRAMMING

• Java Software Developer’s Kit (SDK) : JavaTM 2 SDK


– SDK from JavaSoft, a division of Sun Microsystems Inc.

– Contains the basic tools and libraries necessary for creating, testing,
documenting and executing Java programs.

• JavaTM 2 SDK, Standard Edition


https://java.sun.com/j2se/1.4.2/docs/index.html
– Official site for JavaTM 2 SDK, Standard Edition
TOOLS AVAILABLE FOR JAVA
PROGRAMMING
There are seven main programs in SDK
 javac – the Java Compiler
 java – the Java Interpreter
 javadoc – generates documentation in HTML
 appletviewer – the Java Interpreter to execute Java applets
 jdb – the Java Debugger to find and fix bugs in Java programs
 javap – the Java Disassembler to displays the accessible functions and
data in a compiled class; it also displays the meaning of byte codes
 javah – to create interface between Java and C routines
TOOLS AVAILABLE FOR JAVA
PROGRAMMING

• Additional few sources


 Javatpoint website : Another official site for JavaTM 2 SDK,
Standard Edition, help, tutorial, etc.
1. https://www.javatpoint.com/java-tutorial

 Free Java Download : Download Java for your desktop


computer now https://www.java.com/en/download/index.jsp
RESOURCE FOR JAVA
PROGRAMMING
• There are many resources for learning Java
 The JavaTM2 Tutorials
• The Java tutorials are practical guides for programmers who want to use the Java
programming language to create applications.
https://java.sun.com/docs/books/tutorial/index.html

 Sun Developer Network


• Sun Microsystem's official website listing down all the API
documentation, latest Java Technologies, books and other resources.
https://java.sun.com/reference/docs/
PACKAGES IN JAVA

API (Application Programming Interface) in Java SDK


– The API enables Java programmers to develop varieties of applets
and applications
– It contains nine packages
• java.applet – for applet programming
• java.awt – the Abstract Windowing Toolkit for designing GUI like Button, Checkbox,
Choice, Menu, Pannel, etc.
• java.io – file input/output handling
• java.lang – provides useful classes like to handle Object, Thread, Exception, String,
System, Math, Float, Integer, etc.
PACKAGES IN JAVA
• java.lang – provides useful classes like to handle Object, Thread, Exception, String,
System, Math, Float, Integer etc.

• java.net – classes for network programming; supports TCP/IP networking protocols

• java.util – it contains miscellaneous classes like Vector, Stack, List, Date, Dictionary,
Hash etc.

• javax.swing – for designing graphical user interface (GUI)

• java.sql – for database connectivity (JDBC)


OTHER THIRD PART TOOLS FOR
JAVA PROGRAMMING
Java IDE (Integrated Development Environment)
– Number of IDEs are available to support the productivity of software development
• Sun’s Java Workshop from Sun’s JavaSoft (recently powered with Visual Java)

• Mojo from Penumbra Software (best visual environment for creating Java
applets)

• Jumba from Aimtech and IBM (graphical applet builder)

• Semantic Café from Semantics (a de-facto standard for Java development on


Windows systems)
OTHER THIRD PART TOOLS FOR
JAVA PROGRAMMING

1.Web browser

– Java environment requires Java-enabled web browser to supports Java


applets

– Few (free) popular Java-enabled web browsers:

• HotJava from JavaSoft web site (http://java.sun.com)

• Netscape Navigator from Netscape home page (http://home.nescape.com)

• Internet Explorer from Microsoft’s web page (http://www.microsoft.com)


FEW MORE FROM JAVA
PROFESSIONALS

Net Beans - https://netbeans.org/downloads/


− This is one of the most commonly used IDEs for Java and some major languages.

NotePad++ - https://notepad-plus-plus.org/download/v7.5.8.html

− This is a very advanced and handy NotePad,


it has several built-in tools and functions for making programming easy.
A RICH SUBSET OF THE JAVA
LANGUAGE
BUILT-IN DATA TYPES IN JAVA

In Java, every variable has a type declared in the source code. There are two
kinds of types: reference types and primitive types. Reference types are
references to objects. Primitive types directly contain values.

Type Size Type Size


boolean 1 bit int 32 bits

byte 8 bits long 64 bits

char 16 bits float 32 bits

short 16 bits double 64 bits


THE JAVA CHARACTER SET

•The Java language


alphabet
•Uppercase letters ‘A’ to ‘Z’
•Lowercase letters ‘a’ to ‘z’
•Digits ‘0’ to ‘9’
, < > . _
•Java special characters: ( ) ; $ :
% [ ] # ?
' & { } "
^ ! * / |
- \ ~ +
IDENTIFIERS IN JAVA
• Identifiers
• Names given to various program elements (variables, constants, class, methods,
etc.)
• May consist of letters, digits and the underscore (‘_’) character, with no space
between.
• Blank and comma are not allowed.
• First character must be an alphabet or underscore.
• An identifier can be arbitrary long.
• Identifier should not be a reserved word.

• Java programming language is case sensitive.


• area, AREA and Area are all different!
DATATYPE DECLARATION RULE
Declaration and assignment
statements

int a, b = 0;

a = 123;

b = 45;

int c = a + b;

System.out.print(“The sum is” + c);


ARRAY IN JAVA
0 1 2 3 4 n-2 n-
1
marks

marks.length = n

An array is a finite ordered and collection of homogeneous


data elements.

Following are the three tasks to manipulate an array in


Java
• Declaration of an array.
• Allocate memory for it.
• Loading the values into array.
CREATING AN ARRAY
Declaration of array Allocate memory for an array
<type> <arrayName> = new <type> [<size>];
<arrayName>[ ];
Example:
Example:
int x[ ]; x = new int [100 ];
<type>[ ] <arrayName>;
Example:
int [ ] x;
CREATING AN ARRAY

Define and allocate memory together


<type> <arrayName> [ ] = new <type>
[<size>];

Example:
int x [ ] = new int [100];
STORING ELEMENTS IN ARRAY

Initialization of Array
<arrayName> [<subscript> ] =
<value>; Example:
x [5] = 100;

for (int i = 0; i < 100; i++)


x[i] = <value>;
STORING ELEMENTS IN ARRAY

Initialization of array: An alternative way


<type> <arrayName> [ ] = { <list of

values> }; Example:
int x [ ] = {12, 3, 9, 15};

Here, declaration, allocation of memory and array


initialization all are at one go!
CLASS CONCEPT IN JAVA
CONCEPT OF ENCAPSULATION

• The object is at the core of Java programming.

• Java provides the concept of class to build objects.

• A class defines the shape and working of an object.

• The concept of class is the logical construct upon which the entire Java
language is built.
WHAT IS A CLASS?
• A class is a group of objects, which have common
properties. Fields
• It is a template or blueprint from which objects are
created. Methods
• It is a logical entity.
Constructors
A class in Java can contain:
Blocks
• Fields
• Methods Nested Class
• Constructors
• Blocks Interfaces
• Nested class(es) and interface(s)
GENERAL STRUCTURE OF A CLASS
class <class-name>{
<type> <variable 1>;
<type> <variable 2>;
<type> <variable 3>;
...
<type> <variable n>;

<type> <method 1>(<parameter-list 1>){


// Body of the method 1
}
<type> <method 1>(<parameter-list 2>){
// Body of the method 2
}
...
<type> <method 1>(<parameter-list m>){
// Body of the method m
}
}
JAVA CLASS – AN EXAMPLE

class Circle {
double x, y; // The coordinates of the center r
double r; // The radius
} (x, y)
ADDING METHODS TO CIRCLE
CLASS

class Circle {
double x,y; // The coordinates of the center
double r; // The radius

// Method that returns circumference


double circumference(){
return 2*3.14159*r;
}
// Method that returns area
double area(){
return
(22/7)*r*r;
}
}
Methods in Java can be broadly classified into two types:
• Predefined
• User-defined
HOW TO DECLARE METHODS
IN JAVA?

You can only create a method within a class. There are a total of six
components included in a method declaration.
The components provide various information about the method.
Below is the syntax to declare a method and its components list.

public int addNumbers (int a, int b){


//method body
}
ACCESS SPECIFIER:

It is used to define the access type of the method. The above syntax
sees the use of the “public” access specifier. However, Java provides
four different specifiers, which are:
• Public: You can access it from any class
• Private: You can access it within the class where it is
defined
• Protected: Accessible only in the same package or other
subclasses in another package
• Default: It is the default access specifier used by the Java
compiler if we don’t mention any other specifiers. It is
accessible only from the package where it is declared
RETURNTYPE:

It defines the return type of the method. In the above syntax, “int”
is the return type. We can mention void as the return type if the
method returns no value.
Method name:
1. It is used to give a unique name to the method. In the above
syntax, “addNumbers” is the method name. This tutorial looks at
some tips for naming a method, shortly.
PARAMETER LIST:

It is a list of arguments (data type and variable name) that will be


used in the method. In the above syntax, “int a, int b” mentioned
within the parentheses is the parameter list. You can also keep it
blank if you don’t want to use any parameters in the method.
Method signature:
You don’t have to do anything additional here. The method
signature is just a combination of the method name and parameter
list.
Method body:
This is the set of instructions enclosed within curly brackets that the
method will perform.
METHODS IN JAVA CAN ALSO BE CLASSIFIED
INTO THE FOLLOWING TYPES:

• Static Method
• Instance Method
• Abstract Method
• Factory Method
STATIC METHOD
APPLYING INSTANCE METHODS IN
JAVA CODE
INSTANCE METHODS ARE
FURTHER DIVIDED INTO TWO
TYPES:

• Accessor Method
1. It is used to get a private field’s value, accessor methods in Java
can only read instance variables. They are always prefixed with
the word ‘get’.
• Mutator Method
1. It is used to get and set the value of a private field, mutator
methods in Java can read and modify instance variables. They
are always prefixed with the word ‘set’.
USING ABSTRACT METHODS
IN JAVA
FACTORY METHOD

1. Factory methods are the ones that return an object to the class
where it belongs. Usually, all static methods also fall into this
type of method.
DECLARING OBJECT OF TYPE
CIRCLE CLASS
// A program that uses the circle class
// Call this file circledemo1.java
class Circle { //The following class declare an object of type Circle
double x,y; // The coordinates of the center class CircleDemo1 {
double r; // The radius public static void main(String args[]){
Circle c = new Circle();
// Method that returns circumference c.x = 0.0;
double circumference(){ c.y = 0.0;
return 2*3.14159*r; c.r = 5.0;
} System.out.println("Circumference" +
// Method that returns area c.circumference());
double area(){ System.out.println("Area" + c.area());
return (22/7)*r*r; }
} }
}
MULTIPLE OBJECTS DECLARATION

//The following class declares multiple objects of type


Circle class CircleDemo2 {
public static void main(String args[]){
// A program that declares two objects of the Circle class
Circle c1 = new Circle();
// Call this file CircleDemo2.java
class Circle { Circle c2 = new Circle();
double x, y; // Initialize the circles
double r; c1.x = 3.0;
c1.y = 4.0;
double circumference(){ c1.r = 5.0;
return 2*3.14159*r; c2.x = -4.0;
}
c2.y = -8.0;
double area()
c2.r = 10.0;
{ return
(22/7)*r*r; System.out.println("Circumfe
} } rence Circle 1" +
c1.circumference());
System.out.println("Area Circle 1" + c1.area());
System.out.println("Circumference Circle 2" + c2.circumference());
System.out.println("Area Circle 2" + c2.area());
}
}
MULTIPLE CLASSES IN A PROGRAM
width
depth
r
(x, y) height

class
class Circle {
Box{ double
double x,y; width; double
double r; height; double
double circumference(){ depth; double
area(){
return 2*3.14159*r;
double a;
} a = (width*height + height*depth + width*depth) * 2;
double area(){ return a;
return (22/7)*r*r; }
double volume(){
}
double v;
} v =
width*height
*depth;
return v;
}
}
MULTIPLE CLASS OBJECTS

// Declaring objects of type Circle and


Box class MulticlassDemo {
public static void main(String args[])
{ Circle c = new Circle();
Box b = new Box();
// Initialize the circles
c.x = 3.0; c.y = 4.0; c.r = 5.0;
b.width = 3.0; b.height = 4.0; b.depth = 5.0;
System.out.println("Circumference Circle" + c.circumference());
System.out.println("Area Circle" + c.area());
System.out.println("Area of Box" + b.area());
System.out.println("Volume of Box" + b.volume());
}
}
// Save this file as MulticlassDemo.java
IMPORTANT NOTES

1. There should be a class which contains a method main(). This class is


called main class.

2. There should be only one main class.

3. The name of the program file should be same as the name of the main
class followed by .java as an extension.

4. If there is no main class, then there should be compilation error.


METHOD WITH PARAMETERS
class Circle class CircleDemo3 {
{ double public static void main(String args[])
x,y; double { Circle c1 = new Circle();
r; Circle c2 = new Circle();
double circumference(){ // Initialize the circles
return 2*3.14159*r; c1.setCircle(3.0,4.0,5.0);
} c2.setCircle(-4.0,8.0,10.0);
double area(){ System.out.println("Circumference Circle 1" + c1.circumference());
return (22/7)*r*r; System.out.println("Area of circle 1" + c1.area());
} System.out.println("Circumference Circle 2" + c2.circumference());
void setCircle(double a, double b, double c) System.out.println("Area of circle 2" + c2.area());
{ x = a; // Set center x-coordinate }
y = b; // Set center y-coordinate }
r = c; // Set radius
}
}

Name the file as CircleDemo3.java


CONSTRUCTOR FOR AUTOMATIC
OBJECT INITIALIZATION

1. It can be tedious to initialize all of the variables in a class each time an


object is instantiated.

2. Java allows objects to initialize themselves when they are


created/ declared.

3. This automatic initialization is performed through the concept


of constructor.
CONSTRUCTORS – SOME
PROPERTIES
1. A constructor initializes an object immediately upon creation.

2. Constructor in Java is a method.

3. This method has the same name as the class in which it resides.

4. Once defined, the constructor is automatically called immediately after object is created.

5. Constructor is a method which has no return type.

6. In fact, the implicit return type of a class constructor is the class type itself.

7. Constructor initialize the internal state of an object.


CONSTRUCTOR : AN EXAMPLE
class Circle {
double x,y;
double r;
double circumference(){
return 2*3.14159*r;
}
double area(){
return (22/7)*r*r;
}

Circle (double a, double b, double c){


x = a; // Set center x-coordinate class CircleDemo4 {
y = b; // Set center y-coordinate public static void main(String args[])
r = c; // Set radius { Circle c1 = new Circle(3.0,4.0,5.0);
} Circle c2 = new Circle(-4.0,8.0,10.0);
} System.out.println("Circumference Circle
1" + c1.circumference());
System.out.println("Area Circle 1" + c1.area());
System.out.println("Circumference Circle 2" + c2.circumference());
System.out.println("Area Circle 2" + c2.area());
}
}
THIS KEYWORD CONCEPT

1. this is used to reduce name-space collisions.

2. Sometimes a method will need to refer to the object that invoked it.

3. To allow this Java defines this keyword.

4. this can be used inside any method to refer to the current object.

5. That is, this is always a reference to the object on which the method
is invoked.
CONSTRUCTOR : AN EXAMPLE
class Circle class Circle
{ double { double
x,y; double x,y; double
r; r;
double circumference(){ double circumference(){
return 2*3.14159*r; return 2*3.14159*r;
} }
double area(){ double area(){
return (22/7)*r*r; return (22/7)*r*r;
} }
void setCircle(double a, double b, double c){ Circle (double x, double y, double r)
x = a; // Set center x-coordinate { this.x = x; // Set center x-
y = b; // Set center y-coordinate r = coordinate this.y = y; // Set center
c; // Set radius y-coordinate this.r = r; // Set radius
} }
} }

class CircleDemo5 {
public static void main(String args[])
{ Circle c1 = new Circle();
c1.setCircle(3.0,4.0,5.0);
Circle c2 = new Circle (-
4.0,8.0,10.0);
System.out.println("Circumference Circle 1" + c1.circumference());
System.out.println("Area Circle 1" + c1.area());
System.out.println("Circumference Circle 2" + c2.circumference());
System.out.println("Area Circle 2" + c2.area())
}
}

Name the file as


CircleDemo5.java
MULTIPLE CONSTRUCTORS

1. Sometimes, it is necessary to initialize an object in a number of


ways.

2. Java allows his using the concept of constructor overloading.

3. In other words, Java allows to declare one or more constructor


method with different lists of parameters and different method
definition.
CONSTRUCTOR OVERLOADING : AN
EXAMPLE
class Circle class CircleDemo6 {
{ double public static void main(String args[]){
x,y; double Circle c1 = new Circle(3.0,4.0,5.0);
r;
Circle c2 = new Circle(5.0);
Circle (double x, double y, double r){
this.x = x; this.y = y; this.r =
Circle c3 = new Circle(c1);
r; Circle c4 = new Circle();
} System.out.println("Circumference Circle 1" + c1.circumference());
Circle (double r){ System.out.println("Area Circle 1" + c1.area());
x = 0; y=0; this.r = r; System.out.println("Circumference Circle 2" + c2.circumference());
} x = c.x; y = c.y; r = System.out.println("Area Circle 2" + c2.area());
Circle
} c.r;
(Circle c){ System.out.println("Circumference Circle 3" + c3.circumference());
Circle (){
System.out.println("Area Circle 3" + c3.area());
x = 0.0; y = 0.0; r = 1.0;
System.out.println("Circumference Circle 4" + c4.circumference());
}
double circumference(){ System.out.println("Area Circle 4" + c4.area());
return 2*3.14159*r; }
} }
double area(){
return (22/7)*r*r;
}
}
Name the file as CircleDemo6.java
THIS KEYWORD AGAIN

1. There is a specialized use of this keyword that arises when a class


has multiple constructors.

2. In that case, this can be used from one constructor to invoke one of
the other constructor of the same class.
THIS WITH MULTIPLE
CONSTRUCTORS : AN EXAMPLE
class Circle
{ double x,
y; double r;
Circle (double x, double y, double r){
this.x = x; this.y = y; this.r =
r;
}
Circle (double r)
{ this(0.0, 0.0, Note:
r);
1. There is a very important restriction on the this syntax.
}
Circle (Circle c) 2. It should appear only as the first statement in a
{ this(c.x, c.y. constructor.
c.r);
}
Circle (){
this(0.0, 0.0, 1.0);
}
double circumference(){
return 2*3.14159*r;
}
double area()
{ return
(22/7)*r*r;
}
}
THIS KEYWORD
INHERITANCE IN JAVA

1. Inheritance in Java is a mechanism in which one object


acquires all the properties and behaviors of a parent object.
2. It is an important part of OOPs (Object Oriented programming
system
TERMS USED IN INHERITANCE

•Class: A class is a group of objects which have common properties. It is a


template or blueprint from which objects are created

•Sub Class/Child Class: Subclass is a class which inherits the other class. It is
also called a derived class, extended class, or child class.
•Super Class/Parent Class: Superclass is the class from where a subclass
inherits the features. It is also called a base class or a parent class.
•Reusability: As the name specifies, reusability is a mechanism which facilitates
you to reuse the fields and methods of the existing class when you create a new
class. You can use the same fields and methods already defined in the previous
class
INHERITANCE
TYPES OF INHERITANCE IN
JAVA
TYPES OF INHERITANCE IN
JAVA
SINGLE INHERITANCE
EXAMPLE
MULTILEVEL INHERITANCE
EXAMPLE
HIERARCHICAL INHERITANCE
EXAMPLE
MULTIPLE INHERITANCE IS
NOT SUPPORTED IN JAVA

1. Consider a scenario where A, B, and C are three classes. The C


class inherits A and B classes. If A and B classes have the same
method and you call it from child class object, there will be
ambiguity to call the method of A or B class.
2. Since compile-time errors are better than runtime errors, Java
renders compile-time error if you inherit 2 classes. So whether
you have same method or different, there will be compile time
error
3D ARRAYS : AN EXAMPLE
class a3DArray {
public static void main(String args[]) {
int my3DArray [ ][ ][ ] = new int [3][4][5];
int i, j, k;
for(i=0; i<3; i++)
for(j=0;
j<4; j++)
for(k=0; k<5; k++) my3DArray[i][j]
[k] = i * j * k;
for(i=0; i<3; i++)
{ for(j=0; j<4; j++)
{
for(k=0; k<5; k++)
System.out.print(my3DArray[i][j][k] + " ");
System.out.println();
}
System.out.println();
}
}
}
EXAMPLE PROGRAM USING AN
ARRAY
class TestArray{
public static void main(String args[]){

int a[] = new int[5]; //Declaration and instantiation


a = {10, 20, 30, 40, 50}; //Initialization
//Traversing array
for(int i=0;i<a.length;i++){ //length is the property of array
System.out.println(a[i]);
}
// Average calculation
float sum = 0; avg;
for(i=0;i<a.length;i++) //Calculating the sum of the numbers
sum += a[i];
avg = sum/a.length;
System.out.println(“Avergae = “ + avg);
}
}
PROCESSING ELEMENTS IN AN
ARRAY
• Insertion
• Insertion at any location
• Insertion at front
• Insertion at end
• Insertion is sorted order
• Deletion
• Deletion a particular element
• Deletion an element at a particular location
• Deletion the element at front
• Deletion the element at end
• Searching and Traversal
• Finding the smallest and largest element
• Printing all element or some specific element
• Sorting
• In ascending order, descending order, lexicographical order etc.
ARRAY IN JAVA: A QUICK VISIT
• Declaration of an array • Initialization of an array
Examples Examples
int numbers[ ];
int numbers[] = {5,
float averageScores[ ];
4, 2, 1, 3};
int [ ] rollNo;
float [ ] marks; float marks[] = {2.5,
3.4, 4.5};
What is the size of the array marks?
• Memory allocation for an array
Examples n = marks.length;
numbers = new int [5];
averageScores = new float [20];
rollNo = new int [49];
marks = new float [54];
How to define a two dimensional array?
CREATING A 2D ARRAY

Declare and Allocate


Example:

int myArray [ ] [ ];
myArray = new int [3]
[4];
OR
int myArray [ ] [ ] = new
int [3] [4];
LOADING A 2D ARRAY
Initializing a 2D array : An
example1 2 3

4 5 6

int myArray [2] [3] = {1, 2, 3, 4, 5, 6};

OR
int myArray [ ] [ ] = { {1, 2, 3}, {4, 5, 6} };
VARIABLE SIZED 2D ARRAY

x[0
]

x[1
]

x[2
]
 Creating a variable-sized 2D array Another way: Example
<type><2DarrayName>[][] = new <type> [<rowSize>][]; int x [ ] [ ] = new int [3][ ];
for (int i = 0; i < <rowSize>; i++) x[0] = new int [2];
x[1] = new int [4];
<2DarrayName>[i] = new <type> [<colSizei x[2] = new int [3];
>];

You might also like