This age may be wsed for extra coding space for the previous problems. Make sure
ID: 3738907 • Letter: T
Question
This age may be wsed for extra coding space for the previous problems. Make sure you indicate whict is associated with if you use this page. Part I - Coding (50 points) 1. (1s points) Complete the skeleton below for the method named moreDigitsThanLetters takes that a string as input and returns back a boolean value. The method returns true if the stri ng parameter a t r contains more digit characters ('O·-·9" ) than letter characters(' a.-' ?' or 'Aand false otherwise. For example, if atr is "abe123" the method should return false Ifstr is "a331b" the method should return true. Ifstr is "12a?tn the method should retum true (punctuation does not count as a letter or a digit). If str is cmpty, the method should in docs sot count as a letter orade haracteais lel decriptions of return fa1se Note that you will want to use the methods Characteriapiais(e a Character.isLetter(c) that were used in the closed labs for this method -brief descriptions of and these methods can be found on the reference sheet at the back of the exam. Note also that you must NOT prompt the user for input in this method. private static boolean moreDigitsThanLetters (String str) your code goes hereExplanation / Answer
Method Implementation:
//This function will check no of digits are more than no of letters or not
private static boolean moreDigitsThanLetters(String str) {
int cntLetters=0,cntDigits=0;
char ch;
for(int i=0;i<str.length();i++)
{
ch=str.charAt(i);
if(Character.isLetter(ch))
{
cntLetters++;
}
else if(Character.isDigit(ch))
{
cntDigits++;
}
}
if(cntDigits>cntLetters)
return true;
else
return false;
}
______________
Complete Program Demo:
CheckDigitsAndLetters.java
import java.util.Scanner;
public class CheckDigitsAndLetters {
public static void main(String[] args) {
//Declaring variables
String str;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter String :");
str=sc.next();
boolean bool=moreDigitsThanLetters(str);
if(bool)
System.out.println("No of Digits are more than no of Letters");
else
System.out.println("No of Digits are not more than no of Letters");
}
//This function will check no of digits are more than no of letters or not
private static boolean moreDigitsThanLetters(String str) {
int cntLetters=0,cntDigits=0;
char ch;
for(int i=0;i<str.length();i++)
{
ch=str.charAt(i);
if(Character.isLetter(ch))
{
cntLetters++;
}
else if(Character.isDigit(ch))
{
cntDigits++;
}
}
if(cntDigits>cntLetters)
return true;
else
return false;
}
}
_____________________
Output#1:
Enter String :abc123
No of Digits are not more than no of Letters
_____________
Output#2:
Enter String :a331b
No of Digits are more than no of Letters
_____________
Output#3:
Enter String :12a?!
No of Digits are more than no of Letters
____________Thank You