Questions about simple program

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Oct 2005
Posts
1

## Questions about simple program

I'm a student learning Java at the very beginner level. My tasking is to write a program that takes 3 user inputs for sides of a triangle and calculates area, perimeter, and the cosines of the angles.

Basically, we are asked to create a class called triangle, and a program that will use the triangle class multiple times.

I have having issues with the triangle class.

I've written the code, but I keep getting an error when I compile.

I've been though the program over and over looking for syntax errors and for other mistakes and can't figure it out. Can someone point me in the right direction? Thanks in advance.

JJ

The error is as follows (the carrat under the area line points at the L2):

Triangle.java: 32: ')' expected
area = Math.sqrt(s(s-L1)(s-L2)(s-L3));
^
1 error

The program is below:

public class Triangle

{

public double perimeter (double L1, double L2, double L3);

{
// Perimeter calculations

perimeter = L1+L2+L3;

}

public double area (double L1, double L2, double L3);

{
// Area calculations

double s;

s = (perimeter/2);

area = Math.sqrt(s(s-L1)(s-L2)(s-L3));

}

public double cos (double a, double b, double c);

{
// Cosine calculations

cos = (Math.pow(c,2)-Math.pow(a,2)-Math.pow(b,2))/(-2*a*b);

}

}

2. All variable you use must be declared, you cannot just write:

area= ....;

area must be declared, the compiler must know what it is, an int, a double,
an instance of the MyxTron class...

So its like:

double area=0.0;
.
.
.
area= (something);

or just

double area=(something);

s(s-L2) is ok in the math book, the computer
must have it like: s*(s-L2)

Code:
``` public class Triangle {
// Perimeter calculations
public double perimeter(double L1, double L2, double L3) {

double p = L1 + L2 + L3;

return p;
}

// Area calculations
public double area(double L1, double L2, double L3) {
// perimeter is a method that returns (represents) a double
// value, but you must supply the parameters for it.
double s = (perimeter(L1, L2, L3) / 2);

return Math.sqrt(s * (s - L1) * (s - L2) * (s - L3));
}

// Cosine calculations
public double cos(double a, double b, double c) {

double quack =
(Math.pow(c, 2) - Math.pow(a, 2) - Math.pow(b, 2)) / ( -2 * a * b);

return quack;
// could just as well have written:
// return (Math.pow(c, 2) - Math.pow(a, 2) - Math.pow(b, 2)) / ( -2 * a * b);
}

}```
Last edited by sjalle; 10-02-2005 at 06:57 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
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source