Discussion:
Need to create a default constructor that generates 2 random questions. I'm fairly new to programming
(too old to reply)
Eric Sosman
2013-04-13 00:26:29 UTC
Permalink
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
***@comcast-dot-net.invalid
Roedy Green
2013-04-14 11:07:00 UTC
Permalink
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
2013-04-14 17:30:21 UTC
Permalink
Post by Roedy Green
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
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
2013-04-14 18:57:13 UTC
Permalink
Post by John B. Matthews
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
2013-04-15 01:48:42 UTC
Permalink
Post by markspace
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.
Post by markspace
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.
Post by markspace
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.
Post by markspace
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
Loading...