Lab 4 (Methods)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1. Write a java method that prints hello world.

Modify the method so that the method prints the


string that is passed as argument to it.
2. Write a Java method that takes two integers as argument and returns their sum.
3. Write a Java method to find the smallest number among three numbers.
Test Data:
Input the first number: 25
Input the Second number: 37
Input the third number: 29
Expected Output:
The smallest value is 25.0
4. Write a Java method to compute the average of three numbers.
Test Data:
Input the first number: 25
Input the second number: 45
Input the third number: 65
Expected Output
The average value is 45.0

5. Write a Java method to display the middle character of a string.


Note: a) If the length of the string is even there will be two middle characters.
b) If the length of the string is odd there will be one middle character.
Test Data:
Input a string: 350
Expected Output:
The middle character in the string: 5

6. Write a Java method to count all vowels in a string.


Test Data:
Input the string: w3resource
Expected Output:
Number of Vowels in the string: 4

7. Write a Java method to count all words in a string.


Test Data:
Input the string: The quick brown fox jumps over the lazy dog.
Expected Output:
Number of words in the string: 9

8. Write a Java method to compute the sum of the digits in an integer.


Test Data:
Input an integer: 25
Expected Output:
The sum is 7
9. Write a Java method to compute the future investment value at a given interest rate for a
specified number of years.
r nt
Formula for calculation of future amount= p*(1+ ) , is number of time interest is calculated
n
per unit time t, r is rate in decimal.
Sample data (Monthly compounded) and Output:
Input the investment amount: 1000
Input the rate of interest: 10
Input number of years: 5
Expected Output:
Years FutureValue
1 1104.71
2 1220.39
3 1348.18
4 1489.35
5 1645.31

10. Write a Java method to print characters between two characters (i.e. A to P ).
Note: Prints 20 characters per line
Expected Output:
()*+,-./0123456789:;
<=>?@ABCDEFGHIJKLMNO
PQRSTUVWXYZ[\]^_`abc
defghijklmnopqrstuvw
xyz

11. Write a Java method to check whether a year (integer) entered by the user is a leap year or not.
Expected Output:
Input a year: 2017
false

12. Write a Java method to check whether a string is a valid password.


Password rules:
A password must have at least ten characters.
A password consists of only letters and digits.
A password must contain at least two digits.
Expected Output:
1. A password must have at least ten characters.
2. A password consists of only letters and digits.
3. A password must contain at least two digits
Input a password (You are agreeing to the above Terms and Conditions.): abcd1234
Password is valid: abcd1234
13. Write a Java method (takes a number n as input) to displays an n-by-n matrix. (0’s and 1’s
displayed are random)
Expected Output:
Input a number: 10
1001100011
0010101000
0101000001
1110000111
1101110100
1000110000
0010000111
1101010010
0010000110
1110011110

14. Write Java methods to calculate the area of a triangle.


Expected Output:
Input Side-1: 10
Input Side-2: 15
Input Side-3: 20
The area of the triangle is 72.6184377413890

15. Write a Java method to find all twin prime numbers less than 100.
Expected Output: (define a method to check if a number is prime or not)
(3, 5)
(5, 7)
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)

16. Write a method that takes an integer as argument and checks if the number is palindrome or
not. The method should return true if the number is palindrome or false if not. Write a main
method to check the method you have created.

17. Write a method named operation that takes two integer arguments and a char argument,
perform the calculation as per the char argument, and return the result:
Example: operation(2,5,’+’) should return 10 as result.
18. Write a program that uses the function power() to raise a number m to power n. The function
takes integer values for m and n and returns the result correctly. Use a default value of 2 for n to
make the function calculate squares when this argument is omitted. Write a function main() to
pass the value of m and n and display the calculated result.
19. Write a method that reverses the string passed as argument.
20. Write a method that replaces all the vowels of a string passed as argument with the next
character.
21. Write a method that accepts two strings as an argument, str1 and str2, and checks whether if
str2 is substring of str1 or not.
22. Write the definition of different methods as utility function for array as
a. a method to add item into array at last
b. a method to add item into array at position specified by user
c. a method to remove an item from last of array
d. a method to remove an item for the position specified by user
e. a method to remove an item specified by user, return false if the item is not found.
f. a method to return the size of an array.
g. a method to display all the items in array.
h. a method to return the item from index specified by user.
i. a method to return the index of item specified by user, return -1 if the item is not found.
j. a method to check if array is empty.
k. a method to check if array is full.
l. a method to replace occurrence of old item by new item, both specified by user.

You might also like