0% found this document useful (0 votes)
64 views

Methods Java Assignment

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)
64 views

Methods Java Assignment

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
You are on page 1/ 7

Ex.No.

: 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 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[])

19I433 G.NAVEEN Page 1


Ex.No. : 04
Date : 27/08/2020
{
College ob=new College();
ob.showCollege();
ob.createdep();
}
}
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 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;

19I433 G.NAVEEN Page 2


Ex.No. : 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)
System.out.println("Employee:"+e1.firstname+" salary is greater
than Employee:"+e2.firstname);
else if(e1.month_sal<e2.month_sal)
System.out.println("Employee:"+e1.firstname+" salary is Lesser
than Employee:"+e2.firstname);
else
System.out.println("Employee:"+e1.firstname+" salary is equal
to Employee:"+e2.firstname);
}
}

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);
e2.setfname("Kumar");
e2.setLname("J");
e2.setSalary(90000);
System.out.println("First Name: \t Last Name: \t MonthlySalary \t
yearlysalary");

19I433 G.NAVEEN Page 3


Ex.No. : 04
Date : 27/08/2020
System.out.println(e1.getfname()+"\t"+e1.getLname()+"\t"+e1.getSalary()
+"\t"+(e1.getSalary()*12)+"\t");
System.out.println(e2.getfname()+"\t"+e2.getLname()+"\t"+e2.getSalary()
+"\t"+(e2.getSalary()*12)+"\t");
System.out.println(e3.getfname()+"\t"+e3.getLname()+"\t"+e3.getSalary()
+"\t"+(e3.getSalary()*12)+"\t");
e1.compareSalary(e1,e2);
System.out.println("After 10 % salary raise");
System.out.println(e1.getfname()+"\t"+e1.getLname()
+"\t"+e1.raiseSal());
System.out.println(e2.getfname()+"\t"+e2.getLname()
+"\t"+e2.raiseSal());
System.out.println(e3.getfname()+"\t"+e3.getLname()
+"\t"+e3.raiseSal());
}

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;

19I433 G.NAVEEN Page 4


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

Square n=new Square();


System.out.println("Integer method:"+n.callSquare(10));
System.out.println("Float method:"+n.callSquare(9.00));
System.out.println("Double method:"+n.callSquare(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 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;
}

19I433 G.NAVEEN Page 5


Ex.No. : 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();
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:

19I433 G.NAVEEN Page 6


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

Output:

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

19I433 G.NAVEEN Page 7

You might also like