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!