Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help with java logic

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default <solved>Help with java logic

    i am trying to make some sort of quiz and meet with some problems, this is the kind of quiz where each option is given a certain points and at the end of the quiz, the score is tallied up to see how well you do. the problem right now is the scores does not add up no matter what i try. i managed to get some sort of output but its kinda not working at all. i have a sample of 3 questions right now and the "Total number of As selected:" would only show the option selected for question3 and the " total score" would only calculate the sum for question2.

    this is the .java which i have right now

    import javax.swing.*;
     
    import java.awt.*;
    import java.sql.*;
    import java.text.*;
    import java.awt.event.*;
     
    public class assignTips implements ActionListener
    {
    JFrame que1 = new JFrame();
    JFrame que2 = new JFrame();
    JFrame que3 = new JFrame();
    JFrame res1 = new JFrame();
     
    JLabel label1= new JLabel("1.Question to be filled eventually :");
    JLabel label2= new JLabel("2.Question to be filled eventually:");
    JLabel label3= new JLabel("3.Question to be filled eventually:");
    JLabel label4= new JLabel();
    JLabel label91= new JLabel();
    JLabel label96= new JLabel();
    JLabel label97= new JLabel();
    JLabel label98= new JLabel();
    JLabel label99= new JLabel();
     
    JLabel labelname= new JLabel("enter your name");
    JTextField textname = new JTextField();
     
    JRadioButton a=new JRadioButton("a.option1");
    JRadioButton b=new JRadioButton("b.option2");
    JRadioButton c=new JRadioButton("c.option3");
     
    JRadioButton a2=new JRadioButton("a.option1");
    JRadioButton b2=new JRadioButton("b.option2");
    JRadioButton c2=new JRadioButton("c.option3");
     
    JRadioButton a3=new JRadioButton("a.option1");
    JRadioButton b3=new JRadioButton("b.option2");
    JRadioButton c3=new JRadioButton("c.option3");
     
     
    JButton button=new JButton("NEXT");
    JButton button2=new JButton("NEXT");
    JButton button4=new JButton("NEXT");
    JButton button3=new JButton("Click to submit your score");
    ButtonGroup g=new ButtonGroup();  //  @jve:decl-index=0:
     
    public assignTips()
    {
     
    que1.setLayout(new FlowLayout());
    que1.setSize(400,300);
    que1.setLayout(new GridLayout(8,8));
    que1.setResizable(true);
    que1.setTitle("Question1");
    que1.getContentPane().add(label1);
    que1.getContentPane().add(a);
    que1.getContentPane().add(b);
    que1.getContentPane().add(c);
    que1.getContentPane().add(button);
     
     
    a.addActionListener(this);
    b.addActionListener(this);
    c.addActionListener(this);
    button.addActionListener(this);
     
    g.add(a);
    g.add(b);
    g.add(c);
    que1.setVisible(true);
    que1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
    que2.setLayout(new FlowLayout());
    que2.setSize(400,300);
    que2.setLayout(new GridLayout(8,8));
    que2.setResizable(true);
    que2.setTitle("Question2");
    que2.getContentPane().add(label2);
    que2.getContentPane().add(a2);
    que2.getContentPane().add(b2);
    que2.getContentPane().add(c2);
    que2.getContentPane().add(button2);
     
     
    a2.addActionListener(this);
    b2.addActionListener(this);
    c2.addActionListener(this);
    button2.addActionListener(this);
     
    g.add(a2);
    g.add(b2);
    g.add(c2);
     
    que3.setLayout(new FlowLayout());
    que3.setSize(400,300);
    que3.setLayout(new GridLayout(8,8));
    que3.setResizable(true);
    que3.setTitle("Question3");
    que3.getContentPane().add(label3);
    que3.getContentPane().add(a3);
    que3.getContentPane().add(b3);
    que3.getContentPane().add(c3);
    que3.getContentPane().add(button4);
     
     
    a3.addActionListener(this);
    b3.addActionListener(this);
    c3.addActionListener(this);
    button4.addActionListener(this);
     
    g.add(a3);
    g.add(b3);
    g.add(c3);
     
    res1.setLayout(new FlowLayout());
    res1.setSize(400,300);
    res1.setLayout(new GridLayout(8,8));
    res1.setResizable(true);
    res1.setTitle("Result");
    res1.getContentPane().add(label99);
    res1.getContentPane().add(label98);
    res1.getContentPane().add(label97);
    res1.getContentPane().add(label96);
     
    res1.getContentPane().add(labelname);
    res1.getContentPane().add(textname);
    res1.getContentPane().add(button3);
    button3.setActionCommand("button3");
    button3.addActionListener(this);
    que1.setVisible(true);
     
     
    que3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
     
    }
     
    public static void main(String[] args)
    {    
        new assignTips();
    }
     
    public void actionPerformed(ActionEvent e)
    {
    Object source = e.getSource();
     
    int que1a=0,que1b=0,que1c=0;
    int que2a=0,que2b=0,que2c=0;
    int que3a=0,que3b=0,que3c=0;
     
     
     
    if (a.isSelected())
     
     
        {
        que1a++;
        }
     
     
    else if(b.isSelected())
        {
     
        que1b++;
        }
     
     
    else if(c.isSelected())
        {
     
        que1c++;
     
        }
     
    if(a2.isSelected())
        {
        que2a++;
        }
    else if(b2.isSelected())
        {
     
        que2b++;
        }
     
    else if(c2.isSelected())
        {
     
        que2c++;
     
        }
     
    if(a3.isSelected())
    {
        que3a++;
    }
    else if(b3.isSelected())
    {
     
        que3b++;
    }
     
    else if(c3.isSelected())
    {
     
        que3c++;
     
    }
    int totala=que1a + que2a +que3a;
    int totalb=que1b + que2b +que3b;
    int totalc=que1c + que2c +que3c;
    int totaltotal=(totala*3)+(totalb*2)+totalc;
     
    if(source==button)
        {
            que1.setVisible(false);
            que2.setVisible(true);
            que3.setVisible(false);
        }
     
    if(source==button2)
        {
            que2.setVisible(false);
            que3.setVisible(true);
        }
    if(source==button4)
    {
        que3.setVisible(false);
        res1.setVisible(true);
    }
     
     
     
     
     
    label99.setText("Total number of As selected: " + totala);
    label98.setText("Total number of Bs selected: " + totalb);
    label97.setText("Total number of Cs selected: " + totalc);
     
    totaltotal=(totala*3)+(totalb*2)+totalc;
     
    if(source==button2)
        {
            label96.setText("Your total Score:" + totaltotal);
            label91.setText(""+ totaltotal);
     
        }
     
     
        String cmd = e.getActionCommand();
     
        if(cmd.equals("button3"))
        {
            insertData();}
        }
     
     
     
    private void insertData() {
        Connection con;
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:tips");
            String sql = "Insert Into Table1 (NameParticipant,Score) " +
                    "Values ('"+textname.getText()+"','"+label91.getText()
                    +"')";
            Statement statement = con.createStatement();
            statement.execute(sql);
     
     
        }
        catch(Exception e1)
        {
            System.out.println("error");
        }
     
    }
     
    }
    Last edited by dever; February 6th, 2011 at 08:15 AM. Reason: solved


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Help with java logic

    First off, normally people would put
     
    JFrame que1 = new JFrame();
    JFrame que2 = new JFrame();
    JFrame que3 = new JFrame();
    JFrame res1 = new JFrame();
     
    JLabel label1= new JLabel("1.Question to be filled eventually :");
    JLabel label2= new JLabel("2.Question to be filled eventually:");
    JLabel label3= new JLabel("3.Question to be filled eventually:");
    JLabel label4= new JLabel();
    JLabel label91= new JLabel();
    JLabel label96= new JLabel();
    JLabel label97= new JLabel();
    JLabel label98= new JLabel();
    JLabel label99= new JLabel();
     
    JLabel labelname= new JLabel("enter your name");
    JTextField textname = new JTextField();
     
    JRadioButton a=new JRadioButton("a.option1");
    JRadioButton b=new JRadioButton("b.option2");
    JRadioButton c=new JRadioButton("c.option3");
     
    JRadioButton a2=new JRadioButton("a.option1");
    JRadioButton b2=new JRadioButton("b.option2");
    JRadioButton c2=new JRadioButton("c.option3");
     
    JRadioButton a3=new JRadioButton("a.option1");
    JRadioButton b3=new JRadioButton("b.option2");
    JRadioButton c3=new JRadioButton("c.option3");
     
     
    JButton button=new JButton("NEXT");
    JButton button2=new JButton("NEXT");
    JButton button4=new JButton("NEXT");
    JButton button3=new JButton("Click to submit your score");
    ButtonGroup g=new ButtonGroup();  //  @jve:decl-index=0:

    inside the constructor.

    2nd...why can't all 3 questions be on the same JFrame?

    3rd...I think later in your ActionListener, you should have

    if (source.equals(button))

    instead of

    if(source == button)
    Last edited by javapenguin; February 4th, 2011 at 12:19 PM.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    dever (February 5th, 2011)

  4. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Help with java logic

    Quote Originally Posted by javapenguin View Post
    First off, normally people would put ... inside the constructor.
    That doesn't matter. There is no "normally" there- some people might put it in the constructor, but others might put it where the OP has it.

    Quote Originally Posted by javapenguin View Post
    3rd...I think later in your ActionListener, you should have

    if (source.equals(button))

    instead of

    if(source == button)
    And that's not true. == works just fine in this case.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following 2 Users Say Thank You to KevinWorkman For This Useful Post:

    dever (February 5th, 2011), javapenguin (February 4th, 2011)

  6. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Red face Re: Help with java logic

    Really...the way I thought of it was that it wouldn't go into the main below if it wasn't inside the constructor. Guess I was wrong.

    Also, I guess .equals isn't necessary as they both would point to the same reference in this case.

    I just looked that up. Never thought of that before when I posted that.

  7. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,548
    Thanks
    24
    Thanked 300 Times in 282 Posts

    Default Re: Help with java logic

    Quote Originally Posted by javapenguin View Post
    Really...the way I thought of it was that it wouldn't go into the main below if it wasn't inside the constructor. Guess I was wrong.
    Actually, since the main is in the same class, those variables would be visible by the main regardless if they were initialized in the constructor or not. Notice how I said visible and not accessible. Since main is static, they cannot actually be used in the main unless those variables are also declared static, also regardless of where they are initialized. That is all a moot point however since the dever is not trying to use them in the main.

    When you create the class variables, you are reserving the spot in memory. When you create the class variable and initialize it there, you are reserving the spot in memory AND setting its value. You have to think of the class variable as a reference in memory (since that is pretty much what it is). When you use that variable name, you are referencing the spot in memory associated with that variable, and thus referencing its value. Regardless of where you set the value of the variable, you are still accessing that spot in memory.

    I personally set my class variable values in my constructors because I consider that good practice, but it works either way.

    Also, I guess .equals isn't necessary as they both would point to the same reference in this case.

    I just looked that up. Never thought of that before when I posted that.
    Well that is the point of the == check dever is trying to do. The .equals method will check for equality between the two objects, but since dever is trying to check the actual object to be a specific object, the == is the correct thing to use. Keep in mind that while two objects may have the same values according to the .equals method, that does NOT mean they are the same object in memory.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. The Following User Says Thank You to aussiemcgr For This Useful Post:

    dever (February 5th, 2011)

  9. #6
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Help with java logic

    Quote Originally Posted by aussiemcgr View Post
    Keep in mind that while two objects may have the same values according to the .equals method, that does NOT mean they are the same object in memory.
    Actually in this case, it does mean that they are the same object in memory. JButton inherits its .equals method from Object. By the definition of the equal method in the Object class, .equals will return true if & only if the the two objects refer to the same object in memory.

    But like Kevin pointed out the == the OP is using is fine, and this is another case of javapenguin giving out bad advice without checking his facts.

  10. The Following User Says Thank You to DavidFongs For This Useful Post:

    newbie (February 4th, 2011)

  11. #7
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with java logic

    thanks for all your reply, i have tried many other things but cant seems to get it to work. can someone kindly tweak my code so that it works. thanks in advance

    the thing i dont really get is how can my totala and totaltotal variable be of different value when my totaltotal is calculated off my totala ? just how does java work anyway..........

  12. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Help with java logic

    Quote Originally Posted by dever View Post
    thanks for all your reply, i have tried many other things but cant seems to get it to work.
    I'm still not really sure what your code is supposed to do versus what it's actually doing. You haven't specified what you expected or what's happening other than to say that "the scores does not add up" and "its kinda not working at all"- what does that mean?

    Quote Originally Posted by dever View Post
    can someone kindly tweak my code so that it works. thanks in advance
    No. That's not how this works. If you narrow your code down to a few lines and ask a specific question, then we can go from there. But you can't just post your entire project and expect people to "tweak" it for you. Besides, even if somebody did, you wouldn't actually understand the fix, and you'd just be back here in a couple days with your next problem.

    Quote Originally Posted by dever View Post
    the thing i dont really get is how can my totala and totaltotal variable be of different value when my totaltotal is calculated off my totala ?
    Why would you would expect those variables to contain the same value?

    I suggest you put a ton of print statements into your code, showing you the value of each variable after each line. Or better yet, step through your program with a debugger. Where does the program differ from what you expected? When you get the problem narrowed down to just a few lines, post an SSCCE that shows just those lines- and we'll go from there.

    Quote Originally Posted by dever View Post
    just how does java work anyway..........
    What does this question mean?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Java Dos Logic Test count all non increasing number
    By Jhovarie in forum Object Oriented Programming
    Replies: 3
    Last Post: January 13th, 2011, 02:28 PM
  2. java Logic Random with out repetition
    By Jhovarie in forum Loops & Control Statements
    Replies: 1
    Last Post: January 13th, 2011, 02:25 PM
  3. Latin square logic
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2010, 08:38 AM
  4. Need Help with Operators/ logic
    By codekiller in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 3rd, 2010, 09:25 AM
  5. Simple recursion logic
    By chronoz13 in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 24th, 2009, 09:53 PM