瀏覽代碼

added code

Soujanya Buragapu 5 年之前
父節點
當前提交
0d5a04082d

+ 11
- 1
src/main/java/rocks/zipcode/quiz5/arrays/ArrayUtils.java 查看文件

36
 
36
 
37
     public static String[] removeLastElement(String[] values)
37
     public static String[] removeLastElement(String[] values)
38
     {
38
     {
39
-        return null;
39
+        String str = values[values.length - 1];
40
+        String[] str1 = new String[values.length-1];
41
+        for(int i = 0; i<values.length-1;i++)
42
+        {
43
+            if(values.equals(str))
44
+                break;
45
+            else
46
+                str1[i] = values[i];
47
+        }
48
+
49
+                return str1;
40
     }
50
     }
41
 }
51
 }

+ 5
- 9
src/main/java/rocks/zipcode/quiz5/collections/Bank.java 查看文件

10
  */
10
  */
11
 public class Bank
11
 public class Bank
12
 {
12
 {
13
-    ArrayList<BankAccount> accounts = new ArrayList<>();
14
-    private int numberOfAccounts;
15
-
16
-    public Bank() {
17
-        numberOfAccounts = 0;
18
-        accounts = new ArrayList<BankAccount>();
19
-    }
13
+    List<BankAccount> accounts = new ArrayList<BankAccount>();
20
 
14
 
21
     public BankAccount removeBankAccountByIndex(Integer indexNumber)
15
     public BankAccount removeBankAccountByIndex(Integer indexNumber)
22
     {
16
     {
23
-       return null;
17
+       //BankAccount ba = accounts.remove(indexNumber);
18
+        return null;
24
     }
19
     }
25
 
20
 
26
     public void addBankAccount(BankAccount bankAccount)
21
     public void addBankAccount(BankAccount bankAccount)
30
 
25
 
31
     public Boolean containsBankAccount(BankAccount bankAccount)
26
     public Boolean containsBankAccount(BankAccount bankAccount)
32
     {
27
     {
33
-        throw new UnsupportedOperationException("Method not yet implemented");
28
+        //throw new UnsupportedOperationException("Method not yet implemented");
29
+        return accounts.contains(bankAccount);
34
     }
30
     }
35
 }
31
 }

+ 15
- 5
src/main/java/rocks/zipcode/quiz5/collections/Food.java 查看文件

1
 package rocks.zipcode.quiz5.collections;
1
 package rocks.zipcode.quiz5.collections;
2
 
2
 
3
 import rocks.zipcode.quiz5.objectorientation.Spice;
3
 import rocks.zipcode.quiz5.objectorientation.Spice;
4
+import rocks.zipcode.quiz5.objectorientation.account.BankAccount;
4
 
5
 
6
+import java.util.ArrayList;
5
 import java.util.List;
7
 import java.util.List;
6
 import java.util.Map;
8
 import java.util.Map;
7
 
9
 
8
 /**
10
 /**
9
  * @author leon on 27/12/2018.
11
  * @author leon on 27/12/2018.
10
  */
12
  */
11
-public class Food {
12
-    public List<Spice> getAllSpices() {
13
-        return null;
13
+public class Food implements Spice
14
+{
15
+    ArrayList<Spice> food = new ArrayList<Spice>();
16
+    public List<Spice> getAllSpices()
17
+    {
18
+        return food;
19
+
20
+        //return (List<Spice>) food.get();
14
     }
21
     }
15
 
22
 
16
-    public <SpiceType extends Class<? extends Spice>> Map<SpiceType, Integer> getSpiceCount() {
23
+    public <SpiceType extends Class<? extends Spice>> Map<SpiceType, Integer> getSpiceCount()
24
+    {
17
         return null;
25
         return null;
18
     }
26
     }
19
 
27
 
20
-    public void applySpice(Spice spice) {
28
+    public void applySpice(Spice spice)
29
+    {
30
+        food.add(spice);
21
     }
31
     }
22
 }
32
 }

+ 21
- 2
src/main/java/rocks/zipcode/quiz5/collections/WordCounter.java 查看文件

1
 package rocks.zipcode.quiz5.collections;
1
 package rocks.zipcode.quiz5.collections;
2
+import java.util.HashMap;
2
 import java.util.Map;
3
 import java.util.Map;
3
 
4
 
4
-public class WordCounter {
5
+public class WordCounter
6
+{
7
+    Map<String, Integer> map = new HashMap<String, Integer>();
8
+    String [] strings;
9
+
5
     public WordCounter(String... strings) {
10
     public WordCounter(String... strings) {
11
+        this.strings = strings;
6
     }
12
     }
7
 
13
 
8
     public Map<String, Integer> getWordCountMap()
14
     public Map<String, Integer> getWordCountMap()
9
     {
15
     {
10
-       return null;
16
+       Map<String,Integer> map = new HashMap<>();
17
+       for(String s : strings)
18
+       {
19
+           if(!map.containsKey(s))
20
+           {
21
+               map.put(s,1);
22
+           }
23
+           else
24
+           {
25
+               int count = map.get(s);
26
+               map.put(s,count +1);
27
+           }
28
+       }
29
+       return map;
11
     }
30
     }
12
 }
31
 }

+ 14
- 17
src/main/java/rocks/zipcode/quiz5/fundamentals/Calculator.java 查看文件

26
         return square;
26
         return square;
27
     }
27
     }
28
 
28
 
29
-    public static Double[] squareRoots(Double... value) {
30
-
31
-        return null;
32
-
29
+    public static Double[] squareRoots(Double... value)
30
+    {
31
+        Double[] result = new Double[value.length];
32
+        for(int i = 0; i<value.length;i++)
33
+        {
34
+            result[i] = (Double) Math.sqrt(value[i]);
35
+        }
36
+        return result;
33
     }
37
     }
34
 
38
 
35
     public static Double[] squares(Double... values) {
39
     public static Double[] squares(Double... values) {
36
-        List<Double[]> array = new ArrayList<>();//create a List which take an array of int
37
-
38
-        Double arr[] = new Double[3];//create a temporary array of 2 elements
39
-
40
-        for (int i = 0; i < values.length; i++) {
41
-             values[i]= (Double) Math.pow(values[i], 2);
42
-            arr[0] = values[i];//add your number to your array pos 1
43
-            arr[1] = (Double) Math.pow(values[i], 2);//add the power to the 2ed position
44
-            array.add(arr);//add your array to your list
40
+        Double[] result = new Double[values.length];
41
+        for(int i = 0; i<values.length;i++)
42
+        {
43
+            result[i] = values[i]*values[i];
45
         }
44
         }
46
-        return arr;
45
+        return result;
47
     }
46
     }
48
 
47
 
49
     public static Double add(Double value1, Double value2) {
48
     public static Double add(Double value1, Double value2) {
50
-        Double val = value1 + value2;
51
-        double rounded = (double) Math.round(val * 100) / 100;
52
-        return val;
49
+        return value1 + value2;
53
     }
50
     }
54
 
51
 
55
     public static Double subtract(Double value1, Double value2) {
52
     public static Double subtract(Double value1, Double value2) {

+ 17
- 24
src/main/java/rocks/zipcode/quiz5/fundamentals/StringUtils.java 查看文件

19
 
19
 
20
     public static String capitalizeMiddleCharacter(String str)
20
     public static String capitalizeMiddleCharacter(String str)
21
     {
21
     {
22
-        return str.toUpperCase();
22
+        StringBuilder sb = new StringBuilder(str);
23
+        int len=str.length()/2;
24
+        sb.setCharAt(len,Character.toUpperCase(str.charAt(len)));
25
+        return sb.toString();
23
 
26
 
24
     }
27
     }
25
 
28
 
26
     public static String lowerCaseMiddleCharacter(String str)
29
     public static String lowerCaseMiddleCharacter(String str)
27
     {
30
     {
28
-        int len=str.length();
29
-        int c=len/2;
30
-        char ch[]=str.toCharArray();
31
-        return String.valueOf(ch[c]).toLowerCase();
31
+        StringBuilder sb = new StringBuilder(str);
32
+        int len=str.length()/2;
33
+        sb.setCharAt(len,Character.toLowerCase(str.charAt(len)));
34
+        return sb.toString();
32
     }
35
     }
33
 
36
 
34
-    public static Boolean isIsogram(String str) {
37
+    public static Boolean isIsogram(String str)
38
+    {
35
         String[] ary = str.split("");
39
         String[] ary = str.split("");
36
         Set<String> mySet = new HashSet<String>(Arrays.asList(ary));
40
         Set<String> mySet = new HashSet<String>(Arrays.asList(ary));
37
 
41
 
47
     }
51
     }
48
 
52
 
49
     public static String removeConsecutiveDuplicateCharacters(String str) {
53
     public static String removeConsecutiveDuplicateCharacters(String str) {
50
-        StringBuilder result = new StringBuilder();
51
-        HashMap<Character, Integer> items = new HashMap<>();
52
-
53
-        for (int i = 0; i < str.length(); i++)
54
+        char[] ch = str.toCharArray();
55
+        int length = 0;
56
+        for(int i = 0; i<ch.length ; i++)
54
         {
57
         {
55
-            Character current = str.charAt(i);
56
-            Integer occurrence = items.get(current);
57
-            if (occurrence == null)
58
-                items.put(current, 1);
58
+            if(length ==0 || ch[i] != ch[length - 1])
59
+                ch[length++] = ch[i];
59
             else
60
             else
60
-                items.put(current, occurrence + 1);
61
-        }
62
-
63
-        for (int i = 0; i < str.length(); i++)
64
-        {
65
-            Character current = str.charAt(i);
66
-            Integer occurrence = items.get(current);
67
-            if (occurrence == 1)
68
-                result.append(current);
61
+                length--;
69
         }
62
         }
70
-        return result.toString();
63
+        return new String(ch,0,length);
71
     }
64
     }
72
 
65
 
73
     public static String invertCasing(String str) {
66
     public static String invertCasing(String str) {