60 397
Assignments Done
Successfully Done
In April 2018

Answer to Question #54131 in Java | JSP | JSF for Nicholas

Question #54131
I have to create a class that represents a rectangle. A rectangle is specified 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 fields (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 Main 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.
Expert's answer
public class MainClass {

public static void main(String[] args) {//topLeftX,topLeftY,width,height

try {
double topLeftX = Double.parseDouble(args[0]);
double topLeftY = Double.parseDouble(args[1]);
double width = Double.parseDouble(args[2]);
double height = Double.parseDouble(args[3]);
Rectangle rec = new Rectangle(topLeftX, topLeftY, width, height);

System.out.println("Area " + rec.getArea());
System.out.println("Perimeter " + rec.getPerimeter());

} catch (ArrayIndexOutOfBoundsException | NumberFormatException ex) {
System.out.println("Wrong command line arguments!");



class Rectangle {

private double topLeftX;
private double topLeftY;
private double width;
private double height;

public Rectangle(double topLeftX, double topLeftY, double width, double height) {
this.topLeftX = topLeftX;
this.topLeftY = topLeftY;
this.width = width;
this.height = height;

public double getArea() {
return width * height;

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

public String toString() {
return "Rectangle: " + topLeftX + " " + topLeftY + " " + width + " " + height;


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

Privacy policy Terms and Conditions