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.

Page 3 of 3 FirstFirst 123
Results 51 to 60 of 60

Thread: Beginner help please.

  1. #51
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    thank you!

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class HedgeYourBet extends JFrame
    {
     
    	public static void main (String[] arg)
        {
            HedgeYourBet window = new HedgeYourBet();
            window.setVisible(true);
        }
     
        JLabel title = new JLabel("There is only 1 correct answer for each question.");
      	JLabel one = new JLabel("If you choose 1 correct answer = 5 pts");
        JLabel two = new JLabel("2 answers(with the 1 correct) = 2 pts");
        JLabel three = new JLabel("all 3 answers = 1 pt");
    	JLabel direction = new JLabel("Choose the correct answer for x in each equation below");
    	String q1 = ("Q1) 1x + -1 = 0");
    	String q2 = ("Q2) 14x - 7 = 21");
    	String q3 = ("Q3) 5x + 5 = 10");
    	String q4 = ("Q4) x + 5 = 2x + 2");
    	String q5 = ("Q5) 3x - 5 = 2x - 4");
    	JLabel question = new JLabel(q1);
    	JCheckBox q1a = new JCheckBox("x = 1");
    	JCheckBox q1b = new JCheckBox("x = 2");
    	JCheckBox q1c = new JCheckBox("x = 3");
    	JButton submit = new JButton("Submit");
    	int point = 0;
    	JLabel points = new JLabel ("You have "+point+" points");
     
       	public HedgeYourBet()
        {
        	setTitle("Quiz Game");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300,600);
         	Container pane = getContentPane();
        	pane.setLayout(new FlowLayout());;
        	pane.add(title);
        	pane.add(one);
      		pane.add(two);
        	pane.add(three);
        	pane.add(direction);
     
        	pane.add(question);
        	JPanel one = new JPanel();
        	one.add(q1a);
        	one.add(q1b);
        	one.add(q1c);
        	JPanel submit2 = new JPanel();
        	submit2.add(submit);
        	submit2.add(points);
    		pane.add(one);
        	pane.add(submit2);
        	ClickButtonListener clickListener = new ClickButtonListener();
     
            submit.addActionListener(clickListener);
        }
     
    	public class ClickButtonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent e)
            {
    			if (question.equals(q1))
    			{
            		if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1a.isSelected() && q1b.isSelected() && !q1c.isSelected() || q1a.isSelected() && q1c.isSelected() && !q1b.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1a.isSelected() && !q1b.isSelected() && !q1c.isSelected())
        			{
        				point +=5;
        			}
     
    			}
    			points.setText("You have "+point+" points");
    			question.setText(q2);
     
    			if (question.equals(q2))
    			{
    				if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1b.isSelected() && q1a.isSelected() && !q1c.isSelected() || q1b.isSelected() && q1c.isSelected() && !q1a.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1b.isSelected() && !q1a.isSelected() && !q1c.isSelected())
        			{
        				point +=5;
        			}	
    			}
    			points.setText("You have "+point+" points");
    			question.setText(q3);
     
    			if (question.equals(q3))
    			{
            		if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1a.isSelected() && q1b.isSelected() && !q1c.isSelected() || q1a.isSelected() && q1c.isSelected() && !q1b.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1a.isSelected() && !q1b.isSelected() && !q1c.isSelected())
        			{
        				point +=5;
        			}
     
    			 }
    			 points.setText("You have "+point+" points");
    			 question.setText(q4);
     
    			if (question.equals(q4))
    			 {
            		if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1c.isSelected() && q1b.isSelected() && !q1a.isSelected() || q1c.isSelected() && q1a.isSelected() && !q1b.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1c.isSelected() && !q1b.isSelected() && !q1a.isSelected())
        			{
        				point +=5;
        			}
     
    			 }
    			 points.setText("You have "+point+" points");
    			 question.setText(q5);
     
    			if (question.equals(q5))
    			 {
            		if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1a.isSelected() && q1b.isSelected() && !q1c.isSelected() || q1a.isSelected() && q1c.isSelected() && !q1b.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1a.isSelected() && !q1b.isSelected() && !q1c.isSelected())
        			{
        				point +=5;
        			}
     
    			 }
    			 points.setText("You have "+point+" points");
    		/*	  if (point <= 10)
    			 {
    			 	question.setText("Try Again!");
    			 }
    			 else if (point >= 10)
    			 {
    			 	question.setText("Congrats!");
    			 }
     
           */ }
     
        }
    }

    some reason it jumps from q1 to q5, and discarding going from q1 to q2 and from q2 to q3

  2. #52
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner help please.

    Try debugging the code by adding some println statements to print out the values of the variables that are a problem every time their values are changed so you can see what the program is doing and when and where it is doing it.

    if (question.equals(q1))
    That compiles but makes no sense.
    A JLabel is not a String and a String is not a JLabel. It does not make sense to test if they are equal.
    If you don't understand my answer, don't ignore it, ask a question.

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

    xdorkiee (December 18th, 2012)

  4. #53
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class HedgeYourBet extends JFrame
    {
     
    	public static void main (String[] arg)
        {
            HedgeYourBet window = new HedgeYourBet();
            window.setVisible(true);
        }
     
        JLabel title = new JLabel("There is only 1 correct answer for each question.");
      	JLabel one = new JLabel("If you choose 1 correct answer = 5 pts");
        JLabel two = new JLabel("2 answers(with the 1 correct) = 2 pts");
        JLabel three = new JLabel("all 3 answers = 1 pt");
    	JLabel direction = new JLabel("Choose the correct answer for x in each equation below");
    	String q1 = ("Q1) 1x + -1 = 0");
    	String q2 = ("Q2) 14x - 7 = 21");
    	String q3 = ("Q3) 5x + 5 = 10");
    	String q4 = ("Q4) x + 5 = 2x + 2");
    	String q5 = ("Q5) 3x - 5 = 2x - 4");
    	JLabel question = new JLabel(q1);
    	JCheckBox q1a = new JCheckBox("x = 1");
    	JCheckBox q1b = new JCheckBox("x = 2");
    	JCheckBox q1c = new JCheckBox("x = 3");
    	JButton submit = new JButton("Submit");
    	int point = 0;
    	JLabel points = new JLabel ("You have "+point+" points");
     
       	public HedgeYourBet()
        {
        	setTitle("Quiz Game");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300,600);
         	Container pane = getContentPane();
        	pane.setLayout(new FlowLayout());;
        	pane.add(title);
        	pane.add(one);
      		pane.add(two);
        	pane.add(three);
        	pane.add(direction);
        	pane.add(question);
        	JPanel one = new JPanel();
        	one.add(q1a);
        	one.add(q1b);
        	one.add(q1c);
        	JPanel submit2 = new JPanel();
        	submit2.add(submit);
        	submit2.add(points);
    		pane.add(one);
        	pane.add(submit2);
        	ClickButtonListener clickListener = new ClickButtonListener();
            submit.addActionListener(clickListener);
        }
     
    	public class ClickButtonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent e)
            {
    			if (question.getText().equals(q1))
    			{
            		if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1a.isSelected() && q1b.isSelected() && !q1c.isSelected() || q1a.isSelected() && q1c.isSelected() && !q1b.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1a.isSelected() && !q1b.isSelected() && !q1c.isSelected())
        			{
        				point +=5;
        			}
        			points.setText("You have "+point+" points");
    				question.setText(q2);
     
    			}
     
     
    			else if (question.getText().equals(q2))
    			{
    				if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1b.isSelected() && q1a.isSelected() && !q1c.isSelected() || q1b.isSelected() && q1c.isSelected() && !q1a.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1b.isSelected() && !q1a.isSelected() && !q1c.isSelected())
        			{
        				point +=5;
        			}
        		points.setText("You have "+point+" points");
    			question.setText(q3);
    			}
     
     
    			else if (question.getText().equals(q3))
    			{
            		if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1a.isSelected() && q1b.isSelected() && !q1c.isSelected() || q1a.isSelected() && q1c.isSelected() && !q1b.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1a.isSelected() && !q1b.isSelected() && !q1c.isSelected())
        			{
        				point +=5;
        			}
    			 points.setText("You have "+point+" points");
    			 question.setText(q4);
    			 }
     
     
    			else if (question.getText().equals(q4))
    			 {
            		if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
        			else if(q1c.isSelected() && q1b.isSelected() && !q1a.isSelected() || q1c.isSelected() && q1a.isSelected() && !q1b.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1c.isSelected() && !q1b.isSelected() && !q1a.isSelected())
        			{
        				point +=5;
        			}
    			 question.setText(q5);
    			 points.setText("You have "+point+" points");
    			 }
     
    			else if (question.getText().equals(q5))
    			 {
            		if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1a.isSelected() && q1b.isSelected() && !q1c.isSelected() || q1a.isSelected() && q1c.isSelected() && !q1b.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1a.isSelected() && !q1b.isSelected() && !q1c.isSelected())
        			{
        				point +=5;
        			}
    		     points.setText("You have "+point+" points");
    			 }
     
    		/*	  if (point <= 15)
    			 {
    			 	question.setText("Try Again!");
    			 }
    			 else if (point >= 15)
    			 {
    			 	question.setText("Congrats!");
    			 }
     
           */ }
     
        }
    }

    good morning, how do I clear checkboxes?
    On question 1, i check a and click sumbit, so it goes to question 2, however, the a is still checked, how do i reset the checkboxes when i move on to a different question?

  5. #54
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner help please.

    Did you look at the API doc for the class to see if there are any methods that will do what you want?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 20th, 2012)

  7. #55
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    thank you i got it. one last problem.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class HedgeYourBet extends JFrame
    {
     
    	public static void main (String[] arg)
        {
            HedgeYourBet window = new HedgeYourBet();
            window.setVisible(true);
        }
     
        JLabel title = new JLabel("There is only 1 correct answer for each question.");
      	JLabel one = new JLabel("If you choose 1 correct answer = 5 pts");
        JLabel two = new JLabel("2 answers(with the 1 correct) = 2 pts");
        JLabel three = new JLabel("all 3 answers = 1 pt");
    	JLabel direction = new JLabel("Choose the correct answer for x in each equation below");
    	String q1 = ("Q1) 1x + -1 = 0");
    	String q2 = ("Q2) 14x - 7 = 21");
    	String q3 = ("Q3) 5x + 5 = 10");
    	String q4 = ("Q4) x + 5 = 2x + 2");
    	String q5 = ("Q5) 3x - 5 = 2x - 4");
    	JLabel question = new JLabel(q1);
    	JCheckBox q1a = new JCheckBox("x = 1");
    	JCheckBox q1b = new JCheckBox("x = 2");
    	JCheckBox q1c = new JCheckBox("x = 3");
    	JButton submit = new JButton("Submit");
    	int point = 0;
    	JLabel points = new JLabel ("You have "+point+" points");
     
       	public HedgeYourBet()
        {
        	setTitle("Quiz Game");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300,600);
         	Container pane = getContentPane();
        	pane.setLayout(new FlowLayout());;
        	pane.add(title);
        	pane.add(one);
      		pane.add(two);
        	pane.add(three);
        	pane.add(direction);
        	pane.add(question);
        	JPanel one = new JPanel();
        	one.add(q1a);
        	one.add(q1b);
        	one.add(q1c);
        	JPanel submit2 = new JPanel();
        	submit2.add(submit);
        	submit2.add(points);
    		pane.add(one);
        	pane.add(submit2);
        	ClickButtonListener clickListener = new ClickButtonListener();
            submit.addActionListener(clickListener);
        }
     
    	public class ClickButtonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent e)
            {
    			if (question.getText().equals(q1))
    			{
            		if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1a.isSelected() && q1b.isSelected() && !q1c.isSelected() || q1a.isSelected() && q1c.isSelected() && !q1b.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1a.isSelected() && !q1b.isSelected() && !q1c.isSelected())
        			{
        				point +=5;
        			}
        			q1a.setSelected(false);
        			q1b.setSelected(false);
        			q1c.setSelected(false);
        			points.setText("You have "+point+" points");
    				question.setText(q2);	
    			}
     
    			else if (question.getText().equals(q2))
    			{
    				if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1b.isSelected() && q1a.isSelected() && !q1c.isSelected() || q1b.isSelected() && q1c.isSelected() && !q1a.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1b.isSelected() && !q1a.isSelected() && !q1c.isSelected())
        			{
        				point +=5;
        			}
        			q1a.setSelected(false);
        			q1b.setSelected(false);
        			q1c.setSelected(false);
        			points.setText("You have "+point+" points");
    				question.setText(q3);
    			}
     
    			else if (question.getText().equals(q3))
    			{
            		if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1a.isSelected() && q1b.isSelected() && !q1c.isSelected() || q1a.isSelected() && q1c.isSelected() && !q1b.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1a.isSelected() && !q1b.isSelected() && !q1c.isSelected())
        			{
        				point +=5;
        			}
        			q1a.setSelected(false);
        			q1b.setSelected(false);
        			q1c.setSelected(false);
    			 	points.setText("You have "+point+" points");
    			 	question.setText(q4);
    			 }
     
    			else if (question.getText().equals(q4))
    			 {
            		if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
        			else if(q1c.isSelected() && q1b.isSelected() && !q1a.isSelected() || q1c.isSelected() && q1a.isSelected() && !q1b.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1c.isSelected() && !q1b.isSelected() && !q1a.isSelected())
        			{
        				point +=5;
        			}
        			q1a.setSelected(false);
        			q1b.setSelected(false);
        			q1c.setSelected(false);
    				question.setText(q5);
    			 	points.setText("You have "+point+" points");
    			 }
     
    			else if (question.getText().equals(q5))
    			 {
            		if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected())
        			{
        				point += 1;
        			}
     
        			else if(q1a.isSelected() && q1b.isSelected() && !q1c.isSelected() || q1a.isSelected() && q1c.isSelected() && !q1b.isSelected() )
        			{
        				point += 2;
        			}
        			else if(q1a.isSelected() && !q1b.isSelected() && !q1c.isSelected())
        			{
        				point +=5;
        			}
    		     	points.setText("You have "+point+" points");
    			 }
     
            	if (point <= 15)
    			 {
    			 	question.setText("Try Again!");
    			 }
    			 else if (point >= 15)
    			 {
    			 	question.setText("Congrats!");
    			 }
           	}
        }
    }

    did i put
    	if (point <= 15)
    			 {
    			 	question.setText("Try Again!");
    			 }
    			 else if (point >= 15)
    			 {
    			 	question.setText("Congrats!");
    			 }

    at the wrong place? because when I click on sumbit after the first question, it goes straight to the Try Again/Congrats, it passes all of my codes before that..
    how do i fix it?

  8. #56
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner help please.

    when I click on sumbit after the first question, it goes straight to the Try Again/Congrats, it passes all of my codes
    Try debugging the code by adding some println statements that print out the values of the variables that control the logic and to show the execution flow so you can see what the computer sees and understand why the program does what it does. Keep adding printlns as needed so you can see what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 20th, 2012)

  10. #57
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    can you use println's for JLabels? I thought println's are only for system.in.println

  11. #58
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner help please.

    By println I meant: System.out.println(<put stuff here that is to be printed>);
    These will print on the console.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 20th, 2012)

  13. #59
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    uhm, but i want the thing to be printed as a label.

  14. #60
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner help please.

    For debugging, you should use System.out.println() to print the contents of the programs variables and to show program flow so you can understand what the program is doing and be able to change it to do what you want. You do not want debug output going to labels.

    In the action listener method there is a chain of if/else if statements.
    You should add an else at the end of that chain and in that else you should print out some information about why the else is being executed because none of the preceding if statements were true.
    Last edited by Norm; December 20th, 2012 at 10:08 PM. Reason: add ending else
    If you don't understand my answer, don't ignore it, ask a question.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Please help me I'm a beginner
    By alloka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 14th, 2012, 03:23 AM
  2. I am a beginner please help :)
    By mvonb17 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 6th, 2012, 03:44 PM
  3. Beginner
    By codejava in forum Member Introductions
    Replies: 2
    Last Post: August 22nd, 2011, 08:11 AM
  4. Beginner
    By angelo24 in forum Member Introductions
    Replies: 1
    Last Post: August 19th, 2011, 07:14 AM
  5. I need a help ! i am beginner
    By yinky in forum Java Theory & Questions
    Replies: 3
    Last Post: September 30th, 2009, 07:22 AM