Java Programming

Topic No. 4 : Class Fundamentals

Basic Program of Class in Java
Source Code of main class
package javaapplication1;

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

    Student s1 = new Student();
       
       s1.setsno(100);
       int n = s1.getsno();
       System.out.print(n);
       s1.setsname("Deep");
       String s = s1.getsname();
       System.out.print(s);
      
   }
}               


Source Code of Student class
public class Student 
{
    private int sno;
    private  String sname;
    
    void setsno(int sno)
    {
        this.sno=sno;
    }
    int getsno()
    {
        return this.sno;
    }
    void setsname(String sname)
    {
        this.sname=sname;
    }
    String getsname()
    {
        return this.sname;
    }
    
    
}                    
Calculator Example in Java
Source Code of main class
package javaapplication1;

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

    Calc c1 = new Calc();
        c1.setvalues(20, 10);
        int res = c1.division();
        System.out.print(res);
      
   }
}               


Source Code of Clac class

public class Calc {​​​​​​​
   
    private int a,b,c;
   
    void setvalues(int a,int b)
    {​​​​​​​
        this.a=a;
        this.b=b;
    }​​​​​​​
    int add()
    {​​​​​​​
        c = a+b;
        return c;
    }​​​​​​​
    int subtract()
    {​​​​​​​
        c = a-b;
        return c;
    }​​​​​​​
    int multiply()
    {​​​​​​​
        c = a*b;
        return c;
    }​​​​​​​
    int division()
    {​​​​​​​
        c = a/b;
        return c;
    }​​​​​​​
  
   
}​​​​​​​
 
Constructor in Java
will be updated soon.
Call By Value, Call By Reference and Return By Reference
Source Code of main class
package javaapplication1;

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

int a,b;
a=10;b=20;
System.out.print("Output for Call By Value\n");
System.out.print("\nBefore Swapping");
System.out.print("\nA : "+a+"->B : "+b);
CallByValue c1 = new CallByValue(a,b);
c1.swap();
System.out.print("\nAfter Swapping");
System.out.print("\nA : "+a+"->B : "+b);
System.out.print("\n\nOutput for Call By Reference\n");
System.out.print("\nBefore Swapping");
System.out.print("\nA : "+a+"->B : "+b);
CallByReference c2 = new CallByReference(a,b);
c2.swap(c2);
System.out.print("\nAfter Swapping");
System.out.print("\nA : "+c2.a+"->B : "+c2.b);

System.out.print("\n\nOutput for Return By Reference\n");
System.out.print("\nBefore Swapping");
System.out.print("\nA : "+a+"->B : "+b);
ReturnObject c3 = new ReturnObject(a,b);
ReturnObject c4 = new ReturnObject();
c4= c3.swap(c3);
System.out.print("\nAfter Swapping");
System.out.print("\nA : "+c4.a+"->B : "+c4.b);
      
   }
}               


Source Code of Call By Value
public class CallByValue {
int a,b;
CallByValue(int a,int b)
{
this.a=a;
this.b=b;
}
void swap()
{
int t = a;
a=b;
b=t;
}

}
Source Code of Call By Reference
public class CallByReference {
int a,b;
CallByReference(int a,int b)
{
this.a=a;
this.b=b;
}
void swap(CallByReference temp)
{
int t = temp.a;
temp.a=temp.b;
temp.b=t;

}
}
Source Code of Return by Reference
public class ReturnObject 
{
int a,b;
ReturnObject()
{

}
ReturnObject(int a,int b)
{
this.a=a;
this.b=b;
}
ReturnObject swap(ReturnObject temp)
{
int t = temp.a;
temp.a=temp.b;
temp.b=t;
return temp;
}

}
Static variable
Source Code of main class
package javaapplication1;

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

        staticdemo sd1 = new staticdemo();
        sd1.set(10, 20);
        sd1.display();
        staticdemo sd2 = new staticdemo();
        sd2.set(30, 40);
        sd2.display();
        staticdemo sd3 = new staticdemo();
        sd3.set(50, 60);
        sd3.display();
      
   }
}           
Source Code of Static variable demo
public class staticdemo {
    int a,b;
    static int c=0;
    int d=0;
    
    public void set(int a,int b)
    {
        this.a=a;
        this.b=b;
      
       
    }
    public void display()
    {
        a++;
        b++;
        c++;
        d++;
        System.out.print("\nInstance Varibale A : "+a);
        System.out.print("\nInstance Varibale B : "+b);
        System.out.print("\nStatic Variable C : "+c);
        System.out.print("\nInstance Varibale D : "+d);
       
    }
   
   
}
Practice Exercise : 1
Definition
Create a Java Application which accepts following Employee Information and prints on the screen.

 

EmployeeID
EmployeeName
Designation
CityName
PhoneNo.

 

Use the concept of constuctor and command line arguments
                                
Source Code of main class
package javaapplication1;

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

    String a0 = args[0];
        String a1 = args[1];
        String a2 = args[2];
        String a3 = args[3];
        String a4 = args[4];
//        String a0 = "XXNOEWW";
//        String a1 = "DM";
//        String a2 = "AP";
//        String a3 = "Ahmedabad";
//        String a4 = "9408224007";
        Employee emp = new Employee(a0,a1,a2,a3,a4);
        emp.show();
      
   }
}           
Source Code of Employee Class
public class Employee {
    
     
    String EmployeeID,EmployeeName,Designation,CityName,PhoneNo;
    Employee(String EmployeeID,String EmployeeName,String Designation,String CityName,String PhoneNo)
    {
        this.EmployeeID = EmployeeID;
        this.EmployeeName = EmployeeName;
        this.Designation = Designation;
        this.CityName = CityName;
        this.PhoneNo = PhoneNo;
    }
    public void show()
    {
        System.out.print("\nEmployee ID : " + this.EmployeeID );
        System.out.print("\nEmployee Name : " + this.EmployeeName );
        System.out.print("\nDesignation : " + this.Designation );
        System.out.print("\nCity Name : " + this.CityName );
        System.out.print("\nPhone No. : " +  this.PhoneNo );
    }
}

Practice Exercise : 2
Definition
Create a Java Application which accepts following Student Information and prints the result on screen.

Input : 
Student ID : 200001
Student Name : Naznin
Sub1 : 60
Sub2  : 70
Sub3 : 50


Output :
Student Name : Naznin
Total : 180
Per : 60
Grade : PASS

Note : Passing criteria is : 60%
Use the concept of constuctor and command line arguments
                                
Source Code of main class
package javaapplication1;

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

    String a0 = args[0];
        String a1 = args[1];
        int a2 = Integer.parseInt(args[2]);
        int a3 = Integer.parseInt(args[3]);
        int a4 = Integer.parseInt(args[4]);
         Student s1 = new Student(a0,a1,a2,a3,a4);
         int total= s1.calculateTotal();
         float per = s1.calculatePer();
         String grade = s1.calculateGrade();
         
         System.out.print("\nStudent ID : " + s1.StudentID);
         System.out.print("\nStudent Name : " + s1.StudentName);
         System.out.print("\nTotal : " + total);
         System.out.print("\nPercentage : " + per);
         System.out.print("\nGrade : " +  grade);
      
   }
}           
Source Code of Student Class
public class Student {​​​​​​​
   
   String StudentID,StudentName,grade;
   int sub1,sub2,sub3,total;
   float per;
   Student(String StudentID,String StudentName,int sub1,int sub2,int sub3)
   {​​​​​​​
       this.StudentID = StudentID;
       this.StudentName = StudentName;
       this.sub1 = sub1;
       this.sub2 = sub2;
       this.sub3 = sub3;
   }​​​​​​​
   int calculateTotal()
   {​​​​​​​
      
       this.total = sub1+sub2+sub3;
       return this.total;
   }​​​​​​​
   float calculatePer()
   {​​​​​​​
       per = this.total/3;
       return per;
   }​​​​​​​
   String calculateGrade()
   {​​​​​​​
       if(per>60)
       {​​​​​​​
           this.grade = "PASS";
       }​​​​​​​
       else
       {​​​​​​​
           this.grade = "FAIL";
       }​​​​​​​
       return this.grade;
   }​​​​​​​
  
}​​​​​​​