Results 1 to 4 of 4
  1. #1
    pontiacfan is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Need help with area of triangle

    First off this is not my forte. but im trying to help some by trying to get help help from you guys which know what your doing. She is writing a basic program that base to give her the area of a triangle. The base is 5 and the height is 10. This is the code she has so far but dose not work we have been trying to figure this out for 2 days now. If some one could please tell me and her what is wrong it would be very helpful.



    public class Myfirstprogram
    {

    public static void main(String[] args)
    {
    System.out.print("Hello ");
    System.out.print("My name is Anne Maslanka ");
    System.out.print(" This is my first program and im a little confused.");
    }
    }//End main method


    public class Triangle{

    public Triangle(int base, int height, int area)
    {
    double base = 5;
    double height = 10;
    double area = 1/2 * base * height;
    System.out.println("The area of a triangle with the base" +base+ "and the height" +height+ "is: " +area);
    }
    {
    area=(base*height)/2; base=5; height=10;
    }
    public void print(int base, int height, int area){
    System.out.println("The area of a triangle with the base" +base+ "and the height" +height+ "is: " +area);
    }

    }//End main method

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: Need help with area of triangle

    First, why are you printing what you do in the first three lines?

    Second, your problem is 1/2 in the formula. 1/2 evaluates to 0 because they are both integers and integers round down to whole numbers. Change the 2 to 2.0.
    Also, add some spacing in your final print statement to avoid your values butting up against the text.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    pontiacfan is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: Need help with area of triangle

    thanks that's what there proff wants I guess but I did what you said and it still will not work anymore advice?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: Need help with area of triangle

    Sorry I didn't read this more carefully my first time thru. First, you should read the FAQ and the guide for new posters. All code should be placed between tags. I have done it for you here.



    Java Code:
    public class Myfirstprogram {
    public static void main(String[] args)
    {
       System.out.print("Hello ");
       System.out.print("My name is Anne Maslanka ");
       System.out.print(" This is my first program and im a little confused.");
    }
    }//End main method
    
    
    public class Triangle{
    public Triangle(int base, int height, int area)
    {
       double base = 5;
       double height = 10;
       double area = 1/2 * base * height;
       System.out.println("The area of a triangle with the base" +base+ "and the height" +height+ "is: " +area);
    }
    {
    area=(base*height)/2; base=5; height=10;
    }
    public void print(int base, int height, int area){
    System.out.println("The area of a triangle with the base" +base+ "and the height" +height+ "is: " +area);
    }
    }//End main method
    There are a number of problems here. Are you supposed to be using a separate class for calculating the area? If so, you are declaring your variables inside the constructor. This won't even compile. What errors are you seeing?

    Regards,
    Jim
    Last edited by jim829; 09-29-2013 at 08:56 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Two Thread To Calculate Area Rectangle And Area Triangle
    By muhammad naim in forum Threads and Synchronization
    Replies: 2
    Last Post: 08-28-2013, 05:08 PM
  2. Area of Triangle
    By donfanzu in forum New To Java
    Replies: 7
    Last Post: 04-02-2012, 05:21 AM
  3. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 02:08 PM
  4. Replies: 0
    Last Post: 04-12-2008, 08:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •