2016-20module 4 Java
2016-20module 4 Java
Object − Objects have states and behaviors. Example: A dog has states -
color, name, breed as well as behavior such as wagging their tail, barking,
eating. An object is an instance of a class.
Class − A class can be defined as a template/blueprint that describes the
behavior/state that the object of its type supports.
Methods − A method is basically a behavior. A class can contain many
methods. It is in methods where the logics are written, data is manipulated
and all the actions are executed.
Instance Variables − Each object has its unique set of instance variables. An
object's state is created by the values assigned to these instance variables.
public class MyFirstJavaProgram
{ /* This is my first java program.
* This will print 'Hello World' as the output */
public static void main(String []args) {
System.out.println("Hello World"); // prints
Hello World
}
}
Class SampleOne
{ public static void main(String args[])
{ System.out.println(“Java is better than C++”);
}
}
# Class concepts are similar to one learned earlier
as an integral part of OOP. Class is a keyword
and SampleOne is a Java Identifier
Class SampleOne
{ public static void main(String args[])
{ System.out.println(“Java is better than C++”);
}
}
# main is a method. Every Java program should have a main.
This is the starting point of execution of the program to the
interpreter. A Java program can have many classes but only
one of the classes can contain the main function. Java
Applets do not use main method.
Class SampleOne
{ public static void main(String args[])
{ System.out.println(“Java is better than C++”);
}
}
# public: access specifier for the main function, making it unprotected and
accessible for all other classes.
# Static: declare the method to belong to the class and not to the objects. main is
declared as static for usage before objects are created.
# void – the return type and nothing is returned.
# parameters to the method “main” is declared as strings called args which can be
an array also.
# println: appends newline character to the end of the string
-
class Room { float area;
{ float length; Room room1 = new Room();
float breadth;
void getdata(float a, float b) room1.getdata(14,10);
{ length = a; area = room1.length*
breadth = b; room1.breadth;
} System.out.println("Area =
} "+area);
class RoomArea }
{ public static void main(String }
args[])
class Main {
public static void main (String[] args)
{ float a = 2;
float b = 4;
System.out.println(" Before Swapping: a" + a);
System.out.println(" Before Swapping: b" + b);
float c =a;
a = b;
b = c;
System.out.println(" After Swapping: a " + a);
System.out.println(" After Swapping: b " + b);
}
};
class calculate_accept System.out.println("The difference of given
{ int a,b; numbers = "+d);
void accept_no(int p, int q) System.out.println("The product of given
{ a=p; numbers = "+e);
b=q; System.out.println("The quotient of given
numbers = "+f);
System.out.println("The first number is "+a);
}
System.out.println("The second number is
"+b); };
} class Main{
void calculate_result() public static void main (String[] args)
{ int c = a+b; { calculate_accept obj1 = new
calculate_accept();
int d = a-b;
obj1.accept_no(10,2);
int e = a*b;
obj1.calculate_result();
int f = a/b;
}
System.out.println("The sum of given numbers =
"+c); }
import java.util.Scanner;
class PrimeNumberDemo
{ public static void main(String args[]) for ( int i = 2 ; i <=n ; )
{ int n; { for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
int status = 1; { if ( num%j == 0 )
int num = 3; { status = 0;
//For capturing the value of n break;
Scanner scanner = new Scanner(System.in); }
System.out.println("Enter the value of n:"); }
//The entered value is stored in the var n if ( status != 0 )
n = scanner.nextInt(); { System.out.println(num);
if (n >= 1) i++;
{ System.out.println("First "+n+" prime }
numbers are:"); status = 1;
//2 is a known prime number num++;
System.out.println(2); }
} }
}
PROGRAM TO READ A VALUE AND CALCULATE
The interface can also inherit the other interface but, the class inheriting such an
interface must also implement all the methods of the inherited interface. As the
fields are initialized at the time of their declaration in the interface, so there is no
need of constructor in the interface hence, the interface doesn’t contain any
constructor. Let’s see the example of creating and using an interface.
interface Area };
{
double pi= 3.14; class Main
double find_area(double a,double b); {
} public static void main(String args[ ])
{
class Circle implements Area Circle C= new Circle ( );
{ double F= C.find_area(10,10);
public double find_area(double a, System.out.println("Area of the
double b) circle is : " + F);
{ }
return (pi*a*b); }
}
Both packages and interface are the
containers. Package reduces the size of the
code as we just import the class to be used
instead of again define it.
Example
In the above case, you cannot use testLocal outside of that if block.
VARIABLES
What is an Instance Variable?
// DEPARTMENT is a constant
public static final String DEPARTMENT =
"Development ";
class perimeter
Parameterized Constructor { int length, breadth;
Rectangle (int x, int y) perimeter()
{length =0; breadth = 0;}
{ length =x; width = y;} perimeter(int x, int y)
{length =x; breadth = y;}
void perimeter_calc()
}}
CLASS, OBJECT AND METHOD
Static Members
rollno = r;
name = n;
If the members (variables or functions )
have to be common between the }
objects, then they have to be declared void display ()
as Static {System.out.println(rollno+"
Ex: Static Variable "+name+" "+college);}
public static void main(String args[]){
Student8 s1 = new
class Student8{
Student8(111,"Karan");
int rollno;
Student8 s2 = new
String name; Student8(222,"Aryan");
static String college ="ITS"; s1.display();
s2.display();
Student8(int r,String n){ } }
class Main
{ static int count=0;
public void increment()
{ count++;
}
public static void main(String args[])
{ Main obj1 = new Main();
Main obj2 = new Main();
obj1.increment();
obj2.increment();
System.out.println("Obj1: count is="+obj1.count);
System.out.println("Obj2: count is="+obj2.count);
}
}
* Remove static keyword and see the difference also.
CLASS, OBJECT AND METHOD
Static Method Student9(int r, String n){
If you apply static keyword with any rollno = r;
method, it is known as static method. name = n;
}
A static method belongs to the class rather
than object of a class. void display ()
A static method can be invoked without {System.out.println(rollno+" "+name+" "+college);}
the need for creating an instance of a public static void main(String args[]){
class. Student9.change();
Student9 s1 = new Student9 (111,"Karan");
static method can access static data
Student9 s2 = new Student9 (222,"Aryan");
member and can change the value of it.
Student9 s3 = new Student9 (333,"Sonoo");
class Student9{
s1.display();
int rollno;
s2.display();
String name;
s3.display();
static String college = "ITS";
}
}
static void change(){
college = "BBDIT";
}
class Main{
int rollno; void display ()
String name; {System.out.println(rollno+" "+name+"
static int count =0; "+college+ " " +count);}
static String college = "ITS"; public static void main(String args[]){
Main.change();
static void change(){ Main s1 = new Main (111,"Karan");
college = "BBDIT"; Main s2 = new Main (222,"Aryan");
} Main s3 = new Main (333,"Sonoo");
s1.display();
Main(int r, String n){ s2.display();
rollno = r; s3.display();
name = n; }
} }
CLASS, OBJECT AND METHOD
Static Method Student9(int r, String n){
If you apply static keyword with any rollno = r;
method, it is known as static method. name = n;
}
A static method belongs to the class rather
than object of a class. void display ()
A static method can be invoked without {System.out.println(rollno+" "+name+" "+college);}
the need for creating an instance of a public static void main(String args[]){
class. Student9.change();
Student9 s1 = new Student9 (111,"Karan");
static method can access static data
Student9 s2 = new Student9 (222,"Aryan");
member and can change the value of it.
Student9 s3 = new Student9 (333,"Sonoo");
class Student9{
s1.display();
int rollno;
s2.display();
String name;
s3.display();
static String college = "ITS";
}
}
static void change(){
college = "BBDIT";
}
CLASS, OBJECT AND METHOD
z = x - y;
Inheritance System.out.println("The difference between the given
Inheritance can be defined as the process where one numbers:"+z);
}
class acquires the properties (methods and
}
fields) of another.
The class which inherits the properties of other is public class My_Calculation extends Calculation {
known as subclass (derived class, child class) public void multiplication(int x, int y) {
and the class whose properties are inherited is z = x * y;
known as superclass (base class, parent class). System.out.println("The product of the given numbers:"+z);
}
extends is the keyword used to inherit the
properties of a class. Following is the syntax of public static void main(String args[]) {
extends keyword. int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
class Calculation { demo.Subtraction(a, b);
int z; demo.multiplication(a, b);
}
public void addition(int x, int y) { }
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}
https://www.javatpoint.com/final-keyword
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
System.out.println(speedlimit);
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}
Check the same program by removing final keyword
public class Bike9{
int speedlimit=90;//final variable
void run(){
speedlimit=400;
System.out.println(speedlimit);
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}
class Bike{
final void run(){System.out.println("running");}
}