Help with a Rectangle class program
For our assignment this week, the instructor gave us skeleton code that we had to fill in the body. He gave us some of the body statements so between that and some of the examples in our text book I've filled in the rest. The problem is though I haven't been able to get it to compile. I'm getting an error message that says it's missing a return type (the methods themselves are what the instructor gave to us as skeleton code). I've experimented by naming the methods as public double Rectangle(s) but I still receive a return statement required error. Any and all help would be greatly appreciated!
public class Program6
{
private double width = 2;
private double height = 2;
private static String color = "white";
public static void main(String[] args)
{
}
public Rectangle()
{ //When I compile, it tells me I'm missing a return type
}
public Rectangle(double width, double height, String color)
{
this.width = width;
this.height = height;
this.color = color;
//When I compile, it tells me I'm missing a return type here too
}
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
public double getHeight()
{
return height;
}
public void setHeight(double height)
{
this.height = height;
}
public static String getColor()
{
return color;
}
public static void setColor(String color)
{
color = "white";
}
public double findArea()
{
return width * height;
}
}
[1706 byte] By [
Ranger94a] at [2007-11-26 20:01:40]

The class name and constructor names must be the same. Since your class is called Program6 and the constructors are called Reactangle the compiler assumes they are methods and therefore need to have a return type.
P.S. When posting code highlight it and click the code button above the message box to retain formatting.
Duh- I knew that! Thanks for pointing that out! I'm now able to get it to compile but the only result it showing me is the width. How can I get it to display the length, color and area as well?
public class Rectangle
{
private double area;
private double width;
private double height;
private static String color;
public static void main(String[] args)
{
double area;
double width = 2;
double height = 2;
color = "white";
System.out.println("The width of the rectangle is " + width);
System.out.println("The height of the rectangle is " + height);
System.out.println("The color of the rectangle is " + color);
System.out.println("The area of the rectangle is " + area);
}
public Rectangle()
{
}
public Rectangle(double width, double height, String color)
{
this.width = width;
this.height = height;
this.color = color;
}
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
public double getHeight()
{
return height;
}
public void setHeight(double height)
{
this.height = height;
}
public static String getColor()
{
return color;
}
public static void setColor(String color)
{
color = "white";
}
public double findArea()
{
area = (width * height);
return area;
}
}
> P.S. When posting code highlight it and click the code button above the message box to retain formatting.What part of this did you not understand?
double area;
double width = 2;
double height = 2;
color = "white";
System.out.println("The width of the rectangle is " + width);
System.out.println("The height of the rectangle is " + height);
System.out.println("The color of the rectangle is " + color);
System.out.println("The area of the rectangle is " + area);
This is your entire program. The output will be:
The width of the rectangle is 2.0
The height of the rectangle is 2.0
The color of the rectangle is white
The area of the rectangle is 0.0
Perhaps you need to create a Rectangle object and call its methods
there were too many errors in your progran. you never created an object also. you have 3 parameters in the constructor and you were passing nothing. here is an update version. learn and analyze.
class RectangleTester
{
private double width;
private double height;
private String color;
public RectangleTester(double width, double height, String color)
{
this.width = width;
this.height = height;
this.color = color;
}
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
public double getHeight()
{
return height;
}
public void setHeight(double height)
{
this.height = height;
}
public String getColor()
{
return color;
}
public double findArea()
{
double area = width * height;
return area;
}
}
class Rectangle
{
public static void main(String[] args)
{
RectangleTester rec = new RectangleTester(2, 2, "white"); //try to create an object and invoke it to call its methods.
System.out.println("The width of the rectangle is " + rec.getWidth());
System.out.println("The height of the rectangle is " + rec.getHeight());
System.out.println("The color of the rectangle is " + rec.getColor());
System.out.println("The area of the rectangle is " + rec.findArea());
}
}
output:
The width of the rectangle is 2.0
The height of the rectangle is 2.0
The color of the rectangle is white
The area of the rectangle is 4.0
Well done Supermike, saving students from thinking yet again. Why don't you sit his/her exam for them as well!