r/javahelp • • Mar 05 '23

Solved Implementing a method within a toString method

I'm writing a class in which I have a class representing a circle. With the radius, two public methods were created to calculate the area and perimeter respectively. How might I call these methods in the toString method of the class? I have attached some of the class for visual representation.

Note: There is no good reason for you to down vote my posts.

0 Upvotes

10 comments sorted by

View all comments

2

u/ChaiTRex Mar 06 '23 edited Mar 06 '23

How to do toString

You would make a toString method. Inside that toString method, you'd use + to put things together into a new String. For example, "My name is " + this.getName() + " and my age is " + this.getAge() would be a String you could return.

A few comments on the code you have

A circle with radius zero isn't that useful. A much more common circle is a unit circle (radius 1).


Methods in Circle can access the private fields of other instances of Circle, so you can have public Circle(Circle other) { this.radius = other.radius; }.


It's simpler to use r * r than Math.pow(r, 2).


Because Circle instances already know their radius, the area and perimeter methods should lose the static, have no arguments, and then use this.radius instead of r.


Since you're using get for getting other information from the Circle, it would look more uniform if you named the methods getArea and getPerimeter.


You should use proper indentation. With your starting code, that would be something like:

public class Circle {
    private double radius;
    
    public Circle(double r) {
        radius = r;
    }
    
    public Circle() {
        radius = 0;
    }
    
    public Circle(Circle circle) {
        this.radius = circle.getRadius();
    }
    
    public double getRadius() {
        return this.radius;
    }
    
    public void setRadius(double r) {
        radius = r;
    }
    
    public static double area(double r) {
        return (Math.PI) * Math.pow(r, 2);
    }
    
    public static double perimeter(double r) {
        return 2 * (Math.PI) * r;
    }
}

Notice that every time you have a {, you make a new line and indent it four more spaces than before. Every time you have a } (which should generally be the only thing on that line), you go back to the previous amount of indentation.

This gives the code a uniform and professional appearance, and it's easy to tell what's inside a class or method, because the stuff that's indented more than the class or method header is part of that class or method.


I'd also recommend that if you're just starting out, always use this. in front of instance variables, like:

        public Circle(double r) {
            this.radius = r;
        }

This will always work, even when the method itself makes a new variable with the same name as the instance variable. It also makes it easy to tell at a glance that it's an instance variable.

1

u/Charlemagne-767 Mar 06 '23

Thank you for sharing