Here is my question
Write a Java class definition called FrustumContainer which describes a lab container shaped like the frustum of a cone, and computes the amount of liquid that each container will hold.
Define a constructor for this class and include accessor methods for the container number, height, radius 1 of the container and radius 2 of the container, and a method to compute the amount of liquid (in litres) that the container can hold.
Also include another class, called ContainerTester, which has a main method to test all methods of the FrustumContainer class.
Compile both classes and execute the tester class using the command line environment.
Just wondering how to go about this, do the two classes have to go in separate files? I don't understand how ContainerTester can test all methods in FrustrumContainer
Here is my code so far:
import java.math.*;
import java.util.Scanner;
public class FrustrumContainer {
Scanner in = new Scanner(System.in);
double num = 0;
double h = 0;
double r1 = 0;
double r2 = 0;
double volume;`
public FrustrumContainer(double num, double h, double r1, double r2, double volume) {
this.num = num;
this.h = h;
this.r1 = r1;
this.r2 = r2;
this.volume = volume;
public static double getNum() {
num ++;
return num;
}
public static double getHeight() {
System.out.println("Enter height of container: ");
h = in.next();
return h;
}
public static double getR1() {
System.out.println("Enter bottom radius: ");
r1 = in.next();
return r1;
}
public static double getR2(){
System.out.println("Enter top radius: ");
r2 = in.next();
return r2;
}
public static double computeLiquid()
}
}
class ContainerTester {
public static void main(String[] args) {
}
}
}
You should store them both in two separate classes and you then can test FrustrumContainer by calling the methods using syntax like
ClassName.methodName();
Because every method in FrustrumContainer is static you don't need to instantiate an FrustrumContainer object, you are calling the method directly from the class instead. You can call like this:
public static void main(String[] args) {
FrustrumContainer.getNum();// note this only returns the number
double num1 = FrustrumContainer.getNum();//you can store the returned type i.e double for num
System.out.println(num1);//and print it to the console
}
You should have two classes,FrustumContainer.class and ContainerTester.class,and put both in the same dictionary.
FrustumContainer.class:
import java.util.Scanner;
public class FrustumContainer {
int num = 0;
double height = 0;
double radius1 = 0;
double radius2 = 0;
double volume = 0;
final static double PI = Math.PI;
Scanner in = new Scanner(System.in);
public FrustumContainer(){
this.num ++;
}
public void getHeight(){
System.out.println("please input the value of height (unit:dm):");
height = in.nextDouble();
}
public void getRadius1(){
System.out.println("please the input the value of radius1 (unit:dm):");
radius1 = in.nextDouble();
}
public void getRadius2(){
System.out.println("please the input the value of radius2 (unit:dm):");
radius2 = in.nextDouble();
}
public double computeLiquid(){
double height1 = 0;
if(height <= 0 || radius1 <= 0 || radius2 <= 0){
System.out.println("please input again");
}else{
if(radius1 < radius2) {
// solve height1,(h1+h)/h1 = r2/r1
height1 = height/(radius2/radius1 -1);
// get volume, 1/3 * PI* (r2*r2*(h1+h) -r1*r1*h1)
volume = (PI * ((radius2 * radius2 * (height1 + height)) - (radius1 * radius1 * height1)))/3;
}else{
height1 = height/(radius1/radius2 -1);
volume = (PI * (radius1 * radius1 * (height1 + height)- radius2 * radius2 * height1))/3;
}
}
return volume;
}
}
ContainerTester.class:
public class ContainerTester {
public static void main(String[] args){
FrustumContainer frustumContainer = new FrustumContainer();
frustumContainer.getHeight();
frustumContainer.getRadius1();
frustumContainer.getRadius2();
int num = frustumContainer.num;
double volume = frustumContainer.computeLiquid();
System.out.println("The volume of NO."+ num +" container is " + volume + " L");
}
}
You can call the methods of FrustumContainer.class in ContainerTester.class like frustumContainer.getHeight()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.