Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: help make trig values tables by using while or for loops

1. ## help make trig values tables by using while or for loops

My assignment is Write a static void method called printTrigTables that prints the sin, cos, and tan of angles
////between 0 and 360 in increments of 30 degrees. The first column should be the angle size,
//and the next three columns should be the sin, cos, and tan. Use either a while loop or a for
//loop. One problem you will encounter however is that the Math functions for sin, cos, and
//tan take radians, not degrees! So, you must convert your angle to radians first. Here is the
//formula for degree to radian conversion:
//radians = degrees x PI / 180.

public static double degConvert(double degrees){ // converts to degrees
double radians = degrees * (Math.PI/180);
}

For the trig table, can anyone help me make a method, or several encapsulated methods that do the following:

Count from 0 to 360 by 30's, using a loop; convert those numbers into radians using the equation x*pi/180; take all of those values and put them into Math.sin(); print.

```public static void printTrigTables () {

double a1, a2, a3,a4,a5, a6, a7,a8,a9,a10,a11,a12;4
System.out.println ("degree, sin, cos, tan");
double degree;
System.out.print("Angle 30 degree ");
a1 = 30 * Math.PI/180;
System.out.println ("cos(" + a1 + ") is " + Math.acos(a1));
System.out.println ("sin(" + a1 + ") is " + Math.asin(a1));
System.out.println("tan(" + a1 + ") is " + Math.atan(a1));
System.out.print("Angle 60 degree ");
a2 = 60 * Math.PI/180;
System.out.println("cos(" + a2 + ") is " + Math.acos(a2)) ;
System.out.println("sin(" + a2 + ") is " + Math.asin(a2));
System.out.println("tan(" + a2 + ") is " + Math.atan(a2));
System.out.print("Angle 90 degree ");
a3 = 90 * Math.PI/180;
System.out.println("cos(" + a3 + ") is " + Math.acos(a3)) ;
System.out.println("sin(" + a3 + ") is " + Math.asin(a3));
System.out.println("tan(" + a3 + ") is " + Math.atan(a3));
System.out.print("Angle 120 degree ");
a4 = 120 * Math.PI/180;
System.out.println("cos(" + a4 + ") is " + Math.acos(a4));
System.out.println("sin(" + a4 + ") is " + Math.asin(a4));
System.out.println("tan(" + a4 + ") is " + Math.atan(a4));
System.out.print("Angle 150 degree ");
a5 = 150 * Math.PI/180;
System.out.println("cos(" + a5 + ") is " + Math.acos(a5));
System.out.println("sin(" + a5 + ") is " + Math.asin(a5));
System.out.println("tan(" + a5 + ") is " + Math.atan(a5));
System.out.print("Angle 180 degree ");
a6 = 180 * Math.PI/180;
System.out.println("cos(" + a6 + ") is " + Math.acos(a6));
System.out.println("sin(" + a6 + ") is " + Math.asin(a6));
System.out.println("tan(" + a6 + ") is " + Math.atan(a6));
System.out.print("Angle 210 degree ");
a7 = 210 * Math.PI/180;
System.out.println("cos (" + a7 + ") is " + Math.acos(a7));
System.out.println("sin (" + a7 + ") is " + Math.asin(a7));
System.out.println("tan (" + a7 + ") is " + Math.atan(a7));
System.out.print("Angle 240 degree ");
a8 = 240 * Math.PI/180;
System.out.println("cos (" + a8 + ") is " + Math.acos(a8));
System.out.println("sin (" + a8 + ") is " + Math.asin(a8));
System.out.println("tan (" + a8 + ") is " + Math.atan(a8));
System.out.print("Angle 270 degree ");
a9 = 270 * Math.PI/180;
System.out.println("cos (" + a9 + ") is " + Math.acos(a9));
System.out.println("sin (" + a9 + ") is " + Math.asin(a9));
System.out.println("tan (" + a9 + ") is " + Math.atan(a9));
System.out.print("Angle 300 degree ");
a10 = (300 * Math.PI)/180;
System.out.println("cos (" + a10 + ") is " + Math.acos(a10));
System.out.println("sin (" + a10 + ") is " + Math.asin(a10));
System.out.println("tan (" + a10 + ") is " + Math.atan(a10));
System.out.print("Angle 330 degree ");
a11 = (330 * Math.PI)/180;
System.out.println("cos (" + a11 + ") is " + Math.acos(a11));
System.out.println("sin (" + a11 + ") is " + Math.asin(a11));
System.out.println("tan (" + a11 + ") is " + Math.atan(a11));
System.out.print("Angle 360 degree ");
a12 = (360 * Math.PI)/180;
System.out.println("cos (" + a12 + ") is " + Math.acos(a12));
System.out.println("sin (" + a12 + ") is " + Math.asin(a12));
System.out.println("tan (" + a12 + ") is " + Math.atan(a12));
}

}```

I don't know how to put a while or for loop into the program so the 1st column is the angle measure, the 2,3,and 4th columns are the trig values....Help me please!  Reply With Quote

3. ## Re: help make trig values tables by using while or for loops

You would use a loop to print out the rows. The data on a row (the columns) would be built in a String and printed using a println() statement.

Can you post a sample of the output you are trying to create?  Reply With Quote

4. ## Re: help make trig values tables by using while or for loops

```System.out.println("Degree\tSin\t\tCos\t\tTan");
int i = 0;
String str = "";
for (int degree = 0; degree <= 360; degree += 30) {
if (i == 0)
{  str = String.format("%3d\t%6.4f\t\t%6.4f\t\t%6.4f\n",  degree,
Math.sin(degree * Math.PI / 180),
Math.cos(degree * Math.PI / 180),
Math.tan(degree * Math.PI / 180));
}
else
{
str = str + "\n" + String.format("%3d\t%6.4f\t\t%6.4f\t\t%6.4f\n",  degree,
Math.sin(degree * Math.PI / 180),
Math.cos(degree * Math.PI / 180),
Math.tan(degree * Math.PI / 180));

}
i++;

System.out.printf("%3d\t%6.4f\t\t%6.4f\t\t%6.4f\n", degree,
Math.sin(degree * Math.PI / 180),
Math.cos(degree * Math.PI / 180),
Math.tan(degree * Math.PI / 180));

}}}```
My problem now is that I do not understand how %3d\t%6.4f\t\t%6.4f\t\t%6.4f\n makes the trig values work....Also, for the tan values at 90 and 270 are supposed to be undefined, but when I run it, I get a gigantic number.  Reply With Quote

5. ## Re: help make trig values tables by using while or for loops

It looks like you're borrowing code that you don't yet understand. Where did you get this code from?  Reply With Quote

6. ## Re: help make trig values tables by using while or for loops

I do not understand how %3d\t%6.4f\t\t%6.4f\t\t%6.4f\n makes the trig values work
Those %xxx things are formatting instructions to the format() method of the String class. Read the API doc for that method to see what they mean. The \t is the tab character that is used by the console to move the following String to the next tab position on the screen.  Reply With Quote

7. ## Re: help make trig values tables by using while or for loops

Note that if you check out the PrintSream#printf(...) API, it will direct you to the java.util.Formatter API. As Norm states, it's all explained there.  Reply With Quote

8. ## Re: help make trig values tables by using while or for loops

This is the output I need, but the tan of 90 and 270 made vary because they are supposed to be undefined. I added the tan into the code.

```Degree	Sin		Cos		Tan
0	0.0000		1.0000		0.0000
30	0.5000		0.8660		0.5774
60	0.8660		0.5000		1.7321
90	1.0000		0.0000		16331239353195370.0000
120	0.8660		-0.5000		-1.7321
150	0.5000		-0.8660		-0.5774
180	0.0000		-1.0000		-0.0000
210	-0.5000		-0.8660		0.5774
240	-0.8660		-0.5000		1.7321
270	-1.0000		-0.0000		5443746451065123.0000
300	-0.8660		0.5000		-1.7321
330	-0.5000		0.8660		-0.5774
360	-0.0000		1.0000		-0.0000```

Is there a link you can provide?  Reply With Quote