core java - baabtra

42
WELCOME

Upload: baabtracom-no-1-supplier-of-quality-freshers

Post on 21-Jan-2018

111 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Core java - baabtra

WELCOME

Page 2: Core java - baabtra

Core Java

Page 3: Core java - baabtra

Installation

Environment setup:• Install JDK• Install eclipse

Page 4: Core java - baabtra

JVM, JRE, JDK

Page 5: Core java - baabtra

First java program

First Java Program

public class MyClass{public static void main(String[] args){

System.out.println(“Hello baabtra”);}

}

Compile the program ->javac MyClass.javaRun it->java MyClass

Page 6: Core java - baabtra

The process

Page 7: Core java - baabtra

Features of Java

Page 8: Core java - baabtra

Naming conventionJava follows camelcase syntax for naming

Type Convention

Class Should start with uppercase letter and be a noun e.g. String,

Color, Button, System, Thread etc.

Interface Should start with uppercase letter and be an adjective e.g.

Runnable, Remote, ActionListener etc.

Method Should start with lowercase letter and be a verb e.g.

actionPerformed(), main(), print(), println() etc.

Variable Should start with lowercase letter e.g. firstName, orderNumber

etc.

Package Should be in lowercase letter e.g. java, lang, sql, util etc.

Constants Should be in uppercase letter. e.g. RED, YELLOW,

MAX_PRIORITY etc.

Page 9: Core java - baabtra

Basic Data types

Type Size Max Value Min Value Default

boolean 1 True/ False False

byte 8 -128 127 0

short 16 -215 215-1 0

int 32 -232 232-1 0

long 64 -264 264-1 0L

float 32 -232 232-1 0.0f

double 64 -264 264-1 0.0d

Char 16 0 FFFF

Page 10: Core java - baabtra

Loops and Control Structures

• while • do … while• for

• If

• If … else

• switch … case

Page 11: Core java - baabtra

Java programs1. Write a java program to check given number is even or odd

2. Write a java program to print series of even numbers between 100

and 150

3. Write a java program to find sum of numbers divisible by 9 between

500 and 1000

4. Write a java program to find factorial of a given number

5. Write a java program to reverse a string

6. Write a java program to find whether the given string is palindrome or

not

7. Write a java program to find the sum of prime numbers below 100

8. Write a java program to find area and perimeter of a circle

9. Write a java program to find area and perimeter of a square

10.Write a java program to sort 3 numbers

Page 12: Core java - baabtra

Java programs1. Write a java program to add 10 numbers to an array and sort it in

ascending order

2. Reverse number

3. Add two matrices

4. Display current system date and time

5. swap two numbers

6. Count total number of words in a string

7. Count divisors of an interger number

8. Print multiplication table

9. Save given string to a file

Page 13: Core java - baabtra

Java programs• Write a java program to print following patterns

1 2 3 4

*

* *

* * *

* * * *

* * * *

* * *

* *

*

*

* * *

* * * * *

* * * * * * *

*

* *

* * * *

* * * * *

Page 14: Core java - baabtra

Array and ListArrays have a fixed size

int arr[ ] = new int[10] ;

List is dynamic in nature

List<int> lst = new ArrayList<int>();

Page 15: Core java - baabtra

ModifiersAccess Modifiers

• default - Visible to package

• public - Visible everywhere

• private - Visible inside the class

• protected - Visible to package and all subclasses

Non Access modifiers

• static

• final

• abstract

Page 16: Core java - baabtra

Methods

modifier returnType nameOfMethod (Parameter List) {

// method body

}

Create a class Calculator with methods add, subtract,

multiply and divide. Use it from main method in different

class.

Page 17: Core java - baabtra

OOP Concepts

• Object-oriented programming (OOP) is a style of

programming that focuses on using objects to design and

build applications.

• Think of an object as a model of the concepts, processes, or

things in the real world that are meaningful to your

application.

Page 18: Core java - baabtra

Objects in Real World

Page 19: Core java - baabtra

Real World Objects

Page 20: Core java - baabtra

Objects in real world

• Object will have an identity/name

▪ Eg: reynolds, Cello for pen. Nokia,apple for mobile

• Object will have different properties which describes them best

▪ Eg:Color,size,width

• Object can perform different actions

▪ Eg: writing,erasing etc for pen. Calling, texting for mobile

Page 21: Core java - baabtra

Objects

I have an identity: I'm Volkswagen

I have different properties.My color property is green

My no:of wheel property is 4

I can perform different actionsI can be drived

I can consume fuelI can play Music

I have an identity: I'm Suzuki

I have different properties.My color property is silver

My no:of wheel property is 4

I can perform different actionsI can be drived

I can consume fuelI can play Music

Page 22: Core java - baabtra

How these objects are created?

• All the objects in the real world are created out of a basic prototype or a basic blue print or a base design

Page 23: Core java - baabtra

Objects in Software World

Page 24: Core java - baabtra

Objects in the Software World

• Same like in the real world we can create objects in computer

programming world

– Which will have a name as identity

– Properties to define its behaviour

– Actions what it can perform

Page 25: Core java - baabtra

How these Objects are created

• We need to create a base design which defines the properties and functionalities that the object should have.

• In programming terms we call this base design as Class

• Simply by having a class we can create any number of objects of that type

Page 26: Core java - baabtra

Definition

• Class : is the base design of objects

• Object : is the instance of a class

• No memory is allocated when a class is created.

• Memory is allocated only when an object is created.

Page 27: Core java - baabtra

How to create Class in Java

public class Shape

{

private int int_width;

private int int_height;

public int calculateArea()

{

return int_width * int_height;

}

}

Page 28: Core java - baabtra

How to create Class in Java

public class Shape

{

private int int_width;

private int int_height;

public int calculateArea()

{

return int_width*int_height;

}

}

Is the access specifier

Page 29: Core java - baabtra

How to create Class in Java

public class Shape

{

private int int_width;

private int int_height;

public int calculateArea()

{

return int_width* int_height;

}

}

Is the keyword for creating a class

Page 30: Core java - baabtra

How to create Class in Java

public class Shape

{

private int int_width;

private int int_height;

public int calculateArea()

{

return int_width*int_height;

}

}

Is the name of the class

Page 31: Core java - baabtra

How to create Class in Java

public class Shape

{

private int int_width;

private int int_height;

public int calculateArea()

{

return int_width* int_height;

}

}

Are two variable that referred as the properties. Normally kept private and access using getters and setters. We will discuss getters and setters later in this slide

Page 32: Core java - baabtra

How to create Class in Java

public class Shape

{

private int int_width;

private int int_height;

public int calculateArea()

{

return int_height*int_width;

}

}

Is the only member function of the class

Page 33: Core java - baabtra

How to create Objects in Java

Shape rectangle = new Shape();

rectangle.int_width=20;

rectangle.int_height=35;

rArea=rectangle.calculateArea();

This is how we create an object in java

rectangle

Height: width:

calculateArea(){return height*width;

}

Page 34: Core java - baabtra

How to create Objects in Java

Shape rectangle = new Shape();

rectangle.width=20;

rectangle.height=35;

rArea=rectangle.calculateArea();

Is the class name

Page 35: Core java - baabtra

How to create Objects in Java

Shape rectangle = new Shape();

rectangle.int_width=20;

rectangle.int_height=35;

rArea=rectangle.calculateArea();

Is the object name which we want to create

Page 36: Core java - baabtra

How to create Objects in Java

Shape rectangle = new Shape();

rectangle.int_width=20;

rectangle.int_height=35;

rArea=rectangle.calculateArea();

“new” is the keyword used in java to create an object

Page 37: Core java - baabtra

How to create Objects in Java

Shape rectangle = new Shape();

rectangle.int_width=20;

rectangle.int_height=35;

rArea=rectangle.calculateArea();

What is this???It looks like a function because its having pair of parentheses (). And also its having the same name of our class . But what is it used for ?? We will discuss it soon . Just leave it as it is for now

Page 38: Core java - baabtra

How to create Objects in Java

Shape rectangle = new Shape();

rectangle.width=20;

rectangle.height=35;

rArea=rectangle.calculateArea();

Setting up the property values of object “rectangle”

rectangle

width: 20Height: 35

calculateArea(){return width*height;

}

Page 39: Core java - baabtra

How to create Objects in Java

Shape rectangle = new Shape();

rectangle.width=20;

rectangle.height=35;

rArea=rectangle.calculateArea();

Calling the method calculateArea()

rectangle

width: 20Height: 35

calculateArea(){return 20*35;

}

Page 40: Core java - baabtra

Example

Class : Shape

Height:35 width:20

Object rectangle

calculateArea(){Return 20*35}

Height:10 width:10

Object square

calculateArea(){Return 10*10;}

Member variablesHeightWidth

Member functioncalculateArea{return height*width;

}

Page 41: Core java - baabtra

What we just did was?

• Created an object

Shape rectangle = new Shape();

Same like we declare variable.

eg: int a;

• And assigned values for it

recangle.int_height=35;

Same like we assign variable value.

eg: a=10;

Rectangle

Width:Height:

calculateArea(){return width*height;

}

Rectangle

width: 20Height: 35

calculateArea(){return 20*35;

}

Page 42: Core java - baabtra

THANK YOU