0% found this document useful (0 votes)
14 views7 pages

Java Programs for OOP Concepts

The document contains 5 programs demonstrating different Java concepts: 1. Implementing nested classes to create a College class with a Department inner class. 2. Setting and getting employee details and calculating annual salary using classes. 3. Overloading methods to calculate the area of a square for different data types. 4. Using static variables and methods in an Account class to calculate interest rates. 5. Implementing inheritance by creating subclasses Dog and Cat from a base Animal class.

Uploaded by

naveen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Java Programs for OOP Concepts

The document contains 5 programs demonstrating different Java concepts: 1. Implementing nested classes to create a College class with a Department inner class. 2. Setting and getting employee details and calculating annual salary using classes. 3. Overloading methods to calculate the area of a square for different data types. 4. Using static variables and methods in an Account class to calculate interest rates. 5. Implementing inheritance by creating subclasses Dog and Cat from a base Animal class.

Uploaded by

naveen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link].

: 04
Date : 27/08/2020

Constructor,Methods ,Static variables


and Methods
PROGRAM 1:

Aim:
To write a program to implement Nested classes.
Source Code:
import [Link].*;
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()
{
[Link]("Department name:"+name+"Hod:"+Hod+"
established year: "+estyear+" Number of faculties: "+Noffaculties);
}
}
void createdep()
{
Department in = new Department();
[Link]();
}
void showCollege()
{
[Link]("college name: "+name+" Address:
"+address+" Principal and Established year: "+principal+" &"+year);
}
}
public class Campus
{
public static void main(String args[])

19I433 [Link] Page 1


[Link]. : 04
Date : 27/08/2020
{
College ob=new College();
[Link]();
[Link]();
}
}
Output:

While calling outer class from inner class Error is shown:

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 [Link].*;
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=[Link];
lastname=[Link];
month_sal=ob.month_sal;

19I433 [Link] Page 2


[Link]. : 04
Date : 27/08/2020
}
void setfname(String fname)
{
firstname=fname;
}
void setLname(String Lname)
{
lastname=Lname;
}
void setSalary(double sal){
if(sal<0)
{
month_sal=0.0;
}
else
month_sal =sal;
}
String getfname()
{
return firstname;
}
String getLname()
{
return lastname;
}
double getSalary(){
return month_sal;
}
double raiseSal()
{
return ((month_sal*12)/100.0)*10.0;
}
void compareSalary(Employee e1,Employee e2)
{
if(e1.month_sal>e2.month_sal)
[Link]("Employee:"+[Link]+" salary is greater
than Employee:"+[Link]);
else if(e1.month_sal<e2.month_sal)
[Link]("Employee:"+[Link]+" salary is Lesser
than Employee:"+[Link]);
else
[Link]("Employee:"+[Link]+" salary is equal
to Employee:"+[Link]);
}
}

public class Employeecapabilities {


public static void main(String[] args) {
Employee e1=new Employee("Naveen","G",10000);
Employee e2=new Employee();
Employee e3=new Employee(e1);
[Link]("Kumar");
[Link]("J");
[Link](90000);
[Link]("First Name: \t Last Name: \t MonthlySalary \t
yearlysalary");

19I433 [Link] Page 3


[Link]. : 04
Date : 27/08/2020
[Link]([Link]()+"\t"+[Link]()+"\t"+[Link]()
+"\t"+([Link]()*12)+"\t");
[Link]([Link]()+"\t"+[Link]()+"\t"+[Link]()
+"\t"+([Link]()*12)+"\t");
[Link]([Link]()+"\t"+[Link]()+"\t"+[Link]()
+"\t"+([Link]()*12)+"\t");
[Link](e1,e2);
[Link]("After 10 % salary raise");
[Link]([Link]()+"\t"+[Link]()
+"\t"+[Link]());
[Link]([Link]()+"\t"+[Link]()
+"\t"+[Link]());
[Link]([Link]()+"\t"+[Link]()
+"\t"+[Link]());
}

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 [Link].*;
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;

19I433 [Link] Page 4


[Link]. : 04
Date : 27/08/2020
}
}
public class Test {
public static void main(String[] args) {

Square n=new Square();


[Link]("Integer method:"+[Link](10));
[Link]("Float method:"+[Link](9.00));
[Link]("Double method:"+[Link](11.25555));
}
}

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 [Link].*;
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;
}

19I433 [Link] Page 5


[Link]. : 04
Date : 27/08/2020
static void upIntrest(float rate)
{
intrest_rate=rate;
}
double computeInterest()
{
return (amount/100.0)*intrest_rate;
}

}
public class Testto {
public static void main(String[] args) {
Account a=new Account();
Account a2=new Account();
[Link](123456789);
[Link](20000);
[Link](14567888);
[Link](90000);
[Link]("Account no:"+[Link]+"Intrest
Amount:"+[Link]());
[Link]("Account no:"+[Link]+"Intrest
Amount:"+[Link]());
[Link]((float) 0.19);
[Link]("After Intrest update to 0.19");
[Link]("Account no:"+[Link]+"Intrest
Amount:"+[Link]());
[Link]("Account no:"+[Link]+"Intrest
Amount:"+[Link]());
}
}
Output:

Result:
Thus to write a program to implement static variables and [Link] successfully
executed.
PROGRAM 5:

Aim:

19I433 [Link] Page 6


[Link]. : 04
Date : 27/08/2020
To write a program to implement inheritance.
Source Code:
import [Link].*;
class Animal
{
String name;
void run()
{
[Link]("Animal Running");
}
}
class Dog extends Animal
{
String color;
void bark()
{
[Link]("Bow Bow");
}
}
class Cat extends Animal
{
String pattern;
void meow()
{
[Link]("Meow Meow");
}
}
public class Tester {
public static void main(String[] args) {
Dog d=new Dog();
Cat a=new Cat();
[Link]();
[Link]();
[Link]();
[Link]();
}
}

Output:

Result:
To write a program to implement inheritance..was successfully executed.

19I433 [Link] Page 7

You might also like