Sie sind auf Seite 1von 3

MENU DRIVEN PROGRAM IN JAVA SIMPLE CALCULATOR

import java.io.*;
public class Menu
{
    private int a;  //Instance Variable
    private int b;
    private int c;
    private static InputStreamReader in=new InputStreamReader(System.in);
    private static BufferedReader br=new BufferedReader(in);
    void menu()
    {
        //System.out.println("\f");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.println("5. Exit");
        System.out.println("Press your choice.........>");
    }

    void sum(int a,int b)


    {
        this.a=a;
        this.b=b;
        this.c=this.a+this.b;
        System.out.println("Sum="+this.c);
        // this.a=Integer.parseInt(br.readLine());
    }

    void sub(int x,int y)


    {
        a=x;
        b=y;
        c=a-b;
        System.out.println("Subtraction="+c);
    }

    void mult(int x,int y)


    {
        a=x;
        b=y;
        c=a*b;
        System.out.println("Multplication="+c);
    }

    void div(int x,int y)


    {
        a=x;
        b=y;
        c=a/b;
        System.out.println("Division="+c);
    }
    public static void main(String args[])throws IOException
    {
        Menu ob=new Menu();

        int ch,a,b;


        ch=0;
        do
        {
            ob.menu();
            ch=Integer.parseInt(br.readLine());
            switch(ch)
            {
                case 1:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.sum(a,b);
                break;
                case 2:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.sub(a,b);
                break;
                case 3:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.mult(a,b);
                break;
                case 4:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.div(a,b);
                break;
                case 5:
                System.exit(0);
                default:
                System.out.println("Wrong choice:");
            }
        }while(ch!=5);
    }       
}

Sample Output
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Press your choice.........>
1
Enter two number
2
5
Sum=7
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Press your choice.........>

Das könnte Ihnen auch gefallen