Create a project in Eclipse.
Add a package practice
Add all three source code as separate source files into the practice package
Set Run Configurations... to have Main class as practice.MyEffort
Compile and make sure it runs
Export the project as Runnable JAR file
Double click on the result to see your program run
package practice;
import javax.swing.JOptionPane;
/** This class will be used as a driver class for the application
*@author Zoltan Szabo
*@version 1.0.0
*/
public class MyEffort {
public static void main(String[] args) {
// TODO Auto-generated method stub
Alphabet letters=new Alphabet();
String word;
//char answer;
int answer;
do{
word=JOptionPane.showInputDialog(null,"Please, enter a single word you'd like to convert to a percentage.","Word 2 Effort",JOptionPane.QUESTION_MESSAGE);
Word2Percentage result=new Word2Percentage(word,letters);
StringBuilder msg=new StringBuilder("The effort you put in your work is: "+result.getResult()+"%");
JOptionPane.showMessageDialog(null, msg.toString(),"Answer",JOptionPane.INFORMATION_MESSAGE);
//answer=JOptionPane.showInputDialog("Would you like to convert another word?(y/n): ").charAt(0);
//answer=Character.toUpperCase(answer);
answer=JOptionPane.showConfirmDialog(null,"Would you like to convert another word?","Convert Another One!!!",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE );
if ( answer == 0){
continue;
}
else if (answer == 1){
JOptionPane.showMessageDialog(null,"Thank you for using this application, hope to see you again soon!!!","Good Bye",JOptionPane.INFORMATION_MESSAGE);
break;
}
else{
JOptionPane.showMessageDialog(null,"Invalid selection, bye!!!","Good Bye",JOptionPane.INFORMATION_MESSAGE);
break;
}
} while (answer == 0);
}
}
package practice;
/**This is a class that populates an array with alphabet characters using a loop
* @author Zoltan
* @version 1.0.0
*/
public class Alphabet {
/**
* The field representing a value
*/
public final static int SIZE=26;
private Character letters[];
/**
* Class constructor of Driver class with no particular function in this example
* @param value as an integer
*/
public Alphabet(){
letters=new Character[SIZE];
for(int i=0;i<SIZE;i++)
letters[i]=(char) ('A'+i);
}
/**This method will return a character corresponding to its index value
* @param index of character
* @return character at index
*/
public char getCharacter(int index){
return letters[index].charValue();
}
}
package practice;
/**Class that converts the given word to a percentage representation
*@author Zoltan Szabo
*@version 1.0.0
*/
public class Word2Percentage {
private int total;
/**Constructor to the class
*@param word the user supplied word
*@param toConvert the corresponding array where the index value will be used to convert each character to a numerical value
*/
public Word2Percentage(String word, Alphabet toConvert) {
super();
String temp_word=word.toUpperCase();
for (int i = 0; i < temp_word.length(); i++){
for (int j = 0; j < Alphabet.SIZE; j++){
if (toConvert.getCharacter(j) == temp_word.charAt(i)){
total += (j + 1);
}
}
}
}
/**Method to return the final converted numerical value representation of the given word
*@return numerical value of given word
*/
public int getResult(){
return total;
}
}
No comments:
Post a Comment