Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package projects;
- import java.lang.Math;
- import java.text.DecimalFormat;
- public class p4 {
- public static void main(String[] args) {
- //declare formatting to fix floating point issue, declare count to count lines for breaks
- DecimalFormat form = new DecimalFormat("#.#");
- int count = 0;
- //print heading using tabs
- System.out.print("Angle \t Sin \t Cos \t Tan \n");
- System.out.print("----- \t --- \t --- \t --- \n");
- //start loop for degrees in 0.1 increments
- for(double i=0; i<=180; i=i + 0.1){
- //print degree formatted to #.#
- System.out.print(form.format(i) + "\t");
- //convert i from degrees to radians in variable j, assign sin, cos, and tav values
- double j = Math.toRadians(i);
- double sinValue = Math.sin(j);
- double cosValue = Math.cos(j);
- double tanValue = Math.tan(j);
- //print the rounded values with tabbing
- System.out.print(roundMethod(sinValue) + "\t" + roundMethod(cosValue) + "\t" + roundMethod(tanValue) + "\n" );
- //count each line, printing a new line every 5
- count++;
- if (count == 5){
- System.out.print("\n");
- count = 0;
- }
- }
- }
- //method to round the sin, cos, and tan values, use 10000 for 4 decimal places
- static double roundMethod ( double numberToRound){
- int round = (int)(numberToRound * 10000 + 0.5);
- double roundedValue = (double)round / 10000;
- return roundedValue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement