java help with codes (please)..

anirtak143

New Member
Messages
213
Reaction score
0
Points
0
hey guys. i need help here.. ^^ kindly fix this class for me.. ^^ its just a small java program, but it wont compile and i dont know why.. kindly fix it please. ^^

thanks. heres the code..

Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Project2 implements ActionListener{
    private JFrame MainFrame;
    private JTextField TF1;
    private JLabel Label1, Label2;
    private JPanel Panel1, Panel2, Panel3, Panel4, Panel5;
    private JButton AddButton, SubtractButton, MultiplyButton, DivideButton, ModuloButton,EqualsButton;
    private double D1 = 0, D2;
    private char OpDone = 'e';
    private String UserInput;

public static void main(String[] args)
{ new Project2();
}

public Project2{
    MainFrame = new JFrame("Kevin Mabul's Calculator");
    
    Panel1 = new JPanel();
    Panel2 = new JPanel();
    Panel3 = new JPanel();
    Panel4 = new JPanel();
    Panel5 = new JPanel();
    
    Label1 = new JLabel("Patience is a virtue");
    Label2 = new JLabel("Java is a versatile language");
    
    AddButton = new JButton("+");
    AddButton.addActionListener(this);
    SubtractButton = new JButton("-");
    SubtractButton.addActionListener(this);
    MultiplyButton = new JButton("x");
    MultiplyButton.addActionListener(this);
    DivideButton = new JButton("/");
    DivideButton.addActionListener(this);
    ModuloButton = new JButton("%");
    ModuloButton.addActionListener(this);
    EqualsButton = new JButton("=");
    EqualsButton.addActionListener(this);
    
    TF1 = new JTextField("0",15);
    TF1.selectAll();
    TF1.requestFocus();
    
    Panel1.setBackground(Color.YELLOW);
    Panel2.setBackground(Color.GREEN);
    Panel3.setBackground(Color.GREEN);
    Panel4.setBackground(Color.YELLOW);
    
    Panel1.add(Label1);
    Panel2.add(Label2);
    Panel3.add(TF1);
    
    Panel5.add(Panel1);
    Panel5.add(Panel2);
    Panel5.add(Panel3);
    Panel5.add(Panel4);
    
    Panel4.add(AddButton);
    Panel4.add(SubtractButton);
    Panel4.add(MultiplyButton);
    Panel4.add(DivideButton);
    Panel4.add(ModuloButton);
    Panel4.add(EqualsButton);
    
    Panel5.setLayout(new GridLayout(4,1);
    
    MainFrame.setContentPane(Panel5);
    MainFrame.pack();
    MainFrame.show();
    MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e){
    {
        UserInput = TF1.getText();
        double D2 = Double.parseDouble(UserInput);
        
        {
            if(OpDone =='a')
            D1 = D1 + D2;
            
            if(OpDone =='s')
            D1 = D1 - D2;
            
            if(OpDone =='m')
            D1 = D1 * D2;
            
            if(OpDone =='d')
            D1 = D1 / D2;
            
            if(OpDone =='r')
            D1 = D1 % D2;
            
            if(OpDone =='e')
            D1 = D2;
        }
        
        if(e.getActionCommand().equals("+"))
        OpDone = 'a';
        if(e.getActionCommand().equals("-"))
        OpDone = 's';
        if(e.getActionCommand().equals("x"))
        OpDone = 'm';
        if(e.getActionCommand().equals("/"))
        OpDone = 'd';
        if(e.getActionCommand().equals("%"))
        OpDone = 'r';
        if(e.getActionCommand().equals("="))
        OpDone = 'e';
        
    }
    UserInput = Double.toString(D1);
    TF1.setText(UserInput);
    TF1.selectAll();
    TF1.requestFocus();
}
}
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
In the nicest possible way, do your own damn homework.

And next time you have some Real Java code that doesn't compile, tell us the compile error since we don't want to have to read your whole script when it's probably just a semicolon that anyone who can read debug messages will know in a second where to look.
 

natsuki

New Member
Messages
112
Reaction score
0
Points
0
woah that IS Java and it's waay different from javascript(ECMAScript). Dunno much who knows any java here... that's not even a script it's a complete oo code for an app..
 
Last edited:

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
ever heard of

Code:
System.exit( 0 );

lol
 

ldtwo

New Member
Messages
6
Reaction score
0
Points
0
First, I recommend netbeans if you aren't using it. Second, you will get much better elp in a java forum. Third, you should state what error you are getting and what line. Use the console, output feedback to discover errors and catch errors you expect to happen and then output more data to help you track the origin of the errors.
 

Harry

New Member
Messages
3
Reaction score
0
Points
0
Error 1 : missing paranthesis
Panel5.setLayout(new GridLayout(4,1));

Error 2 : wrong constructor syntax (without paranthesis)
public Project2()

Complete code
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Project2 implements ActionListener{
    private JFrame MainFrame;
    private JTextField TF1;
    private JLabel Label1, Label2;
    private JPanel Panel1, Panel2, Panel3, Panel4, Panel5;
    private JButton AddButton, SubtractButton, MultiplyButton, DivideButton, ModuloButton,EqualsButton;
    private double D1 = 0, D2;
    private char OpDone = 'e';
    private String UserInput;

public static void main(String[] args)
{ new Project2();
}

public Project2(){
    MainFrame = new JFrame("Kevin Mabul's Calculator");
    
    Panel1 = new JPanel();
    Panel2 = new JPanel();
    Panel3 = new JPanel();
    Panel4 = new JPanel();
    Panel5 = new JPanel();
    
    Label1 = new JLabel("Patience is a virtue");
    Label2 = new JLabel("Java is a versatile language");
    
    AddButton = new JButton("+");
    AddButton.addActionListener(this);
    SubtractButton = new JButton("-");
    SubtractButton.addActionListener(this);
    MultiplyButton = new JButton("x");
    MultiplyButton.addActionListener(this);
    DivideButton = new JButton("/");
    DivideButton.addActionListener(this);
    ModuloButton = new JButton("%");
    ModuloButton.addActionListener(this);
    EqualsButton = new JButton("=");
    EqualsButton.addActionListener(this);
    
    TF1 = new JTextField("0",15);
    TF1.selectAll();
    TF1.requestFocus();
    
    Panel1.setBackground(Color.YELLOW);
    Panel2.setBackground(Color.GREEN);
    Panel3.setBackground(Color.GREEN);
    Panel4.setBackground(Color.YELLOW);
    
    Panel1.add(Label1);
    Panel2.add(Label2);
    Panel3.add(TF1);
    
    Panel5.add(Panel1);
    Panel5.add(Panel2);
    Panel5.add(Panel3);
    Panel5.add(Panel4);
    
    Panel4.add(AddButton);
    Panel4.add(SubtractButton);
    Panel4.add(MultiplyButton);
    Panel4.add(DivideButton);
    Panel4.add(ModuloButton);
    Panel4.add(EqualsButton);
    
    Panel5.setLayout(new GridLayout(4,1));
    
    MainFrame.setContentPane(Panel5);
    MainFrame.pack();
    MainFrame.show();
    MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e){
    {
        UserInput = TF1.getText();
        double D2 = Double.parseDouble(UserInput);
        
        {
            if(OpDone =='a')
            D1 = D1 + D2;
            
            if(OpDone =='s')
            D1 = D1 - D2;
            
            if(OpDone =='m')
            D1 = D1 * D2;
            
            if(OpDone =='d')
            D1 = D1 / D2;
            
            if(OpDone =='r')
            D1 = D1 % D2;
            
            if(OpDone =='e')
            D1 = D2;
        }
        
        if(e.getActionCommand().equals("+"))
        OpDone = 'a';
        if(e.getActionCommand().equals("-"))
        OpDone = 's';
        if(e.getActionCommand().equals("x"))
        OpDone = 'm';
        if(e.getActionCommand().equals("/"))
        OpDone = 'd';
        if(e.getActionCommand().equals("%"))
        OpDone = 'r';
        if(e.getActionCommand().equals("="))
        OpDone = 'e';
        
    }
    UserInput = Double.toString(D1);
    TF1.setText(UserInput);
    TF1.selectAll();
    TF1.requestFocus();
}
}
 

anirtak143

New Member
Messages
213
Reaction score
0
Points
0
woah.. thanks harry... and to everyone, sorry i wasnt able to update this with the compile time error thing.. i was about to do it but this answer came.. thanks..

Error 1 : missing paranthesis
Panel5.setLayout(new GridLayout(4,1));

Error 2 : wrong constructor syntax (without paranthesis)
public Project2()

i never knew it was this simple.. anyways, sorry for my stupidity..
 
Top