JavaScript is required to use Bungie.net

OffTopic

Surf a Flood of random discussion.
12/13/2013 5:43:17 AM
2

Tips to improve Java program

I am decently new at programming (just took my 1302 final) so don't be extremely pedantic but I am looking for constructive criticism. I am glad with my product and it is not nearly finished so there will be improvements, keep in mind this only took around a few hours of work. I am also happy with it due to how short the code is. Everywhere I look calculator code examples are about to same length or slightly longer. So, tell me what you think! I would love to hear how to shorten the code. [quote] import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class Main extends JFrame{ public JButton button1 = new JButton("1"); public JButton button2 = new JButton("2"); public JButton button3 = new JButton("3"); public JButton button4 = new JButton("4"); public JButton button5 = new JButton("5"); public JButton button6 = new JButton("6"); public JButton button7 = new JButton("7"); public JButton button8 = new JButton("8"); public JButton button9 = new JButton("9"); public JButton button0 = new JButton("0"); public JButton mult = new JButton("*"); public JButton add = new JButton("+"); public JButton sub = new JButton("-"); public JButton div = new JButton("/"); public JButton equals = new JButton("="); public JButton per = new JButton("."); public JButton clear = new JButton("Clear"); public JTextField display = new JTextField(); public JTextField displaywarning = new JTextField(); JLabel frame = new JLabel(); JFrame component = new JFrame(); //public int selected; public int operation; public double first; public ActionEvent e1; Main(){ display.setSize(240, 70); display.setLocation(50, 50); display.setVisible(true); display.setBackground(Color.WHITE); display.setEditable(false); displaywarning.setSize(240,30); displaywarning.setLocation(50,10); displaywarning.setVisible(true); displaywarning.setBackground(Color.GRAY); displaywarning.setEditable(false); button1.setVisible(true); button1.setSize(60, 25); button1.setLocation(50,200); button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { //selected=1; display.setText(display.getText()+1); } }); button2.setVisible(true); button2.setSize(60, 25); button2.setLocation(110,200); button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { //selected=2; display.setText(display.getText() + 2); } }); button3.setVisible(true); button3.setSize(60, 25); button3.setLocation(170,200); button3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { display.setText(display.getText() + 3); } }); div.setVisible(true); div.setSize(60, 25); div.setLocation(230,175); div.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { first = Double.parseDouble(display.getText()); operation = 1; display.setText(""); displaywarning.setText("" + first + " / "); } }); clear.setVisible(true); clear.setSize(240, 25); clear.setLocation(50,250); clear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { display.setText(""); } }); per.setVisible(true); per.setSize(60, 25); per.setLocation(230,200); per.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { display.setText(display.getText()+ "."); } }); mult.setVisible(true); mult.setSize(60, 25); mult.setLocation(230,150); mult.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { first = Double.parseDouble(display.getText()); operation = 4; display.setText(""); displaywarning.setText("" + first + " * "); } }); equals.setVisible(true); equals.setSize(60, 25); equals.setLocation(230,225); equals.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { displaywarning.setText(displaywarning.getText() + display.getText()); if(operation == 1) { if(Integer.parseInt(display.getText())==0) { displaywarning.setText("Can't divide by zero."); first=0; display.setText(""); } else display.setText("" + (first / Double.parseDouble(display.getText()))); } if(operation == 2) display.setText("" + (first - Double.parseDouble(display.getText()))); if(operation == 3) display.setText("" + (first + Double.parseDouble(display.getText()))); if(operation == 4) display.setText("" + (first * Double.parseDouble(display.getText()))); } }); sub.setVisible(true); sub.setSize(60, 25); sub.setLocation(170,225); sub.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { first = Double.parseDouble(display.getText()); operation = 2; display.setText(""); displaywarning.setText("" + first + " - "); } }); add.setVisible(true); add.setSize(60, 25); add.setLocation(110,225); add.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { first = Double.parseDouble(display.getText()); operation = 3; display.setText(""); displaywarning.setText("" + first + " + "); } }); button4.setVisible(true); button4.setSize(60, 25); button4.setLocation(50,175); button4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { //selected=4; display.setText(display.getText() + 4); } }); button5.setVisible(true); button5.setSize(60, 25); button5.setLocation(110,175); button5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { //selected=5; display.setText(display.getText() + 5); } }); button6.setVisible(true); button6.setSize(60, 25); button6.setLocation(170,175); button6.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { //selected=6; display.setText(display.getText() + 6); } }); button7.setVisible(true); button7.setSize(60, 25); button7.setLocation(50,150); button7.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { //selected=7; display.setText(display.getText() + 7); } }); button8.setVisible(true); button8.setSize(60, 25); button8.setLocation(110,150); button8.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { //selected=8; display.setText(display.getText() + 8); } }); button9.setVisible(true); button9.setSize(60, 25); button9.setLocation(170,150); button9.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { //selected=9; display.setText(display.getText() + 9); } }); button0.setVisible(true); button0.setSize(60, 25); button0.setLocation(50,225); button0.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e1) { //selected=0; display.setText(display.getText() + 0); } }); //Main frame frame.add(display); frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.add(button6); frame.add(button7); frame.add(button8); frame.add(button9); frame.add(button0); frame.add(div); frame.add(sub); frame.add(add); frame.add(equals); frame.add(mult); frame.add(per); frame.add(clear); frame.add(displaywarning); component.add(frame); component.setTitle("Colby's Calculator"); component.setVisible(true); component.setSize(340,320); component.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); component.setResizable(false); } public static void main(String[] args){ Main calculator = new Main(); } } [/quote]

Posting in language:

 

Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

View Entire Topic
  • Edited by dazarobbo: 12/13/2013 7:53:15 AM
    A few things: 1. [quote]import javax.swing.SwingUtilities;[/quote]You don't need this. 2. You can replace all of the variables you have set up for the buttons and use an array or list. Firstly, set up an action listener for [i]all[/i] buttons [quote]class NumberButtonListener implements ActionListener{ final JTextField _Display; public NumberButtonListener(final JTextField dsplay) { _Display = display; } @Override public void actionPerformed(ActionEvent e){ _Display.setText(_Display.getText() + ((JButton)e.getSource()).getText() ); } };[/quote] Create an instance of it:[quote]NumberButtonListener numberListener; Calculator(){ //renamed from Main() //... numberListener = new NumberButtonListener(display); //... }[/quote] Create the buttons:[quote] JButton temp = null; for(int i = 1, j = 0, x = 50, y = 200; i <= 3; ++i, j=0, x = 50, y -= 25){ for(;j<3; ++j, x += 60){ temp = new JButton(Integer.toString((i - 1) * 3 + j + 1)){{ setSize(60, 25); addActionListener(numberListener); setVisible(true); Numbers.add(this); }}; temp.setLocation(x, y); } }[/quote] Then later on where you add them all to the frame:[quote] for(int i=0,l=Numbers.size(); i<l; ++i){ frame.add(Numbers.get(i)); }[/quote] 3. In the program's main method, you don't need the returned instance since you're not going to use it.[quote] public static void main(String[] args) { new Calculator(); }[/quote] There might be some others which I'll have a look at soon as well.

    Posting in language:

     

    Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

    6 Replies
    You are not allowed to view this content.
    ;
    preload icon
    preload icon
    preload icon