Monthly Archives: June 2013

Java Packages – Tutorial 4

1. Expalin Package and what is need of package.

2. Creating Package.

/*

Code:

*/

package com.vinod;

public class Student {

private int id;
private String name;

public Student()
{
id=0;
name=null;
}
public Student(int tid,String tname)
{
id=tid;
name=tname;
}

public void setData()
{
id=0;
name=null;
}

public void setData(int tid,String tname)
{
id=tid;
name=tname;
}

public void display()
{
System.out.println(“\n ID is:”+id);
System.out.println(“\n Name is:”+name);
}
}

package com.second;

public class Employee {

private int id;
private String name;

public void setData(int tid, String tname) {
id = tid;
name = tname;
}

public void display() {
System.out.println(“\nEmployee ID is:” + id);
System.out.println(“\nEmpoyee Name is:” + name);
}
}

package com.vinod;

import com.second.*;

public class MainClass {

public static void main(String[] args) {

Student stu1=new Student();
stu1.setData(1,”Vinod Pillai”);
stu1.display();

Student stu2=new Student(2,”Abc User”); //Calling Constructor with argument.
stu2.display();

Employee emp1=new Employee();
emp1.setData(101, “Ramesh”);
emp1.display();

com.second.Employee emp2=new com.second.Employee();
emp2.setData(102, “Xyz”);
emp2.display();
}
}

http://www.youtube.com/vinodthebest

1 Comment

Filed under General

Java Constructor, Method and Static variable – Tutorial 3

1. Method Overloading.

2. Constructors.

3. Constructors Overloading.

4. Static variable and Methods.

/*

Code:

*/

public class Student {

private int id;
private String name;

private static int count;

//Constructor Overloading
public Student()
{
//id=0;
name=null;

count++;
}
public Student(int tid,String tname)
{
id=tid;
name=tname;

count++;
}

//Method Overloading
public void setData()
{
id=0;
name=null;
}

public void setData(int tid,String tname)
{
id=tid;
name=tname;
}

public void display()
{
System.out.println(“\n ID is:”+id);
System.out.println(“\n Name is:”+name);
}

//Static Method.
public static int getCount()
{
//System.out.println(“\n ID is:”+id); //Error can’t access not static Variable/Method
return count;
}
}

public class MainClass {

public static void main(String[] args) {

Student stu1=new Student();        //Calling the user defined default constructor.
stu1.setData(1,”Vinod Pillai”);

Student stu2=new Student(2,”Abc User”); //Calling Constructor with argument.
stu2.display();

int rel=Student.getCount(); //Accessing the Static method.
System.out.println(“\nThe Static result is:”+rel);
rel=0;

rel=stu2.getCount();    //Its not the correct way but this is allowed.
System.out.println(“\nThe New Static result is:”+rel);

Student stu3=new Student();
stu3.display();
}
}

 

 

http://www.youtube.com/vinodthebest

3 Comments

Filed under General

Java Classes and Objects – Tutorial 2

1. Creating Objects.

2. Creating Class in the same File.

3. Default value of the Class instance variable and Local variable.

Code:

/*

Student Class

*/

public class Student {

private int id;
private String name;

public void setData(int tid,String tname)
{
id=tid;
name=tname;
}

public void display()
{
int testing=0;

System.out.println(“\n ID is:”+id);
System.out.println(“\n Name is:”+name);

System.out.println(testing);     //Not allowed without initialization
}
}

/*

Employee Class

*/
class Employee {

private int id;
private String name;

public void setData(int tid,String tname)
{
id=tid;
name=tname;
}

public void display()
{
System.out.println(“\nEmployee ID is:”+id);
System.out.println(“\nEmpoyee Name is:”+name);
}
}

/*

MainClass Class

*/
public class MainClass {

public static void main(String args[])
{
//Student Class
Student stuobj1=new Student(); //[ID/Name]

stuobj1.setData(1, “Vinod Pillai”);
stuobj1.display();

Student stuobj2;    //Reference created.

stuobj2=new Student(); //Memory is allocated now. [ID/Name]
stuobj2.display();        //Default values.
stuobj2.setData(2, “Vinod”);
stuobj2.display();

//Employee Class
Employee eobj=new Employee();
eobj.setData(1, “AbcXyz”);
eobj.display();
}
}

http://www.youtube.com/vinodthebest

Leave a comment

Filed under Java

Java Hello World – Tutorial 1

1. Java is an Object Oriented Programming language.

2. Importance of Class.

3. Creating simple “Hello World Application”.

4. Running the program.

5. Java works in two stage “Compiler and Interpreter” in Java.

Code:

public class MainClass
{
public static void main(String args[])
{
System.out.println(“\n Hello World \n”);
}
}
Prof. Vinod Pillai.
vinodthebest@gmail.com

http://www.youtube.com/vinodthebest

Leave a comment

Filed under Java