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

Java Program For Practice

This document provides examples of Java programs and methods for practice, including: 1) A program to count positive, negative and zero numbers entered by the user. 2) A program to find the largest and smallest numbers entered by the user. 3) A method to find the smallest of three numbers. 4) Methods to find the middle character of a string, count vowels in a string, count words in a string, and calculate the sum of digits in an integer. 5) A method to validate a password meets criteria of minimum length and number of digits.

Uploaded by

Karan Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views

Java Program For Practice

This document provides examples of Java programs and methods for practice, including: 1) A program to count positive, negative and zero numbers entered by the user. 2) A program to find the largest and smallest numbers entered by the user. 3) A method to find the smallest of three numbers. 4) Methods to find the middle character of a string, count vowels in a string, count words in a string, and calculate the sum of digits in an integer. 5) A method to validate a password meets criteria of minimum length and number of digits.

Uploaded by

Karan Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Program for practice

1. Write a program to enter the numbers till the user wants and at the end it
should display the count of positive, negative and zeros entered.
2. Write a program to enter the numbers till the user wants and at the end
the program should display the largest and smallest numbers entered.
3. Write a program to print Fibonacci series of n terms where n is input by
user:
0 1 1 2 3 5 8 13 24 .....
4. Write a program that generates a random number and asks the user to
guess what the number is. If the user's guess is higher than the random
number, the program should display "Too high, try again." If the user's
guess is lower than the random number, the program should display "Too
low, try again." The program should use a loop that repeats until the user
correctly guesses the random number.
5. Write a program to print following:

i) ********** ii) * iii) *


********** ** **
********** *** ***
********** **** ****
***** *****

iv) * v) 1 vi) 1
222 212
*** 33333 32123
***** 4444444 4321234
******* 555555555 543212345

*********
Java Method Exercises

1. 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

2. 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
3. Write a Java method to display the middle character of a string
Note: a) If the length of the string is odd there will be two middle characters.
b) If the length of the string is even there will be one middle character.
Test Data:
Input a string: 350
Expected Output:

The middle character in the string: 5


4. 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
5. 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
6. 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
Write a Java method to display the middle character of a string

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

The middle character in the string: 5


3. Write a Java method to display the middle character of a string. Note: a) If the
length of the string is odd there will be two middle characters.
b) If the length of the string is even there will be one middle character.
Test Data:
Input a string: 350
Expected Output:

The middle character in the string: 5


4. 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
5. 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
6. 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

7. 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
8. 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
9. 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
10. 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 eight 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

You might also like