HIDE NAV

Cube-This with Methods

The below command line program demonstrates the declaration and use of static methods with both a parameter and return value.

Main.java


package ceccs;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	double userNumber = askForNumber("Input your number to be cubed");
        double cube = cubeNumber(userNumber);
        System.out.println("The cube of " + userNumber + " is " + cube);
    }

    static double askForNumber(String questionText){
        System.out.println(questionText);
        Scanner s = new Scanner(System.in);
        double input = s.nextDouble();
        return input;
    }

    static double cubeNumber(double input){
        return input * input * input;
    }
}