23.8k views
25 votes
In this program we are going to practice using the Math class by computing some important values on the unit circle. Starting at 0 and going up by

PI/4 radians (or 45 degrees), print out information of the format below.
Radians: (cos, sin)
0.0: 1.0, 0.0
0.79: 0.7, 0.71
1.57: 0.0, 1.0
2.36: -0.71, 0.7
3.14: -1.0, 0.0
3.93: -0.7, -0.71
4.71: 0.0, -1.0
5.5: 0.71, -0.71
Hints:
You’ll need to use the Math.sin, Math.cos methods
and the Math.PI constant!
You’ll also need to loop from 0 to 2*PI
You can round a decimal to 2 decimal places by multiplying by 100, rounding to the nearest int, and then dividing by 100. Here’s an example:
double value = 0.431675;
value = value * 100; // 43.1675
value = Math.round(value) // 43.0
value = value / 100.0; // 0.43
// Or put it all on one line:
value = Math.round(value * 100) / 100.0;

User Omatt
by
4.3k points

1 Answer

1 vote

Answer:

The program in Java is:

import java.lang.*;

public class MyClass {

public static void main(String args[]) {

for(int i= 0;i<360;i+=45){

double angle = Math.round(Math.toRadians(i)*100.0)/100.0;

double cosX = Math.round(Math.cos(angle)*100.0)/100.0;

double sinX = Math.round(Math.sin(angle)*100.0)/100.0;

System.out.println(angle+": "+cosX +" "+ sinX);

}

}

}

Step-by-step explanation:

This line iterates through the angles from 0 to 360 or 0 to 2*Pi

for(int i= 0;i<360;i+=45){

This calculates the angle to radians

double angle = Math.round(Math.toRadians(i)*100.0)/100.0;

This calculats the cosine of the angle rounded to 2 decimal place

double cosX = Math.round(Math.cos(angle)*100.0)/100.0;

This calculats the sine of the angle rounded to 2 decimal place

double sinX = Math.round(Math.sin(angle)*100.0)/100.0;

This prints the required output

System.out.println(angle+": "+cosX +" "+ sinX);

}

User Piyush Zalani
by
5.0k points