From e9fa6e6a47b7ad42188a9ba627f4e378be6628b5 Mon Sep 17 00:00:00 2001 From: Ujjwal Shrestha Date: Sat, 21 Mar 2020 13:51:21 -0400 Subject: [PATCH 1/3] ArrayUtils done. --- .../zipcode/quiz4/arrays/ArrayUtils.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/rocks/zipcode/quiz4/arrays/ArrayUtils.java b/src/main/java/rocks/zipcode/quiz4/arrays/ArrayUtils.java index 6deaf61..9b07610 100644 --- a/src/main/java/rocks/zipcode/quiz4/arrays/ArrayUtils.java +++ b/src/main/java/rocks/zipcode/quiz4/arrays/ArrayUtils.java @@ -1,22 +1,33 @@ package rocks.zipcode.quiz4.arrays; +import java.util.Arrays; + /** * @author leon on 01/01/2019. */ public class ArrayUtils { public static String getMiddleElement(String[] values) { - return null; + return values[values.length/2]; } public static String[] removeMiddleElement(String[] values) { - return null; + + String[] result = new String[values.length-1]; + int count = 0; + for (int i = 0; i < values.length; i++) { + if(!values[i].equals(getMiddleElement(values))){ + result[count] = values[i]; + count++; + } + } + return result; } public static String getLastElement(String[] values) { - return null; + return values[values.length-1]; } public static String[] removeLastElement(String[] values) { - return null; + return Arrays.copyOf(values,values.length-1); } } \ No newline at end of file From 635cfb91f8a2f26352789e7efa07543327c73805 Mon Sep 17 00:00:00 2001 From: Ujjwal Shrestha Date: Sun, 22 Mar 2020 01:02:41 -0400 Subject: [PATCH 2/3] 2-3 done. --- .../quiz4/collections/WordCounter.java | 13 ++++- .../quiz4/collections/culonary/Curry.java | 4 +- .../quiz4/collections/culonary/Food.java | 19 +++++++- .../quiz4/collections/culonary/Ginger.java | 2 +- .../quiz4/collections/culonary/Pepper.java | 4 +- .../quiz4/fundamentals/Calculator.java | 27 +++++++---- .../quiz4/fundamentals/StringUtils.java | 48 ++++++++++++++++--- 7 files changed, 96 insertions(+), 21 deletions(-) diff --git a/src/main/java/rocks/zipcode/quiz4/collections/WordCounter.java b/src/main/java/rocks/zipcode/quiz4/collections/WordCounter.java index 2bf5ec2..42ea0d6 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/WordCounter.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/WordCounter.java @@ -1,12 +1,23 @@ package rocks.zipcode.quiz4.collections; +import java.util.HashMap; import java.util.Map; public class WordCounter { + Map myMap = new HashMap<>(); + public WordCounter(String... strings) { + for (String x : strings) { + Integer count = 1; + if (myMap.containsKey(x)) { + count = myMap.get(x); + count++; + } + myMap.put(x, count); + } } public Map getWordCountMap() { - return null; + return myMap; } } diff --git a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Curry.java b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Curry.java index 199cefc..56b7c6e 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Curry.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Curry.java @@ -1,4 +1,6 @@ package rocks.zipcode.quiz4.collections.culonary; -public class Curry { +import sun.security.provider.ConfigFile; + +public class Curry implements Spice { } diff --git a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java index e06abbc..2828de1 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java @@ -1,5 +1,7 @@ package rocks.zipcode.quiz4.collections.culonary; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -7,14 +9,27 @@ * @author leon on 27/12/2018. */ public class Food { + private List spices = new ArrayList<>(); + public List getAllSpices() { - return null; + return this.spices; } public > Map getSpiceCount() { - return null; + Map result = new HashMap<>(); + + for(Spice s: spices){ + Integer count = 1; + if(result.containsKey((SpiceType) s.getClass())){ + count = result.get((SpiceType) s.getClass()); + count++; + } + result.put((SpiceType) s.getClass(), count); + } + return result; } public void applySpice(Spice spice) { + spices.add(spice); } } diff --git a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Ginger.java b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Ginger.java index 3c4e177..1667d46 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Ginger.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Ginger.java @@ -3,5 +3,5 @@ /** * @author leon on 27/12/2018. */ -public class Ginger { +public class Ginger implements Spice { } diff --git a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Pepper.java b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Pepper.java index 4b771a8..7304a0b 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Pepper.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Pepper.java @@ -1,7 +1,9 @@ package rocks.zipcode.quiz4.collections.culonary; +import sun.security.provider.ConfigFile; + /** * @author leon on 27/12/2018. */ -public class Pepper { +public class Pepper implements Spice { } diff --git a/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java b/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java index 02f2618..8931fd8 100644 --- a/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java +++ b/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java @@ -1,35 +1,46 @@ package rocks.zipcode.quiz4.fundamentals; +import static java.lang.Math.sqrt; + /** * @author leon on 21/12/2018. */ public class Calculator { public static Double squareRoot(Double value) { - return null; + return sqrt(value); } public static Double square(Double value) { - return null; + + return value * value; } public static Double[] squareRoots(Double... value) { - return null; + + Double[] result = new Double[value.length]; + for (int i = 0; i < value.length ; i++) { + result[i] = Math.sqrt(value[i]); + } + return result; } public static Double[] squares(Double... values) { - return null; + Double[] result = new Double[values.length]; + for (int i = 0; i < values.length ; i++) { + result[i] = Math.pow(values[i],2); + } + return result; } public static Double add(Double value1, Double value2) { - return null; + return value1 + value2; } public static Double subtract(Double value1, Double value2) { - return null; + return value1 - value2; } - public static Double divide(Double divisor, Double dividend) { - return null; + return divisor/dividend; } } diff --git a/src/main/java/rocks/zipcode/quiz4/fundamentals/StringUtils.java b/src/main/java/rocks/zipcode/quiz4/fundamentals/StringUtils.java index 5ec61be..3e44c24 100644 --- a/src/main/java/rocks/zipcode/quiz4/fundamentals/StringUtils.java +++ b/src/main/java/rocks/zipcode/quiz4/fundamentals/StringUtils.java @@ -5,30 +5,64 @@ */ public class StringUtils { public static Character getMiddleCharacter(String string) { - return null; + return string.charAt(string.length()/2); } public static String capitalizeMiddleCharacter(String str) { - return null; + + return str.substring(0,str.length()/2) + getMiddleCharacter(str.toUpperCase()) + str.substring((str.length()/2)+1); } public static String lowerCaseMiddleCharacter(String str) { - return null; + + return str.substring(0,str.length()/2) + getMiddleCharacter(str.toLowerCase()) + str.substring((str.length()/2)+1); } public static Boolean isIsogram(String str) { - return null; + + char[] element = str.toCharArray(); + for(int i = 0; i < element.length; i++){ + for (int j = i+1; j < element.length;j++){ + if (element[i] == element[j]){ + return false; + } + } + } + return true; } public static Boolean hasDuplicateConsecutiveCharacters(String str) { - return null; + + char[] element = str.toCharArray(); + for (int i = 0;i < element.length-1;i++){ + if (element[i] == element[i+1]){ + return true; + } + } + return false; } public static String removeConsecutiveDuplicateCharacters(String str) { - return null; + + for(int i = 0; i < str.length()-1; i++){ + if (str.charAt(i) == str.charAt(i+1)){ + str = str.substring(0,i)+ str.substring(i+2); + } + } + return str; } public static String invertCasing(String str) { - return null; + + char[] element = str.toCharArray(); + for(int i = 0; i < element.length; i++) { + if(Character.isLowerCase(element[i])) { + element[i] = Character.toUpperCase(element[i]); + } + else { + element[i] = Character.toLowerCase(element[i]); + } + } + return String.valueOf(element); } } \ No newline at end of file From 158fe4bbc89f9efa08d4213ebc13801b32d32cb3 Mon Sep 17 00:00:00 2001 From: Ujjwal Shrestha Date: Sun, 22 Mar 2020 11:05:19 -0400 Subject: [PATCH 3/3] completed --- .../objectorientation/account/Account.java | 7 ++- .../quiz4/objectorientation/account/Bank.java | 13 +++++- .../account/BankAccount.java | 22 ++++++++- .../objectorientation/account/Employee.java | 45 ++++++++++++++++++- 4 files changed, 80 insertions(+), 7 deletions(-) diff --git a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Account.java b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Account.java index 40eb4a8..f42ef3e 100644 --- a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Account.java +++ b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Account.java @@ -3,11 +3,14 @@ /** * @author leon on 30/12/2018. */ -public class Account extends BankAccount { +public class Account{ + Long id; + public Long getId() { - return null; + return id; } public void setId(Long id) { + this.id = id; } } diff --git a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Bank.java b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Bank.java index 0dd4183..49d976d 100644 --- a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Bank.java +++ b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Bank.java @@ -1,17 +1,26 @@ package rocks.zipcode.quiz4.objectorientation.account; +import java.util.ArrayList; +import java.util.List; + /** * @author leon on 27/12/2018. */ public class Bank { + + private List accounts = new ArrayList<>(); + public BankAccount removeBankAccountByIndex(Integer indexNumber) { - return null; + + return accounts.remove(indexNumber.intValue()); } public void addBankAccount(BankAccount bankAccount) { + accounts.add(bankAccount); } public Boolean containsBankAccount(BankAccount bankAccount) { - throw new UnsupportedOperationException("Method not yet implemented"); + //throw new UnsupportedOperationException("Method not yet implemented"); + return accounts.contains(bankAccount); } } diff --git a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/BankAccount.java b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/BankAccount.java index 70ccb74..255cc6d 100644 --- a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/BankAccount.java +++ b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/BankAccount.java @@ -3,7 +3,27 @@ /** * @author leon on 27/12/2018. */ -public class BankAccount { +public class BankAccount extends Account implements Transactable { + Double balance = 0.0; + public void setBalance(Double val) { + balance = val; + } + + @Override + public void deposit(Double amountToIncreaseBy) { + if (amountToIncreaseBy < 0) throw new IllegalArgumentException(); + balance += amountToIncreaseBy; + } + + @Override + public void withdrawal(Double amountToDecreaseBy) { + if (amountToDecreaseBy < 0 || amountToDecreaseBy > balance) throw new IllegalArgumentException(); + balance -= amountToDecreaseBy; + } + + @Override + public Double getBalance() { + return balance; } } diff --git a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Employee.java b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Employee.java index 8407f1a..03acfb4 100644 --- a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Employee.java +++ b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Employee.java @@ -3,18 +3,59 @@ /** * @author leon on 30/12/2018. */ -public class Employee { +public class Employee implements Worker, Transactable { + private BankAccount account; + private Double hourlyWage = 35.0; + private Double hoursWorked = 0.0; + public Employee() { + account = new BankAccount(); } public Employee(BankAccount bankAccount) { + account = bankAccount; } public BankAccount getBankAccount() { - return null; + return account; } public void setBankAccount(BankAccount bankAccount) { + account = bankAccount; + } + + @Override + public void deposit(Double amountToIncreaseBy) { + account.deposit(amountToIncreaseBy); + } + + @Override + public void withdrawal(Double amountToDecreaseBy) { + account.withdrawal(amountToDecreaseBy); + } + + @Override + public Double getBalance() { + return account.getBalance(); + } + + @Override + public void increaseHoursWorked(Double numberOfHours) { + hoursWorked += numberOfHours; + } + + @Override + public Double getHoursWorked() { + return hoursWorked; + } + + @Override + public Double getHourlyWage() { + return hourlyWage; + } + @Override + public Double getMoneyEarned() { + return hoursWorked * hourlyWage; } }