Polymorphism and Random Numbers

Im having issues correctly using random numbers in this program.All I want to do is: generate 4 random ints,and 3 random operators,then prints the results in a toString() method

import java.util.Random;

publicclass TestArithmetic2{

// evaluate (int / int) - (int * int)) = result

//((8.0 / 8.0) - (2.0 * 5.0)) = -10.0

publicstaticvoid main(String[] args){

Node n =new RandomOps(

new RandomOps(

new RandomConst(),new RandomConst()),

new RandomOps(new RandomConst(),new RandomConst()));

public Character RandomOperator(){

Random random =new Random();

//char array with characters

char[] chars =newchar[]{'+','-','*','/'};

//Generate a random number

int r = randomChar.nextInt(chars.length);

//Get char at random index

char randomOps = chars[r];

}

publicint RandomConstant(){

int randomConst = 1 + (int)(Math.random() * 20);

}

public String toString(){

return"(" +"(" + lChild.eval() +"-" + rChild.eval() +")" +")" +"=" n.eval();

}

System.out.println(""+ n.eval());

}

}

[2767 byte] By [exl5a] at [2007-11-27 8:48:31]
# 1

can you display an SSCCE of your problem? I'm having trouble understanding your code because much of it doesn't exist in this post. I'm not asking you to add more info per se, just delete anything extraneous and add anything lacking that will prevent your published code from compiling.

For info on the SSCCE:

http://homepage1.nifty.com/algafield/sscce.html

petes1234a at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 2

Node n = new RandomOps(

new RandomOps(

new RandomConst(), new RandomConst()),

new RandomOps(new RandomConst(), new RandomConst()));

Words fail me.

floundera at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 3

Opps.Sorry.Here's everything that should help it compile.

public class Node {

public Node() {}

public double eval() {

System.out.println("Error: eval Node");

return 0;

}

}

import java.util.Random;

public class TestArithmetic2 {

// evaluate (int / int) - (int * int)) = result

//((8.0 / 8.0) - (2.0 * 5.0)) = -10.0

public static void main(String[] args) {

Node n = new RandomOps(

new RandomOps(

new RandomConst(), new RandomConst()),

new RandomOps(new RandomConst(), new RandomConst()));

public Character RandomOperator(){

Random random = new Random();

//char array with characters

char[] chars = new char[]{'+','-','*','/'};

//Generate a random number

int r = randomChar.nextInt(chars.length);

//Get char at random index

char randomOps = chars[r];

}

public int RandomConstant(){

int randomConst = 1 + (int)(Math.random() * 20);

}

public String toString(){

return "(" + "(" + lChild.eval() + "-" + rChild.eval() + ")" + ")" + "=" n.eval();

}

System.out.println(""+ n.eval());

}

}

public class Binop extends Node {

protected Node lChild, rChild;

public Binop(Node l, Node r) {

lChild = l; rChild = r;

}

}

exl5a at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 4

> [code]

> Node n = new RandomOps(

> new RandomOps(

> new RandomConst(), new RandomConst()),

> new RandomOps(new RandomConst(), new

> RandomConst()));

> /code]

> Words fail me.

This above represents what is below basically:

// ( (8.0 / 8.0) - (2.0 * 5.0)) = -10.0

Not quite sure by what you mean by "words fail me".

exl5a at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 5
> Not quite sure by what you mean by "words fail me".I mean, that's some ugly code.
floundera at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 6
lol...ohh thanks :-). With a little bit of hard work,some day,some day,it would be some beautiful code.Lets talk then.Anyway,It seems that Im going about this the hard way,so any clear and good direction,would be greatly appreciated.
exl5a at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 7

Ok,let me take a step back. Cause It seems like Im confusing everyone.Here's what Im trying to do:

I want to generate ints randomly from 1-20

I want to generate operators(+,-,*,/) randomly

I want to print the result using a toString method.

Here's my skeleton:

Any help on this is greatly appreciated, sorry for the confusion..

public class TestArithmetic {

// evaluate (1.1 + 2.2) + 3.3

public static void main(String[] args) {

Node n = new Plus(

new Plus(

new Const(1.1), new Const(2.2)),

new Const(3.3));

System.out.println(""+ n.eval());

}

// Random Operators method{

}

// Random Constants method{

}

// toString method

public String toString(){

}

}

exl5a at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 8

I don't understand why you have all these other classes. Are they part of the requirements?

All you need is a Random object to generate your numbers. If you place your operators in an array and use the same Random object to generate numbers from 0-3 inclusive then you can access the operator at that position in the array.

Once you have got everything (or while you are getting them) chuck your values and operators into a string.

floundera at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 9
Thats why I backed out a bit cause it seems like I was doing more than I needed to.I dont think I need those classes,but I will try your suggestions and see what happens. Thanks.
exl5a at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 10
Help me please.!!! Anyone with some good guidance or ideas on how to proceed here?
exl5a at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 11
You were given a suggestion.You said you were going to try it.What have you done along those lines, and what problems are you having now?
jverda at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 12

Yes,Ive tried those suggestions.Here's what I got.Im having issues with the return type for the Operators in the Operators method().

Also,Im thinking that I may need a for loop to generate 4 ints,and 3 ops at the same time.

Here's what I got so far:

import java.util.Random;

public class TestArithmetic2 {

// evaluate (int / int) - (int * int)) = result

//((8.0 / 8.0) - (2.0 * 5.0)) = -10.0

public static void main(String[] args) {

public Character RandomOperator(){

Random random = new Random();

//char array with characters

char[] chars = new char[]{'+','-','*','/'};

//Generate a random number from 0-3

int r = randomChar.nextInt(chars.length) * 3;

//Get char at random index

char randomOps = chars[r];

}

public int RandomConstant(){

int randomConst = 1 + (int)(Math.random() * 20);

}

public String toString(){

return "(" + "(" + lChild.eval() + "-" + rChild.eval() + ")" + ")" + "=" n.eval();

}

System.out.println(""+ n.eval());

}

}

exl5a at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 13
> Here's what I got.Im having issues with the return> type for the Operators in the Operators method().Again, provide details. What issues are you having? What exact error message--copy/paste the complete, exact message--and on which line?
jverda at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 14

The error message:

TestArithmetic2.java:9: illegal start of expression

public Character RandomOperator()

^

1 error

I have provided all the details,aint no more. Im trying to produce random ints( 4 of them),with random operators(3 of them) just as I explained in the previous posts. While implementing some of the suggestions,I run into few problems here and there,but I can deal with that.What Im not quite certain about is whether or not im on the right track.Thats it.

exl5a at 2007-7-12 20:55:44 > top of Java-index,Java Essentials,New To Java...
# 15
You cant declare a method within another method in Java. Check your braces.
DarumAa at 2007-7-21 22:47:34 > top of Java-index,Java Essentials,New To Java...