31 Jul 2016

Java Program using Two different Classes in Two Different File

TestEmployee1.java

public class TestEmployee1 {
public static void main(String[] args) {
Employee1 alex = new Employee1(); //creating 3 objects "alex, linda and john" of the class Employee1
Employee1 linda= new Employee1();
Employee1 john= new Employee1();

alex.salary = 10000; // assigning salary to the object "alex"
alex.bonus = 2000;

linda.salary = 12000;
linda.bonus =1000;

john.salary = 8000;
john.bonus = 3000;

alex.calculateTotalPay(); //here calculateTotalPay() is a method defined in Employee1 class
linda.calculateTotalPay(); // we r invoking that method
john.calculateTotalPay(); //will print total pay of john

}
}

Employee1.java

public class Employee1 {
double salary;
double bonus;

void calculateTotalPay() {
double totalPay = salary + bonus;
System.out.println("Total Pay = " +totalPay); //concatenation used to get totalPay
}
}

Output:
C:\Program Files\Java\jdk1.8.065\bin>javac Employee1.java
C:\Program Files\Java\jdk1.8.065\bin>javac TestEmployee1.java
C:\Program Files\Java\jdk1.8.065\bin>java TestEmployee1

Total Pay = 12000.0
Total Pay = 13000.0
Total Pay = 11000.0

0 comments:

Post a Comment

Comment Here