Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
948 views

Java Methods Exercises

The document describes 9 exercises to create Java methods that accept parameters and return values to perform calculations and string operations. The exercises include methods to find the next even number, get the month name from number, calculate distance and slope between two points, check if a year is a leap year, print a name multiple times, sum the digits of a number, print the Fibonacci series, find the greatest common divisor of two numbers, and get the range of numbers in an array. Each method is described with examples of its parameters, return type, and functionality.

Uploaded by

Novelyn Rabino
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
948 views

Java Methods Exercises

The document describes 9 exercises to create Java methods that accept parameters and return values to perform calculations and string operations. The exercises include methods to find the next even number, get the month name from number, calculate distance and slope between two points, check if a year is a leap year, print a name multiple times, sum the digits of a number, print the Fibonacci series, find the greatest common divisor of two numbers, and get the range of numbers in an array. Each method is described with examples of its parameters, return type, and functionality.

Uploaded by

Novelyn Rabino
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Methods Exercises

1. Make a Java program that accepts an integer number and display the next even number. You must
declare a method named “GetNextEvenNumberOf” with a single integer parameter and returns an
integer number (the next even number).
Example1:
Enter a number: 5
Next Even number: 6

Example2:
Enter a number: 22
Next Even number: 24

public static int GetNextEvenNumber(int num){}

2. Make a Java program that accepts integer number as month and display the equivalent month name.
Your program must have a method with one integer argument and returns a string(month name).
Note: 1 for January, 2 for February, and so on.
If the number is not in the range of 1 to 12, return a string “No Corresponding Month Name”.
Example1:
Enter month number(1 to 12): 11
Month Name: November

Example2:
Enter month number(1 to 12): 15
Month Name: No Corresponding Month Name

public static string GetMonthName(int month){}

3. Make a Java program that accepts four integer numbers representing the coordinates of the two
points in a Cartesian plane. Display the distance between two points and the slope of the line. Your
program must have two methods. One method is “GetDistance” that has four integer parameters and
returns the distance of two points. Another method is “GetSlope” that has four integer parameters
also and returns the slope of the line.
( y 2− y 1)
Formulas: distance = √ (x 2−x 1)2 +( y 2− y 1)2 and slope =
( x 2−x 1)
Example:
Enter X1: 5
Enter Y1: 3
Enter X2: 12
Enter Y2: 7
Distance: 8.06225775
Slope: 0.57142857

public static double GetDistance(int x1,int y1,int x2,int y2) {}


public static double GetSlope(int x1,int y1,int x2,int y2) {}
4. Make a program that accepts integer number as a year and print “true” if it is a leap year and “false” if
not. Your program must have a method “IsLeapYear” with one integer parameter and returns a
Boolean status (true/false). Leap year is a year that is divisible by 4 but not 100, or divisible by 400.
Example1:
Enter a year: 2019
Leap Year: false

Example2:
Enter a year: 2016
Leap Year: true

public static Boolean IsLeapYear(int year) {}

5. Make a Java program that accepts user’s name and an integer number(N) and display the name Nth
times. Your program must declare a method name “Print” with two parameters (string and integer)
and prints the string nth times specified on the integer parameter.
Example:
Enter your name: “Aiden”
Enter number of times to display: 5
Aiden
Aiden
Aiden
Aiden
Aiden

public static void Print(string name, int N){}

6. Make a Java program that accepts an integer number and display the sum of all digits of a number.
Your program must have a method name “SumOfAllDigits” with single integer parameter and returns
the sum of all digits of the number.
Example:
Enter a number: 12345
Sum of all digits: 15

public static int SumOfAllDigits(int num) {}


7. Make a Java program that display a Fibonacci series. The program must accepts three integer numbers
representing the first two number of the series and the iteration. Your program must declare a method
“PrintFibonacci” with three integer parameters and print the series separated by space.
Example1:
Enter 1st number: 1
Enter 2nd number: 1
Enter iteration: 8
Fibonacci Series: 1 1 2 3 5 8 13 21

Example2:
Enter 1st number: 4
Enter 2nd number: 1
Enter iteration: 5
Fibonacci Series: 4 1 5 6 11

public static void PrintFibonacci(int n1,int n2,in titer){}

8. Make a Java program that accepts two integer number and display the Greatest Common Divisor (GCD)
of two inputted number. Your program must have a method “GetGCD” that has two integer
parameters and returns an integer number representing the GCD of the two numbers.
Example:
Enter number1: 12
Enter number2: 32
GCD: 4

public static int GetGCD(int n1,int n2){ }

9. Make a Java program that accepts 10 integer numbers and stores them in array variable. Display the
range of the inputted numbers. Range is the difference of the highest number and the lowest number
from the list of numbers. Your program must declare a method “GetRange” that accepts integer array
and return the range of the array numbers.
Example:
Enter number[1]: 2
Enter number[1]: 4
Enter number[1]: 5
Enter number[1]: 2
Enter number[1]: 3
Enter number[1]: 9
Enter number[1]: 5
Enter number[1]: 5
Enter number[1]: 3
Enter number[1]: 7
Range: 7

public static int GetRange(int[] arr){}

You might also like