PRACTICLE - 4
Object Oriented Programming-I LAB Session
Basic JAVA Programming and using Methods and User Input Processing
1.Write a method with following method header.
public static int gcd(int num1, int num2)
Write a program that prompts the user to enter two integers and compute the gcd of two integers.
2.Write a test program that prompts the user to enter ten numbers. Create a method and invoke it to reverse the
numbers, display the numbers. The input for the 10 numbers should be taken in one line. (the 10 numbers should be separated by space.)
Example: Enter 10 numbers: 1 2 40 5 63 7 8 30 20
Output: The reversed numbers are: 20 30 8 7 63 5 40 2 1
3.String Checker : Write a program to check if both halves of the String contain same number of characters.
a. Take the input String from user.
b. Create a method as void stringChecker(String test){} and call the method using the input string.
c. Print “YES” if both the halves of the String contain same number of character and print “NO” otherwise.
Ignore the case of the characters. If length of String is odd, ignore the middle character.
d. Example:
i. Input : xyzayzax Output: YES
ii. Input: xyZayzax Output: YES
iii. Input: xyzzxxyz Output: NO
iv. Input: xyzaayzax Output: YES
4. Password Validator : We have a requirement for the password as given here.
a. Minimum 9 characters.
b. The alphabets must be between [a-z]
c. Should contain at least one Upper Case letter [A-Z]
d. Should contain at least 1 number [0-9].
e. Should contain at least one special character from [ _ or % or * or $].
Your task is as below.
i. Create a method String passwordValidator(String password){}
ii. Take the password as input from user.
iii. Call the method with password as parameter.
iv. The method will return “Password Accepted” if the password is valid. or “Invalid Password” otherwise.
v. Print the String returned by the method.
Example:
a. Input: demoPassword6$ Output: Password Accepted
b. Input: demoPassword6& Output: Invalid Password
c. Input: demopassword6$ Output: Invalid Password
d. Input: deMopassword$ Output: Invalid Password
5. Interest Calculator : you need to calculate the compound interest and value of the investment after each year by taking the initial amount, interest rate and number of years as input from user.
a. Create a method double[] interestCalculator(doubleinitial_amount, doublenumber_of_years, double
nterest_rate){}
b. Take initial amount, interest rate and number of years as input from user.
c. Pass these values to method. The method will return a double array that will have the size equal to number_of_years. The array will contain the amount by calculating the interest each year.
d. Print the content of array which is returned by the method.
Example:
Enter Initial Amount: 1000
Enter Number of Years: 5
Enter Interest Rate(%): 10
The output of the method will contain the double array with content array=[1100.00, 1210.00, 1331.00, 1464.10, 1610.51]
Print the values present in array in each line like:
The amount after 1 year = 1100.00
The amount after 2year = 1210.00
The amount after 3year = 1331.00
The amount after 4year = 1464.10
The amount after 5year = 1610.51
No comments:
Post a Comment