I'm learning java on my own and I have this code that I'm trying to work through.
Here are the requirements:
- Write a class Rectangle that has two double fields width and height, and a method
void setDimensions(double w, double h)
that assigns its parameter values to these fields. - Then, write the accessor (“getter”) methods double
getArea()
and doublegetPerimeter()
that compute and return the area and the perimeter length of the rectangle, and a mutator (“setter”) methodvoid scale(double sf)
that multiplies the width and the height of the rectangle by the scaling factor sf. Finish up by writing a proper toString method in your class.
public class Rectangle { double width; double length; /** */ public void setDimensions(double width, double length) { width = 5; length = 10; } /* */ public static double getPerimeter(double width, double length) { double perimeter = 2 * (width + length); return perimeter; } /* */ public static double getArea(double width, double length) { double area = (width * length); return area; } /** * */ public void scale(double sf) { sf *= length; sf *= width; } public String toString() { System.out.println("The length is: " + length); System.out.println("The width is: " + width); getArea(width, length); getPerimeter(width, length); return "The area is:" + area; return "The perimeter is:" + perimeter; }
}
With that being said, I am confused with how to format the toString method. Is this toString method simply supposed to print out the values of the area, perimeter, sf, etc? Or am I supposed to return a string of some sort(Sorry I am new I don't really understand completely) Also, I don't think I have my SF method working as it should be. Should the method have SF initialized in the method?
Thanks