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

Thread: Can you help me?

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

    Default Can you help me?

    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.Next, add the code necessary to check whether the values entered do indeed form a triangle. To do this, use the triangle inequality theorem: "any side of a triangle is always shorter than the sum of the other two sides". Print a warning message and stop if the values are invalid.
    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 know that triangle inequality theorem is a+b>c a+c>b b+c>a I can write it as a code but how can i get a warning message and stop the programme if the values are invalid. Can you help me?


    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:");
    int a=in.nextInt();
    System.out.print("Please enter lenght for b:");
    double b=in.nextInt();
    System.out.print("Please enter lenght for c:");
    int c=in.nextInt();




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

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



    }
    }

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

    Default Re: Can you help me?

    You need to cast these two lines as below

    int s = (int) ((a + b + c) / 2);
    int x = (int) (s * (s - a) * (s - b) * (s - c));
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Oct 2017
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can you help me?

    I think I've solved the question by doing that

    import java.util.Scanner;



    public class İlkProgram
    {




    public static void main(String[] args)
    {



    //variables

    int a;
    int b;
    int c;
    int halfperimeter;
    int x;
    double Area;
    double Flower;
    int roundVal;


    Scanner in=new Scanner(System.in);


    System.out.println("Please enter the first lenght : ");
    a = in.nextInt();

    System.out.println("Please enter the second lenght : ");
    b = in.nextInt();

    System.out.println("Please enter the third lenght : ");
    c = in.nextInt();



    halfperimeter = ((a + b + c) / 2);
    x = ((halfperimeter) * (halfperimeter - a) * (halfperimeter - b) * (halfperimeter - c));
    Area=Math.sqrt (x);
    Flower=(Area * 17);




    roundVal=(int)Math.round(Flower);

    if(roundVal==0)
    {

    if( a+b<c)
    {
    System.out.println("Please change third lenght");
    }

    if(a+c<b)
    {
    System.out.println("Please change second lenght");
    }

    if (b+c<a)
    {
    System.out.println("Please change the first lenght!!!");
    }

    }
    else
    {
    System.out.printf("Max flower is: " + roundVal);
    }

    }
    }

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    258
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Can you help me?

    Thanks for the update
    Whatever you are, be a good one