Java Programming Help! Will post question and code below. =)
class MathMethodsProgram { public static void main(String[] args) { // Prints the pow method. System.out.println("Math.pow(2.0, 3.0) = " + Math.pow(2.0, 3.0)); // Prints the sqrt method System.out.println("Math.sqrt(25) = " + Math.sqrt(25)); // Prints the max number System.out.println("Math.max( 6, 3) = " + Math.max(6, 3)); // Prints the max number System.out.println("Math.max(-50.0,7.0) = " + Math.max(-50.0, 7.0)); // Prints a random number System.out.println("Math.random(); = " + Math.random()); randomStudy(); } public static void randomStudy(){ int total = 0; double min = 11; double max = -1; for(int i = 0; i < 1000; i++){ int randomInt = (int)(11.0 * Math.random()); System.out.println("Random integer between 1 and 10: " + randomInt); int newtotal = (randomInt + total); if (newtotal < min) { min = Math.random(); } else { System.out.println("Min: " + min); } if (newtotal > max) { max = Math.random(); } else { System.out.println("Max: " + max); } } System.out.println("Min value:" + min); System.out.println("Max Value:" + max); System.out.println("Average:"+ newtotal / 1000d); } }
So whats your question? I have basic Java, I could help you :)
Add a method to your program called randomStudy that has no parameters and returns no value. In this method, do the following: A. Declare three int variables: total, min and max. Set total to 0, min to 11, and max to -1. B. Create a loop that will run 1,000 times. In the body of the loop, generate a random int value between 1 and 10, inclusive. Add this number to your total. If this number is less that min, update min with the new number. If it is greater than max, update max with the new number. C. After the look display the following: Min value: x Max value: y Average: z Replace x and y with you min and max values. Calculate z by dividing your total by 1000d.
So you want me to check if your code meets your question?
In my program in eclipse, the last line with code: System.out.println("Average:"+ newtotal / 1000d); , it does not recognize newtotal as a variable. But I defines the variable in the for loop. Also when I run it I get: Math.sqrt(25) = 5.0 Math.max( 6, 3) = 6 Math.max(-50.0,7.0) = 7.0 Math.random(); = 0.7894687499703334 Exception in thread "main" java.lang.Error: Unresolved compilation problem: newtotal cannot be resolved to a variable at MathMethodsProgram.randomStudy(MathMethodsProgram.java:50) at MathMethodsProgram.main(MathMethodsProgram.java:19)
Hmmm...I don't really know about loops. Let me check a little more. Oh @SapphireMoon, can you help?
Thank you very much! This was due yesterday so before I start the rest of my classes I was going to get this done!
yw
Yep, let me see, @NvidiaIntely and @BlazeRyder, I took APCS so I know a bit about this.
Thanks! I don't know about loops, I only learned PJS Loops.
Thanks! This is for a Java Programming 1 class, so this is my first class with Java and it gets a little hard at some times. =)
Yeah
And I do see the problem. It's a scope issue. int newtotal is defined within the scope of its for loop, and gets garbage collected after that last bracket, so it doesn't exist when the print statement wants it. I usually declare all of my variables that are going to be used in and out of loops at the top, like maybe int newtotal; somewhere at the beginning so the variable retains its value.
Great job! I did not notice that one.
So I need to put int newtotal = (randomInt + total); where I put the ints before the for loop?
public static void randomStudy(){ int total = 0; double min = 11; double max = -1; int newtotal = (randomInt + total); for(int i = 0; i < 1000; i++){ int randomInt = (int)(11.0 * Math.random()); System.out.println("Random integer between 1 and 10: " + randomInt);
I think so, but ask Luna because I am not sure.
No, just put: int newtotal; there because it won't have randomInt defined properly at that point.
Okay, I guess I am learning too.
So is this what it should look like???? class MathMethodsProgram { public static void main(String[] args) { // Prints the pow method. System.out.println("Math.pow(2.0, 3.0) = " + Math.pow(2.0, 3.0)); // Prints the sqrt method System.out.println("Math.sqrt(25) = " + Math.sqrt(25)); // Prints the max number System.out.println("Math.max( 6, 3) = " + Math.max(6, 3)); // Prints the max number System.out.println("Math.max(-50.0,7.0) = " + Math.max(-50.0, 7.0)); // Prints a random number System.out.println("Math.random(); = " + Math.random()); randomStudy(); } public static void randomStudy(){ int total = 0; double min = 11; double max = -1; int newtotal; for(int i = 0; i < 1000; i++){ int randomInt = (int)(11.0 * Math.random()); System.out.println("Random integer between 1 and 10: " + randomInt); int newtotal = (randomInt + total); if (newtotal < min) { min = Math.random(); } else { System.out.println("Min: " + min); } if (newtotal > max) { max = Math.random(); } else { System.out.println("Max: " + max); } } System.out.println("Min value:" + min); System.out.println("Max Value:" + max); System.out.println("Average:"+ newtotal / 1000d); } }
Take the int out of the second time you use newtotal, double declarations cause errors. That second line should say: newtotal = (randomInt + total);
Yep :)
Its funny that I have Tech Crew, and Luna does not have Source of All Code lol.
@NvidiaIntely I've only been here two weeks, lol. My only title is in Chem.
When I run this code: class MathMethodsProgram { public static void main(String[] args) { // Prints the pow method. System.out.println("Math.pow(2.0, 3.0) = " + Math.pow(2.0, 3.0)); // Prints the sqrt method System.out.println("Math.sqrt(25) = " + Math.sqrt(25)); // Prints the max number System.out.println("Math.max( 6, 3) = " + Math.max(6, 3)); // Prints the max number System.out.println("Math.max(-50.0,7.0) = " + Math.max(-50.0, 7.0)); // Prints a random number System.out.println("Math.random(); = " + Math.random()); randomStudy(); } public static void randomStudy(){ int total = 0; double min = 11; double max = -1; int newtotal = 0; for(int i = 0; i < 1000; i++){ int randomInt = (int)(11.0 * Math.random()); System.out.println("Random integer between 1 and 10: " + randomInt); newtotal = (randomInt + total); if (newtotal < min) { min = Math.random(); } else { System.out.println("Min: " + min); } if (newtotal > max) { max = Math.random(); } else { System.out.println("Max: " + max); } } System.out.println("Min value:" + min); System.out.println("Max Value:" + max); System.out.println("Average:"+ newtotal / 1000d); } } ; I get this output: Math.pow(2.0, 3.0) = 8.0 Math.sqrt(25) = 5.0 Math.max( 6, 3) = 6 Math.max(-50.0,7.0) = 7.0 Math.random(); = 0.8173042410773685 Random integer between 1 and 10: 6 Random integer between 1 and 10: 7 Min: 0.97248952155768 Random integer between 1 and 10: 6 Min: 0.97248952155768 Random integer between 1 and 10: 0 But the thing is, the Random integer between 1 and 10 along with min runs probably 1000 times........
@SapphireMoon
@BlazeRyder b/c you told it to... Your for loop goes from 0 to 1000, so it's going to run 1000 times...
xD!! That's what was thinking! Anyways, that's what you told it to do Blaze, so what's the problem?
Open up the word doc to see what my teacher wants as the the output. =)
I can't download that now :(
I am so sorry, I should have told you guys that!
Why?
Because my internet is overloaded right now.
Let me type it out. One sec
Ok
I have Word Online so I can see it. The first one is wrong because you're doing 2^3 instead of 3^2 (giving you 8 instead of 9). For the part with the randoms, for a random int we usually do (int) (Math.random()*max + max - min + 1).
/* Results: Math.pow(2.0 , 3.0) = 8 Math.sqrt(25.0) = 5.0 Math.max(6,2) = 6 Math.max(-50.0 , 7.0) = 7.0 Math.random() = _A randmom number_ Min value: Random number between 1 and 10 Max value: Random number between 1 and 10 Average: Average for the 1,000 numbers created in the loop */
Luna said it right, try that.
And where do I put that in my code?
Hang on, I might be confused. Can you comment your code for us? Give us a line by line of what your code is supposed to do so we can see if it's conceptual or code related problem (and understand what to do to fix it).
These are the directions: Write a program that calls each of the methods of the Math class from the list below and provides output showing the method called, values sent to the method, and the returned results. Each listed method will tell you which values to use. For example: Listed method: double pow(double a, double b): use 2.0 and 3.0. Your program would display: Math.pow(2.0, 3.0) = 8.0. When calling a method that accepts doubles, use numbers with at least one decimal digit like the example above, even if it's a zero. Remember that numbers with decimals are double literals. Try to use values that will produce easily verifiable results. Here is the list: double pow(double a, double b): Use 3.0 and 2.0 double sqrt(double x): Use 25.0 int max(int a, int b): Use 6 and 2 double max(double a, double b): Use -50.0 and 7.0 static double random() Sample output: Math.pow(3.0, 2.0) = 9.0 Math.sqrt(25.0)=5.0 Math.max(6,2)=6 Math.max(-50.0,7.0)=7.0 Math.random()= -random value- Test your program with the values shown in the examples, fix any errors. Add a method to your program called randomStudy that has no parameters and returns no value. In this method, do the following: a. Declare three int variables: total, min, and max. Set total to 0, min to 11, andmax to -1. b. Create a loop that will run 1,000 times. In the body of the loop, generate a random int value between 1 and 10, inclusive. Add this number to your total. If this number is less than min, update min with the new number. If it is greater than max, update max with the new number. c. After the loop, display the following: Min value: x Max value: y Average: z Replace x and y with your min and max values. Calculate z by dividing your total by 1000d. Call your new randomStudy method from the main method. Highlight and copy the output shown in Eclipse. Paste the output at the bottom of your source code file. Add the text Results:above your output, and then comment out the text your just dded plus the output text. Your commented results should look something like this: /* Results: Math.pow(3.0, 2.0)= 9.0 Math.sqrt(25.0) = 5.0 Math.max(6, 2) = 6 Math.max(-50.0, 7.0) = 7.0 Math.random() = -random number- Min value: 1 Max value: 10 Average: 5.553 */
Here is the code I have now: class MathMethodsProgram { public static void main(String[] args) { // Prints the pow method. System.out.println("Math.pow(2.0, 3.0) = " + Math.pow(2.0, 3.0)); // Prints the sqrt method System.out.println("Math.sqrt(25) = " + Math.sqrt(25)); // Prints the max number System.out.println("Math.max( 6, 3) = " + Math.max(6, 3)); // Prints the max number System.out.println("Math.max(-50.0,7.0) = " + Math.max(-50.0, 7.0)); // Prints a random number System.out.println("Math.random(); = " + Math.random()); randomStudy(); } public static void randomStudy(){ int total = 0; double min = 11; double max = -1; int newtotal = 0; for(int i = 0; i < 1000; i++){ int randomInt = (int)(11.0 * Math.random()); System.out.println("Random integer between 1 and 10: " + randomInt); newtotal = (randomInt + total); if (newtotal < min) { min = Math.random(); } else { System.out.println("Min: " + min); } if (newtotal > max) { max = Math.random(); } else { System.out.println("Max: " + max); } } System.out.println("Min value:" + min); System.out.println("Max Value:" + max); System.out.println("Average:"+ newtotal / 1000d); } }
Sorry, I really do not know this stuff.
I am just learned ProcessingJavaScript.
That's okay! @NvidiaIntely
I gave it a shot.
I see it now. Instead of checking newtotal against min and max, check randomInt against min and max. That's the actual random number generated, newtotal is just the place you're storing the total so you can average it. @BlazeRyder @NvidiaIntely Never even heard of that, where'd you learn it? I took Java with Edhesive.
I gtg now, cya!
I learned it here: https://www.khanacademy.org/computing/computer-programming @SapphireMoon
Ah, I knew JS already so I didn't even bother with those. @NvidiaIntely
I'm learning it in my High School. Java gets very confusing at times! Alright, so what do I need to fix now? Something about the min and max and newtotal?
@SapphireMoon Is it in the if statements?
HOLD ON! I think I figured it out!
class MathMethodsProgram { public static void main(String[] args) { // Prints the pow method. System.out.println("Math.pow(2.0, 3.0) = " + Math.pow(2.0, 3.0)); // Prints the sqrt method System.out.println("Math.sqrt(25) = " + Math.sqrt(25)); // Prints the max number System.out.println("Math.max( 6, 3) = " + Math.max(6, 3)); // Prints the max number System.out.println("Math.max(-50.0,7.0) = " + Math.max(-50.0, 7.0)); // Prints a random number System.out.println("Math.random(); = " + Math.random()); randomStudy(); } public static void randomStudy(){ int total = 0; double min = 11; double max = -1; int newtotal = 0; int i; int randomInt = 0; for( i = 0; i < 1000; i++){ randomInt = 1 + (int)( Math.random() * 11); newtotal = (randomInt + total); } if (randomInt < min) { min = Math.random(); } else { System.out.println("Min: " + min); } if (randomInt > max) { max = Math.random(); } else { System.out.println("Max: " + max); } System.out.println("Min value:" + min); System.out.println("Max Value:" + max); System.out.println("Average:"+ newtotal / 1000d); } } Output: Math.pow(2.0, 3.0) = 8.0 Math.sqrt(25) = 5.0 Math.max( 6, 3) = 6 Math.max(-50.0,7.0) = 7.0 Math.random(); = 0.008447488934866354 Min value:0.5001060656492547 Max Value:0.8400095249566999 Average:0.009
Is the output correct? If it is you MOST likely got it correct. Ask Luna though anyways.
@SapphireMoon
She's MIA
Just wait a little, she should be back soon.
Will do! Again, thanks for your help! Especially getting SapphireMoon involved!
Sorry guys, was my lunch break and if I work through my lunch breaks I go crazy later on. O.O Looks good though, @BlazeRyder, the only thing is, looks like you need to do something about turning those doubles into ints, multiply them by something to get them into normal decimal places (I'm awful with randoms).
Your code min and max values definitely need to be ints as per the instructions having trouble getting it to let me do that as it gives me a possible lossy conversion from long to int error. Not too familiar with the built in maths methods, I've been messing around with the code, but in the meantime when googling I found this StackOverflow page which might help you. http://stackoverflow.com/questions/27610675/beginners-java-help-needed-using-eclipse
I did notice a little error, you should have used 6 and 2 here: Prints the max number System.out.println("Math.max( 6, 3) = " + Math.max(6, 3));
That is what I am having trouble with. Getting the double turned into ints. But then Eclipse says there is an error and it says change them back into doubles. And I am having trouble with the last the lines of the output. Whatever I do it is not outputting the right thing. And yes, I did fix the 3 to a 2. Thanks =)
Join our real-time social learning platform and learn together with your friends!