012-002-010

Distinguishing Static and Instance Methods

Hard

Problem Description

Distinguishing static and Instance Methods

In this problem, you will create a program that implements static methods max() and factorial() in a MathUtil class, reads input values with Scanner, calls the methods directly by class name, and displays the results to standard output.

Learning Objective: Understand the difference between static and instance methods and when to use each

Overview

Static methods can be called without an instance as class methods. Instance methods operate on specific objects.

Specifications

  • Create MathUtil class
  • static int max(int a, int b): returns maximum of two values
  • static int factorial(int n): calculates factorial
  • Use Scanner in main to read input, then call MathUtil.max() and MathUtil.factorial()

Input Format

a b
c d
n
m
  • Line 1: pair of integers a, b for first max() call (space-separated)
  • Line 2: pair of integers c, d for second max() call (space-separated)
  • Line 3: integer n for first factorial() call
  • Line 4: integer m for second factorial() call

Output Format

max(a, b) = [maximum of a and b]
max(c, d) = [maximum of c and d]
n! = [factorial of n]
m! = [factorial of m]

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