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
}
}
Once you know the fields of the class and how to use the methods, you can solve them by applying them.
ball.getRadius();
with setRadius just like the constructor above.return
, so let's use it as a reference.this
keyword well.ball.getRadius();
with setRadius just like the constructor above.return
, so let's use it as a reference.this
keyword well.public String to String(){
return String.format("The radius of the ball is %d and the volume is %f.", radius, computeVolume();
}
© 2024 OneMinuteCode. All rights reserved.