Labels

Saturday, February 6, 2016

A Java program to use multiple methods in a class;

A Java program to use multiple methods in a class;

import java.util.Scanner;

class Teacher{
    String n, a, s, h;
    long p;
    double ms, it;
    void accept()
    {
        Scanner i=new Scanner(System.in);
        System.out.print("Enter name of the teacher: ");
        n= i.nextLine();
        System.out.print("Enter address of the teacher: ");
        a= i.nextLine();
        System.out.print("Enter phone number of the teacher: ");
        p= i.nextLong();
        System.out.print("Enter highest qualification of the teacher: ");
        h= i.nextLine();
        System.out.print("Enter subject specialization of the teacher: ");
        s= i.nextLine();
        System.out.print("Enter monthly salary of the teacher: ");
        ms= i.nextDouble();
    }
    void display()
    {
        System.out.println("Detail of teacher:-\nName: "+n+"\nAddress: "+a+
                        "\nPhone Number: "+p+"\nHighest Qualification: "+h+
                        "\nSubject Specialization: "+s+"\nMonthly Salary: "+ms);
    }
    double isalary()
    {
        int year;
        Scanner i= new Scanner(System.in);
        System.out.print("Enter the years of service: ");
        year= i.nextInt();
        if (year>1)
        {
            ms= ms+ (year*5000);
        }
        return ms;
    }
    double annual()
    {
        double ann;
        ann= ms*12;
        if(ann<=150000)
            it=0;
        else
        {
            it= (5*ann)/100;
        }
        return it;
    }
}
public class multi {
    public static void main(String[] args){
        Teacher t=new Teacher();
        t.accept();
        t.display();
        System.out.println("Monthly Salary of teacher: "+t.isalary());
        System.out.println("Income tax of the teacher: "+t.annual());
    }
}

To run the above code follow following steps:-

  • Save the above code with file name multi and extension .java
  • Open terminal or cmd and move to location of file using cd command.
  • To compile type: javac multi.java
  • To run type: java multi

No comments:

Post a Comment