Create a class named Exponent. Its main() method accepts an integer value from a user at the keyboard, and in turn passes the value to a method that squares the number (multiplies it by itself) and to a method that cubes the number (multiplies it by itself twice). The main() method displays the results. Create the two methods that respectively square and cube an integer that is passed to them, returning the calculated value. Save the application as Exponent.java
import java.util.*;
class Exponent {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter number: ");
int number = keyboard.nextInt();
System.out.println("Square: " + square(number));
System.out.println("Cube: " + cube(number));
keyboard.close();
}
private static int cube(int number) {
return number * number * number;
}
private static int square(int number) {
return number * number;
}
}
Comments
Leave a comment