Explorar el Código

Updating Files

Trinh Tong hace 6 años
padre
commit
ec2c1a450e

+ 40
- 0
README.md Ver fichero

@@ -0,0 +1,40 @@
1
+# Polymorphism Lab 1
2
+
3
+## Objectives
4
+
5
+1. Students will practice basic polymorphic program design in this lab.
6
+2. After completing this lab students should be familiar with running tests against their code to confirm it is functioning properly.
7
+
8
+
9
+## Overview
10
+
11
+In this lab you will practice creating a simple Java program designed to make use of polymorphic design.
12
+
13
+## Unit Test
14
+
15
+Select a partner from your tribe; You will each write tests for the requirements below, but your partner must develop against your tests and vice versa. Be sure to use the `io.zipcoder.pets` package for your Pet classes to allow tests to execute properly.
16
+
17
+**Hint:** *An easy way to achieve this is for each partner to set up a GitHub repository for this lab, and add the other partner as a collaborator with write access (in the repository settings).*
18
+
19
+## Instructions
20
+
21
+### Part 1:
22
+
23
+Create a program that asks the user how many pets they have. Once you know how many pets they have, ask them what kind of pet each one is, along with each pet's name. For now your program should just hold onto the user input and print out the list at the end; we'll modify this in part 3.
24
+
25
+### Part 2:
26
+
27
+Create a Pet class, and a subclass for each type of pet that you want your program to support. Your classes should follow the following requirements:
28
+
29
+- You must support at least three types of pets.
30
+- Dog must be one of the types you support.
31
+- Cat must be one of the types you support.
32
+- The Pet class must have a `speak` method that each subclass overrides.
33
+- The Pet class must have a `name` field with setters and getters.
34
+
35
+Use the tests provided as examples to write your own tests for other supported types of pets.
36
+
37
+### Part 3:
38
+
39
+Modify your program from part 1 to use the Pet class and its subclasses. Keep a list of the pets your user lists and at the end of the program print out a list of their names and what they say when they speak.
40
+

+ 20
- 0
pom.xml Ver fichero

@@ -0,0 +1,20 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <modelVersion>4.0.0</modelVersion>
6
+
7
+    <groupId>io.zipcoder</groupId>
8
+    <artifactId>polymorphism</artifactId>
9
+    <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>RELEASE</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
18
+
19
+
20
+</project>

+ 10
- 0
src/main/java/io/zipcoder/polymorphism/Cat.java Ver fichero

@@ -0,0 +1,10 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Cat extends Pet {
4
+
5
+    public String speak() {
6
+        return null;
7
+    }
8
+
9
+
10
+}

+ 8
- 0
src/main/java/io/zipcoder/polymorphism/Dog.java Ver fichero

@@ -0,0 +1,8 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Dog extends Pet {
4
+
5
+    public String speak() {
6
+        return null;
7
+    }
8
+}

+ 8
- 0
src/main/java/io/zipcoder/polymorphism/Duck.java Ver fichero

@@ -0,0 +1,8 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Duck extends Pet {
4
+
5
+    public String speak() {
6
+        return null;
7
+    }
8
+}

+ 8
- 0
src/main/java/io/zipcoder/polymorphism/Frog.java Ver fichero

@@ -0,0 +1,8 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Frog extends Pet {
4
+
5
+    public String speak() {
6
+        return null;
7
+    }
8
+}

+ 7
- 0
src/main/java/io/zipcoder/polymorphism/MainApplication.java Ver fichero

@@ -0,0 +1,7 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+/**
4
+ * Created by leon on 11/6/17.
5
+ */
6
+public class MainApplication {
7
+}

+ 21
- 0
src/main/java/io/zipcoder/polymorphism/Pet.java Ver fichero

@@ -0,0 +1,21 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public abstract class Pet {
4
+
5
+    public String name;
6
+
7
+    public abstract String speak();
8
+
9
+    public void setName(String newName) {
10
+
11
+    }
12
+
13
+    public String getName() {
14
+
15
+        return null;
16
+
17
+    }
18
+}
19
+
20
+
21
+

+ 130
- 0
src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java Ver fichero

@@ -0,0 +1,130 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class MainApplicationTest {
7
+
8
+    @Test
9
+    public void speakDogTest() {
10
+        //Given
11
+        Dog rover = new Dog();
12
+        String expected = "What's up dawg?";
13
+
14
+        //When
15
+        String actual = rover.speak();
16
+
17
+        //Then
18
+        Assert.assertEquals(expected, actual);
19
+
20
+    }
21
+
22
+    @Test
23
+    public void speakCatTest() {
24
+        //Given
25
+        Cat stripes = new Cat();
26
+        String expected = "How do you like meow?";
27
+
28
+        //When
29
+        String actual = stripes.speak();
30
+
31
+        //Then
32
+        Assert.assertEquals(expected, actual);
33
+
34
+    }
35
+
36
+    @Test
37
+    public void speakFrogTest() {
38
+        //Given
39
+        Frog kermit = new Frog();
40
+        String expected = "This code is ribbeting!";
41
+
42
+        //When
43
+        String actual = kermit.speak();
44
+
45
+        //Then
46
+        Assert.assertEquals(expected, actual);
47
+
48
+    }
49
+
50
+    @Test
51
+    public void speakDuckTest() {
52
+        //Given
53
+        Duck donald = new Duck();
54
+        String expected = "You quack me up!";
55
+
56
+        //When
57
+        String actual = donald.speak();
58
+
59
+        //Then
60
+        Assert.assertEquals(expected, actual);
61
+
62
+    }
63
+
64
+    @Test
65
+    public void dogNameTest() {
66
+        //Given
67
+        Dog rover = new Dog();
68
+        String newName = "Fido";
69
+        String expected = "Fido";
70
+
71
+        //When
72
+        rover.setName(newName);
73
+        String actual = rover.getName();
74
+
75
+        //Then
76
+        Assert.assertEquals(expected, actual);
77
+
78
+    }
79
+
80
+    @Test
81
+    public void catNameTest() {
82
+        //Given
83
+        Cat stripes = new Cat();
84
+        String newName = "Jingles";
85
+        String expected = "Jingles";
86
+
87
+        //When
88
+        stripes.setName(newName);
89
+        String actual = stripes.getName();
90
+
91
+        //Then
92
+        Assert.assertEquals(expected, actual);
93
+
94
+    }
95
+
96
+    @Test
97
+    public void frogNameTest() {
98
+        //Given
99
+        Frog kermit = new Frog();
100
+        String newName = "Mr. Frog";
101
+        String expected = "Mr. Frog";
102
+
103
+        //When
104
+        kermit.setName(newName);
105
+        String actual = kermit.getName();
106
+
107
+        //Then
108
+        Assert.assertEquals(expected, actual);
109
+
110
+    }
111
+
112
+
113
+    @Test
114
+    public void duckNameTest() {
115
+        //Given
116
+        Duck donald = new Duck();
117
+        String newName = "Daffy";
118
+        String expected = "Daffy";
119
+
120
+        //When
121
+        donald.setName(newName);
122
+        String actual = donald.getName();
123
+
124
+        //Then
125
+        Assert.assertEquals(expected, actual);
126
+
127
+    }
128
+
129
+
130
+}