I am in desperate need for help with a Java Program I need to compete for school! I have been working on this for at least 3 hours for the past 3 days and I can not figure it out.... =( Here 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
*/
So, the problem I am having is with the last part about running the loop 1,000 times and then displaying the largest and smallest number out of the 1,000 numbers. And also displaying the mean of the 1,000 numbers. Here is My 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;
int min = 11;
int 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 = 1 + (int) Math.random();
} else {
System.out.println("Min: " + min);
}
if (randomInt > max) {
max = 1 + (int) 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);
}
}
And when I run the code in Eclipse this is the 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.1824716748725944
Min value:1
Max Value:1
Average:0.008
Thanks in advanced!
Aucun commentaire:
Enregistrer un commentaire