Results 1 to 5 of 5

Thread: javac errors

  1. #1
    MBD
    MBD is offline Member
    Join Date
    Sep 2011
    Location
    Sale, Cheshire
    Posts
    10
    Rep Power
    0

    Default javac errors

    I am trying to compile the program below, which gives 2 errors:
    <identifier> expected (lines 6 & 7)
    class, interface, or enum expected (lines 9,10,11,12). Would anyone know why please?

    Java Code:
    class TwoDShape {
      private double width;
      private double height;
      private String name;
     {
        width = height = 0.0;
        name = "null";
      }
     TwoDShape(double w, double h, String n) {
        width = w; height = h;
        name = n;
      }
     TwoDShape(double x, String n) {
        width = height = x;
        name = n;
      }
     TwoDShape(TwoDShape ob) {
        width = ob.width;
        height = ob.height;
        name = ob.name;
      }
      double getWidth() { return width; }
      double getHeight() { return height; }
      void setWidth(double w) { width = w; }
      void setHeight(double h) { height = h; }
      String getName() { return name; }
      void showDim() {
        System.out.println("Width and height are " +
                           width + " and " + height);
      }
      double area() {
        System.out.println("area() must be overridden");
        return 0.0;
      }
    }
    
    class Triangle extends TwoDShape { 
        private String style;
    Triangle() {
        super();
        style = "null";
      }
    Triangle(String s, double w, double h) {
        super(w, h, "triangle");
        style = s;
      }
    Triangle(double x) {
        super(x, "triangle");
        style = "isosceles";
      }
    Triangle(Triangle ob) {
        super(ob);
        style = ob.style;
      }
    double area() {
        return getWidth() * getHeight() / 2;
      }
      void showStyle() {
        System.out.println("Triangle is " + style);
      }
    }
    class Rectangle extends TwoDShape {
      Rectangle() {
        super();
      }
    Rectangle(double w, double h) {
        super(w, h, "rectangle");
      }
    Rectangle(double x) {
        super(x, "rectangle");
      }
    Rectangle(Rectangle ob) {
        super(ob);
      }
      boolean isSquare() {
        if(getWidth() == getHeight()) return true;
        return false;
      }
    double area() {
        return getWidth() * getHeight();
      }
    }
    
    class DynShapes {
      public static void main(String args[]) {
        TwoDShape shapes[] = new TwoDShape[5];
        shapes[0] = new Triangle("right", 8.0, 12.0);
        shapes[1] = new Rectangle(10);
        shapes[2] = new Rectangle(10, 4);
        shapes[3] = new Triangle(7.0);
        shapes[4] = new TwoDShape(10, 20, "generic");
        for(int i=0; i < shapes.length; i++) {
          System.out.println("object is " + shapes[i].getName());
    
          System.out.println("Area is " + shapes[i].area());
          System.out.println();
        } 
      }
    }
    Thanks MBD.
    Last edited by JosAH; 10-17-2011 at 02:10 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: javac errors

    Quote Originally Posted by MBD View Post
    I am trying to compile the program below, which gives 2 errors:
    <identifier> expected (lines 6 & 7)
    class, interface, or enum expected (lines 9,10,11,12). Would anyone know why please?
    The code you've show us isn't the code you tried to compile, e.g. lines 6 and 7 are syntactically fine (although a bit strange).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: javac errors

    Actually, the code he showed is syntactically off in that general area. There is a superfluous pair of curly braces in lines 5 and 8. MBD, I'm guessing you meant to make lines 5-8 a constructor? You just forgot the "TwoDShape()" bit before the opening curly brace.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: javac errors

    Quote Originally Posted by DiamondSoul View Post
    Actually, the code he showed is syntactically off in that general area. There is a superfluous pair of curly braces in lines 5 and 8. MBD, I'm guessing you meant to make lines 5-8 a constructor? You just forgot the "TwoDShape()" bit before the opening curly brace.
    No, lines 5 through 8 make a fine initialization block; it isn't necessary though because member variables can be perfectly initialized when defined; but lets wait what the OP has to say ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: javac errors

    Quote Originally Posted by JosAH View Post
    it isn't necessary though because member variables can be perfectly initialized when defined
    It isn't necessary due to OP simply reassigning the default values anyway.
    </nitpick>
    ;)

Similar Threads

  1. help with javac
    By copy you in forum New To Java
    Replies: 8
    Last Post: 09-18-2011, 09:54 PM
  2. javac
    By Sephij in forum New To Java
    Replies: 9
    Last Post: 08-05-2011, 09:00 AM
  3. Replies: 4
    Last Post: 01-04-2011, 01:36 AM
  4. Replies: 3
    Last Post: 03-08-2009, 02:51 AM
  5. javac DOS
    By rdunne in forum New To Java
    Replies: 3
    Last Post: 01-01-2008, 12:49 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
  •