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...RectangleDemo.java:76: reached end of file while parsing

but i'm not sure where to put it, and i dont know if it will work after i had a bracket. could someone please help me

this is what i have:

public class RectangleDemo {


private class Rectangle {


private double height;
private double width;
private String color; 

public Rectangle(double wid, double high){
height = high;
width = wid;
} 

public Rectangle(){
height = 1;
width = 1;
color = "White";
} 



public void setHeight(double high){
height = high;
}

public void setWidth(double wid){
width = wid;
}

public void setColor(String col){
color = col;
} 


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

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

public void getColor(){
System.out.println("Color is: " + color +"\n");
return;
}

}
public class RectangleDemo {
public static void main(String[] args){
Rectangle box1 = new Rectangle();
Rectangle box2 = new Rectangle(4, 40);
Rectangle box3 = new Rectangle(3.5, 35.9);

String Color = "Red";

box1.setColor(Color);
box2.setColor(Color);
box3.setColor(Color);

box1.getColor();
box2.getColor();
box3.getColor();

System.out.println("The perimeter of the first box is: " + box1.getPerimeter() + "\n");
System.out.println("The perimeter of the second box is: " + box2.getPerimeter() + "\n");
System.out.println("The perimeter of the third box is: " + box3.getPerimeter() + "\n");

System.out.println("The area of the first box is: " + box1.getArea() + "\n");
System.out.println("The area of the second box is: " + box2.getArea() + "\n");
System.out.println("The area of the third box is: " + box3.getArea() + "\n");
}
}

Edited 5 Years Ago by peter_budo: Adding code tags to old post

public class RectangleDemo {


private class Rectangle {


private double height;
private double width;
private String color; 

public Rectangle(double wid, double high){
height = high;
width = wid;
} 

public Rectangle(){
height = 1;
width = 1;
color = "White";
} 



public void setHeight(double high){
height = high;
}

public void setWidth(double wid){
width = wid;
}

public void setColor(String col){
color = col;
} 


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

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

public void getColor(){
System.out.println("Color is: " + color +"\n");
[B]return;[/B]
}
}
}

NEW CLASS
public class RectangleDemo {
public static void main(String[] args){
Rectangle box1 = new Rectangle();
Rectangle box2 = new Rectangle(4, 40);
Rectangle box3 = new Rectangle(3.5, 35.9);

String Color = "Red";

box1.setColor(Color);
box2.setColor(Color);
box3.setColor(Color);

box1.getColor();
box2.getColor();
box3.getColor();

System.out.println("The perimeter of the first box is: " + box1.getPerimeter() + "\n");
System.out.println("The perimeter of the second box is: " + box2.getPerimeter() + "\n");
System.out.println("The perimeter of the third box is: " + box3.getPerimeter() + "\n");

System.out.println("The area of the first box is: " + box1.getArea() + "\n");
System.out.println("The area of the second box is: " + box2.getArea() + "\n");
System.out.println("The area of the third box is: " + box3.getArea() + "\n");
}
}

(For future refrence, put your code in code brackets, it's easier to read)

You actually have three problems here.
One: The RectangleDemo class should be in it's own file.
Two: Line 49 (between the two }) should have another }. The error you got meant that java was expecting more code, meaning you didn't close the class.
Three: Your public void GetColor() should have no return statment, being that it is a void.

Cheers,
Joe

Edited 6 Years Ago by joehms22: Fix Code

how do i put the rectangledemo in its own file?

Make a new file with the same name, same caps and spelling, with a .java extension (RectangleDemo.java), and cut and paste the RectangleDemo class code. This all needs to be in the same folder. Unless you are using an ide like BlueJ.

Make a new file with the same name, same caps and spelling, with a .java extension (RectangleDemo.java), and cut and paste the RectangleDemo class code. This all needs to be in the same folder. Unless you are using an ide like BlueJ.

Wouldn't naming them the same overwrite the origional file? I'm confused as to what code should be in what files?

This article has been dead for over six months. Start a new discussion instead.