Coding Practice
Showing posts with label Java Exercise. Show all posts
Showing posts with label Java Exercise. Show all posts

Write a program in Java to perform the followings - Java Teacher Example | Java Method Overloading

  1. Create a class Teacher with three private members Name, ID and Salary.
  2. Design three constructors of the Teacher class to initialize an object with default values, three different values and the values of another object of the same class.
  3. Design a display method to display the information of the calling object.
  4. Create a TeacherExample class and define a main method inside it. Now create three objects of the Teacher class inside the main method with three types of initialization.
  5. Display the information of the three objects of Teacher class
Write a program in Java to perform the followings
Sample Output
-----------------------------
 Teacher Name : Mohammad Istiaq
 Teacher ID   : 1002401
 Teacher Salary: 87500.0 Taka
-----------------------------
 
-----------------------------
 Teacher Name : Mohammad Arman
 Teacher ID   : 1002402
 Teacher Salary: 85500.0 Taka
-----------------------------
 
-----------------------------
 Teacher Name : Mohammad Istiaq
 Teacher ID   : 1002401
 Teacher Salary: 87500.0 Taka
-----------------------------
Source Code
package teacherexample;
 
class Teacher {
 
    private String Name;
    private long ID;
    private double Salary;
 
    Teacher() {
        Name = "Mohammad Istiaq";
        ID = 1002401;
        Salary = 87500;
    }
 
    Teacher(String name, long id, double salary) {
        Name = name;
        ID = id;
        Salary = salary;
    }
 
    Teacher(Teacher info) {
        Name = info.Name;
        ID = info.ID;
        Salary = info.Salary;
 
    }
 
    void display() {
        System.out.println("-----------------------------");
        System.out.println(" Teacher Name : " + Name);
        System.out.println(" Teacher ID   : " + ID);
        System.out.println(" Teacher Salary: " + Salary + " Taka");
        System.out.println("-----------------------------\n");
    }
}
 
public class TeacherExample {
 
    public static void main(String[] args) {
 
        Teacher info = new Teacher();
        info.display();
        Teacher info2 = new Teacher("Mohammad Arman", 1002402, 85500);
        info2.display();
        Teacher info3 = new Teacher(info);
        info3.display();
    }
}
Sample Output
-----------------------------
 Teacher Name : Mohammad Istiaq
 Teacher ID   : 1002401
 Teacher Salary: 87500.0 Taka
-----------------------------
 
-----------------------------
 Teacher Name : Mohammad Arman
 Teacher ID   : 1002402
 Teacher Salary: 85500.0 Taka
-----------------------------
 
-----------------------------
 Teacher Name : Mohammad Istiaq
 Teacher ID   : 1002401
 Teacher Salary: 87500.0 Taka
-----------------------------
Change Theme
X