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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help with class java program

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with class java program

    I am stuck and having a problem trying to find what i believe is a simple fix but can't wrap my head around it. For class I needed to make a table that prints out Sin,Cos,Tan for angles in steps of 5 to 180. The problem is in my output with the first couple lines of the table. for angle 5.0 the Sin(.0872) Cos(.9962) and Tan (.0875) are being displayed on the angle line 10.0

    public class TrigTable {
     
    	public static void main(String[] args) {
    		double angle = 0;
    		double sin =0;
    		double cos = 0;
    		double tan = 0;
     
    		System.out.println("Angle\tSin\tCos\tTan"); // Table header for the values
    		System.out.println("-----\t---\t---\t---");
    		printTable(angle, sin, cos, tan);
     
     
    	}
    	public static void printTable(double angle,double sin,double cos,double tan) {   // Method to create a table for the values
     
     
     
    		for (double i = 0; i <= 180; i = i+5) {
     
    			sin = round4(Math.sin(angle));
    			cos = round4(Math.cos(angle));
    			tan = round4(Math.tan(angle));
     
     
    			angle = Math.toRadians(i);
     
    			System.out.println(i + "\t" + sin + "\t" + cos + "\t" + tan);
     
     
    			}
     
     
    		}
     
    	public static double round4(double n) {			//rounds the numbers to only 4 decimal places
    		return Math.round(n * 10000.0) / 10000.0;
    	}
     
     
    }
    Here is the first couple lines of output:
    Angle Sin Cos Tan
    ----- --- --- ---
    0.0 0.0 1.0 0.0
    5.0 0.0 1.0 0.0
    10.0 0.0872 0.9962 0.0875
    15.0 0.1736 0.9848 0.1763
    20.0 0.2588 0.9659 0.2679
    Last edited by cwebz; October 1st, 2014 at 04:22 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with class java program

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with class java program

    okay thank you GregBrannon I put in the highlight tags, much easier to read. now hopefully someone can help me figure out where it's going wrong. I'm guessing it's with my for loop

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with class java program

    Code is usually (always?) executed in a sequential manner, in the order listed, one statement at a time. With that in mind, what do you think is wrong with the following statements, copied in order from your code:
    sin = round4(Math.sin(angle));
    cos = round4(Math.cos(angle));
    tan = round4(Math.tan(angle));
     
    angle = Math.toRadians(i);

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with class java program

    Oh so by moving the angle line of code above the first three should execute then in proper order right? I'll give it a shot
    sin = round4(Math.sin(angle));
    cos = round4(Math.cos(angle));
    tan = round4(Math.tan(angle));
     
    angle = Math.toRadians(i);
    [/QUOTE]

    --- Update ---

    Thank you very much, that thought didn't even cross my mind. if you are able to help point me in the right direction for one more question I would be very grateful. for extra credit my teacher told us to put an empty line after every 5 lines of output using two nested for loops. I know the outside loop controls the numbers of times to run the inner loop and the inner loop should control printing each group of 5 lines but having trouble, I am not sure how to get it to print the 5 lines, go back to the outer loop to space then back to the inner to print the next five lines.
    for (double i = 0; i <= 180; i = i+5) {
     
    			sin = round4(Math.sin(angle));
    			cos = round4(Math.cos(angle));
    			tan = round4(Math.tan(angle));
     
     
    			angle = Math.toRadians(i);
     
    			System.out.println(i + "\t" + sin + "\t" + cos + "\t" + tan);
     
     
    			}

Similar Threads

  1. Help designing a class hierarchy for a Java program
    By zmsras in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 21st, 2014, 03:31 AM
  2. Replies: 2
    Last Post: January 11th, 2014, 06:58 AM
  3. need help with creating a catalogue class for my java program
    By purple_ninjaa in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 2nd, 2014, 02:58 PM
  4. I'm new to java and need help with creating a catalogue class for my java program
    By purple_ninjaa in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 2nd, 2014, 02:10 PM
  5. Help with creating a Program class for a BASIC Interpreter in Java
    By sampsoninc916 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 15th, 2013, 07:04 AM