Objectives: *learn Java syntax *display to the terminal *for loops *static metho
ID: 3815088 • Letter: O
Question
Objectives:
*learn Java syntax
*display to the terminal
*for loops
*static methods with parameters and returns
For this homework, you are going to implement approximations for ex and sin(x). Each of these functions can be approximated with Taylor Series, as follows:
Restrictions
This program cannot use the exp() or sin() method in the Math class, nor use the Math.E constant.
Do not use any if statements in your solution.
Calculation Specification:
The ! operation is known as Factorial. n! is the product of all the positive integers <= n, where n is positive. In otherwords
n! = 1 * 2 * 3 * ... * n
5! = 1 * 2 * 3 * 4 * 5
To calculation ex, you must use a loop and calculate the sum above for the first 15 terms. To obtain maximum numerical range and improve efficiency, you will want to progressively compute each term: don't calculate the 4th term by raising x to the 4th power and dividing by 4! Instead, determine the 4th term based on the value you already have for the 3rd term. In other words, determine how the 4th term differs mathematically from the 3rd term, and perform only those additional operations.
To calculate sin(x), you must use a loop to calculate the sum above for the first 15 terms. Again, progressively compute each term.
Note that the unit for x in sin(x) is in radians, not degrees. Here is a table showing degree to radian equivalencies:
You can read more about radians here http://www.purplemath.com/modules/radians.htm
Display
Your program should create a display similar to this:
Note: We know that the sin of and 2 are both zero. What you see here are accuracy issues with floating point number representation/calculation in computers. You can read more about that here https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
Other Requirements
Your design must use procedural decomposition.
Use class constants where appropriate.
The program must start with main().
Your file must have a program comment at the top. This description, at a minimum, is one to two lines explaining what the program does.
Include your name and date in the file comments.
Each method must include a block comment that describes
what the method does
a description of parameters (if there are any)
what is returned (if anything is returned)
In addition to the block comments, also include algorithm comments, that help to explain your algorithms.
Use good style, such as good variable and method names, class constants, appropriate indentation, etc.
Grading
/15 program runs, and good decomposition
/5 proper comments, style
Good Luck!
degrees radians 90 /2 180 270 3/2 360 2Explanation / Answer
Program:
public class Calculations {
public static void main(String str[]){
System.out.println("x e^x");
System.out.println("= ===");
for(int x=-3;x<=3;x++){
System.out.println(x+" "+Math.exp(x));
}
System.out.println(" ");
System.out.println("x sin(x)");
System.out.println("= ======");
for(int x=1;x<=4;x++){
System.out.println(x+"PI/2 "+Math.sin(x*Math.PI/2));
}
}
}
Result:
x e^x
= ===
-3 0.049787068367863944
-2 0.1353352832366127
-1 0.36787944117144233
0 1.0
1 2.718281828459045
2 7.38905609893065
3 20.085536923187668
x sin(x)
= ======
1PI/2 1.0
2PI/2 1.2246467991473532E-16
3PI/2 -1.0
4PI/2 -2.4492935982947064E-16
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.