012-002-007
Static Method Utility
Medium
Problem Description
static method Utility
In this problem, you will create a program that implements static methods (square, max, isEven) in a MathHelper class, reads values from Scanner, calls them directly by class name without creating instances, and displays the results to standard output.
Learning Objective: Understand how to design utility classes using static methods that can be called without creating instances
Overview
Create a utility class MathHelper with static methods for mathematical calculations that can be called directly without creating instances. In the Main class, read values from standard input and pass them to each method.
Specifications
MathHelper class
- Define
public static int square(int n)that returns the square of the argument - Define
public static int max(int a, int b)that returns the larger of two arguments - Define
public static boolean isEven(int n)that returnstruefor even numbers,falsefor odd
Main class
- Read integer
nfrom standard input and print the result ofMathHelper.square(n)in the format"Square of n: result" - Read integers
aandbfrom standard input and print the result ofMathHelper.max(a, b)in the format"Max: result" - Read integer
xfrom standard input and print the result ofMathHelper.isEven(x)in the format"x is even: result" - Read integer
yfrom standard input and print the result ofMathHelper.isEven(y)in the format"y is even: result"
Input Format
n
a b
x
y
Output Format
Square of n: result
Max: result
x is even: result
y is even: result
Sample Input/Output
Input:
5
10 20
7
4
Output:
Square of 5: 25
Max: 20
7 is even: false
4 is even: true
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up