Results 1 to 4 of 4
Thread: Need help with area of triangle
 09292013, 06:38 PM #1Member
 Join Date
 Sep 2013
 Posts
 2
 Rep Power
 0
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
 09292013, 06:54 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,492
 Rep Power
 9
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09292013, 07:02 PM #3Member
 Join Date
 Sep 2013
 Posts
 2
 Rep Power
 0
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?
 09292013, 07:32 PM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,492
 Rep Power
 9
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
Regards,
JimLast edited by jim829; 09292013 at 08:56 PM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Two Thread To Calculate Area Rectangle And Area Triangle
By muhammad naim in forum Threads and SynchronizationReplies: 2Last Post: 08282013, 05:08 PM 
Area of Triangle
By donfanzu in forum New To JavaReplies: 7Last Post: 04022012, 05:21 AM 
Triangle
By jkswebsite in forum New To JavaReplies: 8Last Post: 01102009, 03:08 PM 
Computing the area of a triangle using Heron's Formula
By Java Tip in forum java.langReplies: 0Last Post: 04122008, 08:39 PM
Bookmarks