0

I'm currently studying Java in general and inheritance in particular. When I run the code below in an online editor it throws an error related to the main method, however, when I modify it as suggested it throws another similar error. Could someone please cast an eye other the code for an obvious error?

Thanks.

// A simple class hierachy.

// A class for two-dimentional objects.
class TwoDShape {
    double width;
    double height;

    void showDim() {
        System.out.println("Width and height are " + width + " and " + height);
    }
}

// A subclass of TwoDShape for triangles.
    class Triangle extends TwoDShape {
    String style;

    double area() {
    return width = height / 2;
    }

    void showStyle() {
    System.out.println("Triangle is " + style);
    }
}

class Shapes { 
 public static void main (String args[]) {
    Triangle t1 = new Triangle () ;
    Triangle t2 = new Triangle () ;

    t1.width = 4.0;
    t1.height = 4.0;
    t1.style = "filled";

    t2.width = 8.0;
    t2.height = 12.0;
    t2.style = "outlined";

    System.out.println("info for t1: ");
    t1.showStyle();
    t1.showDim();
    System.out.println("Area is " + t1.area());

    System.out.println();

    System.out.println("info for t2: ");
    t2.showStyle();
    t2.showDim();
    System.out.println("Area is " + t2.area());
    }
}
6
  • 3
    What error are you getting? What modification are you refering to? Commented Jun 19 at 8:20
  • 2
    You have a typo in return width = height / 2; must be return width * height / 2; Commented Jun 19 at 13:32
  • The error is as follows: 'Error: Main method not found in class TwoDShape, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application' Thanks for the typo correction Eddi, the issue however remains although the typo would have caught me eventually. Commented Jun 20 at 1:48
  • If I remember correctly, the class with main method must also be public. Commented Jun 20 at 7:58
  • probably executing the file and not compiling, that is, using source-file mode (java file.java) - in that mode, Java will try to execute the first class it finds, in posted code TwoDShape -- Solution: move the Shapes class (containing the main method) to the top of the file; or compile the file (javac file.java) and start the correct class (java Shapes) -- some guessing here (file name, how it is being executed, ...) Commented Jul 18 at 19:00

1 Answer 1

0

I believe the issue here is caused by the online compiler you are using. Judging by the error message, it is looking in TwoDShape for the main method and TwoDShape is the first class created. Try putting Shapes first.

***I would highly recommend installing Java on your computer with an IDE. You will have complete control over your code with an IDE.


An online compiler is extremely limited and could cause issues out of your control.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.