

InterviewSolution
Saved Bookmarks
1. |
Solve : Help with Java Random Class for Double values 0-100? |
Answer» Hello, I am working on a class assignment for my Computer Science class I have been working on it this week and it is due Sunday and I am not able to discuss this with my instructor. This is part of a larger assignment and this is the part I am questioning. The instructions say: The cost of a given ROCKETSHIP will be in billions of space credits. The number should be displayed using a DecimalFormat object. Print the number of billions of space credits just like you would print dollars and cents. (For instance, 5.75 billion space credits.) The price of a rocketship will be between 3 and 20 billion space credits. To determine the price of a given rocketship, generate a random double between 0 and 100. If this number is less than 3.0, REPLACE its value with 3.0. If the randomly-generated number is greater than 20.0, replace its value with 20.0. Then use whatever number you arrive at (between 3.0 and 20.0) as the rocketship’s price. Here is the output I get: "Rocketship "Bob" costs 20.00 billion space credits." Here is sample output: "Rocketship "Ohio" costs 14.71 billion space credit" "Rocketship "Illinois" costs 20.00 billion space credit" I'm not SURE if this correct way to do this assignment Here is the code I wrote: //Randomly give the cost of the ships from a vaule of 0 to 100 costRandom = randomNumbers.nextInt(101); //If this number is less than 3.0, replace its value with 3.0. if (costRandom<3.0) { costRandom=3.0; } //If the number is greater than 20.0, replace its value with 20.0. else if (costRandom>20.0) { costRandom=20.0; } I tried: costRandom = randomNumbers.nextDouble(101); and it said there needs to be no arguments The problem is it only seems to print out just INTEGER values and not doubles. I understand if you feel you shouldn't help because it a school assignment. Let me know if you need to see the full code to understand. Thank you for any help you can give. This is Java? Quote randomNumbers.nextDoubleLooks like NET 4 instead. But in Java one would see Quote Random rand = new Random();Early in the code. Did I miss something?I am new to Java and may have made a mistake. I will show what I did. // Create a Random object. Random randomNumbers = new Random(); // Create a DecimalFormat object. DecimalFormat formatter = new DecimalFormat("0.00"); double costRandom; //Holds the cost of the ships //Randomly give the cost of the ships from a vaule of 0 to 100 costRandom = randomNumbers.nextInt(101); //If this number is less than 3.0, replace its value with 3.0. if (costRandom<3.0) { costRandom=3.0; } //If the number is greater than 20.0, replace its value with 20.0. else if (costRandom>20.0) { costRandom=20.0; } (earlier in the program I ask the user for a name) //Display Rocketship name ands its cost System.out.println("Rocketship \"" + r.Name +"\" costs " + formatter.format(costRandom) + " billion space credits."); Right now it is outputting a integer value for "costRandom" and I need it to output a double value so it have the ability to give both a integer and two decimal places in the output. I don't know how to turn it into a double.It would be nice to have a thing that souled just convert a integer into a double. I don't know the elegant way to do it, being that I am so lazy and In don't want to do a lot of research, I might just do this. Have an item that already is a double. Call it mydouble get the inter form the random thing 0 - 100 call it myinteger now say something like this: add myinteger to itself 5 times. Use a tmp if you wish. Now say mydouble = myinteger /5 dividing an integer forces a offloading point do-da-do. You have a real number in mydouble. Yeah, I know, not very elegant. First get it to work. Polish it later. Code: [Select]costRandom = (randomNumbers.nextDouble()*17)+3; Edit, Oh, I see they don't want you to do that. Code: [Select]costRandom = (randomNumbers.nextDouble()*100); if(costRandom > 20) costRandom =20; else if(costRandom < 3) costRandom=3; Or you want to be unnecessarily terse... Code: [Select]costRandom = (randomNumbers.nextDouble()*100); costRandom = costRandom > 20 ? 20 : costRandom < 3 ? 3 : costRandom; Quote from: Geek-9pm on October 22, 2011, 12:52:39 AM It would be nice to have a thing that souled just convert a integer into a double. Code: [Select]double doublevalue = (int)intvalue; Quote mydouble = myinteger /5As you said, you didn't do any research. the division in that expression would be integer division. dividing by 5.0 would force the literal to be a double, I think, thus making the division a floating point operation.Thanks, BC It is very hard for me to do research. I just use what I can.Thank you very much BC. The code you suggested below works great for what I needed to do. costRandom = (randomNumbers.nextDouble()*100); if(costRandom > 20) costRandom =20; else if(costRandom < 3) costRandom=3; |
|