Ball class representing java spheres

Asked 1 years ago, Updated 1 years ago, 343 views

I want to create a program like the one below, but it doesn't work.I just don't know, so could someone tell me the answer?
Thank you m(__)m

/*
 * Ex07
 * Create a class ball that represents a sphere.
 * Specifically, create the following constructors and methods:
 *     Ball(intradius)--------------------------------------------------------------------------------------------
 *     Ball (Ball ball) --------------------------------------------------------------------------
 *     int getRadius()------------------------------------------------------------
 *     void setRadius(intradius) -- Set radius
 *     double computeVolume() ------- Calculate Volume
 *     boolean equalTo (Ball ball) -- determine if it is the same size as a sphere ball
 *     String to String() ----------- Get string expression "Ball(radius)"
 *
 * However, use intradius as the field.
 */
import java.util.Scanner;
US>classBall}

    // Fields (No Changes)
    private intradius;// radius

    // Constructor (No Changes)
    public Ball() {
    }

    // Constructor (requires creation)
    public Ball (intradius) {

    }

    // Constructor (requires creation)
    public ball (ball ball) {

    }

    // Method for obtaining radius (required creation)
    public int getRadius() {

        return-1; // Delete if not required
    }

    // Method for setting radius (required creation)
    public void setRadius(intradius){
        double; // radius
        Scanner scanner = new Scanner (System.in);

        System.out.print("Radius:" );
        r=scanner.nextDouble();

    }

      // Method for calculating volume (requires creation)
    public double computeVolume() {

        return (4.0/3.0*3.14*radius*radius);
     
    }

    // Determine if it is the same size as the sphere ball (requires creation)
    public boolean equalTo (Ball ball) {

        return false; // Delete if not needed
    }

    // Obtain string representation (requires creation)
    public String to String() {

        US>";// Delete if not required
    }
}

java

2022-09-30 21:50

1 Answers

Once you know the fields of the class and how to use the methods, you can solve them by applying them.

  • Ball(intradius)--------------------------------------------------------------------------------------------------
    • Rewrite radius, so calling the setRadius method in the constructor will solve this problem.
  • Ball(Ball ball)--------------------------------------------------------------------------------
    • I will take the radius from another instance and rewrite the radius, so I will rewrite the radius obtained in ball.getRadius(); with setRadius just like the constructor above.
  • int getRadius()----------------------------------------------------------------------
    • I think the textbook up until this exercise showed you how to use return, so let's use it as a reference.
      Similar QA for this site: About return values and void
  • void setRadius(intradius)--Set radius
  • double computeVolume()------- Calculate Volume
  • boolean equalTo (Ball ball)--Determine if it is the same size as a sphere ball
    • Return the comparison operator between radius and ball.getRadius() in this instance.
  • String to String() ------------- Get string expression "Ball(radius)"
    • I don't know the format of the string, but for example, you can use the sample code below to represent radius and volume.
  • Rewrite radius, so calling the setRadius method in the constructor will solve this problem.
  • I will take the radius from another instance and rewrite the radius, so I will rewrite the radius obtained in ball.getRadius(); with setRadius just like the constructor above.
  • I think the textbook up until this exercise showed you how to use return, so let's use it as a reference.
    Similar QA for this site: About return values and void
  • Return the comparison operator between radius and ball.getRadius() in this instance.
  • I don't know the format of the string, but for example, you can use the sample code below to represent radius and volume.
public String to String(){
    return String.format("The radius of the ball is %d and the volume is %f.", radius, computeVolume();
}


2022-09-30 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.