0

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 double getPerimeter() that compute and return the area and the perimeter length of the rectangle, and a mutator (“setter”) method void 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

mhatch
  • 4,441
  • 6
  • 36
  • 62
BodyBingers
  • 65
  • 3
  • 9
  • Possible duplicate of [How to use the toString method in Java?](http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java) – Frederic Klein Sep 21 '16 at 19:08
  • [In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read.](http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) – azurefrog Sep 21 '16 at 19:10
  • 1
    P.S. Bookmark the API docs now. You will spend many hours reading them in the future. – azurefrog Sep 21 '16 at 19:10
  • `sf *= length;`turn those around (`length *= sf;`), otherwise you are changing sf instead of length/width. Also: yes, the toString method is supposed to return a string, that describes the object. – Frederic Klein Sep 21 '16 at 19:10
  • You might want to check if your IDE can generate toString method for you. If it can't, switch to one which can, because coding Java without code generation aid of IDE is going to be *tedious*, and you'll make many totally unnecessary bugs copypasting or manually typing boilerplate code. Of course always check any IDE-generated code so you understand it... – hyde Sep 21 '16 at 19:44

1 Answers1

1

Usually, the toString method is a human readable string outputting the properties/values of your object.

@Override
public String toString() { 
    String myStr= "{length: " + getLength() + ", width: " + getWidth() + 
    ", perimiter: " + getPerimiter() + ", area: " + getArea() + "}"; 
    return myStr;
} 

// will output something like "{length: 2, width: 3, perimiter: 10, area: 6}"

Also, the scale method should increase length and width by the factor provided as argument.

public void scale(double sf)
{
    length *= sf;
    width *= sf;
}

// better to create getters and setters and use these.

public void setLength(ln){
    this.length = ln;
}
public double getLength(){
    return this.length;
}
// same for width, then...

public void scale(double sf){
    setLength(getLength()*sf);
    setWidth(getWidth()*sf);
}
mhatch
  • 4,441
  • 6
  • 36
  • 62