Methods Java Assignment
Methods Java Assignment
: 04
Date : 27/08/2020
Aim:
To write a program to implement Nested classes.
Source Code:
import java.util.*;
class College
{
String name,address,principal;
int year;
College()
{
name="PSG";
address="Peelamedu, coimbatore";
principal="Mohan raja";
}
class Department
{
String name, Hod;
int estyear, Noffaculties;
Department()
{
name="Information Technology(IT)";
Hod="Naveen";
estyear=1968;
Noffaculties=20;
}
void showDept()
{
System.out.println("Department name:"+name+"Hod:"+Hod+"
established year: "+estyear+" Number of faculties: "+Noffaculties);
}
}
void createdep()
{
Department in = new Department();
in.showDept();
}
void showCollege()
{
System.out.println("college name: "+name+" Address:
"+address+" Principal and Established year: "+principal+" &"+year);
}
}
public class Campus
{
public static void main(String args[])
Result:
Thus to write a program to implement Nested class was successfully executed.
PROGRAM 2:
Aim:
To write a program to set and get Employee details and calculate Annual Salary.
Source Code:
import java.util.*;
class Employee
{
String firstname,lastname;
double month_sal;
Employee()
{
firstname="None";
lastname="None";
month_sal=0.0;
}
Employee(String fname,String lname,double sal)
{
firstname=fname;
lastname=lname;
month_sal=sal;
}
Employee(Employee ob)
{
firstname=ob.firstname;
lastname=ob.lastname;
month_sal=ob.month_sal;
Output:
Result:
Thus to write a program to set and get Employee details and calculate Annual Salary was
successfully executed.
.PROGRAM 3:
Aim:
To write a program to implement Method overloading to calculate area of square.
Source Code:
import java.util.*;
class Square
{
static int callSquare(int no)
{
return no*no;
}
static float callSquare(float no)
{
return no*no;
}
static double callSquare(double no)
{
return no*no;
Output:
Result:
Thus to write a program to implement Method overloading to calculate area of square
was successfully executed.
PROGRAM 4:
Aim:
To write a program to implement static variables and methods.
Source Code:
import java.util.*;
class Account
{
double accno;
double amount;
static float intrest_rate;
void setacc(double acc)
{
accno=acc;
}
void setamt(double amt)
{
amount=amt;
}
static {
intrest_rate=(float) 0.3;
}
}
public class Testto {
public static void main(String[] args) {
Account a=new Account();
Account a2=new Account();
a.setacc(123456789);
a.setamt(20000);
a2.setacc(14567888);
a2.setamt(90000);
System.out.println("Account no:"+a.accno+"Intrest
Amount:"+a.computeInterest());
System.out.println("Account no:"+a2.accno+"Intrest
Amount:"+a2.computeInterest());
Account.upIntrest((float) 0.19);
System.out.println("After Intrest update to 0.19");
System.out.println("Account no:"+a.accno+"Intrest
Amount:"+a.computeInterest());
System.out.println("Account no:"+a2.accno+"Intrest
Amount:"+a2.computeInterest());
}
}
Output:
Result:
Thus to write a program to implement static variables and methods.was successfully
executed.
PROGRAM 5:
Aim:
Output:
Result:
To write a program to implement inheritance..was successfully executed.