Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

Thread: Java got complicated for me :( Need Help!

  1. #1
    ram alday is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Question Java got complicated for me :( Need Help!

    So I was planning to create a java program wherein the user will have the option to choose among addition, subtraction, multiplication and random and give him/her a random 1 digit number for computation. The program then will check if it is correct or wrong by pressing the check button. By clicking another button named Clear it gives you the option to reset or start a new set of question.

    With Random the program chooses under add, minus, multiply and generate a set of numbers.

    It might be simple but it got a little complicated somewhere at the computation. :( More like under nested if(s).

    Here's how it should somehow look like
    Java got complicated for me :( Need Help!-woah.png
    Pressed addition
    Java got complicated for me :( Need Help!-woah2.png
    Put a wrong result
    Java got complicated for me :( Need Help!-woah3.png
    Put a correct result
    Java got complicated for me :( Need Help!-woah4.png
    Pressed Clear button
    Java got complicated for me :( Need Help!-woah5.png

    By the way, here's the code that I've started if you're interested into solving this

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;

    public class Compute extends JFrame implements ActionListener//required to overwrite and give other functionality
    {
    //Message for input
    JLabel num1Label = new JLabel("Number1:");
    JLabel num2Label = new JLabel(" Number2:");
    JLabel equalLabel = new JLabel("Equals:");
    JLabel Message = new JLabel ("");
    //Input Dialog Box
    JTextField num1Field = new JTextField(10);
    JTextField num2Field = new JTextField(10);
    JTextField equalField = new JTextField(10);
    //Ok, Cancel, Clear, Buttons
    JButton equalButton = new JButton("Check");
    JButton clearButton = new JButton("Clear");
    JButton addButton = new JButton("Addition");
    JButton subtractButton= new JButton("Subtraction");
    JButton multiplyButton= new JButton("Multiplication");
    JButton randomButton = new JButton("Random");

    public Compute() //constructor method should be the same to public class name
    {
    //Container c = new getContentPane();
    //c.add()

    setTitle("Compute"); //or super

    Container c = getContentPane();
    c.setLayout(new FlowLayout());//Layout Manager
    c.add(addButton);
    c.add(subtractButton);
    c.add(multiplyButton);
    c.add(randomButton);

    c.add(num1Label);
    c.add(num1Field);
    c.add(num2Label);
    c.add(num2Field);
    c.add(equalLabel);
    c.add(equalField);
    c.add(equalButton);
    c.add(clearButton);
    c.add(Message);

    //To register calcButton for event handling
    subtractButton.addActionListener(this);
    addButton.addActionListener(this);
    multiplyButton.addActionListener(this);
    randomButton.addActionListener(this);
    equalButton.addActionListener(this);
    clearButton.addActionListener(this);//To register clear button for event handling
    num2Field.addActionListener(this);
    num1Field.setEditable(false);
    num2Field.setEditable(false);
    equalField.setEditable(false);


    setSize(400,150);
    setVisible(true);
    setResizable(false);
    }




    public void actionPerformed(ActionEvent e)//ActionListener
    {
    int x=(int)(Math.random()*2+0);
    //disabler--------------------------------------------------------------------------------------
    if(e.getSource() == addButton)
    {
    addButton.setEnabled(false);
    subtractButton.setEnabled(false);
    multiplyButton.setEnabled(false);
    randomButton.setEnabled(false);
    equalField.setEditable(true);
    num1Field.setText((int)(Math.random()*9+0)+"");
    num2Field.setText((int)(Math.random()*9+0)+"");
    equalLabel.setText(" Sum:");


    }//end add
    //subtract
    if(e.getSource() == subtractButton)
    {
    addButton.setEnabled(false);
    subtractButton.setEnabled(false);
    multiplyButton.setEnabled(false);
    randomButton.setEnabled(false);
    equalField.setEditable(true);
    num1Field.setText((int)(Math.random()*9+0)+"");
    num2Field.setText((int)(Math.random()*9+0)+"");
    equalLabel.setText("Difference:");


    }//end subtract
    //multiply
    if(e.getSource() == multiplyButton)
    {
    addButton.setEnabled(false);
    subtractButton.setEnabled(false);
    multiplyButton.setEnabled(false);
    randomButton.setEnabled(false);
    equalField.setEditable(true);
    num1Field.setText((int)(Math.random()*9+0)+"");
    num2Field.setText((int)(Math.random()*9+0)+"");
    equalLabel.setText("Product:");
    }//end multiply
    //random
    if(e.getSource() == randomButton)
    {
    addButton.setEnabled(false);
    subtractButton.setEnabled(false);
    multiplyButton.setEnabled(false);
    randomButton.setEnabled(false);
    equalField.setEditable(true);
    num1Field.setText((int)(Math.random()*9+0)+"");
    num2Field.setText((int)(Math.random()*9+0)+"");



    if(x==0){
    equalLabel.setText(" Sum:");
    }
    if(x==1){
    equalLabel.setText("Difference:");
    }
    if(x==2){
    equalLabel.setText("Product:");
    }

    }//end random



    //disabler
    //computer
    if (e.getSource() == addButton && e.getSource() == equalButton && e.getSource() != subtractButton && e.getSource() != multiplyButton && e.getSource() != randomButton);
    {

    int numadd1 = Integer.parseInt(num1Field.getText());//gets value from numField1
    int numadd2 = Integer.parseInt(num2Field.getText());
    int sumadd=numadd1+numadd2;
    int answeradd = Integer.parseInt(equalField.getText());
    if(answeradd==sumadd)
    {

    Message.setText("Congratulations");
    }
    if (answeradd!=sumadd)
    {
    Message.setText("Wrong, please try again");
    }
    }
    if(e.getSource() == subtractButton && e.getSource() == equalButton && e.getSource() != addButton && e.getSource() != multiplyButton && e.getSource() != randomButton);
    {

    int numsub1 = Integer.parseInt(num1Field.getText());//gets value from numField1
    int numsub2 = Integer.parseInt(num2Field.getText());
    int dif=numsub1-numsub2;
    int answersub = Integer.parseInt(equalField.getText());
    if(answersub==dif)
    {

    Message.setText("Congratulations");
    }
    }//end subtract compute

    if(e.getSource() == multiplyButton && e.getSource() == equalButton && e.getSource() != addButton && e.getSource() != subtractButton && e.getSource() != randomButton);
    {

    int nummul1 = Integer.parseInt(num1Field.getText());//gets value from numField1
    int nummul2 = Integer.parseInt(num2Field.getText());
    int pro=nummul1*nummul2;
    int answermul = Integer.parseInt(equalField.getText());
    if(answermul==pro)
    {

    Message.setText("Congratulations");
    }
    }//end multiply compute
    //================================================

    if(e.getSource() == randomButton && e.getSource()==equalButton && e.getSource() != addButton && e.getSource() != multiplyButton && e.getSource() != subtractButton){
    int numran1 = Integer.parseInt(num1Field.getText());//gets value from numField1
    int numran2 = Integer.parseInt(num2Field.getText());
    equalLabel.setText("Product:");
    if(x==0){
    int sumaddran=numran1+numran2;
    int answeraddran = Integer.parseInt(equalField.getText());
    if(answeraddran==sumaddran)
    {

    Message.setText("Congratulations");
    }
    }
    if(x==1){
    int subran=numran1-numran2;
    int answersubran = Integer.parseInt(equalField.getText());
    if(answersubran==subran)
    {

    Message.setText("Congratulations");
    }
    }
    if(x==2){
    int mulran=numran1*numran2;
    int answermulran = Integer.parseInt(equalField.getText());
    if(answermulran==mulran)
    {

    Message.setText("Congratulations");
    }
    }
    }//end random button compute

    if(e.getSource()== clearButton)

    {
    num1Field.setText("");
    num2Field.setText("");
    equalField.setText("");
    addButton.setEnabled(true);
    subtractButton.setEnabled(true);
    multiplyButton.setEnabled(true);
    randomButton.setEnabled(true);
    equalField.setEditable(false);
    equalLabel.setText("Equals");
    Message.setText("");

    }


    }//end first if



    public static void main(String args[]) //ordinary method
    {

    new Compute();

    }

    }
    Last edited by ram alday; 01-26-2014 at 12:50 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java got complicated for me :( Need Help!

    So, what's your problem? Please don't answer: "it doesn't work, you have to do it".

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ram alday is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Default Re: Java got complicated for me :( Need Help!

    Of course it runs perfectly and works :) My only problem is whenever I put on results for example: I've clicked addition button and the generated numbers are 3 and the second is 5 , the program will congratulate you if your answers are 8,15,-2 instead of a must congratulate because you have only successfully answered the correct result of adding two numbers which is equivalent to 8. The reason why 15 and -2 are both correct as well because it is also the result when you subtract and multiply them. Thank for the quick reply for my question anyways!~ :D Hope you could help me with this.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java got complicated for me :( Need Help!

    Why don't you make JToggleButtons out of those 'add', 'subtract', 'multiply' buttons; and put them in a ButtonGroup; that way the program wants the user to either add, subtract or multiply those two numbers and it can check the single result ... The user decides what s/he wants to do: add, subtract or multiply.

    kind regards,

    Jos
    ram alday likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ram alday is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Default Re: Java got complicated for me :( Need Help!

    Too bad, I don't know how to use those. Anyways, thanks!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java got complicated for me :( Need Help!

    Quote Originally Posted by ram alday View Post
    Too bad, I don't know how to use those. Anyways, thanks!
    People only used to know how to use sharp, pointy rocks; if they all reasoned like you do, they'd all still live in caves nowadays ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Complicated for loop's
    By JordiLaForge in forum New To Java
    Replies: 6
    Last Post: 05-27-2013, 02:44 PM
  2. Complicated problem between classes
    By uhertz in forum New To Java
    Replies: 3
    Last Post: 06-20-2011, 01:58 AM
  3. replacement for complicated if else?
    By TopNFalvors in forum New To Java
    Replies: 12
    Last Post: 04-09-2011, 07:05 PM
  4. Complicated Draw
    By Desdenova in forum New To Java
    Replies: 9
    Last Post: 05-27-2010, 08:44 PM
  5. Complicated Method
    By Desmond in forum New To Java
    Replies: 5
    Last Post: 03-17-2010, 11:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •