-1

Now before i get into the question, i am NOT asking to be spoon fed the code for the program, i just need help on how to do it myself, whether its you explaining it, a link to a video, or some kind of online resource. I am heavily in need of help for my school assignment. Here are the instructions:

"1. 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 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 vales 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()= 0.7131281909476174
  1. Test your program with the values shown in the examples and verify your program produces the same results. (Your random value will be different.) Troubleshoot and fix any errors that you find.

  2. 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 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

  1. Call your new randomStudy method from the main method."

Like i said in the first few sentences, im not looking for someone to just give me the code to copy and paste, i just want to know how to do this, as i am new to java, and just need someone to explain this to me with some more simplified instructions and sample code, or to link me to videos or another resource. I don't have any code yet. Thanks.

Longinus
  • 3
  • 1
  • 5
    if you want to know how to do it: start by reading up your book/course notes, and try. – Stultuske Sep 27 '17 at 07:06
  • 3
    Welcome to SO! I'm afraid this is too broad for SO's format. Assignments aren't usually arbitrary, though; your instructor, tutorial, or course will have covered the necessary topics to make it possible for you to do this. Surely if you review your course materials, class notes, book, etc...? If not, perhaps classmates or the teacher him/herself can help? In any case, good luck with it. – T.J. Crowder Sep 27 '17 at 07:07
  • 2
    To get you started: `System.out.println(String.format("Math.pow(%.1f, %.1f) = %.1f", 3.0, 2.0, Math.pow(3.0, 2.0)));` – Robby Cornelissen Sep 27 '17 at 07:12
  • I'm voting to close this question as off-topic because it looks like a cut & paste homework posting. – duffymo Sep 27 '17 at 11:52
  • "im not looking for someone to just give me the code to copy and paste" and "sample code" are contradictory. Your instructor has given YOU a well written, concise assignment. I'd advise you to buckle down and try it, without help. If you can't do this, you can't learn Java or anything else. – duffymo Sep 27 '17 at 11:54
  • @duffymo I dont think you understand that sample code and code to copy and paste are two different things. SAMPLE as in example of how to use the math class, not a sample of my assignment. If my instructor has given me well written instructions, I cant understand them because I was thrown straight into this with no prior experience because Im new. – Longinus Sep 27 '17 at 19:59

2 Answers2

1

You have to declare a void method, this mean that it doesn't return any value, and it has to be static because you have to call the method from the main method that is also static.

For example: public static void randomStudy();

In this method you have to declare 3 int variables and I think you know how to do that. Then you have to loop 1000 times. In java you can loop with while or for instructions. I suggest to use a for, in my opinion it's easier to use. Hence declare a for loop that goes from 0 to 1000.

for(int i = 0; i < 1000; i++)

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.

I think you should know how to do this part.

At the end you have to print the numbers. You can use System.out.println("");

Here you can find how to generate random numbers in Java.

Hope this can help you understand how to do your assignment.

amicoderozer
  • 2,046
  • 6
  • 28
  • 44
0

Import java.lang.Math so that the functions of the Math class can be called. Then, create function definitions for each function in the list. Declare them as static so that the class doesn't have to be instantiated to be able to call the functions.

import java.lang.Math;

public class Main {
    static double pow(double a, double b) {

    }

    public static void main(String[] args) {

    }
}

Since the result of the Math function needs to be printed out and also returned by your function, it needs to be stored in a local variable. This is more efficient than calling it twice. Pass the parameters of your function to the Math function.

static double pow(double a, double b) {
    double result = Math.pow(a, b);
}

Then, print out the requirements specified by the instructions. Robby Cornelissen gave some code on how to do this. However, use the parameters (a and b in this case) and the result variable instead of calling Math.pow() again. Refer to Java's documentation for more info on String.format().

Then return result.

static double pow(double a, double b) {
    double result = Math.pow(a, b);
    System.out.println("...");

    return result;
}

Implement the rest of the functions in a similar manner. Once you have them all, call them in main() with the arguments specified in the instructions.

public static void main(String[] args) {
    pow(3.0, 2.0);
    sqrt(25.0);
    // Call the rest...
}

See the answer by amicoderozer for the implementation of randomStudy().

bgfvdu3w
  • 1,548
  • 1
  • 12
  • 17