1. Member
Join Date
Nov 2010
Posts
87
Rep Power
0

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

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

2. 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!

3. 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. Member
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 :(

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

6. Originally Posted by Atia of the julii
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 :(

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. Member
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!

8. Member
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

// 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
bd = 12345678.0;
cd = 1.01233995;

aread = Math.sqrt(sd * (sd - ad) * (sd - bd) * (sd - cd));
}
}

9. Member
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....

10. Member
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;

// 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));
}
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. Originally Posted by Fubarable
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

12. Member
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....

13. Member
Join Date
Nov 2010
Posts
87
Rep Power
0
Originally Posted by JosAH
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

14. Originally Posted by Atia of the julii
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.

15. Member
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 :)

16. Originally Posted by Atia of the julii
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 ;-)

17. Member
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 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;
}}```
Last edited by Fubarable; 11-07-2010 at 05:25 PM. Reason: Moderator Edit: Code tags added

18. 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?

19. Member
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 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 ... Last

Posting Permissions

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