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());
}
}
return width = height / 2;must bereturn width * height / 2;java file.java) - in that mode, Java will try to execute the first class it finds, in posted codeTwoDShape-- Solution: move theShapesclass (containing themainmethod) 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, ...)