Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need to create a default constructor that generates 2 random questions. I'm fairly new to programming

21 views
Skip to first unread message

sunny...@gmail.com

unread,
Apr 12, 2013, 7:11:33 PM4/12/13
to
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;
int addition = num1 + num2;
invalid = addition;


operand = '-';
num1 = ((int)(Math.random())*12+6);
num2 = (int)(Math.random()) << num1;
int subtraction = num1 - num2;
negative = subtraction;
}

public String toString()
{
String str = new String(num1 + " " + operand + " " + num2);
return str;
}

}

Eric Sosman

unread,
Apr 12, 2013, 8:26:29 PM4/12/13
to
On 4/12/2013 7:11 PM, sunny...@gmail.com wrote:
> 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 = '+';

This would usually be called an "operator," not an "operand."

> num1 = (int)(Math.random())*12;

Look carefully at this line (and at other similar constructs
elsewhere). What, exactly, does it do?

- It calls the Math.random() method, producing a `double'
value greater than or equal to 0.0 and strictly less
than 1.0. Let's call that value 0.xxxxx.

- It converts that value to an `int'. The conversion discards
any fractional part, so 0.xxxxx converts to 0.

- It then multiplies 0 by 12, producing 0.

In short, no matter what value Math.random() produces, this line
sets num1 to zero. Similar constructs elsewhere have the same
kind of problem. Study your placement of parentheses, and see
whether some adjustments might be called for.

> num2 = (int)(Math.random())*12;
> int addition = num1 + num2;
> invalid = addition;
>
>
> operand = '-';
> num1 = ((int)(Math.random())*12+6);
> num2 = (int)(Math.random()) << num1;
> int subtraction = num1 - num2;
> negative = subtraction;
> }
>
> public String toString()
> {
> String str = new String(num1 + " " + operand + " " + num2);
> return str;
> }
>
> }
>


--
Eric Sosman
eso...@comcast-dot-net.invalid

Roedy Green

unread,
Apr 14, 2013, 7:07:00 AM4/14/13
to
On Fri, 12 Apr 2013 16:11:33 -0700 (PDT), sunny...@gmail.com wrote,
quoted or indirectly quoted someone who said :

>Need to create a default constructor that generates a random question, addi=
>tion or subtraction. And when adding the numbers must be random from 0-12 a=

That is not a problem any sane human would need to solve, especially
the weird constructor restriction.

see http://mindprod.com/jgloss/homework.html
--
Roedy Green Canadian Mind Products http://mindprod.com
Computer programming is the best remedy for pain (physical or emotional)
I have encountered. It requires so much concentration there is nothing left
over to pay attention to the pain. They should teach this in AA.

John B. Matthews

unread,
Apr 14, 2013, 1:30:21 PM4/14/13
to
In article <qc3lm85qcq7usmh1p...@4ax.com>,
Roedy Green <see_w...@mindprod.com.invalid> wrote:

> On Fri, 12 Apr 2013 16:11:33 -0700 (PDT), sunny...@gmail.com wrote,
> quoted or indirectly quoted someone who said :
>
> > 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:
>
> That is not a problem any sane human would need to solve,
> especially the weird constructor restriction.
>
> see http://mindprod.com/jgloss/homework.html

I'm guessing that the goal of the exercise is to generate random
addition and subtraction problems for homework or testing. Instead
of a constructor, perhaps a static factory method might be in
order:

<http://mindprod.com/jgloss/factorymethod.html>

<http://www.drdobbs.com/jvm/creating-and-destroying-java-objects-par/208403883>

An acquaintance was always amused when her online math homework
posed an exact duplicate. Because the problem space is small, it
might be possible to construct a complete List<Problem> and use
Collections.sort() to avoid repetition.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

markspace

unread,
Apr 14, 2013, 2:57:13 PM4/14/13
to
On 4/14/2013 10:30 AM, John B. Matthews wrote:

> I'm guessing that the goal of the exercise is to generate random
> addition and subtraction problems for homework or testing. Instead
> of a constructor, perhaps a static factory method might be in
> order

I think using a ctor for the OP's problem is fine. We're definitely
getting to the point here where there's a surfeit of advice. I object
to file I/O in a ctor, it's gauche. Almost anything else is fine, as
long as you adhere to best practice and avoid things like this-escape
and so forth. The OP's ctor is probably about 7 lines of code. That's
pretty trivial for a ctor, imo.

The OP's main problem is he doesn't seem to understand how to construct
his if-statement to give him the output he needs. Well, that and a
certain newbishness using the language overall, but that's to be expected.


Lew

unread,
Apr 14, 2013, 9:48:42 PM4/14/13
to
markspace wrote:
> I think using a ctor for the OP's problem is fine. We're definitely

Bad habits begun early are the hardest to break later.

> getting to the point here where there's a surfeit of advice. I object
> to file I/O in a ctor, it's gauche. Almost anything else is fine, as

Constructors are for construction.

Anything that can logically be considered necessary for the object to
be in a "constructed" state is fair game, but certain things (like I/O)
incur extra responsibility. So it's wise to design your types so that
weird stuff is not needed to establish the "constructed" state.

> long as you adhere to best practice and avoid things like this-escape
> and so forth. The OP's ctor is probably about 7 lines of code. That's
> pretty trivial for a ctor, imo.

Maybe trivial, but short, easy things are the best for starting good
practices.

> The OP's main problem is he doesn't seem to understand how to construct
> his if-statement to give him the output he needs. Well, that and a
> certain newbishness using the language overall, but that's to be expected.

--
Lew
0 new messages