/* Association : is a process in which we define object of a class inside another class. example. class Engine { } class Car { Engine e=new Engine(); // we have created object of Engine inside Car } need of Association : reusability (we can reuse the functionlaity/methods of any other class)
Types of Association : 1. Composition: also called as Has-A relation, like Car Has-A Engine 2. Aggregation: also called as Uses-A relation, like Car Uses-A MusicPlayer
THIS IS A JAVA PROGRAM TO SHOW THE COMPOSITION: */ // lets define a class class Adder { // non-static-data-members int x; int y; // non-static-method void set(int a,int b) // a and b are local variables { x=a; y=b; } // non-static-method void add() { System.out.println("****Method of Adder class****"); // z is a local variable int z=x+y; // z is a local var System.out.println("Addition is "+z); } } // let define another class class TestAdder { // lets create object of class Adder Adder obj=new Adder(); // non-static-method void test() { System.out.println("****Method of TestAdder class****"); // lets call the methods of Adder class obj.set(100,200); obj.add(); } public static void main(String[] args) { // create object of class TestAdder TestAdder ta=new TestAdder(); // call method test() of TestAdder ta.test(); } }
THIS IS A JAVA PROGRAM TO PRINT A PYRAMID. I HAVE DIVIDED THIS PROGRAM INTO MODULES (METHODS) TO REDUCE THE COMPLEXITY
import java.util.Scanner; // Scanner class is used to take input (like from keyboard) public class DrawPyramid { int num; void input() { // object of Scanner is created Scanner sc=new Scanner(System.in); // System.in is a notation of keyboard System.out.print("How many stars to be printed :"); num=sc.nextInt(); } void printUpperTriangle() { for(int i=1;i=i;j--) { System.out.print(" "); } // print stars for(int k=1;k<=i;k++) { System.out.print(" *"); } // print a line after every row System.out.println(); } } void printLowerTriangle() { for(int i=1;i<=num;i++) { // print spaces for(int j=i;j>=1;j--) { System.out.print(" "); } // print stars for(int k=num;k>=i;k--) { System.out.print(" *"); } // print a line after every row System.out.println(); } } public static void main(String[] args) { // create object of DrawPyramid DrawPyramid obj=new DrawPyramid(); // call methods obj.input(); obj.printUpperTriangle(); obj.printLowerTriangle(); } }
What is Java : Java is a beautifully designed object oriented programming language.
What is history of Java (Point by point) : 1. Designed by Mr. James Gosling in Sun-Micro-system Inc (USA), this company was co-founded by Mr. Vinod Khosla. 2. Java was designed for Consumer electronics (mainly for controlling Set-Top-Box using remote control). 3. The Project in which Java was designed named as Project Stealth. It was renamed into Project Green. 4. The first name of Java was GreenTalk, then Oak (taken from tree outside Gosling office), and finally Java. 5. Java is influenced by C, Mesa, and Objective-C programming languages (Objective-C is used for developing ios based applications). 6. Sun-Micro-system was acquired by Oracle in 2010. 7. Java was introduced in 1995.
"C or C++" VS JAVA Similarities:- 1. Case Sensitive. (e.g. we can not us 'a' in the place of 'A') 2. Context Free Grammar (CFG- empty spaces like enter and tab doesn't counts.) Differences:- Available in c or c++ but fortunately not in java- 1.Pointer 2.Garbage value 3.Global value 4.Structs/Union 5.Header Files 6.Storage classes if we only talk both object oriented programing languages c ++ and JAVA, these are some features that are available in c++ but fortunately not in java- *Multiple Inheritance *Copy Constructor *Operator Overloading *Inline Function *Reference Operator *Virtual Functions *Distructors *Call by Value, Call by Reference, Call by Address Note: Call by value is supported by java also.
/* Serialization--> it is a process in which an object is converted into bytes. (now these bytes can be written into the file.) The reverse process is de-serialization in which the bytes are reassembled into a new object.
How to implement-->
Step 1: Create object of FOS and connect it with a file. step 2: Create object of ObjectOutputStream class and connect it with FileOutputStream. Step 3: Create object of the class whose object is to be saved. step 4: Call writeObject() of ObjectOutputStream class and pass your object into it.
Retrieve object from the file-->
step 1: create object of FIS. step 2: create object of OIS and connect it to FIS. step 3: call ReadObject() on object of ObjectInputStream.
NOTE:-"The ReadObject() returns the object of specific class in form of OBJECT Type, so it is mandatory to downcast the type as per use requirement."
NOTE:-"If we apply Transient on a variable it will not save into the file during serialization. During de-serialization it's default value will be fetched."
NOTE:-"When object is created during de-serialization the constructor never evokes because the object is created by using an internal process known as Deep Cloning. (Byte by Byte assembly)"
NOTE:-"If super class is not serializable than during de-serialization the constructor of super class will be evoked and data members will be reset to their previous state."
*/
//Let's create a program, First we need a class whose object we can use.
import java.io.*; import java.util.*;
class Person implements Serializable { // data members are private, methods are public, if class would also public, it would be a perfect example of Encapsulation.
private String name; private String city; transient int age; // if we want any data unaffected by serialization process, make it transient.
public Person() { Scanner sc= new Scanner(System.in); System.out.print("Enter name:"); name=sc.next(); System.out.print("Enter age:"); age=sc.nextInt(); System.out.print("Enter city:"); city=sc.next(); } public void show() { System.out.println("Name-"+name+"\nAge-"+age+"\nCity-"+city); } }
// now we create a class in which we will write our serialization and de-serialization code.
public class SerialiZation {
//let's create an individual methods to perform serialization
void Out() throws Throwable { FileOutputStream fos= new FileOutputStream("object_holder.txt"); ObjectOutputStream oos= new ObjectOutputStream(fos); Person p1=new Person(); oos.writeObject(p1); System.out.println("Object saved..!!"); oos.close(); fos.close(); }
// one more method, this will perform de-serialization
void In() throws Throwable { FileInputStream fis= new FileInputStream("object_holder.txt"); ObjectInputStream ois= new ObjectInputStream(fis); Person p= (Person)ois.readObject(); p.show(); fis.close(); ois.close(); }
//now we create main() in which we will instantiate serialization class then call out() and in()
public static void main(String [] adb) throws Throwable { SerialiZation s= new SerialiZation(); s.Out(); s.In(); } }
If we apply final on non primitives data types, the address of object that they contains can't be changed. It means the object pointed by them is fixed.
/*
ReplyDeleteAssociation : is a process in which we define object of a class inside another class.
example.
class Engine
{
}
class Car
{
Engine e=new Engine(); // we have created object of Engine inside Car
}
need of Association :
reusability (we can reuse the functionlaity/methods of any other class)
Types of Association :
1. Composition: also called as Has-A relation, like Car Has-A Engine
2. Aggregation: also called as Uses-A relation, like Car Uses-A MusicPlayer
THIS IS A JAVA PROGRAM TO SHOW THE COMPOSITION:
*/
// lets define a class
class Adder
{
// non-static-data-members
int x;
int y;
// non-static-method
void set(int a,int b) // a and b are local variables
{
x=a;
y=b;
}
// non-static-method
void add()
{
System.out.println("****Method of Adder class****");
// z is a local variable
int z=x+y; // z is a local var
System.out.println("Addition is "+z);
}
}
// let define another class
class TestAdder
{
// lets create object of class Adder
Adder obj=new Adder();
// non-static-method
void test()
{
System.out.println("****Method of TestAdder class****");
// lets call the methods of Adder class
obj.set(100,200);
obj.add();
}
public static void main(String[] args)
{
// create object of class TestAdder
TestAdder ta=new TestAdder();
// call method test() of TestAdder
ta.test();
}
}
THIS IS A JAVA PROGRAM TO PRINT A PYRAMID.
ReplyDeleteI HAVE DIVIDED THIS PROGRAM INTO MODULES (METHODS) TO REDUCE THE COMPLEXITY
import java.util.Scanner; // Scanner class is used to take input (like from keyboard)
public class DrawPyramid
{
int num;
void input()
{
// object of Scanner is created
Scanner sc=new Scanner(System.in);
// System.in is a notation of keyboard
System.out.print("How many stars to be printed :");
num=sc.nextInt();
}
void printUpperTriangle()
{
for(int i=1;i=i;j--)
{
System.out.print(" ");
}
// print stars
for(int k=1;k<=i;k++)
{
System.out.print(" *");
}
// print a line after every row
System.out.println();
}
}
void printLowerTriangle()
{
for(int i=1;i<=num;i++)
{
// print spaces
for(int j=i;j>=1;j--)
{
System.out.print(" ");
}
// print stars
for(int k=num;k>=i;k--)
{
System.out.print(" *");
}
// print a line after every row
System.out.println();
}
}
public static void main(String[] args)
{
// create object of DrawPyramid
DrawPyramid obj=new DrawPyramid();
// call methods
obj.input();
obj.printUpperTriangle();
obj.printLowerTriangle();
}
}
What is Java :
ReplyDeleteJava is a beautifully designed object oriented programming language.
What is history of Java (Point by point) :
1. Designed by Mr. James Gosling in Sun-Micro-system Inc (USA), this company was co-founded
by Mr. Vinod Khosla.
2. Java was designed for Consumer electronics (mainly for controlling Set-Top-Box using remote control).
3. The Project in which Java was designed named as Project Stealth. It was renamed into Project Green.
4. The first name of Java was GreenTalk, then Oak (taken from tree outside Gosling office), and finally Java.
5. Java is influenced by C, Mesa, and Objective-C programming languages (Objective-C is used for developing ios based applications).
6. Sun-Micro-system was acquired by Oracle in 2010.
7. Java was introduced in 1995.
"C or C++" VS JAVA
ReplyDeleteSimilarities:-
1. Case Sensitive. (e.g. we can not us 'a' in the place of
'A')
2. Context Free Grammar (CFG- empty spaces like enter
and tab doesn't counts.)
Differences:- Available in c or c++ but fortunately not in
java-
1.Pointer
2.Garbage value
3.Global value
4.Structs/Union
5.Header Files
6.Storage classes
if we only talk both object oriented programing languages c
++ and JAVA, these are some features that are available in
c++ but fortunately not in java-
*Multiple Inheritance
*Copy Constructor
*Operator Overloading
*Inline Function
*Reference Operator
*Virtual Functions
*Distructors
*Call by Value, Call by Reference, Call by
Address
Note: Call by value is supported by java also.
soon i will post a program too...
ReplyDeleteimport java.util.Scanner;
ReplyDeleteimport java.util.Random;
class wordPuzzle
{
Scanner sc = new Scanner(System.in);
private char userInput;
private boolean b=true;
private int turnLeft=8;
String st;
private char[] c = {'_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_'};
String difwords() {
String[] gameString = {"TEVAR","GUNDAY","TIGER","HAPPY NEW YEAR","BANG BANG","BADLAPUR","RANG DE BASANTI","BABY","HOLIDAY","WEDNESDAY","EK VILLAIN","COCKTAIL","YAARIYAN","MARY KOM","HAIDER","HAPPY ENDING","LINGAA","UNGLI","HIGHWAY","FINDING FANNY","MARDAANI"};
Random r = new Random();
int random = r.nextInt(21);
return gameString[random];
}
void findVowels(String s) {
st=s;
System.out.println("______________________________________________________________");
System.out.println();
for(int i=0; i<s.length(); i++)
{
if ((s.charAt(i) == 'A') | (s.charAt(i)=='E') | (s.charAt(i)=='I') | (s.charAt(i)=='O') | (s.charAt(i)=='U') | (s.charAt(i)==' '))
{
c[i] = s.charAt(i);
System.out.printf("%s ",c[i]);
continue;
}
System.out.printf("%s ",c[i]);
}
System.out.println();
System.out.println("______________________________________________________________");
}
void play(String s) {
while (b)
{
System.out.print("Enter Your Character : ");
userInput = sc.next().charAt(0);
System.out.println();
System.out.println();
System.out.println("______________________________________________________________");
System.out.println();
System.out.print("********** ");
for(int i=0; i<s.length(); i++)
{
if (s.charAt(i)==userInput || s.charAt(i)==Character.toUpperCase(userInput))
{
c[i] = Character.toUpperCase(userInput);
}
System.out.printf("%s ",c[i]);
}
if ((s.indexOf(Character.toUpperCase(userInput))==-1))
{
turnLeft--;
}
System.out.println("********** ");
System.out.println("______________________________________________________________");
System.out.println("Turn Left: "+turnLeft);
System.out.println();
System.out.println();
System.out.println();
String result = String.copyValueOf(c);
String Comp = result.substring(0, s.length());
if (turnLeft<=0){
System.out.println("You Have Lost");
}
if (s.equalsIgnoreCase(Comp) || turnLeft<=0)
{
b=false;
System.out.print("Hurrayyy!!!! You have WON!!!!");
}
}
}
}
class startPlay
{
public static void main(String args[])
{
wordPuzzle pc = new wordPuzzle();
pc.findVowels(pc.difwords());
pc.play(pc.st);
}
}
its a small game created by me...
ReplyDeletei hope you would like it sir!!!!...
very very good rahil
Deletenice work
god bless
/*
ReplyDeleteSerialization-->
it is a process in which an object is converted into bytes. (now these bytes can be written into the file.) The reverse process is de-serialization in which the bytes are reassembled into a new object.
How to implement-->
Step 1: Create object of FOS and connect it with a file.
step 2: Create object of ObjectOutputStream class and connect it with FileOutputStream.
Step 3: Create object of the class whose object is to be saved.
step 4: Call writeObject() of ObjectOutputStream class and pass your object into it.
Retrieve object from the file-->
step 1: create object of FIS.
step 2: create object of OIS and connect it to FIS.
step 3: call ReadObject() on object of ObjectInputStream.
NOTE:-"The ReadObject() returns the object of specific class in form of OBJECT Type, so it is mandatory to downcast the type as per use requirement."
NOTE:-"If we apply Transient on a variable it will not save into the file during serialization. During de-serialization it's default value will be fetched."
NOTE:-"When object is created during de-serialization the constructor never evokes because the object is created by using an internal process known as Deep Cloning. (Byte by Byte assembly)"
NOTE:-"If super class is not serializable than during de-serialization the constructor of super class will be evoked and data members will be reset to their previous state."
*/
//Let's create a program, First we need a class whose object we can use.
import java.io.*;
import java.util.*;
class Person implements Serializable
{
// data members are private, methods are public, if class would also public, it would be a perfect example of Encapsulation.
private String name;
private String city;
transient int age; // if we want any data unaffected by serialization process, make it transient.
public Person()
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter name:");
name=sc.next();
System.out.print("Enter age:");
age=sc.nextInt();
System.out.print("Enter city:");
city=sc.next();
}
public void show()
{
System.out.println("Name-"+name+"\nAge-"+age+"\nCity-"+city);
}
}
// now we create a class in which we will write our serialization and de-serialization code.
public class SerialiZation
{
//let's create an individual methods to perform serialization
void Out()
throws Throwable
{
FileOutputStream fos= new FileOutputStream("object_holder.txt");
ObjectOutputStream oos= new ObjectOutputStream(fos);
Person p1=new Person();
oos.writeObject(p1);
System.out.println("Object saved..!!");
oos.close();
fos.close();
}
// one more method, this will perform de-serialization
void In()
throws Throwable
{
FileInputStream fis= new FileInputStream("object_holder.txt");
ObjectInputStream ois= new ObjectInputStream(fis);
Person p= (Person)ois.readObject();
p.show();
fis.close();
ois.close();
}
//now we create main() in which we will instantiate serialization class then call out() and in()
public static void main(String [] adb)
throws Throwable
{
SerialiZation s= new SerialiZation();
s.Out();
s.In();
}
}
good udit
DeleteSir,
ReplyDeletewhat is the significance of using final to non primitive variables?
Hello aayush.
DeleteSorry for delay,
If we apply final on non primitives data types, the address of object that they contains can't be changed. It means the object pointed by them is fixed.
we can use the system.exit() method to terminate the JVM
ReplyDeleteGo through this example:
import java.lang.*;
public class SystemDemo {
public static void main(String[] args) {
int arr1[] = { 0, 1, 2, 3, 4, 5 };
int arr2[] = { 0, 10, 20, 30, 40, 50 };
int i;
// copies an array from the specified source array
System.arraycopy(arr1, 0, arr2, 0, 1);
System.out.print("array2 = ");
System.out.print(arr2[0] + " ");
System.out.print(arr2[1] + " ");
System.out.println(arr2[2] + " ");
for(i = 0;i < 3;i++) {
if(arr2[i] > = 20) {
System.out.println("exit...");
System.exit(0);
}
else {
System.out.println("arr2["+i+"] = " + arr2[i]);
}
}
}
}
ReplyDeleteThank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
mbe sas reseller services
compliance and cybersecurity governance
compliance governance north america
Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
ReplyDeletembe microsoft partner
mbe sas reseller services
I ALWAYS THANKFUL TO YOU.
ReplyDeleteJAVA COURSE IN GURGAON