• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to fix error in Rectangle Class problem in Java?  RSS feed

 
Nick Hallloran
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is pretty long so i will do my best to explain it..(The Rectangle class) 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 have the
same color. The default color is white.

-
A no-arg constructor that creates a default
rectangle.

-
A constructor that creates a rectangle with the
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 for the class. Implement the class.
Write a test program that creates two Rectangle objects. Assign width 4 and height 40 to the first
object and width 3.5 and height 35.9 to the second object. Assign color red to all Rectangle
objects. Display the properties of both
objects and find their areas and perimeters.

My question is that i keep gettin this error...Rectange.java:59: RectangleDemo is public, should be declared in a file named RectangleDemo.java
public class RectangleDemo {

 
Paul Clapham
Sheriff
Posts: 22816
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Hallloran wrote:My question is that i keep gettin this error...Rectange.java:59: RectangleDemo is public, should be declared in a file named RectangleDemo.java


That seems like a pretty good error message. Unlike many, it tells you what you have to do to solve the problem. Why don't you do that?
 
ahamed irshad
Ranch Hand
Posts: 31
C++ Notepad Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please make rectangledemo class as public and let the other class be default.

if you make rectangle class as public and rectangledemo class as default,you will get an exception that no such main method present.

we should save the file name with the name of the class which is declared public
 
Colin Wright
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes just to clarify;

you can only have one public class in a java source file and you have 2 hence the error.

So pop RectangleDemo in it's own file as suggested.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!