Page 2 of 4 FirstFirst 1234 LastLast
Results 21 to 40 of 68
  1. #21
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Darn, why did you ruin my lengthConstraint( ... ) method; I had hoped that you had continued my line of thoughts:

    Java Code:
    for (int a= 1; a <= 20; a++) {
       for (int b= a; b <= 20; b++) {
          for (int c= b; c <= 20; c++) {
             // is the circumpherence <= 20?
             if (!lengthConstraint(a, b, c)) break;
             // is it a triangle?
             if (!triangleConstraint(a, b, c)) break;
             // the rest of the logic goes here ...
          }
       }
    }
    Don't stick all those if statements in your main loop; keep your main logic clean at the upper level and hide the details in separate little methods (see above).

    kind regards,

    Jos

  2. #22
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    Argh! I just wrote a long reply and it didn't post!

    I have removed the 'side' name on the sides because it was unnecessary and confusing. I thought I needed to declare the isValid boolean line to be able to use the code I use further down that prevents improperly formed triangles from being used...

    (You call a lengthConstraint method, but I don't see code for it? Or a main method?)

    ...the lengthConstraint method was written as part of Jos' code to help me but I was confused by it and followed eclipse's suggestion for a 'TODO' line (something I am again unfamiliar with)

    I thought I was starting to get somewhere but clearly i'm not... cry :(

  3. #23
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    Back to the drawing board... does this look like a better starting point?

    public class TriangleArea {

    public static boolean areas (String[] args)
    {
    for (int a= 1; a <= 20; a++) {
    for (int b= a; b <= 20; b++) {
    for (int c= b; c <= 20; c++) {
    // is the circumpherence <= 20?
    if (!lengthConstraint(a, b, c)) break;
    // is it a triangle?
    if (!triangleConstraint(a, b, c)) break;
    // the rest of the logic goes here ...
    }
    }
    private static boolean lengthConstraint(int a, int b, int c) {
    // TODO Auto-generated method stub
    return false;
    }
    private static boolean triangleConstraint(int a, int b, int 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;
    Last edited by Atia of the julii; 11-07-2010 at 07:03 PM.

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

    Default

    public class TriangleArea {

    public static boolean areas (String[] args)
    {
    for (int a= 1; a <= 20; a++) {
    for (int b= a; b <= 20; b++) {
    for (int c= b; c <= 20; c++) {
    // is the circumpherence <= 20?
    if (!lengthConstraint(a, b, c)) break;
    // is it a triangle?
    if (!triangleConstraint(a, b, c)) break;
    // the rest of the logic goes here ...
    }
    }
    private static boolean lengthConstraint(int a, int b, int c) {
    // TODO Auto-generated method stub
    return false;
    }
    private static boolean triangleConstraint(int a, int b, int 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;
    }
    public void output ()
    {
    System.out.println ("[s]");
    }
    private double a;
    private double b;
    private double c;
    private double area;
    }

  5. #25
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Looks like you could benefit from going through all four links in Fubarable's signature.

    db

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

    Default

    My train of thoughts ran like this:

    Java Code:
    for all values of a, b and c:
       if the length of the circumpherence isn't ok, skip the rest
       if a, b and c don't form a triangle, skip the rest
       do some interesting stuff with a, b and c
    My three nested for-loops take care that I don't have to deal with equivalent triiangles; the lengthConstraint( ... ) method takes care that I don't have to deal with things where a+b+c > 20 (see a previous reply). I had hoped that you'd come up with a similar small method that'd test if we're dealing with an actual triangle, i.e. a < b+c && b < a+c && c < a+b. If all the constraints are satisfied only then we'll do the calculation for the area where a, b and c form a triangle with a circumpherence that <= 20.

    kind regards,

    Jos

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

    Default

    public class TriangleArea {

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

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

    I'm just not sure how to put it all together...

    should the above look more like this now, to print the table would I need to include 4 separate System.out.println's at the end?!

    I really do appreciate all of your help and I'm sorry I'm not proficient enough to make best use of your advice :(

  8. #28
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Atia of the julii View Post
    public class TriangleArea {

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

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

    I'm just not sure how to put it all together...

    should the above look more like this now, to print the table would I need to include 4 separate System.out.println's at the end?!

    I really do appreciate all of your help and I'm sorry I'm not proficient enough to make best use of your advice :(
    You don't know basic Java syntax do you? Ok, this is going to take a while: try to define a small boolean method that, given three parameter values a, b and c, returns true if those values make up a triangle. The method is supposed to return false if those values don't form a valid triangle.

    kind regards,

    Jos

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

    Default

    Ok, thx, attempting now...

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

    Default

    The only way I can think to do this is to include an angle for the 3rd side? Or should I not be using angles at all? :S


    public class Triangle {

    double a, b, angle;

    public Triangle( double a, double b, double angle) {

    public boolean isValid() {

    if ((a > 0 && b > 0) && (angle > 0 && angle <= 180)){

    return true;

    } else

    return false;
    Last edited by Atia of the julii; 11-07-2010 at 09:02 PM.

  11. #31
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    Can anyone advise if i've done what Jos instructed me to do correctly please?

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

    Default

    There's no need for angles here. You know that a triangle with lengths 1, 4, 6 is impossible because the two smaller sides added together are smaller than the largest size and so there would be no way to connect the two smaller sides if they're attached to the larger side.

    So logically, we can say that if the two smaller sizes added together <what goes here?> the larger side, the triangle is valid.

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

    Default

    if (a+b) < c return true

    (not sure if i've written that out correctly... i'm sure you're used to me saying that by now)

    public class Triangle {

    double a, b, c;

    public Triangle( double a, double b, double c) {

    public boolean isValid() {

    if ((a+b) < (c)){

    return true;

    } else

    return false;

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

    Default

    Fuba,

    Can you tell me if I am getting anywhere with this?

    public class TriangleArea {

    public static void main( String[] args )
    {
    int a;
    int b;
    int c;
    boolean state;
    if ((a+b) < (c)){
    state = true;

    state = false;
    }

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

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

  15. #35
    doomsword2001 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    this kind of code will make you confused:
    if ( a+ b <= c){
    if (b + c <= a){
    if (a + c <= b){
    return false;
    }else{
    returnable = true;}}

    else {
    returnable = true;
    }

    you can use this: & means and
    if ( (a+ b <= c) & (b + c <= a) & (a + c <= b) )
    {returnable = false;}
    else
    {returnable = true;}

    return returnable;

    sry i didnt know how to use code tags
    Java Code:
     testing :D

  16. #36
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Atia of the julii View Post
    if (a+b) < c return true

    (not sure if i've written that out correctly... i'm sure you're used to me saying that by now)
    Are you sure this is right, that a triangle is valid if the two smaller sides together are smaller than the larger side?? Come on now. This requires nothing more than elementary logic.

  17. #37
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    Thankyou Doomsword, all constructive criticism is appreciated!

    So you can use this: & means and
    if ( (a+ b <= c) & (b + c <= a) & (a + c <= b) )
    {returnable = false;}
    else
    {returnable = true;}

    return returnable;

    Is that to determine if it is a triangle? I'm sure curmudgeon you will find it pathetic that i'm asking that but i'm completely new to all of this and I haven't studied maths in over 10years.

  18. #38
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Atia of the julii View Post
    Is that to determine if it is a triangle? I'm sure curmudgeon you will find it pathetic that i'm asking that but i'm completely new to all of this and I haven't studied maths in over 10years.
    Again this has nothing to do being pathetic or with studying math and only requires a simple thought experiment. If you have a stick that is 10 inches long and make it the base of your triangle, and you attach a 2 inch stick to the base's left and a 4 inch stick to the right of the base, attach them so they can swivel, can the two shorter sticks ever meet to form a triangle? If not, what is the requirement for the two shorter sticks to meet? Draw this out if it still doesn't make sense.

  19. #39
    Join Date
    Nov 2010
    Posts
    87
    Rep Power
    0

    Default

    Java Code:
    public class TriangleArea {
    
    	public static void main( String[] args )
    	{}
    		boolean isTriangle (double a, double b, double c)
    		{
    			if ( a < 0 && b < 0 && c < 0)
    			return false;
    
    			else
    			return  true;
    	}
    		int a;
    		int b;
    		int c;
    		boolean returnable;{
    		if ( (a+ b <= c) & (b + c <= a) & (a + c <= b) )
    		{returnable = false;}
    		else
    		{returnable = true;}
    	{
    		   for (a= 1; a <= 20; a++) {
    			   for (b= a; b <= 20; b++) {
    			      for ( c= b; c <= 20; c++) {}
    			      
    			      if ((a < b+c) && (b < a+c) && (c < a+b)) break;
    			      else;
    			      continue;
    			      }
    			         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;
    			      }
    	}
    This is what I have so far... I have a syntax error on the line saying 'continue;' but when I insert '}' as suggested, the error remains.

    Curmudgeon, I understand that 4+2=6 and the base is 10 so the total of the non-base sides would have to equal 10 for it to be a triangle. Am i now using correct code for this in my code above?

    I am sorry if i snapped, I have been doing this for a long time and i'm just pissed off with everything now.

    :)

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

    Default

    Sorry I meant to post this as updated code...

    Java Code:
    public class TriangleArea {
    
    	public static void main( String[] args )
    	{}
    		
    		int a;
    		int b;
    		int c;
    		boolean isTriangle;{
    		if ( (a+ b <= c) & (b + c <= a) & (a + c <= b) )
    		{isTriangle = true;}
    		else
    		{isTriangle = false;}
    	{
    		   for (a= 1; a <= 20; a++) {
    			   for (b= a; b <= 20; b++) {
    			      for ( c= b; c <= 20; c++) {}
    			      
    			      if ((a < b+c) && (b < a+c) && (c < a+b)) break;
    			      else;
    			      continue;
    			      }
    			         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 2 of 4 FirstFirst 1234 LastLast

Similar Threads

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