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 10 of 10

Thread: Area of a triangle (using 2 extra methods) error help

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Area of a triangle (using 2 extra methods) error help

    EDITED..

    Oke, I managed to sort everything out, except, it does not calculate the area in area() method:

    [COLOR="Red"]public static double area(double side1, double side2, double side3, double areaTri)[/COLOR]
    	{
    		double s = (side1 +  side2 + side3)/2;
    		areaTri = Math.sqrt(s*(s - side1)*(s - side2)*(s - side3));
     
                    //Returns to main() and should print out the last statement with the area
                    return areaTri;	
    	}

    Updated Code:
    //Calculate the area of a triangle
     
    import java.util.Scanner;
    import static java.lang.Math.*;
     
    [COLOR="Lime"]public class MyTriangle[/COLOR]
    {	
    	[COLOR="Red"]public static void main(String[] args)[/COLOR]
    	{		
    		double areaTri=0;
    		Scanner input = new Scanner(System.in);
     
    		System.out.print("Enter the size of the first side: ");	
    		double side1 = input.nextDouble();
     
    		System.out.print("Enter the size of the second side: ");	
    		double side2 = input.nextDouble();
     
    		System.out.print("Enter the size of the third side: ");	
    		double side3 = input.nextDouble();
     
    		isValid(side1,side2, side3, areaTri);
     
    		System.out.println("");
    		System.out.println("The area of the triangle is "+ areaTri);	
    	}	
    	 //Check if the sum of side1 and side2 is greater than side3 and if not returns false
    	//If true calls on method area()
    	[COLOR="Red"]public static boolean isValid(double side1, double side2, double side3, double areaTri)[/COLOR]
    	{
    		if(side1 * side2 > side3)
    		{
    			area(side1,side2,side3, areaTri);
    			return true;
    		}
    		else
    		{
    			System.out.println("The input is invalid!");
    			return false;
    		}
    	}
    	//calculates area of the triangle assuming conditions are met at method isValid()
    	[COLOR="red"]public static double area(double side1, double side2, double side3, double areaTri)[/COLOR]
    	{
    		double s = (side1 +  side2 + side3)/2;
    		areaTri = Math.sqrt(s*(s - side1)*(s - side2)*(s - side3));
    		return areaTri;	
    	}
    }
    Last edited by SilentPirate; September 12th, 2010 at 04:30 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Area of a triangle (using 2 extra methods) error help

    , it does not calculate the area in area() method:
    What is it supposed to do in that method?
    Do you have a question?

    If you need help debugging your code, I suggest that you separate out all the subexpressions in the sqrt() method call and print them out to see if they are computing to the values you expect.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Area of a triangle (using 2 extra methods) error help

    I managed to fix it in the main():

    From:

    System.out.println("The area of the triangle is "+ areaTri);

    To:

    System.out.println("The area of the triangle is "+ area(side1,side2,side3,areaTri));

    This solved the problem and lead to another. How do I round it off to 1 decimal place. Right now the output would look something like this:

    0.7261843774138906

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Area of a triangle (using 2 extra methods) error help

    How do I round it off to 1 decimal place
    A couple of ways
    The printf() has formatting strings
    The DecimalFormat class will format a number.

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Area of a triangle (using 2 extra methods) error help

    I decided to use printf() since I'm more familiar with it, but it doesnt seem to work the same:

    In C this would normally work:
    printf("The area of the triangle is %0.1f", area(side1,side2,side3,areaTot));

    I get a format specifier error in java when running:
    System.out.printf("The area of the triangle is %0.1f", area(side1,side2,side3,areaTot));

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Area of a triangle (using 2 extra methods) error help

    Have you looked up the error message to see what it means? It could tell you what the problem is.

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Area of a triangle (using 2 extra methods) error help

    I know what it means...the format specifier "%0.1f" doesnt work, my question is how could I interprate this into java to show a single decimal place.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Area of a triangle (using 2 extra methods) error help

    Try changing it. See the Formatter class for descriptions.

  9. #9
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Area of a triangle (using 2 extra methods) error help

    Oke, I figured it out with the help of your link.

    My problem was the 0 in %0.1f. It should look like this: %.1f

    Thank you!

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Area of a triangle (using 2 extra methods) error help

    You're welcome. Most of it is in the doc.

Similar Threads

  1. [SOLVED] Handling Errors / calling Error-Catching Methods
    By movsesinator in forum Object Oriented Programming
    Replies: 4
    Last Post: April 6th, 2010, 05:35 AM
  2. Jigloo help to produce a text area which only scrolls down
    By rtumatt in forum AWT / Java Swing
    Replies: 0
    Last Post: February 2nd, 2010, 06:09 PM
  3. Problem in implementing mortgage calculator
    By American Raptor in forum AWT / Java Swing
    Replies: 1
    Last Post: April 1st, 2009, 02:09 PM
  4. How to read character from image area(jpg image)?
    By sundarjothi in forum Java Theory & Questions
    Replies: 5
    Last Post: August 6th, 2008, 02:08 AM