a. Create a class named Exponent. Its main() method accepts an integer value fro
ID: 3640212 • Letter: A
Question
a. 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. b. Modify the Exponent program so that the cubing method calls the square method, then multiplies that result by the numbers to calculate the cubed value that it returns. Save the application as Exponent2. java.Explanation / Answer
public class Exponent {
public static int square(int value) {
return value * value;
}
public static int cube(int value, int square) {
return value * square;
}
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter a value : ");
int value = sc.nextInt();
System.out.println("Square Value :" + Exponent.square(value));
System.out.println("Cube Value :" + Exponent.cube(value, Exponent.square(value)));
}
}
public class Exponent2 {
public static int square(int value) {
return value * value;
}
public static int cube(int value) {
return value * square(value);
}
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter a value : ");
int value = sc.nextInt();
System.out.println("Square Value :" + Exponent.square(value));
System.out.println("Cube Value :" + Exponent.cube(value));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.