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
39 changes: 33 additions & 6 deletions src/main/java/com/dtcc/exams/arrays/IntegerArrayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ public class IntegerArrayUtils {
* @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;
int length=integerArray.length;
Integer[] newArray=new Integer[length+1];
//copy array integerArray to newArray.
System.arraycopy(integerArray,0,newArray,0,length);
newArray[length]=valueToBeAdded;
return newArray;
}

/**
Expand All @@ -16,8 +21,13 @@ public static Integer[] add(Integer[] integerArray, Integer valueToBeAdded) {
* @param valueToBeInserted - value of the element to be inserted
* @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 +36,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;
Integer fetchValue = null;
for(int i=0;i<integerArray.length;i++){
if(i==indexToFetch)
fetchValue=integerArray[i];
}
return fetchValue;
}

/**
* @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]=integerArray[i]+1; }
else if(integerArray[i]%2==1) {integerArray[i]=integerArray[i]-1;}
}
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] = integerArray[i] + 1;

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 == 1)
input[i] = input[i] - 1;

return input;
}
}
20 changes: 18 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,15 @@ 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;
if(startingIndex< 0 || endingIndex< 0){throw new IllegalArgumentException();}

Integer length=arrayToBeSpliced.length;
String[] subArray=new String[(endingIndex-startingIndex)];
int index=0;
for(int i=startingIndex;i<endingIndex;i++){
subArray[index++]=arrayToBeSpliced[i];
}
return subArray;
}


Expand All @@ -18,6 +28,12 @@ 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;

if(startingIndex<0){throw new IndexOutOfBoundsException(); }
else if(startingIndex>arrayToBeSpliced.length){throw new IllegalArgumentException();}

String[] newArray=new String[arrayToBeSpliced.length-startingIndex];
System.arraycopy(arrayToBeSpliced,startingIndex,newArray,0,newArray.length);
return newArray;
}
}
17 changes: 13 additions & 4 deletions src/main/java/com/dtcc/exams/atm/Account.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
package com.dtcc.exams.atm;

public class Account {

private double balance;
public Account(double v) {
this.balance=v;
}

public double balance() {
return 0.0;
return this.balance;
}

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

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

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

public void transfer(Account b, double v) {
if(this.balance>=v) {
this.balance = this.balance() - v;
b.balance = b.balance() + v;
}
}

}
25 changes: 20 additions & 5 deletions src/main/java/com/dtcc/exams/collections/Inventory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dtcc.exams.collections;

import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -9,36 +10,50 @@ 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();
}

/**
* nullary constructor initializes a new list
*/
public Inventory() {

this(new ArrayList<String>());
}

/**
* @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;
if(strings!=null) {
strings.remove(item);
this.itemQuantity=strings.size();
}
}

public List<String> getStrings() {
return this.strings;
}

/**
* @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;
}
}
35 changes: 27 additions & 8 deletions src/main/java/com/dtcc/exams/collections/MonthConversion.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,80 @@
package com.dtcc.exams.collections;


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

/**
* Use a map to solve
*/
public class MonthConversion {

public Map<Integer,String> map=new HashMap<>();

/**
* @param monthNumber - ordinal of month in the year; i.e. January = 1, February = 2
* @param monthName - name of month
*/
public void add(Integer monthNumber, String monthName) {

map.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(map.get(monthNumber)!=null) {
if(map.containsKey(monthNumber)){
return map.get(monthNumber); //{throw new NullPointerException();}
}else return null;
}else return null;
}

/**
* @param monthName - name of month
* @return - the ordinal of the month in the year
*/
public int getNumber(String monthName) {
return (Integer)null;
public Integer getNumber(String monthName) {
Integer key = null;
for(Map.Entry<Integer, String> entry: map.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 map.containsKey(monthNumber);
}

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

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

/**
* @param monthNumber - number of month in year
* @param monthName - name of month
*/
public void update(Integer monthNumber, String monthName) {

if(map.containsKey(monthNumber)){
map.replace(monthNumber,monthName);
}
}
}
37 changes: 32 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 newString = "";
if(string1!=null) {
for (int i = string1.length()-1; i >= 0; i--) {
newString +=string1.charAt(i) + "";
}
}
return newString;
}

/**
Expand All @@ -24,7 +30,17 @@ 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 newString1 = "";
String newString2 = "";
if(string1!=null || string2!=null) {
for (int i = string1.length()-1; i >= 0; i--) {
newString1 +=string1.charAt(i) + "";
}
for (int j= string2.length()-1; j >= 0; j--) {
newString2 +=string2.charAt(j) + "";
}
}
return newString1+newString2;
}

/**
Expand All @@ -33,7 +49,12 @@ 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 + "]", "") ;

}

public static String removeChar(String str,int index){
return str.substring(0,index)+str.substring(index+1);
}

/**
Expand All @@ -42,6 +63,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 newString = "";
int length = string1.length();
for(int i = length - 1; i >= 0; i--){
newString += string1.charAt(i);
}
return newString;
}
}
Loading