From d09641f0623dd0eb861b00275625e43b42346eb2 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Thu, 8 Oct 2020 21:57:18 +0100 Subject: [PATCH] Uploading all Java Projects --- 2018 - 2019/2018/Arrays/src/SimpleArrays.java | 29 +++ .../src/ConditionalStatements.java | 46 +++++ .../2018/Using Scanner/src/UsingScanner.java | 30 +++ .../src/WorkingWithStrings.java | 77 ++++++++ .../New Calc Engine/src/NewCalcEngine.java | 43 ++++ .../Calc Engine/src/CalcEngine.java | 187 ++++++++++++++++++ .../pluralsite/getorganised/GetOrganised.java | 7 + .../Hello World/src/HelloWorld.java | 5 + .../src/OperatorPrecedence.java | 17 ++ .../Type Conversion/src/TypeConversion.java | 41 ++++ README.md | 7 + 11 files changed, 489 insertions(+) create mode 100644 2018 - 2019/2018/Arrays/src/SimpleArrays.java create mode 100644 2018 - 2019/2018/Conditionals/src/ConditionalStatements.java create mode 100644 2018 - 2019/2018/Using Scanner/src/UsingScanner.java create mode 100644 2018 - 2019/2018/Working With Strings/src/WorkingWithStrings.java create mode 100644 Java Classes & Interfaces/New Calc Engine/src/NewCalcEngine.java create mode 100644 Java Fundamentals/Calc Engine/src/CalcEngine.java create mode 100644 Java Fundamentals/Get Organised/src/com/pluralsite/getorganised/GetOrganised.java create mode 100644 Java Fundamentals/Hello World/src/HelloWorld.java create mode 100644 Java Fundamentals/Operator Precedence/src/OperatorPrecedence.java create mode 100644 Java Fundamentals/Type Conversion/src/TypeConversion.java create mode 100644 README.md diff --git a/2018 - 2019/2018/Arrays/src/SimpleArrays.java b/2018 - 2019/2018/Arrays/src/SimpleArrays.java new file mode 100644 index 0000000..82ff8e3 --- /dev/null +++ b/2018 - 2019/2018/Arrays/src/SimpleArrays.java @@ -0,0 +1,29 @@ +import java.util.Scanner; + +public class SimpleArrays { + public static void main(String[] args) { + + final int LENGTH = 9; + int[] values = new int[LENGTH]; + int currentSize = 0; + + //Read Inputs + System.out.println("Please Enter Values: \nPress Q To Quit"); + Scanner in = new Scanner(System.in); + while (in.hasNextInt() && currentSize < values.length) { + values[currentSize] = in.nextInt(); + currentSize++; + } + + //Find The Largest Value + double largest = values[0]; + for (int i = 1; i < currentSize; i++) { + if (values[i] > largest) { + largest = values[i]; + } + } + + System.out.println("\nThe Largest Is: " + largest); + } +} + diff --git a/2018 - 2019/2018/Conditionals/src/ConditionalStatements.java b/2018 - 2019/2018/Conditionals/src/ConditionalStatements.java new file mode 100644 index 0000000..ba5b4fd --- /dev/null +++ b/2018 - 2019/2018/Conditionals/src/ConditionalStatements.java @@ -0,0 +1,46 @@ +import java.util.Scanner; + +public class ConditionalStatements { + public static void main(String[] args) { + char grade; // Defines Grade As Char + Scanner in = new Scanner(System.in); // Creates a New Scanner Object + + System.out.print("Please enter your score: "); + int score = in.nextInt(); + in.close(); + + String output = "Your Grade is: "; + // Checks if Grade is A + if (score >= 70) { + grade = 'A'; + System.out.println(output + grade); + } + // Checks if Grade is B + else if (score >= 60) { + grade = 'B'; + System.out.println(output + grade); + } + // Checks if Grade is C + else if (score >= 50) { + grade = 'C'; + System.out.println(output + grade); + } + // Checks if Grade is D + else if (score >= 40) { + grade = 'D'; + System.out.println(output + grade); + } + // Checks if Grade is F By Using Else + else { + grade = 'F'; + System.out.println(output + grade); + } + + // Using modulus to determine is number is odd or even + if (score % 2 == 0) { + System.out.println("The Number is Even"); + } else if (score % 2 == 1) { + System.out.println("The Number is Odd"); + } + } +} diff --git a/2018 - 2019/2018/Using Scanner/src/UsingScanner.java b/2018 - 2019/2018/Using Scanner/src/UsingScanner.java new file mode 100644 index 0000000..8acfbc7 --- /dev/null +++ b/2018 - 2019/2018/Using Scanner/src/UsingScanner.java @@ -0,0 +1,30 @@ +import java.util.Scanner; + +public class UsingScanner { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + + // Simply printing out the name + System.out.println("Please Enter Your Forename"); + String forename = in.next(); + System.out.println("Please Enter Your Surname"); + String surname = in.next(); + + System.out.printf("%s %s%n", forename, surname); + + // Read price of the packs + System.out.print("Please enter the price of a six-pack: "); + double packPrice = in.nextDouble(); + // Read Can Volume + System.out.print("Please enter the volume for each can (in ounces): "); + double canVolume = in.nextDouble(); + + // Calculating volume + final double CANS_PER_PACK = 6; + double packVolume = canVolume * CANS_PER_PACK; + double pricePerOunce = packPrice / packVolume; + + System.out.printf("Price per ounce: %8.2f", pricePerOunce); + } +} + diff --git a/2018 - 2019/2018/Working With Strings/src/WorkingWithStrings.java b/2018 - 2019/2018/Working With Strings/src/WorkingWithStrings.java new file mode 100644 index 0000000..7311d88 --- /dev/null +++ b/2018 - 2019/2018/Working With Strings/src/WorkingWithStrings.java @@ -0,0 +1,77 @@ +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.Scanner; + +public class WorkingWithStrings { + public static void main(String[] args) { + + Scanner in = new Scanner(System.in); + String Info = "Bobby, M, wdjoiwqnoiqfcoiqjdwqjd"; + + //split the array using "," as a delimiter + String[] parts = Info.split(","); + System.out.println("Using , as a delimiter " + "\n" + Arrays.toString(parts)); + + //split the array using "w" as a delimiter + parts = Info.split("w"); + System.out.println(Arrays.toString(parts)); + + //Tokenize the string into words simply by using " " + String Token = "Tokenize Commence"; + parts = Token.split(" "); + System.out.println(Arrays.toString(parts)); + + //Using a limit on the delimiter + String Limit = "abdc:psdv:sdvosdv:dfpbkdd"; + parts = Limit.split(":", 2); + System.out.println("Using : as a delimiter " + Arrays.toString(parts)); + + //Split numbers + String number = "abdc124psdv456sdvos456dv568dfpbk0dd"; + parts = number.split("[0-9]"); + System.out.println(Arrays.toString(parts)); + + // Split the array using a whole number + parts = number.split("[0-9]+"); + System.out.println(Arrays.toString(parts)); + + //Create 3 Sub-strings + String first = "Glasgow Clyde College"; + String Sub1 = first.substring(0, 7); + String Sub2 = first.substring(8, 13); + String Sub3 = first.substring(13, 21); + + System.out.println("\nFirst Sub-String is " + Sub1); + System.out.println("Second Sub-String is " + Sub2); + System.out.println("Third Sub-String is" + Sub3); + + // Working with characters of strings + System.out.println("Please Enter Your Name: "); + String name = in.nextLine(); + int n = name.length(); + char start = name.charAt(0); + char last = name.charAt(n - 1); + + System.out.println("The First Letter Of The Name is " + start); + System.out.println("The Last Letter Of The Name is " + last); + + // Generating userID from strings entered + Scanner sc = new Scanner(System.in); + System.out.println("Please enter your first name: "); + String firstName = sc.next(); + System.out.println("Please enter your last name: "); + String secondName = sc.next(); + sc.close(); + + LocalDateTime nowIsTheTime = LocalDateTime.now(); + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd"); + String s = nowIsTheTime.format(dtf); + + String firstInitial = firstName.substring(0, 1); + String secondInitial = secondName.substring(0, 1); + String userName = firstInitial + secondInitial + s; + + System.out.println("Your UserID Is : " + userName); + } +} diff --git a/Java Classes & Interfaces/New Calc Engine/src/NewCalcEngine.java b/Java Classes & Interfaces/New Calc Engine/src/NewCalcEngine.java new file mode 100644 index 0000000..6a07783 --- /dev/null +++ b/Java Classes & Interfaces/New Calc Engine/src/NewCalcEngine.java @@ -0,0 +1,43 @@ +public class NewCalcEngine { + + public static void main(String[] args) { + performCalculations(); + } + + static void performCalculations() { + double[] leftVals = {100.0d, 25.0d, 225.0d, 11.0d}; + double[] rightVals = {50.0d, 92.0d, 17.0d, 3.0d}; + char[] opCodes = {'d', 'a', 's', 'm'}; + double[] results = new double[opCodes.length]; + + for (int i = 0; i < opCodes.length; i++) { + results[i] = execute(opCodes[i], leftVals[i], rightVals[i]); + } + for (double currentResult : results) + System.out.println("result = " + currentResult); + + } + + static double execute(char opCode, double leftVal, double rightVal) { + double result; + switch (opCode) { + case 'a': + result = leftVal + rightVal; + break; + case 's': + result = leftVal - rightVal; + break; + case 'm': + result = leftVal * rightVal; + break; + case 'd': + result = rightVal != 0 ? leftVal / rightVal : 0.0d; + break; + default: + System.out.println("Invalid opCode: " + opCode); + result = 0.0d; + break; + } + return result; + } +} \ No newline at end of file diff --git a/Java Fundamentals/Calc Engine/src/CalcEngine.java b/Java Fundamentals/Calc Engine/src/CalcEngine.java new file mode 100644 index 0000000..22ff746 --- /dev/null +++ b/Java Fundamentals/Calc Engine/src/CalcEngine.java @@ -0,0 +1,187 @@ +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Scanner; + +public class CalcEngine { + + static double execute(char opCode, double leftVal, double rightVal) { + /* Return the correct value + + // Enhanced Switch Statement + switch (opCodes[i]) { + case 'a' -> results[i] = leftVals[i] + rightVals[i]; + case 's' -> results[i] = leftVals[i] - rightVals[i]; + case 'm' -> results[i] = leftVals[i] * rightVals[i]; + case 'd' -> results[i] = rightVals[i] != 0 ? leftVals[i] / rightVals[i] : 0.0d; + default -> { + System.out.println("Invalid opCode" + opCodes[i]); + results[i] = 0.0d; + } + } + */ + double result; + switch (opCode) { + case 'a': + result = leftVal + rightVal; + break; + case 's': + result = leftVal - rightVal; + break; + case 'm': + result = leftVal * rightVal; + break; + case 'd': + result = rightVal != 0 ? leftVal / rightVal : 0.0d; + break; + default: + System.out.println("Invalid opCode" + opCode); + result = 0.0d; + break; + } + return result; + } + + static double getCommandLine(String[] args) { + /* Handle the command line arguments and get the result */ + + char opCode = args[0].charAt(0); + double leftVal = Double.parseDouble(args[1]); + double rightVal = Double.parseDouble(args[2]); + return execute(opCode, leftVal, rightVal); + + } + + static char getOpCodeFromString(String operationName) { + /* Return the opcode from the string*/ + + return operationName.charAt(0); + } + + static double valueFromWord(String word) { + /* Return the double representation of the number */ + + String[] values = { + "zero", "one", "two", "three", "four", "five", + "six", "seven", "eight", "nine", "ten" + }; + double value = -1d; + for (int i = 0; i < values.length; i++) { + if (word.equals(values[i])) { + value = i; + break; + } + } + + // Checking to see if value had changed, if not. Parse the string into a double + if (value == -1) + value = Double.parseDouble(word); + + return value; + } + + static char symbolFromOpCode(char opCode) { + /* Get the relevant symbol for the operation given by the user*/ + + char[] opCodes = {'a', 's', 'm', 'd'}; + char[] symbols = {'+', '-', '*', '/'}; + char symbol = ' '; + + // Loop through the list of chars and get the relevant symbol + for (int i = 0; i < opCodes.length; i++) { + if (opCode == opCodes[i]) { + symbol = symbols[i]; + break; + } + } + return symbol; + } + + static void displayResult(char opCode, double leftVal, double rightVal, double result) { + /* Display the result of the expression but nicely formatted */ + + char symbol = symbolFromOpCode(opCode); + // StringBuilder stringBuilder = new StringBuilder(); + + // stringBuilder.append(leftVal); + // stringBuilder.append(" " + symbol + " "); + // stringBuilder.append(rightVal); + // stringBuilder.append(" = "); + // stringBuilder.append(result); + + // String finalResult = stringBuilder.toString(); + + String finalResult = String.format("%.1f %c %.1f = %.1f", + leftVal, symbol, rightVal, result); + System.out.println(finalResult); + + } + + static void handleWhen(String[] stringParts) { + /* Return date arithmetic operation (days added) */ + + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); + LocalDate startDate = LocalDate.parse(stringParts[1], dateTimeFormatter); + long daysToAdd = (long) valueFromWord(stringParts[2]); + + LocalDate newDate = startDate.plusDays(daysToAdd); + String output = String.format("%s + %d days is %s", + startDate.format(dateTimeFormatter), daysToAdd, newDate.format(dateTimeFormatter)); + System.out.println(output); + + } + + static void performOperation(String[] stringParts) { + /* Execute the operation with the numbers given by the user */ + + char opCode = getOpCodeFromString(stringParts[0]); + + if (opCode == 'w') { + handleWhen(stringParts); + } else { + double leftVal = valueFromWord(stringParts[1]); + double rightVal = valueFromWord(stringParts[2]); + double result = execute(opCode, leftVal, rightVal); + displayResult(opCode, leftVal, rightVal, result); + + } + } + + + static void executeInteractively() { + /* Allow the user to be able to enter in numbers for operations */ + + Scanner in = new Scanner(System.in); + System.out.println("Enter in an operation and two numbers"); + String userInput = in.nextLine(); + in.close(); + + String[] stringParts = userInput.split(" "); + performOperation(stringParts); + + } + + public static void main(String[] args) { + + double[] leftVals = {100.0d, 25.0d, 225.0d, 11.0d}; + double[] rightVals = {50.0d, 92.0d, 17.0d, 3.0}; + char[] opCodes = {'d', 'a', 's', 'm'}; + double[] results = new double[opCodes.length]; + + // Make sure the user has entered in command line arguments before trying to use them + if (args.length == 0) { + // Get the results of the operation and store it into an array + for (int i = 0; i < opCodes.length; i++) + results[i] = execute(opCodes[i], leftVals[i], rightVals[i]); + + // Print out all the results stored in the array + for (double currentResult: results) + System.out.println(currentResult); + + } else if (args.length == 1 && args[0].equals("interactive")) { + executeInteractively(); + } else if (args.length == 3) + System.out.println(getCommandLine(args)); + else + System.out.println("Please Enter 3 Command Line Arguments!"); + } +} diff --git a/Java Fundamentals/Get Organised/src/com/pluralsite/getorganised/GetOrganised.java b/Java Fundamentals/Get Organised/src/com/pluralsite/getorganised/GetOrganised.java new file mode 100644 index 0000000..79a6774 --- /dev/null +++ b/Java Fundamentals/Get Organised/src/com/pluralsite/getorganised/GetOrganised.java @@ -0,0 +1,7 @@ +package com.pluralsite.getorganised; + +public class GetOrganised { + public static void main(String[] args) throws Exception { + System.out.println("I'm organised!"); + } +} diff --git a/Java Fundamentals/Hello World/src/HelloWorld.java b/Java Fundamentals/Hello World/src/HelloWorld.java new file mode 100644 index 0000000..70fd330 --- /dev/null +++ b/Java Fundamentals/Hello World/src/HelloWorld.java @@ -0,0 +1,5 @@ +public class HelloWorld { + public static void main(String[] args) { + System.out.println("Hello, World!"); + } +} diff --git a/Java Fundamentals/Operator Precedence/src/OperatorPrecedence.java b/Java Fundamentals/Operator Precedence/src/OperatorPrecedence.java new file mode 100644 index 0000000..6ca8738 --- /dev/null +++ b/Java Fundamentals/Operator Precedence/src/OperatorPrecedence.java @@ -0,0 +1,17 @@ +public class OperatorPrecedence { + public static void main(String[] args) { + + int valA = 21; + int valB = 6; + int valC = 3; + int valD = 1; + + int result1 = valA / valC * valD + valB; + int result2 = valA / (valC * (valD + valB)); + + System.out.println(result1); + System.out.println(result2); + + } +} + diff --git a/Java Fundamentals/Type Conversion/src/TypeConversion.java b/Java Fundamentals/Type Conversion/src/TypeConversion.java new file mode 100644 index 0000000..c568c63 --- /dev/null +++ b/Java Fundamentals/Type Conversion/src/TypeConversion.java @@ -0,0 +1,41 @@ +public class TypeConversion { + public static void main(String[] args) { + float floatVal = 1.0f; + double doubleVal = 4.0d; + byte byteVal = 7; + short shortVal = 7; + long longVal = 5; + + // Possible + short result1 = byteVal; + + // Not possible + // short result2 = longVal; + + // Possible + short result2 = (short) longVal; + + // Not Possible + // short result3 = byteVal - longVal; + + // Possible + short result3 = (short) (byteVal - longVal); + + // Not Possible + // long result4 = longVal - floatVal; + + // Better to do this - Possible + float result4 = longVal - floatVal; + + // Possible + double result5 = doubleVal + shortVal; + + + System.out.println("Success"); + System.out.println(result1); + System.out.println(result2); + System.out.println(result3); + System.out.println(result4); + System.out.println(result5); + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..49334dc --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# LearningJava + +#### Uploading any projects/code that shows my progression in learning Java + +# Courses Included +- [Java Fundamentals](https://app.pluralsight.com/library/courses/getting-started-programming-java/table-of-contents) +- [Java Classes & Interfaces](https://app.pluralsight.com/library/courses/working-classes-interfaces-java/table-of-contents) \ No newline at end of file