javafunctionexceptiondegreesradians

How can I use functions to complete the following table? I came close to having it solved, but I'm getting exception errors


[Code on SoloLearn Part 1][Code on SoloLearn Part 2]1[My Take Home Assignment [][1]2` [Radians, Tangent, Sine, Cosine, and Degrees Table

import java.util.Scanner;
public class St_Johns_University
{
    public static void main(String[] args) {
        System.out.printf("%-10s%-10s%-10s%-10s%-10s\n", "Radians", "Tangent", "Sine", "Cosine", "Degrees");

        int degrees = 60;
        double radians = Math.toRadians(degrees);
        System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n", radians, Math.tan(radians), Math.sin(radians), Math.cos(radians), degrees);
         degrees = 80;
         radians = Math.toRadians(degrees);
        System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n", radians, Math.tan(radians), Math.sin(radians), Math.cos(radians), degrees);
         degrees = 100;
         radians = Math.toRadians(degrees);
        System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n", radians, Math.tan(radians), Math.sin(radians), Math.cos(radians), degrees);
         degrees = 130;
         radians = Math.toRadians(degrees);
        System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n", radians, Math.tan(radians), Math.sin(radians), Math.cos(radians), degrees);
         degrees = 160;
         radians = Math.toRadians(degrees);
    System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n", radians, Math.tan(radians), Math.sin(radians), Math.cos(radians), degrees);
    }
}

`


Solution

  • Your formatting using print f is wrong. Also import of Scanner is not required. degree is int but you have provided f instead of d and also provided precision 4. radians is double. but you have provided d which is wrong I have corrected it please check the below code.

    public class St_Johns_University
    {
        public static void main(String[] args) {
            System.out.printf("%-10s%-10s%-10s%-10s%-10s\n", "Radians", "Tangent", "Sine", "Cosine", "Degrees");
    
            int degrees = 60;
            double radians = Math.toRadians(degrees);
            System.out.printf("%-10f%-10.4f%-10.4f%-10.4f%-10d\n", radians, Math.tan(radians), Math.sin(radians), Math.cos(radians), degrees);
             degrees = 80;
             radians = Math.toRadians(degrees);
            System.out.printf("%-10f%-10.4f%-10.4f%-10.4f%-10d\n", radians, Math.tan(radians), Math.sin(radians), Math.cos(radians), degrees);
             degrees = 100;
             radians = Math.toRadians(degrees);
            System.out.printf("%-10f%-10.4f%-10.4f%-10.4f%-10d\n", radians, Math.tan(radians), Math.sin(radians), Math.cos(radians), degrees);
             degrees = 130;
             radians = Math.toRadians(degrees);
            System.out.printf("%-10f%-10.4f%-10.4f%-10.4f%-10d\n", radians, Math.tan(radians), Math.sin(radians), Math.cos(radians), degrees);
             degrees = 160;
             radians = Math.toRadians(degrees);
             System.out.printf("%-10f%-10.4f%-10.4f%-10.4f%-10d\n", radians, Math.tan(radians), Math.sin(radians), Math.cos(radians), degrees);
        }
    }