DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2005

    Questions about simple program

    Ladies and gents:

    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.


    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. #2
    Join Date
    Nov 2004
    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)

     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.
    eschew obfuscation

Similar Threads

  1. Help Needed w/ simple Calculator Program!
    By blumoon02k in forum Java
    Replies: 4
    Last Post: 03-24-2005, 04:39 PM
  2. Replies: 2
    Last Post: 03-07-2002, 07:49 AM
  3. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 10:06 AM
  4. How can I build copy protection into my program
    By W.Pierce in forum VB Classic
    Replies: 1
    Last Post: 12-11-2001, 08:28 AM
  5. Some simple networking questions
    By David in forum Java
    Replies: 1
    Last Post: 07-18-2000, 05:13 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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.