Results 1 to 20 of 68
 11072010, 01:19 PM #1Member
 Join Date
 Nov 2010
 Posts
 87
 Rep Power
 0
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*((sSidea)*(sSideb)*(sSidec)); 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 addedLast edited by Atia of the julii; 11082010 at 01:51 PM. Reason: Moderator Edit: code tags added

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.
 11072010, 01:51 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,375
 Blog Entries
 7
 Rep Power
 20
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
 11072010, 02:10 PM #4Member
 Join Date
 Nov 2010
 Posts
 87
 Rep Power
 0
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 :(

But again to reiterate, if you could tell us what specifically has you stumped, we can give you better help.
 11072010, 02:18 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,375
 Blog Entries
 7
 Rep Power
 20
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 } } }
Java Code:private boolean lengthConstraint(int a, int b, int c) { return a+b+c <= 20; }
kind regards,
Jos
 11072010, 02:33 PM #7Member
 Join Date
 Nov 2010
 Posts
 87
 Rep Power
 0
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!
 11072010, 02:55 PM #8Member
 Join Date
 Nov 2010
 Posts
 87
 Rep Power
 0
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);
}
}
 11072010, 03:02 PM #9Member
 Join Date
 Nov 2010
 Posts
 87
 Rep Power
 0
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....
 11072010, 03:05 PM #10Member
 Join Date
 Nov 2010
 Posts
 87
 Rep Power
 0
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; 11072010 at 03:10 PM.

Don't copy code from the forum. For this assignment you're much better off creating your code from scratch.
 11072010, 03:20 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,375
 Blog Entries
 7
 Rep Power
 20
 11072010, 03:23 PM #13Member
 Join Date
 Nov 2010
 Posts
 87
 Rep Power
 0
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....
 11072010, 03:27 PM #14Member
 Join Date
 Nov 2010
 Posts
 87
 Rep Power
 0

 11072010, 04:41 PM #16Member
 Join Date
 Nov 2010
 Posts
 87
 Rep Power
 0
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 :)
 11072010, 04:45 PM #17
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,375
 Blog Entries
 7
 Rep Power
 20
 11072010, 05:09 PM #18Member
 Join Date
 Nov 2010
 Posts
 87
 Rep Power
 0
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 Autogenerated 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; }}
Last edited by Fubarable; 11072010 at 05:25 PM. Reason: Moderator Edit: Code tags added

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?
 11072010, 05:31 PM #20Member
 Join Date
 Nov 2010
 Posts
 87
 Rep Power
 0
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 Autogenerated 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;
}}
Similar Threads

JavaEntwickler (m/w) im schönen Freiburg gesucht (Firmenwagen, Laptop, iPhone inkl.)
By simon.erhardt in forum Jobs OfferedReplies: 0Last Post: 08112010, 08:19 AM 
Laptop Help
By Lil_Aziz1 in forum Forum LobbyReplies: 9Last Post: 08082010, 05:04 PM 
Access Macaddress on laptop
By dhaivat in forum AWT / SwingReplies: 0Last Post: 07112010, 02:49 PM 
AWT: Painting buttons and text areas in a canvas
By chappa in forum AWT / SwingReplies: 6Last Post: 01092010, 02:37 PM 
text areas, printstream, and "\n"
By diggitydoggz in forum New To JavaReplies: 10Last Post: 12262008, 04:03 PM
Bookmarks