Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2676 > unrolled thread

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

Started bysunnyaleem@gmail.com
First post2013-04-12 16:11 -0700
Last post2013-04-14 18:48 -0700
Articles 6 — 6 participants

Back to article view | Back to comp.lang.java.help


Contents

  Need to create a default constructor that generates 2 random questions. I'm fairly new to programming sunnyaleem@gmail.com - 2013-04-12 16:11 -0700
    Re: Need to create a default constructor that generates 2 random questions. I'm fairly new to programming Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-04-12 20:26 -0400
    Re: Need to create a default constructor that generates 2 random questions. I'm fairly new to programming Roedy Green <see_website@mindprod.com.invalid> - 2013-04-14 04:07 -0700
      Re: Need to create a default constructor that generates 2 random questions. I'm fairly new to programming "John B. Matthews" <nospam@nospam.invalid> - 2013-04-14 13:30 -0400
        Re: Need to create a default constructor that generates 2 random questions. I'm fairly new to programming markspace <markspace@nospam.nospam> - 2013-04-14 11:57 -0700
          Re: Need to create a default constructor that generates 2 random questions. I'm fairly new to programming Lew <lewbloch@gmail.com> - 2013-04-14 18:48 -0700

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

Fromsunnyaleem@gmail.com
Date2013-04-12 16:11 -0700
SubjectNeed to create a default constructor that generates 2 random questions. I'm fairly new to programming
Message-ID<063e6687-49a9-49d6-b7d4-29fd86115329@googlegroups.com>
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;
}

}

[toc] | [next] | [standalone]


#2677

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-04-12 20:26 -0400
Message-ID<kka8ij$t61$1@dont-email.me>
In reply to#2676
On 4/12/2013 7:11 PM, sunnyaleem@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
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#2678

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-04-14 04:07 -0700
Message-ID<qc3lm85qcq7usmh1ph9an44j2mskps43o1@4ax.com>
In reply to#2676
On Fri, 12 Apr 2013 16:11:33 -0700 (PDT), sunnyaleem@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.

[toc] | [prev] | [next] | [standalone]


#2679

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-04-14 13:30 -0400
Message-ID<nospam-559D38.13302014042013@news.aioe.org>
In reply to#2678
In article <qc3lm85qcq7usmh1ph9an44j2mskps43o1@4ax.com>,
 Roedy Green <see_website@mindprod.com.invalid> wrote:

> On Fri, 12 Apr 2013 16:11:33 -0700 (PDT), sunnyaleem@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>

[toc] | [prev] | [next] | [standalone]


#2680

Frommarkspace <markspace@nospam.nospam>
Date2013-04-14 11:57 -0700
Message-ID<kkeu13$l7i$1@dont-email.me>
In reply to#2679
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.

[toc] | [prev] | [next] | [standalone]


#2681

FromLew <lewbloch@gmail.com>
Date2013-04-14 18:48 -0700
Message-ID<aee3a436-3a9b-4631-9c12-5838ed707b96@googlegroups.com>
In reply to#2680
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web