Quellcode durchsuchen

Added the ArrayListCombiner microlab.

Zach Marcin vor 7 Jahren
Ursprung
Commit
c5fcff1f1e

+ 4
- 1
README.md Datei anzeigen

@@ -14,4 +14,7 @@ Implement Table<K,V> so that it manages an ArrayList of Entry<K,V>.  You must im
14 14
     * `remove` which takes a key and removes it from the ArrayList if it's in there.  It's a void method; no return type.
15 15
 4. TableNested -- Take the previous microlab, and make Entry a nested class.  Think about if it'll need to be generic
16 16
 or not.
17
-5. Swap -- Get the test to pass.  Look at the specific values being passed in to help you figure it out.
17
+5. Swap -- Get the test to pass.  Look at the specific values being passed in to help you figure it out.
18
+6. ArrayListCombiner -- Write two methods, `superCombiner` and `extendCombiner`, which each take two arraylists and append
19
+all of the items from the second to the first.  `superCombiner` should use `? super E` and `extendCombiner` should use 
20
+`? extends E`.

+ 12
- 0
src/main/java/ArrayListCombiner/ArrayListCombiner.java Datei anzeigen

@@ -0,0 +1,12 @@
1
+package ArrayListCombiner;
2
+
3
+import java.util.ArrayList;
4
+
5
+/**
6
+ * Create two generic methods that take two arraylists.  The methods should both append the second ArrayList's items,
7
+ * to the first.  Use a wildcard for one of the type arguments in each method.
8
+ * The first method should be called extendCombiner and should use ? extends E
9
+ * The second method should be called superCombiner and should use ? super E
10
+ */
11
+public class ArrayListCombiner {
12
+}

+ 25
- 0
src/main/java/Employee/Employee.java Datei anzeigen

@@ -0,0 +1,25 @@
1
+package Employee;
2
+
3
+public class Employee {
4
+    private String name;
5
+    private double salary;
6
+
7
+    public Employee(String name, double salary) {
8
+        this.name = name;
9
+        this.salary = salary;
10
+    }
11
+
12
+    public void raiseSalary(double byPercent) {
13
+        double raise = salary * byPercent / 100;
14
+        salary += raise;
15
+    }
16
+
17
+    public final String getName() {
18
+        return name;
19
+    }
20
+
21
+    public double getSalary() {
22
+        return salary;
23
+    }
24
+}
25
+

+ 18
- 0
src/main/java/Employee/Manager.java Datei anzeigen

@@ -0,0 +1,18 @@
1
+package Employee;
2
+
3
+public class Manager extends Employee {
4
+    private double bonus;
5
+
6
+    public Manager(String name, double salary) {
7
+        super(name, salary);
8
+        bonus = 0;
9
+    }
10
+
11
+    public void setBonus(double bonus) {
12
+        this.bonus = bonus;
13
+    }
14
+
15
+    public double getSalary() { // Overrides superclass method
16
+        return super.getSalary() + bonus;
17
+    }
18
+}

+ 48
- 0
src/test/java/ArrayListCombiner/ArrayListCombinerTest.java Datei anzeigen

@@ -0,0 +1,48 @@
1
+package ArrayListCombiner;
2
+
3
+import Employee.Employee;
4
+import Employee.Manager;
5
+import org.junit.Test;
6
+
7
+import org.junit.Assert;
8
+
9
+import java.util.ArrayList;
10
+
11
+public class ArrayListCombinerTest {
12
+//    Employee foo = new Employee("FOO", 100);
13
+//    Manager bar = new Manager("BAR", 100);
14
+//    @Test
15
+//    public void testExtendCombiner() throws Exception {
16
+//        // Given an array list with employees
17
+//        ArrayList<Employee> first = new ArrayList<>();
18
+//        first.add(foo);
19
+//        // An an array list with managers
20
+//        ArrayList<Manager> second = new ArrayList<>();
21
+//        second.add(bar);
22
+//        // When  I combine them
23
+//        ArrayListCombiner.extendCombiner(first, second);
24
+//        // Then I should get an arrayList with both
25
+//        ArrayList<Employee> expected = new ArrayList<>();
26
+//        expected.add(foo);
27
+//        expected.add(bar);
28
+//        Assert.assertEquals(expected, first);
29
+//    }
30
+//
31
+//    @Test
32
+//    public void testSuperCombiner() throws Exception {
33
+//        // Given an array list with employees
34
+//        ArrayList<Employee> first = new ArrayList<>();
35
+//        first.add(foo);
36
+//        // An an array list with managers
37
+//        ArrayList<Manager> second = new ArrayList<>();
38
+//        second.add(bar);
39
+//        // When  I combine them
40
+//        ArrayListCombiner.superCombiner(first, second);
41
+//        // Then I should get an arrayList with both
42
+//        ArrayList<Employee> expected = new ArrayList<>();
43
+//        expected.add(foo);
44
+//        expected.add(bar);
45
+//        Assert.assertEquals(expected, first);
46
+//    }
47
+
48
+}