Udemy coupon 100 off 2017 ::Udemy coupon code free :: Get 100+ Free Udemy coupons Here

Java Basic Programs Introduction to class,object,methods,constructors

Introduction to class,object,methods,constructors.

Students are advised to go through all 5 basic examples of this blog for better understanding of class, object,   methods,constructors.


Description :
Class defines a new data type. 
Once defined, this new type can be used to create objects of that type.

=========================================



In Program 1 we have class Student.Which has variables mark1,mark2,mark3,StuName.
Other Class StudentDemo to use variables of student class.


Program 1:

class Student
{
int mark1;
int mark2;
int mark3;
String StuName;
}


// The class StudentDemo declares objects of type Student.
class StudentDemo 
{

public static void main(String args[]) 
{
Student stu1 = new Student(); // object stu1 created
Student stu2 = new Student(); // object stu2 created

double percentage;

// assign values to stu1's instance variables
stu1.StuName="chandani";
stu1. mark1 = 60;
stu1. mark2 = 70;
stu1. mark3 = 80;

// assign values to stu2's instance variables
stu2.StuName="ml";
stu2. mark1 = 71;
stu2. mark2 = 84;
stu2. mark3 = 95;

System.out.println();
// compute % of first student and display results
percentage = (stu1.mark1 + stu1.mark2 + stu1.mark3)/3;
System.out.println(stu1.StuName +"  got  " + percentage +"  % ");

System.out.println();
// compute % of second student and display results
percentage = (stu2.mark1 + stu2.mark2 + stu2.mark3)/3;
System.out.println(stu2.StuName +"  got  " + percentage +"  % ");

}
}
  • Save the file with name StudentDemo.java 
  • Complie with: javac StudentDemo.java
  • Run with: java StudentDemo

Out Put is as following :

========================================================================

Program 2 is another version of Program 1.
One Method is introduced to display result.Method does not have any argument and void return type.

Program 2:

class Student
{
int mark1;
int mark2;
int mark3;
String StuName;
double percentage;

// compute % of student and display results
void displayResult()
{
System.out.println();
percentage = (mark1 + mark2 + mark3)/3;
System.out.println(StuName +"  got  " + percentage +"  % ");
}

}

// The class StudentDemo declares objects of type Student.
class StudentDemo 
{
public static void main(String args[]) 
{
Student stu1 = new Student();
Student stu2 = new Student();

// assign values to stu1's instance variables
stu1.StuName="chandani";
stu1. mark1 = 60;
stu1. mark2 = 70;
stu1. mark3 = 80;

// assign values to stu2's instance variables
stu2.StuName="ml";
stu2. mark1 = 71;
stu2. mark2 = 84;
stu2. mark3 = 95;

stu1.displayResult();
stu2.displayResult();

}
}
  • Save the file with name StudentDemo.java 
  • Complie with: javac StudentDemo.java
  • Run with: java StudentDemo

Out Put is as following :





========================================================================

Program 3 is another version of Program 1.
One Method is introduced to count result.Method does not have any argument but it returns double value To calling method(here main method).

Program 3:

class Student
{
int mark1;
int mark2;
int mark3;
String StuName;
double percentage;

// compute % of student and return results
double countResult()
{
percentage = (mark1 + mark2 + mark3)/3;
return percentage;
}

}

// This StudentDemo declares objects of type Student.
class StudentDemo 
{
public static void main(String args[]) 
{
Student stu1 = new Student();
Student stu2 = new Student();
double percentage;

// assign values to stu1's instance variables
stu1.StuName="chandani";
stu1. mark1 = 60;
stu1. mark2 = 70;
stu1. mark3 = 80;

// assign values to stu2's instance variables
stu2.StuName="ml";
stu2. mark1 = 71;
stu2. mark2 = 84;
stu2. mark3 = 95;

percentage=stu1.countResult();
System.out.println();
System.out.println(stu1.StuName +"  got  " + percentage +"  %");

percentage=stu2.countResult();
System.out.println();
System.out.println(stu2.StuName +"  got  " + percentage +"  % ");

}
}
  • Save the file with name StudentDemo.java 
  • Complie with: javac StudentDemo.java
  • Run with: java StudentDemo

Out Put is as following :





========================================================================

Program 4 is another version of Program 1.
One Method is introduced set variables.Method has arguments but it does not return any thing.

Program 4:

class Student
{
int mark1;
int mark2;
int mark3;
String StuName;
double percentage;

void setMarks(String name,int m1,int m2,int m3)
{
StuName=name;
mark1=m1;
mark2=m2;
mark3=m3;
}

// compute % of student and return results
double countResult()
{
percentage = (mark1 + mark2 + mark3)/3;
return percentage;
}

}

// This StudentDemo declares objects of type Student.
class StudentDemo 
{

public static void main(String args[]) 
{
Student stu1 = new Student();
Student stu2 = new Student();
double percentage;

// assign values to stu1's instance variables
stu1.setMarks("chandani",60,70,80);

// assign values to stu2's instance variables
stu2.setMarks("ml",71,84,95);

percentage=stu1.countResult();
System.out.println();
System.out.println(stu1.StuName +"  got  " + percentage +"  %");

percentage=stu2.countResult();
System.out.println();
System.out.println(stu2.StuName +"  got  " + percentage +"  % ");

}
}
  • Save the file with name StudentDemo.java 
  • Complie with: javac StudentDemo.java
  • Run with: java StudentDemo

Out Put is as following :





========================================================================

Program 5 is another version of Program 1.
Constructor is introduced to set variables.
Constructor is over loaded.One is having four arguments and other with out any argument.
On creation of 1st object, Stu1 Constructor with argument is called.
On creation of 2nd object Stu2, Constructor with out argument is called.

Program 5:

class Student
{
int mark1;
int mark2;
int mark3;
String StuName;
double percentage;

Student()
{
StuName="ml";
mark1=71;
mark2=84;
mark3=95;
}
Student(String name,int m1,int m2,int m3)
{
StuName=name;
mark1=m1;
mark2=m2;
mark3=m3;
}

// compute % of student and return results
double countResult()
{
percentage = (mark1 + mark2 + mark3)/3;
return percentage;
}

}

// This StudentDemo declares objects of type Student.
class StudentDemo 
{
public static void main(String args[]) 
{
Student stu1 = new Student("chandani",60,70,80);
Student stu2 = new Student();
double percentage;

percentage=stu1.countResult();
System.out.println();
System.out.println(stu1.StuName +"  got  " + percentage +"  %");

percentage=stu2.countResult();
System.out.println();
System.out.println(stu2.StuName +"  got  " + percentage +"  % ");
}
}
  • Save the file with name StudentDemo.java 
  • Complie with: javac StudentDemo.java
  • Run with: java StudentDemo

Out Put is as following :





========================================================================

No comments:

Post a Comment