You are here: Home / Topics / Create sqr and cube methods that take an integer (int) and return its square and cube, respectively.

Create sqr and cube methods that take an integer (int) and return its square and cube, respectively.

Filed under: Java on 2024-02-27 13:44:13

This example explains the said statement.

Methods must be public and static, with an int return type.

Method main is not involved in testing.

Requirements:
The Solution class must have public static sqr(int) method, which returns a square of a number (type int).
The Solution class must have public static cube(int) method, which returns a cube of a number (type int).

 

public class Solution {
   public static int sqr(int num){
       return num * num;
   }
   
   public static int cube(int num){
       return num * num * num;
   }
   public static void main(String[] args) {
       int number = 3;
       System.out.printf("%d - %d - %d", number, sqr(number), cube(number));
   }

   //write your code here
}

About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.