Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions src/main/java/com/dtcc/exams/arrays/IntegerArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.dtcc.exams.arrays;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class IntegerArrayUtils {
/**
* @param integerArray - array to have value added to it
* @param valueToBeAdded - value to be added to the end of the array
* @return - identical array with one additional element of `valueToBeAdded` at the end of the array
*/
public static Integer[] add(Integer[] integerArray, Integer valueToBeAdded) {
return null;
List<Integer> list = new ArrayList<>(Arrays.asList(integerArray));
list.add(valueToBeAdded);
return list.toArray(new Integer[0]);
}

/**
Expand All @@ -17,7 +25,12 @@ public static Integer[] add(Integer[] integerArray, Integer valueToBeAdded) {
* @return `integerArray` with `valueToBeInserted` at index number `indexToInsertAt`
*/
public static Integer[] replace(Integer[] integerArray, int indexToInsertAt, Integer valueToBeInserted) {
return null;
for(int i = 0; i < integerArray.length; i++){
if(i == indexToInsertAt){
integerArray[i] = valueToBeInserted;
}
}
return integerArray;
}

/**
Expand All @@ -26,30 +39,47 @@ public static Integer[] replace(Integer[] integerArray, int indexToInsertAt, Int
* @return element located at `indexToFetch`
*/
public static Integer get(Integer[] integerArray, Integer indexToFetch) {
return null;
int x = (int)Array.get(integerArray,indexToFetch);
return x;
}

/**
* @param integerArray - array to be evaluated
* @return identical array with even-values incremented by 1 and odd-values decremented by 1
*/
public static Integer[] incrementEvenDecrementOdd(Integer[] integerArray) {
return null;
}

for (int i = 0; i < integerArray.length; i++) {
if (integerArray[i] % 2 == 0) {
integerArray[i]++;
} else {
integerArray[i]--;
}
}
return integerArray;
}
/**
* @param integerArray - array to be evaluated
* @return identical array with even-values incremented by 1
*/
public static Integer[] incrementEven(Integer[] integerArray) {
return null;
for (int i = 0; i < integerArray.length; i++) {
if (integerArray[i] % 2 == 0) {
integerArray[i]++;
}
}
return integerArray;
}

/**
* @param input - array to be evaluated
* @return identical array with odd-values decremented by 1
*/
public static Integer[] decrementOdd(Integer[] input) {
return null;
for (int i = 0; i < input.length; i++) {
if (input[i] % 2 != 0) {
input[i]--;
}
}
return input;
}
}
7 changes: 5 additions & 2 deletions src/main/java/com/dtcc/exams/arrays/StringArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dtcc.exams.arrays;

import java.util.Arrays;

public class StringArrayUtils {
/**
* @param arrayToBeSpliced - array to be evaluated
Expand All @@ -8,7 +10,8 @@ public class StringArrayUtils {
* @return an array with all elements between `startingIndex` and `endingIndex`
*/
public static String[] getSubArray(String[] arrayToBeSpliced, int startingIndex, int endingIndex) {
return null;

return Arrays.copyOfRange(arrayToBeSpliced, startingIndex,endingIndex + 1);
}


Expand All @@ -18,6 +21,6 @@ public static String[] getSubArray(String[] arrayToBeSpliced, int startingIndex,
* @return an array all elements between after `startingIndex`
*/
public static String[] getEndingArray(String[] arrayToBeSpliced, int startingIndex) {
return null;
return Arrays.copyOfRange(arrayToBeSpliced, startingIndex, arrayToBeSpliced.length);
}
}
19 changes: 16 additions & 3 deletions src/main/java/com/dtcc/exams/atm/Account.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
package com.dtcc.exams.atm;

public class Account {

double balance;
public Account(double v) {
double balance = v;
}

public double balance() {
return 0.0;

return balance;
}

public boolean closeAccount() {
if(balance <= 0.0){
return true;
}else
return false;
}

public void deposit(double v) {
balance += v;
}

public Double withdraw(double v) {
return 0.0;
if (balance >= v) {
balance -= v;
}
return balance;
}

public void transfer(Account b, double v) {
if(balance >= v){
balance -= v;
b.balance += v;
}
}
}
17 changes: 12 additions & 5 deletions src/main/java/com/dtcc/exams/collections/Inventory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.dtcc.exams.collections;

import java.util.List;
import java.util.*;

/**
* Use a map to keep track of inventory in a store
Expand All @@ -9,8 +9,11 @@ public class Inventory {
/**
* @param strings list of strings to add / remove / fetch from
*/
List<String> strings;
Integer itemQuantity;
public Inventory(List<String> strings) {

this.strings = strings;
this.itemQuantity = strings.size();
}

/**
Expand All @@ -24,21 +27,25 @@ public Inventory() {
* @param item - increment the number of this item in stock by 1
*/
public void addItemToInventory(String item) {
return;
this.strings.add(item);
this.itemQuantity = strings.size();
}

/**
* @param item - decrement the number of this item in stock by 1
*/
public void removeItemFromInventory(String item) {
return;
strings.remove(item);
}

/**
* @param item - Search for this item in stock
* @return - return the number of items
*/
public Integer getItemQuantity(String item) {
return null;
if(strings.contains(item)){

}
return this.itemQuantity;
}
}
33 changes: 26 additions & 7 deletions src/main/java/com/dtcc/exams/collections/MonthConversion.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.dtcc.exams.collections;

import java.util.HashMap;
import java.util.Map;

/**
* Use a map to solve
*/
Expand All @@ -8,54 +11,70 @@ public class MonthConversion {
* @param monthNumber - ordinal of month in the year; i.e. January = 1, February = 2
* @param monthName - name of month
*/
Map<Integer, String> month = new HashMap<>();
public void add(Integer monthNumber, String monthName) {

month.put(monthNumber,monthName);
}

/**
* @param monthNumber - ordinal of month in the year
* @return the name of the respective month
*/
public String getName(Integer monthNumber) {
throw new NullPointerException();
if (month.get(monthNumber) != null) {
if (month.containsKey(monthNumber)) {
return month.get(monthNumber);
// throw new NullPointerException();
}
}return null;
}

/**
* @param monthName - name of month
* @return - the ordinal of the month in the year
*/
public int getNumber(String monthName) {
return (Integer)null;
Integer key = null;
for(Map.Entry<Integer, String> entry: month.entrySet()){
if(entry.getValue().equals(monthName)){
key = entry.getKey();
}
}
return key;
}

/**
* @param monthNumber
* @return true if the monthNumber is in the keySet
*/
public Boolean isValidNumber(Integer monthNumber) {
return null;

return month.containsKey(monthNumber);
}

/**
* @param monthName
* @return true if the monthName is in the valueSet
*/
public Boolean isValidMonth(String monthName) {
return null;
return month.containsValue(monthName);
}

/**
* @return number of entries in this mapping
*/
public Integer size() {
return -1;

return month.size();
}

/**
* @param monthNumber - number of month in year
* @param monthName - name of month
*/
public void update(Integer monthNumber, String monthName) {
if(month.containsKey(monthNumber)){
month.replace(monthNumber, monthName);
}

}
}
34 changes: 29 additions & 5 deletions src/main/java/com/dtcc/exams/fundamentals/BasicStringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ public class BasicStringUtils {
* @return concatenation of `string1` and `string2`
*/
public static String concatentate(String string1, String string2) {
return null;

return string1 + string2;
}

/**
* @param string1 - String to be reversed
* @return an identical string with characters in reverse order
*/
public static String reverse(String string1) {
return null;
String string2 = "";
int length = string1.length();
for(int i = length - 1; i >= 0; i--){
string2 += string1.charAt(i);
}
return string2;
}

/**
Expand All @@ -24,7 +30,18 @@ public static String reverse(String string1) {
* @return concatenation of the reverse of `string1` and reverse of `string2`
*/
public static String reverseThenConcatenate(String string1, String string2) {
return null;
String string3 = "";
int length = string1.length();
for(int i = length - 1; i >= 0; i--){
string3 += string1.charAt(i);
}
String string4 = "";
int length2 = string2.length();
for(int i = length2 - 1; i >= 0; i--){
string4 += string2.charAt(i);
}
String string5 = string3 + string4;
return string5;
}

/**
Expand All @@ -33,7 +50,8 @@ public static String reverseThenConcatenate(String string1, String string2) {
* @return `string` with `charactersToRemove` removed
*/
public static String removeCharacters(String string, String charactersToRemove) {
return null;

return string.replaceAll("[ " + charactersToRemove + "]", "") ;
}

/**
Expand All @@ -42,6 +60,12 @@ public static String removeCharacters(String string, String charactersToRemove)
* @return reverse of `string` with `charactersToRemove` removed
*/
public static String removeCharactersThenReverse(String string, String charactersToRemove) {
return null;
String string1 = string.replaceAll("[ " + charactersToRemove + "]", "");
String string2 = "";
int length = string1.length();
for(int i = length - 1; i >= 0; i--){
string2 += string1.charAt(i);
}
return string2;
}
}
Loading