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

•

u/AutoModerator Mar 05 '23

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

4

u/desrtfx Out of Coffee error - System halted Mar 05 '23

Since you haven't in the faintest mentioned what your actual toString method should output, we cannot directly help.

Also, neither the area, nor the perimeter method should be static and even less take an argument. The radius is already part of the Circle object and this is the radius that should be used for the calculations.

What I would imagine is that you will want some string concatenation to integrate the calculated perimeter and area in the output.

1

u/Charlemagne-767 Mar 05 '23

Yes, that is my goal. To include the results of the calculation of the area and perimeter within the toString. I will take what you said to heart and see what I can do

2

u/AzurasTsar Mar 05 '23

You can override toString()... just declare a new method called toString() and have it return perimeter() + area() and whatever else (eg text etc) you might want

1

u/Charlemagne-767 Mar 05 '23

I will try that

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

0

u/[deleted] Mar 06 '23

[deleted]

2

u/Charlemagne-767 Mar 06 '23

I figured it out, bye rewriting the methods themselves and doing area() and perimeter within the toString