Best Programming Practice
1. Use Variables including for Fixed, User Inputs, and Results
2. Use Methods instead of writing code in the main() function
3. Proper naming conventions for all variables and methods
4. Proper Program Name and Class Name
5. Handle Checked and Unchecked Exceptions wherever possible
6. Proper Method Name which indicates action taking inputs and providing result
Sample Program 1: Create a program to find all the occurrences of a character in a string using
charAt() method
a. Take user input for the String and occurrences of the Character to find
b. Write a method to find all the occurrences of the characters.
i. The logic used is to first find the number of occurrences of the character and
ii. then create an array to store the indexes of the character
c. Call the method in the main and display the result
Java
// Program to find all the occurrences of a character in a string
import [Link];
class StringAnalyzer {
// Method to find all the index of a character in a string using charAt()
// method and return them in an array
public static int[] findAllIndexes(String text, char ch) {
// The count is used to find the number of occurrences of the character
int count = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == ch) {
count++;
}
}
// Create an array to store the indexes of the character
int[] indexes = new int[count];
int j = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == ch) {
indexes[j] = i;
j++;
}
}
return indexes;
1
}
public static void main(String[] args) {
// Take user input for Text and Character to check Occurrences
Scanner sc = new Scanner([Link]);
[Link](Enter a text: ");
String text = [Link]();
[Link]("Enter a character to find the occurrences: ");
char ch = [Link]().charAt(0);
// Find the occurrences of the character
int[] indexes = findAllIndexes(text, ch);
// Display the occurrences of the character
[Link]("Indexes of the character '" + ch + "': ");
for (int i = 0; i < [Link]; i++) {
[Link](indexes[i] + " ");
}
}
}
Lab Practice Programs (Any Six)
1. Write a program to find and return the length of a string without using the length() method
Hint =>
a. Take user input using the Scanner next() method
b. Create a method to find and return a string's length without using the built-in length()
method. The logic for this is to use the infinite loop to count each character till the
charAt() method throws a runtime exception, handles the exception, and then return
the count
c. The main function calls the user-defined method as well as the built-in length() method
and displays the result
2. Write a program to split the text into words, compare the result with the split() method and
display the result
Hint =>
a. Take user input using the Scanner nextLine() method
b. Create a Method to find the length of the String without using the built-in length()
method.
2
c. Create a Method to split the text into words using the charAt() method without using the
String built-in split() method and return the words. Use the following logic
i. Firstly Count the number of words in the text and create an array to store the
indexes of the spaces for each word in a 1D array
ii. Then Create an array to store the words and use the indexes to extract the words
d. Create a method to compare the two String arrays and return a boolean
e. The main function calls the user-defined method and the built-in split() method. Call the
user defined method to compare the two string arrays and display the result
3. Write a program to split the text into words and return the words along with their lengths in a
2D array
Hint =>
a. Take user input using the Scanner nextLine() method
b. Create a Method to split the text into words using the charAt() method without using the
String built-in split() method and return the words.
c. Create a method to find and return a string's length without using the length() method.
d. Create a method to take the word array and return a 2D String array of the word and its
corresponding length. Use String built-in function [Link]() to generate the String
value for the number
e. The main function calls the user-defined method and displays the result in a tabular
format. During display make sure to convert the length value from String to Integer and
then display
4. Write a program to split the text into words and find the shortest and longest strings in a
given text
Hint =>
a. Take user input using the Scanner nextLine() method
b. Create a Method to split the text into words using the charAt() method without using the
String built-in split() method and return the words.
c. Create a method to find and return a string's length without using the length() method.
d. Create a method to take the word array and return a 2D String array of the word and its
corresponding length. Use String built-in function [Link]() to generate the String
value for the number
e. Create a Method that takes the 2D array of word and corresponding length as
parameters, find the shortest and longest string and return them in an 1D int array.
f. The main function calls the user-defined methods and displays the result.
5. Write a program to find vowels and consonants in a string and display the count of Vowels
and Consonants in the string
Hint =>
a. Create a method to check if the character is a vowel or consonant and return the result.
The logic used here is as follows:
3
i. Convert the character to lowercase if it is an uppercase letter using the ASCII values
of the characters
ii. Check if the character is a vowel or consonant and return Vowel, Consonant, or Not
a Letter
b. Create a Method to Method to find vowels and consonants in a string using charAt()
method and finally return the count of vowels and consonants in an array
c. Finally, the main function takes user inputs, calls the user-defined methods, and displays
the result.
6. Write a program to find vowels and consonants in a string and display the character type -
Vowel, Consonant, or Not a Letter
Hint =>
a. Create a method to check if the character is a vowel or consonant and return the result.
The logic used here is as follows:
i. Convert the character to lowercase if it is an uppercase letter using the ASCII values
of the characters
ii. Check if the character is a vowel or consonant and return Vowel, Consonant, or Not
a Letter
b. Create a Method to find vowels and consonants in a string using charAt() method and
return the character and vowel or consonant in a 2D array
c. Create a Method to display the 2D Array of Strings in a Tabular Format
d. Finally, the main function takes user inputs, calls the user-defined methods, and displays
the result.
7. Write a program to trim the leading and trailing spaces from a string using the charAt()
method
Hint =>
a. Create a method to trim the leading and trailing spaces from a string using the charAt()
method. Inside the method run a couple of loops to trim leading and trailing spaces and
determine the starting and ending points with no spaces. Return the start point and end
point in an array
b. Write a method to create a substring from a string using the charAt() method with the
string, start, and end index as the parameters
c. Write a method to compare two strings using the charAt() method and return a boolean
result
d. The main function calls the user-defined trim and substring methods to get the text after
trimming the leading and trailing spaces. Post that use the String built-in method trim()
to trim spaces and compare the two strings. And finally display the result
8. Write a program to take user input for the age of all 10 students in a class and check
whether the student can vote depending on his/her age is greater or equal to 18.
Hint =>
4
a. Create a method to define the random 2-digit age of several students provided as
method parameters and return a 1D array of ages of n students
b. Create a method that takes an array of age as a parameter and returns a 2D String array
of age and a boolean true or false to indicate can and cannot vote. Inside the method
firstly validate the age for a negative number, if a negative cannot vote. For valid age
check for age is 18 or above to set true to indicate can vote.
c. Create a method to display the 2D array in a tabular format.
d. Finally, the main function takes user inputs, calls the user-defined methods, and displays
the result.
9. Rock-Paper-Scissors is a game played between a minimum of two players. Each player can
choose either rock, paper, or scissors. Here the game is played between a user and a
computer. Based on the rules, either a player or a computer will win. Show the stats of
player and computer win in a tabular format across multiple games. Also, show the winning
percentage between the player and the computer.
Hint =>
a. The rule is: rock-scissors: rock will win (rock crushes scissors); rock-paper: paper wins
(paper covers rock); scissors-paper: scissors win (scissors cuts paper)
b. Create a Method to find the Computer Choice using the [Link]
c. Create a Method to find the winner between the user and the computer
d. Create a Method to find the average and percentage of wins for the user and the
computer and return a String 2D array
e. Create a Method to display the results of every game and also display the average and
percentage wins
f. In the main take user input for the number of games and call methods to display results
10.Create a program to take input marks of students in 3 subjects physics, chemistry, and
maths. Compute the percentage and then calculate the grade as shown in figure below
Hint =>
5
a. Write a method to generate random 2-digit scores for Physics, Chemistry and Math
(PCM) for the students and return the scores. This method returns a 2D array with PCM
scores for all students
b. Write a Method to calculate the total, average, and percentages for each student and
return a 2D array with the corresponding values. Please ensure to round off the values to
2 Digits using [Link]() method
c. Write a Method to calculate the grade based on the percentage as shown in the ref table
and return a 2D array of students' grade
d. Finally write a Method to display the scorecard of all students with their scores, total,
average, percentage, and grade in a tabular format.