Need help with following Java method: import java.util.Scanner; import java.lang.Math; public class CosineFromScratch { /* The following code demonstrates how to estimate the cosine of a number. Your assignment is to migrate the calculation of the cosine into a new static method named cosine. The method should take one double input and return one double output. Make sure to call the method from main and set the variable estimate equal to the returned value. */ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("\nGive me a number: "); double x = input.nextDouble(); //Trying to find cosine of this number double correct_cos = Math.cos(x); //START HERE: The following code goes into //the method. //Source for the following: //https://austinhenley.com/blog/cosine.html double num_terms = 10; int div = (int)(x / Math.PI); x = x - (div * Math.PI); int sign = -1; double estimate = 1.0; double inter = 1.0; double num = x * x; for (int i = 1; i

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%
Need help with following Java method: import java.util.Scanner; import java.lang.Math; public class CosineFromScratch { /* The following code demonstrates how to estimate the cosine of a number. Your assignment is to migrate the calculation of the cosine into a new static method named cosine. The method should take one double input and return one double output. Make sure to call the method from main and set the variable estimate equal to the returned value. */ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("\nGive me a number: "); double x = input.nextDouble(); //Trying to find cosine of this number double correct_cos = Math.cos(x); //START HERE: The following code goes into //the method. //Source for the following: //https://austinhenley.com/blog/cosine.html double num_terms = 10; int div = (int)(x / Math.PI); x = x - (div * Math.PI); int sign = -1; double estimate = 1.0; double inter = 1.0; double num = x * x; for (int i = 1; i
### Estimating the Cosine of a Number Using Java

This guide demonstrates how to estimate the cosine of a number programmatically in Java. The code provided below gives a step-by-step approach for computing the cosine using a series expansion.

#### Task Overview
You are tasked with migrating the given cosine calculation into a new static method named `cosine`. This method should accept a single double input and return the estimated cosine value as a double output.

Make sure to call this new method from the `main` method of your program and assign the returned value to the variable `estimate`.

#### Java Code Example

```java
import java.util.Scanner;
import java.lang.Math;

public class CosineFromScratch
{
    /*
    The following code demonstrates how to estimate the cosine of a number.

    Your assignment is to migrate the calculation of the cosine into a new static method named cosine. The method should take one double input and return one double output.

    Make sure to call the method from main and set the variable estimate equal to the returned value.
    */
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("\nGive me a number: ");
        double x = input.nextDouble(); // Trying to find cosine of this number

        double correct_cos = Math.cos(x);

        // START HERE: The following code goes into the method.
        // Source for the following: https://austinhenley.com/blog/cosine.html
        double num_terms = 10;
        int div = (int)(x / Math.PI);
        x = x - (div * Math.PI);
        int sign = -1;
        double estimate = 1.0;
        double inter = 1.0;
        double num = x * x;
        for (int i = 1; i <= num_terms; i++)
        {
            double comp = 2.0 * i;
            double den = comp * (comp - 1.0);
            inter *= num / den;
            estimate += inter * sign;
            sign *= -1; // flip the sign
        }
        // If div is odd, flip the estimate
        if (div % 2 != 0) {
            estimate = estimate * -1;
        }
        // STOP HERE
    }
}
```

### Explanation

1. **Input Handling:** The program begins by importing the necessary Java utilities and
Transcribed Image Text:### Estimating the Cosine of a Number Using Java This guide demonstrates how to estimate the cosine of a number programmatically in Java. The code provided below gives a step-by-step approach for computing the cosine using a series expansion. #### Task Overview You are tasked with migrating the given cosine calculation into a new static method named `cosine`. This method should accept a single double input and return the estimated cosine value as a double output. Make sure to call this new method from the `main` method of your program and assign the returned value to the variable `estimate`. #### Java Code Example ```java import java.util.Scanner; import java.lang.Math; public class CosineFromScratch { /* The following code demonstrates how to estimate the cosine of a number. Your assignment is to migrate the calculation of the cosine into a new static method named cosine. The method should take one double input and return one double output. Make sure to call the method from main and set the variable estimate equal to the returned value. */ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("\nGive me a number: "); double x = input.nextDouble(); // Trying to find cosine of this number double correct_cos = Math.cos(x); // START HERE: The following code goes into the method. // Source for the following: https://austinhenley.com/blog/cosine.html double num_terms = 10; int div = (int)(x / Math.PI); x = x - (div * Math.PI); int sign = -1; double estimate = 1.0; double inter = 1.0; double num = x * x; for (int i = 1; i <= num_terms; i++) { double comp = 2.0 * i; double den = comp * (comp - 1.0); inter *= num / den; estimate += inter * sign; sign *= -1; // flip the sign } // If div is odd, flip the estimate if (div % 2 != 0) { estimate = estimate * -1; } // STOP HERE } } ``` ### Explanation 1. **Input Handling:** The program begins by importing the necessary Java utilities and
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY