This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to use (int)(Math.random()*  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've drawn two trees in Java that stand next to each other that are the same color and height. But I'm trying to code three sets of trees with different widths and get those three sets of trees to generate randomly when I run the code. The way I've coded it makes each triangle change widths at random, but I want the whole tree to be one of three sizes everytime I run and compile. What am I doing wrong?

Scanner console = new Scanner(System.in);

int width = 50* (int)(Math.random()*3+1);
int width2 = 50* (int)(Math.random()*3+1);
int width3 = 50* (int)(Math.random()*3+1);
int width4 = 50* (int)(Math.random()*3+1);
int width5 = 50* (int)(Math.random()*3+1);
int width6 = 50* (int)(Math.random()*3+1);

int height = 50;

Triangle triangle1;
Triangle triangle2;
Triangle triangle3;
Square trunk1;
Square trunk2;

Triangle triangle4;
Triangle triangle5;
Triangle triangle6;
Square trunk3;
Square trunk4;

triangle1 = new Triangle();
triangle1.changeSize(50,140);
triangle1.moveHorizontal(25);
triangle1.moveVertical(70);
triangle1.makeVisible();


triangle2 = new Triangle();
triangle2.changeSize(50,140);
triangle2.moveHorizontal(25);
triangle2.moveVertical(120);
triangle2.makeVisible();

triangle3 = new Triangle();
triangle3.changeSize(50,140);
triangle3.moveHorizontal(25);
triangle3.moveVertical(170);
triangle3.makeVisible();

trunk1 = new Square();
trunk1.changeColor("black");
trunk1.moveVertical(185);
trunk1.makeVisible();

trunk2 = new Square();
trunk2.changeColor("black");
trunk2.moveVertical(200);
trunk2.makeVisible();

triangle4 = new Triangle();
triangle4.changeSize(50,140);
triangle4.moveHorizontal(150);
triangle4.moveVertical(70);
triangle4.makeVisible();

triangle5 = new Triangle();
triangle5.changeSize(50,140);
triangle5.moveHorizontal(150);
triangle5.moveVertical(120);
triangle5.makeVisible();

triangle6 = new Triangle();
triangle6.changeSize(50,140);
triangle6.moveHorizontal(150);
triangle6.moveVertical(170);
triangle6.makeVisible();

trunk3 = new Square();
trunk3.changeColor("black");
trunk3.moveHorizontal(125);
trunk3.moveVertical(185);
trunk3.makeVisible();

trunk4 = new Square();
trunk4.changeColor("black");
trunk4.moveHorizontal(125);
trunk4.moveVertical(200);
trunk4.makeVisible();

System.out.println ("Hello, Welcome to terminal.\n");
System.out.println ("This program will draw two trees next to each other where the widths");
System.out.println ("of the triangles on the trees randomly vary between 50,100, and 150");
}
}
 
Sheriff
Posts: 23706
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Crea Smith wrote:I want the whole tree to be one of three sizes everytime I run



I don't understand this requirement. What is the "size" of a tree?
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not using any of the variables width, width1, width2 etc. so they can't impact anything.
 
Crea Smith
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The widths of the six triangles that make up the trees should be 50, 100, or 150 pixels, but the height should remain a constant 140 pixels. The size of the trunk should keep the same height and width attributes but it has to move around to accomodate the different sizes of the trees.
 
Crea Smith
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted an earlier version of my code mistakenly. Here is the latest one:

 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this code work or not? Please describe what exactly is the problem. Taking this code:



I've run it numerous times, and always get either 50, 100, or 150, which is what I think you want. So can you tell us exactly what is not working?
 
Paul Clapham
Sheriff
Posts: 23706
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Crea Smith wrote:The widths of the six triangles that make up the trees should be 50, 100, or 150 pixels, but the height should remain a constant 140 pixels.



So then "int height = 50" doesn't quite agree with that. But all of this discussion seems kind of futile when we don't know what that code is doing. Is it producing some kind of picture?
 
Marshal
Posts: 60125
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find it easier to import java.util.Random, create a Random object, and call nextInt(3) on that Random object.
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crea Smith, welcome to the Ranch.

Please UseCodeTags when you post source code - that way your code will be much easier to read on the forum.
 
Campbell Ritchie
Marshal
Posts: 60125
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Crea Smith, welcome to the Ranch.

Please UseCodeTags . . .

Sorry, I hadn't notice he was a new member. In that case I'll add some code tags
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!