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.


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

Results 1 to 2 of 2

Thread: Whats wrong with my code

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Thanked 0 Times in 0 Posts

    Default 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,


    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);


  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    My Mood
    Thanked 18 Times in 18 Posts

    Default 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);
    Whatever you are, be a good one

Similar Threads

  1. Help! Whats wrong with my code?
    By Carolineawood in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 5th, 2014, 06:56 PM
  2. Whats wrong with my code?
    By incredibleX in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2014, 03:40 PM
  3. Whats wrong with my code?
    By patelvrajn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 15th, 2013, 10:54 AM
  4. Whats wrong in my code?
    By kalaicse30@gmail.com in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 19th, 2013, 12:44 AM
  5. Can anyone see whats wrong with this code
    By javapol in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 27th, 2013, 03:59 PM