Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 1 decade ago

can anyone send me java code for mileage calculator using swing,arrays,iteration,objects,classes,methods,garba

The ABC corporations has set out different rates for different vehicles.1-(vehicle engine size(cc))0-1000cc,1001-1500,1501-2000,2001=<.2-(Mileage rate-cents per kilometer) 38c\km,40c\km,45c\km,50c\km.

Also in the code please insert an image for the calculator.

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    import java.awt.*;

    import java.applet.*;

    import java.awt.event.*;

    public class Calculator extends Applet implements ActionListener{

    char arr2[]=new char[5];

    int i=0,j=0,flag=0;

    int temp1=0,temp2=0,temp3=0;

    private TextField txt1=new TextField();

    private Button btn1=new Button("1");

    private Button btn2=new Button("2");

    private Button btn3=new Button("3");

    private Button btn4=new Button("4");

    private Button btn5=new Button("5");

    private Button btn6=new Button("6");

    private Button btn7=new Button("7");

    private Button btn8=new Button("8");

    private Button btn9=new Button("9");

    private Button btn0=new Button("0");

    private Button btn11=new Button("+");

    private Button btn12=new Button("-");

    private Button btn13=new Button("*");

    private Button btn14=new Button("/");

    private Button btn15=new Button("=");

    public void init(){

    setLayout(null);

    }

    public void paint(Graphics g){

    //Color clr=new Color(Color.BLACK);

    g.setColor(Color.black);

    g.fillRect(50,50,180,300);

    txt1.setBounds(60,60,160,30);

    btn1.setBounds(60,110,30,30);

    btn2.setBounds(120,110,30,30);

    btn3.setBounds(180,110,30,30);

    btn4.setBounds(60,160,30,30);

    btn5.setBounds(120,160,30,30);

    btn6.setBounds(180,160,30,30);

    btn7.setBounds(60,210,30,30);

    btn8.setBounds(120,210,30,30);

    btn9.setBounds(180,210,30,30);

    btn0.setBounds(60,260,30,30);

    btn11.setBounds(120,260,30,30);

    btn12.setBounds(180,260,30,30);

    btn13.setBounds(60,310,30,30);

    btn14.setBounds(120,310,30,30);

    btn15.setBounds(180,310,30,30);

    add(txt1);

    add(btn1);

    add(btn2);

    add(btn3);

    add(btn4);

    add(btn5);

    add(btn6);

    add(btn7);

    add(btn8);

    add(btn9);

    add(btn0);

    add(btn11);

    add(btn12);

    add(btn13);

    add(btn14);

    add(btn15);

    btn1.addActionListener(this);

    btn2.addActionListener(this);

    btn3.addActionListener(this);

    btn4.addActionListener(this);

    btn5.addActionListener(this);

    btn6.addActionListener(this);

    btn7.addActionListener(this);

    btn8.addActionListener(this);

    btn9.addActionListener(this);

    btn0.addActionListener(this);

    btn11.addActionListener(this);

    btn12.addActionListener(this);

    btn13.addActionListener(this);

    btn14.addActionListener(this);

    btn15.addActionListener(this);

    }

    public void actionPerformed(ActionEvent ae){

    String str1=ae.getActionCommand();

    if("1".equals(str1))

    txt1.setText("1");

    else if("2".equals(str1))

    txt1.setText("2");

    else if("2".equals(str1))

    txt1.setText("2");

    else if("3".equals(str1))

    txt1.setText("3");

    else if("4".equals(str1))

    txt1.setText("4");

    else if("5".equals(str1))

    txt1.setText("5");

    else if("6".equals(str1))

    txt1.setText("6");

    The above code is only frontend designed of calculator U need to do more work to enhance backend. If U further need any types of java code. just contact with me

    akmaalik03@yahoo.com

Still have questions? Get your answers by asking now.