Initial Commit
This commit is contained in:
@ -0,0 +1,109 @@
|
||||
/**
|
||||
* Class containing the methods for conversion
|
||||
* @author Isaac Shoebottom (3429069)
|
||||
*/
|
||||
|
||||
public class Converter {
|
||||
|
||||
/**
|
||||
* Convert hexadecimal to base 10
|
||||
* @param hex String containing the hex digits
|
||||
* @return returns the decimal value
|
||||
*/
|
||||
static long hex2Decimal(String hex) {
|
||||
String hexChars = "0123456789ABCDEF";
|
||||
hex = hex.toUpperCase();
|
||||
long decimal = 0;
|
||||
int intermediaryValue;
|
||||
char index;
|
||||
for (int i = hex.length(), p = 0; i != 0; i--, p++) {
|
||||
index = hex.charAt(i-1);
|
||||
intermediaryValue = hexChars.indexOf(index);
|
||||
if (intermediaryValue == -1)
|
||||
return -1;
|
||||
decimal = decimal + intermediaryValue*(int)(Math.pow(16, p));
|
||||
}
|
||||
return decimal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the english text to the encoded text
|
||||
* @param english String containing standard english
|
||||
* @return Returns encoded text
|
||||
*/
|
||||
static String english2Encrypted(String english) {
|
||||
english = english.toUpperCase();
|
||||
if (english.length() > 1) {
|
||||
english = swapFirstAndLastLettersInString(english);
|
||||
}
|
||||
|
||||
for (int i = 0; i < english.length(); i++) {
|
||||
char index = english.charAt(i);
|
||||
switch (index) {
|
||||
case 'E':
|
||||
english = replaceInString(english, i, "A");
|
||||
break;
|
||||
case 'A':
|
||||
english = replaceInString(english, i, "E");
|
||||
break;
|
||||
case 'O':
|
||||
english = replaceInString(english, i, "I");
|
||||
break;
|
||||
case 'I':
|
||||
english = replaceInString(english, i, "O");
|
||||
break;
|
||||
case 'U':
|
||||
english = replaceInString(english, i, "Y");
|
||||
break;
|
||||
case 'Y':
|
||||
english = replaceInString(english, i, "U");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return english;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace a letter in a string
|
||||
* @param str String to be modified
|
||||
* @param index The character's index to be replaced
|
||||
* @param replace The string that will be replacing the character
|
||||
* @return The string with the string replaced
|
||||
*/
|
||||
private static String replaceInString(String str, int index, String replace){
|
||||
return str.substring(0, index) + replace + str.substring(index+1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Swaps the first and letters in every word in a string
|
||||
* @param str The string to be swapped
|
||||
* @return The string with letters swapped
|
||||
*/
|
||||
private static String swapFirstAndLastLettersInString(String str) {
|
||||
StringBuilder output = new StringBuilder();
|
||||
String[] splitStr = str.trim().split("\\s+");
|
||||
|
||||
for(int i = 0; i < splitStr.length; i++) {
|
||||
if (splitStr[i].length() != 1) {
|
||||
splitStr[i] = swapFirstAndLastLetterFromWord(splitStr[i]);
|
||||
}
|
||||
|
||||
output.append(" ").append(splitStr[i]);
|
||||
if (i == 0) {
|
||||
output = new StringBuilder(splitStr[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to swap the first and last letters in a word
|
||||
* @param str The string to be swapped
|
||||
* @return The swapped string
|
||||
*/
|
||||
private static String swapFirstAndLastLetterFromWord(String str) {
|
||||
return str.charAt(str.length() - 1) + str.substring(1, str.length() - 1) + str.charAt(0);
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
import javafx.application.Application;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.FlowPane;
|
||||
import javafx.scene.text.Text;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
* GUI Class
|
||||
* @author Isaac Shoebottom (3429069)
|
||||
*/
|
||||
|
||||
public class Driver extends Application {
|
||||
FlowPane flowPane = new FlowPane();
|
||||
Text textInstructions = new Text("Enter a hex value or English word or phrase:");
|
||||
TextField textFieldMain = new TextField("");
|
||||
Button buttonH2D= new Button("Hex To Decimal");
|
||||
Button buttonE2E = new Button("English to Encrypted");
|
||||
Text textResult = new Text("Welcome to the Converter App!");
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
primaryStage.setTitle("Package Calculator");
|
||||
flowPane.setPadding(new Insets(10, 10, 10, 10));
|
||||
flowPane.setHgap(10);
|
||||
flowPane.setVgap(15);
|
||||
flowPane.setAlignment(Pos.CENTER);
|
||||
|
||||
buttonH2D.setOnAction(this::calculateHex);
|
||||
buttonE2E.setOnAction(this::calculateEncrypted);
|
||||
|
||||
textFieldMain.setPrefWidth(150);
|
||||
|
||||
flowPane.getChildren().addAll(
|
||||
textInstructions,
|
||||
textFieldMain,
|
||||
buttonH2D, buttonE2E,
|
||||
textResult
|
||||
);
|
||||
|
||||
primaryStage.setScene(new Scene(flowPane, 250, 200));
|
||||
primaryStage.setResizable(false);
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
private void calculateHex(ActionEvent actionEvent) {
|
||||
long input = Converter.hex2Decimal(textFieldMain.getText());
|
||||
if (input == -1) {
|
||||
textResult.setText("Invalid input");
|
||||
}
|
||||
else {
|
||||
textResult.setText(Long.toString(input));
|
||||
}
|
||||
}
|
||||
private void calculateEncrypted(ActionEvent actionEvent) {
|
||||
textResult.setText(Converter.english2Encrypted(textFieldMain.getText()));
|
||||
}
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 7.2 KiB |
Binary file not shown.
After Width: | Height: | Size: 7.0 KiB |
Binary file not shown.
After Width: | Height: | Size: 7.0 KiB |
Binary file not shown.
After Width: | Height: | Size: 7.0 KiB |
Binary file not shown.
After Width: | Height: | Size: 6.9 KiB |
Binary file not shown.
After Width: | Height: | Size: 7.9 KiB |
Reference in New Issue
Block a user