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


Groups > comp.lang.java.help > #2677

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

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.help
Subject Re: Need to create a default constructor that generates 2 random questions. I'm fairly new to programming
Date 2013-04-12 20:26 -0400
Organization A noiseless patient Spider
Message-ID <kka8ij$t61$1@dont-email.me> (permalink)
References <063e6687-49a9-49d6-b7d4-29fd86115329@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web