Initial Commit
This commit is contained in:
8
Source Code/Assignment10/.idea/.gitignore
generated
vendored
Normal file
8
Source Code/Assignment10/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/../../../../../../:\ProgrammingProjects\JavaProjects\JavaYear1\Assignment10\.idea/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
25
Source Code/Assignment10/.idea/codeStyles/Project.xml
generated
Normal file
25
Source Code/Assignment10/.idea/codeStyles/Project.xml
generated
Normal file
@ -0,0 +1,25 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<JetCodeStyleSettings>
|
||||
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
|
||||
<value>
|
||||
<package name="java.util" alias="false" withSubpackages="false" />
|
||||
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
|
||||
<package name="io.ktor" alias="false" withSubpackages="true" />
|
||||
</value>
|
||||
</option>
|
||||
<option name="PACKAGES_IMPORT_LAYOUT">
|
||||
<value>
|
||||
<package name="" alias="false" withSubpackages="true" />
|
||||
<package name="java" alias="false" withSubpackages="true" />
|
||||
<package name="javax" alias="false" withSubpackages="true" />
|
||||
<package name="kotlin" alias="false" withSubpackages="true" />
|
||||
<package name="" alias="true" withSubpackages="true" />
|
||||
</value>
|
||||
</option>
|
||||
</JetCodeStyleSettings>
|
||||
<ScalaCodeStyleSettings>
|
||||
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
|
||||
</ScalaCodeStyleSettings>
|
||||
</code_scheme>
|
||||
</component>
|
5
Source Code/Assignment10/.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
5
Source Code/Assignment10/.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
@ -0,0 +1,5 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||
</state>
|
||||
</component>
|
6
Source Code/Assignment10/.idea/discord.xml
generated
Normal file
6
Source Code/Assignment10/.idea/discord.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="PROJECT" />
|
||||
</component>
|
||||
</project>
|
6
Source Code/Assignment10/.idea/misc.xml
generated
Normal file
6
Source Code/Assignment10/.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
Source Code/Assignment10/.idea/modules.xml
generated
Normal file
8
Source Code/Assignment10/.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/Assignment10.iml" filepath="$PROJECT_DIR$/Assignment10.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
11
Source Code/Assignment10/Assignment10.iml
Normal file
11
Source Code/Assignment10/Assignment10.iml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
Binary file not shown.
Binary file not shown.
109
Source Code/Assignment10/src/Converter.java
Normal file
109
Source Code/Assignment10/src/Converter.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
65
Source Code/Assignment10/src/Driver.java
Normal file
65
Source Code/Assignment10/src/Driver.java
Normal file
@ -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()));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user