Page 1 of 4 123 ... LastLast
Results 1 to 20 of 68
  1. #1
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default Triangle Areas - Please help before I smash my laptop into pieces!

    Java Code:
    public class TriangleArea 
    {
    	  int a, b, c;
    	       
    	      TriangleArea()
    	       {
    	          this.a=0;
    	          this.b=0;
    	          this.c=0;
    	       }
    	       TriangleArea(int a, int b, int c)
    	       {
    	          this.a=a;
    	          this.b=b;
    	          this.c=c;
    	       }
    
    
    public static void main( String[] args){}
    
    
    public double Sidea =0.0, Sideb =0.0, Sidec =0.0, area=0.0;
    
    public TriangleArea(double sa, double sb, double sc)
    {
    Sidea=sa;
    Sideb=sb;
    Sidec=sc;
    }
    public void calcArea()
    {
    double s=0.0, Num=0.0;
    
    s = 0.5*(Sidea+Sideb+Sidec);
    Num = s*((s-Sidea)*(s-Sideb)*(s-Sidec));
    
    area = Math.sqrt(Num);
    
    }
    public double getArea()
    {
    return area;
    
    public static boolean isValid(double side1, double side2, double side3)
         {
              return(((side1 + side2) >side3) && ((side2 + side3) > side1) && ((side3 + side1) >side2));
              
         public static double area(double side1, double side2, double side3)
    
         {
    
              double s = (side1 + side2 + side3)/2;
    
              double area = Math.sqrt(s * (s - side1) * (s - side3));
    
              return area;

    Moderator Edit: code tags added
    Last edited by Atia of the julii; 11-08-2010 at 01:51 PM. Reason: Moderator Edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Can you tell us what has you "the most" stuck right now? Is it deciding how to create the triangles sides that satisfy the rules? The more specific the question, usually the more helpful the answer.

    Luck and welcome to Java forums!

    Oh, I've added code tags in your post above.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    You could do it the stupid way, i.e. enumerate all the sides a, b and c up to a certain number and check the two constraints: the total length constraint: a+b+c <= 20 and the triangle constraint: a < b+c && b < a+c && c < a+b. If both constraints are satisfied do the area calculation. If you keep a <= b <= c you don't have to check for those equivalent triangles. A cowardesque upperbound for those three sides would be 20.

    kind regards,

    Jos

  4. #4
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    Thanks guys, I've only been teaching myself for 2 weeks and I'm still finding allot of java very confusing, I'm not too bad at logic I just haven't had much practice actually writing it out into a coherent program. I'm just getting used to things like while and if statements and I can't even work out if i need say, an if statement to work the constraints that JosAH mentioned above. I am a complete beginner as you can probably tell! xD

    Apparently this exercise should not take long at all, i've been working on it for over 6 hours now and just can't get my head around it :(

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    But again to reiterate, if you could tell us what specifically has you stumped, we can give you better help.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Atia of the julii View Post
    Thanks guys, I've only been teaching myself for 2 weeks and I'm still finding allot of java very confusing, I'm not too bad at logic I just haven't had much practice actually writing it out into a coherent program. I'm just getting used to things like while and if statements and I can't even work out if i need say, an if statement to work the constraints that JosAH mentioned above. I am a complete beginner as you can probably tell! xD

    Apparently this exercise should not take long at all, i've been working on it for over 6 hours now and just can't get my head around it :(
    Ok, I'll help you with the loop boundaries:

    Java Code:
    for (int a= 1; a <= 20; a++) {
       for (int b= a; a <= 20; b++) {
          for (int c= b; c <= 20; c++) {
             if (!lengthConStraint(a, b, c)) break;
             // rest of the logic goes here
          }
       }
    }
    Note that the way the loop boundaries are set up it is guaranteed that a <= b <= c so you don't have to check for equivalent triangles. I added one constraint check, the length constraint. It is a simple method:

    Java Code:
    private boolean lengthConstraint(int a, int b, int c) {
       return a+b+c <= 20;
    }
    i.e. it checks whether or not the sum of the lengths of the sides is less or equal to 20 as the assignment states. If the constraint fails you can simply break out of the loop because only larger values of a, b or c are tested in a next pass through the loop.

    kind regards,

    Jos

  7. #7
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    Thanks very much Jos, I appreciate it. I'll keep working at it, I came across 'exceptions' but I haven't studied or learnt them yet so wasn't too keen to use them as I would only get more confused so thanks for the 'if' statements, they are more at my level!

  8. #8
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    I searched the forum for any examples and I found this.... I am unfamiliar with floats and I am not sure what the term 'precision' is used for, could anyone please enlighten me? I know I will be using the constraints Jos kindly laid out for me above but the question seems to be similar to mine so I am wondering if I can build on this example I found?

    Thanks,

    Atia

    Code:
    public class Heron {
    public static void main(String[] args) {
    // Sides for triangle in float
    float af, bf, cf;
    float sf, areaf;

    // Ditto in double
    double ad, bd, cd;
    double sd, aread;

    // Area of triangle in float
    af = 12345679.0f;
    bf = 12345678.0f;
    cf = 1.01233995f;

    sf = (af+bf+cf)/2.0f;
    areaf = (float)Math.sqrt(sf * (sf - af) * (sf - bf) * (sf - cf));
    System.out.println("Single precision: " + areaf);

    // Area of triangle in double
    ad = 12345679.0;
    bd = 12345678.0;
    cd = 1.01233995;

    sd = (ad+bd+cd)/2.0d;
    aread = Math.sqrt(sd * (sd - ad) * (sd - bd) * (sd - cd));
    System.out.println("Double precision: " + aread);
    }
    }

  9. #9
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    Ah... i think i understand, a double it a longer byte form than a float, so it will show more decimal places, I will follow this code but stick only to double....

  10. #10
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    So I am now working off this as a base...

    public class Area {

    public static void main(String[] args) {

    // Sides for a triangle in double
    double a, b, c;
    double s, aread;

    // Area of triangle in double
    a = 12345679.0;
    b = 12345678.0;
    c = 1.01233995;

    s = (a+b+c)/2.0d;
    aread = Math.sqrt(s * (s - a) * (s - b) * (s - c));
    System.out.println("Area: " + aread);
    }
    for (int a= 1; a <= 20; a++) {
    for (int b= a; a <= 20; b++) {
    for (int c= b; c <= 20; c++) {
    if (!lengthConStraint(a, b, c)) break;
    // rest of the logic goes here
    }
    }
    }
    Last edited by Atia of the julii; 11-07-2010 at 03:10 PM.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    Don't copy code from the forum. For this assignment you're much better off creating your code from scratch.
    I call this `Frankenstein code', code that is glue together from scraps copied and pasted from the internet. Most of the time it turns out a complete failure or just an ugly monster. ;-)

    kind regards,

    Jos

  13. #13
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    I just don't feel confident enough to do it all from scratch, my BA of several years ago was in English.. and I haven't studied maths since my GCSE in 90's lol. I wish I could find some similar tasks on the net to study but they're difficult to find and typically much more advanced with coordinates etc that derail me even more. I just got a java for dummies book from amazon that isn't proving very useful either.... frustration....

  14. #14
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    I call this `Frankenstein code', code that is glue together from scraps copied and pasted from the internet. Most of the time it turns out a complete failure or just an ugly monster. ;-)

    kind regards,

    Jos
    x) This made me laugh!

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Atia of the julii View Post
    I just don't feel confident enough to do it all from scratch, my BA of several years ago was in English.. and I haven't studied maths since my GCSE in 90's lol. I wish I could find some similar tasks on the net to study but they're difficult to find and typically much more advanced with coordinates etc that derail me even more. I just got a java for dummies book from amazon that isn't proving very useful either.... frustration....
    frustration == brain cells working, so don't let it dismay you, but rather embrace it. Again, try to create it on your own, and if you run into a road block, post your code here and your errors, and if we can, we'll try to help you.

  16. #16
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    Thanks Furarable, I'm determined to get there in the end! I'm going from scratch atm so i'll post my results soon hopefully :)

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Atia of the julii View Post
    Thanks Furarable, I'm determined to get there in the end! I'm going from scratch atm so i'll post my results soon hopefully :)
    We can hardly wait to see the result and our nipples will explode in delight if you didn't name your class 'Area'.

    kind regards,

    Jos ;-)

  18. #18
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    Haha Jos, I had 'TriangleArea' and 'Area' because I wanted to have 2 classes to play around with in eclipse, i just deleted all the crap I had in 'TriangleArea' and made this from scratch with the help of your 'if' statement. I have cleaned away all the errors apart from the syntax error I am getting on the 'return returnable' line... it says insert '}' but I do this and it remains an error??

    I'm sure I haven't accomplished anything here and it'll all be wrong yet again but i just wanted to run it and see what this did, thought I am aware I don't have a System.out.println in it yet...

    Java Code:
    public class TriangleArea {
    
    	public static boolean isValid(double sidea, double sideb, double sidec){
    		  boolean returnable = false;
    		  
    		  for (int a= 1; a <= 20; a++) {
    			   for (int b= a; a <= 20; b++) {
    			      for (int c= b; c <= 20; c++) {
    			         if (!lengthConStraint(a, b, c)) break;
    		  
    		  int  s;
    
    		    if ( sidea+ sideb <= sidec){
    		       if (sideb + sidec <= sidea){
    		         if (sidea + sidec <= sideb){
    		           return false;
    		         }else{
    		         returnable = true;}}
    		         
    		      else {
    		       returnable = true;
    		      }
    		    return returnable;
    		    }
    			 
    		    private static boolean lengthConStraint(int sidea, int sideb, int sidec) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    			public static double area(double sidea, double sideb, double sidec){
    		    	   double s = Math.sqrt( (sidea + sideb + sidec) / 2 ) ;
    		    	   s = s * (s - sidea) * ( s - sideb ) * (s - sidec );
    		    	   return s;
    		    	}}
    Moderator Edit: Code tags added
    Last edited by Fubarable; 11-07-2010 at 05:25 PM. Reason: Moderator Edit: Code tags added

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I added code tags to your post to have the code retain its formatting and be readable, but you should edit it and fix your indentation because as posted, it's unreadable.

    I'm confused: what is the purpose of your isValid method?
    You call a lengthConstraint method, but I don't see code for it? Or a main method?

  20. #20
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    public class TriangleArea {

    public static boolean isValid(double a, double b, double c){
    boolean returnable = false;


    for ( a = 1; a <= 20; a++) {
    for ( b = a; a <= 20; b++) {
    for ( c = b; c <= 20; c++) {
    if (!lengthConStraint(a, b, c)) break;

    int s;

    if ( a+ b <= c){
    if (b + c <= a){
    if (a + c <= b){
    return false;
    }else{
    returnable = true;}}

    else {
    returnable = true;
    }

    private static boolean lengthConStraint(double a, double b, double c) {
    // TODO Auto-generated method stub
    return false;
    }
    public static double area(double a, double b, double c){
    double s = Math.sqrt( (a + b + c) / 2 ) ;
    s = s * (s - a) * ( s - b ) * (s - c );
    return s;
    }}

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Replies: 0
    Last Post: 08-11-2010, 08:19 AM
  2. Laptop Help
    By Lil_Aziz1 in forum Forum Lobby
    Replies: 9
    Last Post: 08-08-2010, 05:04 PM
  3. Access Macaddress on laptop
    By dhaivat in forum AWT / Swing
    Replies: 0
    Last Post: 07-11-2010, 02:49 PM
  4. AWT: Painting buttons and text areas in a canvas
    By chappa in forum AWT / Swing
    Replies: 6
    Last Post: 01-09-2010, 02:37 PM
  5. text areas, printstream, and "\n"
    By diggitydoggz in forum New To Java
    Replies: 10
    Last Post: 12-26-2008, 04:03 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
  •