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 16 of 16

Thread: Default constructor generating random questions

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Default constructor generating random questions

    Need to create a default constructor that generates a random question, addition or subtraction. And when adding the numbers must be random from 0-12 and when subtracting the first number must be from 6-12, while the second is less than the first number. Here's my progress as of now:


    package project4;
     
    public class Question {
    	private int num1;
    	private int num2;
    	private char operand;
     
    	public Question()
    	{
    		operand = '+';
    		num1 = (int)(Math.random())*12;
    		num2 = (int)(Math.random())*12;
     
     
     
     
    		operand = '-';
    		num1 = ((int)(Math.random())*12+6);
    		num2 = (int)(Math.random()) << num1;
     
    			}
     
     
    	public String toString()
    	{
    		String str = new String(num1 + " " + operand + " " + num2);
    		return str;
    	}
     
     
    }
    Also, when called via the toString method I get: 6-0. Every time I run it.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,370
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Default constructor generating random questions

    How are you trying to debug the code to find the problem? Try adding some println() statements that print the values of variables as they are set and used.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default constructor generating random questions

    I am actually calling my constructor and the toString method in an another application class not in the programmer defined class.

     
    package project4;
     
    public class Project4App
     {
    	public static void main(String[] args)
    {
    	Question question1 = new Question();
    	System.out.println(question1.toString());
     
     
    }
         }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,370
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Default constructor generating random questions

    Find where the toString() method gets the values it returns in the String.
    Then look at where the variables used in the toString() method are given values.
    Then look at the code that puts values in those variables.
    Add some println() statements to print all the values used to compute the values that finally are given to the variables used in the toString() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default constructor generating random questions

    I see where the values are given in, it takes the num1 and num2 values declared in the constructor, what I'm not understanding is why I'm getting 6-0 as my only values and better yet, how do I ensure that my code will produce random equations instead of just a subtraction one.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,370
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Default constructor generating random questions

    I'm not understanding is why I'm getting 6-0
    Try Add some println() statements to print the values of all the variables used to compute the values so you can see what the statements are doing.

    how do I ensure that my code will produce random equations
    Use the Random class or the random method of the Math to generate random numbers for the equations.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default constructor generating random questions

    Okay, the thing is I'm assigning the instance variables in the constructor and the constructor is automatically assigning the value of num1 as 6 and num2 as 0. Just browse my initial code to tell me what I may be missing, because I've already used the .random method. Also, I created a method to call the instance variables themselves, and it didn't work out.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,370
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Default constructor generating random questions

    The posted code does not compile without errors. You need to fix the compiler errors in the posted code. I can not execute the code because of the errors.

    If you are having problems fixing the compiler's errors, copy the full text and paste it here.

    tell me what I may be missing
    That's what I trying to get you to find by asking you to add some println() methods that will should you what the problem is and help you find what is missing.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default constructor generating random questions

    Ok, I altered the Question class I posted, it should be able to compile without any problems. If not I'll paste the entire text. And I attempted to see what was wrong, the only problem I see right now is that my instance variables' values are being replaced at the end of the constructor with the numbers 6 and 0, instead of random numbers.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,370
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Default constructor generating random questions

    problem I see right now is that my instance variables' values are being replaced at the end of the constructor with the numbers 6 and 0
    What values are the instance variables given before their values are replaced with 6 and 0?

    A problem I see is this compound expression: ((int)(Math.random())*12+6);
    Needs to be separated into several single, simple steps and the results of each step should be printed so you see what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default constructor generating random questions

    Okay I fixed the problem with the variables. So far I have created the equation to add the two numbers, not subtraction. Also, I don't understand how I can tell my program to choose a random question.

     
    	public Question()
    	{
    		operand = '+';
    		num1 = (int)(Math.random()*12);
    		num2 = (int)(Math.random()*12);
    	}

    That's just the top part after I changed it and it now gives me a random value for num1 + a random value for num2

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,370
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Default constructor generating random questions

    how I can tell my program to choose a random question.
    If the questions were in an array, get a random index to get the question from the array.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default constructor generating random questions

    We aren't allowed to use arrays, I was thinking maybe a for loop but I don't how I'd go about that.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,370
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Default constructor generating random questions

    Can you use if/else if or switch statements? Generate an int in the range 0-n and use one of those statement to select the statements to execute.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default constructor generating random questions

    We can use if/else statements, but how would I go about doing that in the constructor?

    --- Update ---

    Just so you know, I've only been programming for 2 months, so feel free to elaborate as much as you possibly can, lol.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,370
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Default constructor generating random questions

    create a random number in range 0-n
    the use if/else if statements to test it: ==0 or ==1 or ==2 ... or ==n
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Only uses Default Constructor
    By ZBixby in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 17th, 2013, 01:42 PM
  2. add a default constructor in abstract class
    By Kifli in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 8th, 2012, 12:42 AM
  3. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  4. [SOLVED] Class constructor default values
    By srs in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2010, 08:51 PM
  5. Generating random numbers
    By abelned in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2010, 07:24 AM