Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default Need help with Java!

    I need help with this problem; I keep getting the message "Exception in thread "main" java.lang.NoSuchMethodError: main", any help weill be appreciated.

    The problem in my book was to design a class named Rectangle to represent a rectangle . the class contains:

    * two double data fields named Width and Height that specify the width and height of the rectangle. the default values are 1 for both Width and Height.

    * a string data field named color that specifies the color of a rectangle .Hypothetically , assume that all rectangles color are the same , the default color is white .

    * a no arg constructor that creates a default rectangle.

    * a constructor that creates a rectangle with a specified width and height.

    * the accessor and mutator methods for all three data fields.

    * a method named getArea() that returns the area of this rectangle.

    * a method named getPerimeter() that returns the perimeter.

    Draw the UML diagram , implement the class , and write a test program that creates two Rectangle objects . assign width 4 and height 40 to first object and width 3.5 and height 35.9 for the second one . assign color red to all Rectangle objects .
    display the properties of both objects and find their areas and perimeters.

    __________________________________________________
    //Rectangle.java

    public class Rectangle {

    // fields
    private double width;
    private double height;
    private String color;

    // constructor
    public Rectangle(){
    width = 1;
    height = 1;
    color = "white";
    }

    // accessor methods
    public double getWidth(){
    return width;
    }

    public double getHeight(){
    return height;
    }

    public String getColor(){
    return color;
    }

    // mutator methods
    public void setWidth(double width){
    this.width = width;
    }

    public void setHeight(double height){
    this.height = height;
    }

    public void setColor(String color){
    this.color = color;
    }

    public double getArea(){
    return width * height;
    }

    public double getPerimeter(){
    return (width * 2) + (height * 2);
    }

    }

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Sorry, without reading your very long post, I'm guessing that you're compiling against a different version of Java to the one you're running against.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Nay....

    I'm assuming that you tried to run the class the following way?
    Java Code:
    Java Rectangle
    if so... you can't. The class has no main method. You need to call the methods of this class from another class that does have a main method.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •