HelpGetting started with objects

Posts 12 of 2 · Page 1 of 1
Getting started with objects
SOLVED! Not sure how to delete threads :/


Hi everyone,

First of all I'd like to say what up? Feeling fresh to be a new member to this forum ^_^

To the request!

So a small task I've been assigned with as homework for my course is to

Code:
Create a class that represents a rectangle. A rectangle is specied by the coordinates (x; y) of its
top-left corner and its width and height. This class will be used to create many rectangle objects,
and it must feature:
{ dynamic elds (variables) to store x, y, the width and the height as fractional numbers;
{ a constructor with parameters that provide x, y, the width and the height;
{ a function that returns the area of the rectangle;
{ a function that returns the length of the perimeter of the rectangle; and
{ a function that returns a string representation of the rectangle, the four numbers, within a pair
of parentheses, separated by commas. This function must be named toString().
 Write, in a separate class, a main method that accepts the four numbers as command line arguments,
instantiates a rectangle object, and prints the string representation of the rectangle, the area and
the perimeter.
So I created 2 classes, TestRect and Rectangle.

Rectangle.java

Code:
package lab3;

public class Rectangle {
    private double x;
    private double y;
    private double width;
    private double height;
    public double area;
    public double perimeter;
    public String rectangle;

    public Rectangle(double newX, double newY, double newWidth, double newHeight){
       x = newX;
       y = newY;
       width = newWidth;
       height = newHeight;
    }
    
    public double area(){
        area = width*height;
        return area;
    }
    
    public double perimeter(){
       perimeter = (width*2)+(height*2);
       return perimeter;
    }
    
    public String toString(){
       rectangle = "(" + x + ", " + y + ", " + width + ", " + height + ")";
       return rectangle;
    }
}
TestRect.java

Code:
package lab3;

public class TestRect {

    private Rectangle rectangle;
    
    public void main(String[] args){
       rectangle = new Rectangle( Integer.parseInt(args[0]), 
                                  Integer.parseInt(args[1]), 
                                  Double.parseDouble(args[2]), 
                                  Double.parseDouble(args[3]));
       rectangle.area();
       rectangle.perimeter();
       rectangle.toString();
       System.out.println("Rectangle: " + rectangle.rectangle);
       System.out.println("Area: " + rectangle.area);
       System.out.println("Perimeter: " + rectangle.perimeter);
       
    }
}
So everything compiles all nice but I get no output when I execute the program.

I'm a bit stumped at the moment.

Any help would be so greatly appreciated!
edit: removed. looks good.

Why did you make 3 of the member variables public and 3 private?

Also, you should put
Code:
       rectangle.area();
       rectangle.perimeter();
       rectangle.toString();
inside the constructor of the rectangle class. Otherwise you're forcing anyone who uses your class to *remember* to call the functions before using the properties.

-Actually, since you're storing all the data anyway (and not allowing x,y,width,height to be changed: ie. size is constant), you should really only calculate it all once! Inside the constructor, calculate perimeter,area, and tostring. Then make the functions simply return the private members - no calculations needed. And you avoid the above issue about remembering to call the functions to 'initialize' the values before use.
Posts 12 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?