str : to store a word
max : to store the highest alphabetical character present in the word
min : to store the lowest alphabetical character present in theword
WordOp() : default constructor
Void input() : to accept the word in UPPERCASE
Void calcMaxMin() : to find the highest and the lowest character present in the word. Void display() : to display the word along with the highest and the lowestcharacter
Example: If word is "TWEAK" then
Highest Character =W
Lowest Character = A
Read a word using next() method of Scanner class. Now extract the individual character from the word using charAt() method and store into a charcter variable called ch. Now compare the ch to max or min and store the highest character in max variable and lowest character in min variable. Now print the variable.
import java.util.*; public class WordOp { private String str; private char max,min; public WordOp() { max='A'; min='Z'; } void input() { Scanner sc=new Scanner(System.in); System.out.println("Enter a String in a upper case : "); str=sc.next().toUpperCase(); } void calcMaxMin() { char ch; for(int i=0;i<str.length();i++) { ch=str.charAt(i); if(ch<min) min=ch; if(ch>max) max=ch; } } void display() { System.out.println("Highest Character = " + max); System.out.println("Lowest Character = " + min); } public static void main(String []args) { WordOp op=new WordOp(); op.input(); op.calcMaxMin(); op.display(); } }
Enter a String in a upper case :
TWEAK
Highest Character = W
Lowest Character = A