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: Whats wrong with my code

1. ## Whats wrong with my code

Hello I'm a new java student and I have an assignment which is

Design and implement a program that computes the maximum number of flowers that can be planted in a triangular-shaped garden having sides of length a, b and c meters, assuming at most 17 flowers can be planted in each square meter. You can use Heron's formula (below) to compute the area of the garden, but remember, the total number of flowers should be an integer! Tip: the Math class has a function Math.sqrt that you can use to compute the square root of a value.

Heron's formula: the area, A, of a triangle whose sides have lengths a, b, and c is

A = sqrt{s(s-a)(s-b)(s-c)},

where s is the semi-perimeter of the triangle; that is,

s={a+b+c}/{2}.

I have a little problem with this, I can get the max flower but it's not an integer but the teacher wants this to be an integer. How can i solve that? Here's my code

package berk;

import java.util.Scanner;

public class FirstProgramme{

public static void main(String args[])
{
Scanner in=new Scanner(System.in);

System.out.print("Please enter lenght for a:");
double a=in.nextDouble();
System.out.print("Please enter lenght for b:");
double b=in.nextDouble();
System.out.print("Please enter lenght for c:");
double c=in.nextDouble();

double s=(a+b+c)/2;
double x=(s*(s-a)*(s-b)*(s-c));
double Area=Math.sqrt(x);
double flower=(Area*17);

System.out.printf("Max flower is: %8.2f",flower);

}
}  Reply With Quote

2. ## Re: Whats wrong with my code

These two lines are what you need

```int roundVal= (int) Math.round(flower);
System.out.printf("Max flower is: "+ roundVal);```  Reply With Quote