瀏覽代碼

Without Tests

Elliott Stansbury 6 年之前
父節點
當前提交
accb6245f0

+ 15
- 0
src/main/java/io/zipcoder/polymorphism/Cat.java 查看文件

@@ -0,0 +1,15 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Cat extends Pet {
4
+
5
+    //public static Speak()
6
+
7
+    public Cat(String petName){
8
+        super(petName);
9
+    }
10
+
11
+    @Override
12
+    public  String speak(){
13
+        return "MEOW MEOW MEOW MEOW MEOW MEOW MEOW!";
14
+    }
15
+}

+ 32
- 0
src/main/java/io/zipcoder/polymorphism/Dog.java 查看文件

@@ -0,0 +1,32 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Dog extends Pet {
4
+
5
+//    public void sound(){
6
+//
7
+//        System.out.println("Woof Woof");
8
+//
9
+//    }
10
+//    public void name(){
11
+//        System.out.println();
12
+//    }
13
+//    public static void main(String args[]){
14
+//        Pet obj = new Dog();
15
+//        obj.sound();
16
+//    }
17
+
18
+    private String petName;
19
+
20
+    public Dog(String petName){
21
+        super(petName);
22
+    }
23
+
24
+
25
+
26
+
27
+    @Override
28
+    public String speak(){
29
+        return "BARK BARK BARK BARK BARK!!!";
30
+    }
31
+}
32
+

+ 81
- 0
src/main/java/io/zipcoder/polymorphism/MainApplication.java 查看文件

@@ -1,7 +1,88 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
+import java.util.Scanner;
4
+
3 5
 /**
4 6
  * Created by leon on 11/6/17.
5 7
  */
6 8
 public class MainApplication {
9
+
10
+    public static void main(String[] args) {
11
+
12
+        Scanner scanner = new Scanner(System.in);
13
+        StringBuilder builder1 = new StringBuilder();
14
+        StringBuilder builder2 = new StringBuilder();
15
+
16
+        String typeOfPet = "";
17
+        String nameOfPet = "";
18
+
19
+        System.out.println("How many pets do you currently own? You can include your kids as well! :)");
20
+        int numberOfPets = Integer.parseInt(scanner.nextLine());
21
+
22
+
23
+        for (int i = 0; i < numberOfPets; i++) {
24
+            typeOfPet = scanner.nextLine();
25
+            builder1.append(typeOfPet);
26
+            if (i < numberOfPets) {
27
+                System.out.println("Do you have more pets? If so what kind is the next?");
28
+                builder1.append(", ");
29
+            }
30
+
31
+
32
+            System.out.println("What's the first one's name?");
33
+
34
+            for (int j = 0; j < i; j++) {
35
+                nameOfPet = scanner.nextLine();
36
+                builder2.append(nameOfPet);
37
+                if (j < numberOfPets) {
38
+                    System.out.println("Do you have more pets? If so what kind is the next?");
39
+                    builder2.append(", ");
40
+
41
+                }
42
+            }
43
+
44
+
45
+            System.out.println(builder1.toString());
46
+            System.out.println(builder2.toString());
47
+
48
+            String[] petNameArray = builder2.toString().split(", ");
49
+            String[] petKindArray = builder1.toString().split(", ");
50
+
51
+            for (int k = 0; k < numberOfPets; k++) {
52
+                Dog dog = new Dog(petNameArray[k]);
53
+                Cat cat = new Cat(petNameArray[k]);
54
+                String sounds = "";
55
+
56
+                if (petKindArray[k].equals("cat")) {
57
+                    sounds = cat.speak();
58
+                } else if (petKindArray[k].equals("dog")) {
59
+                    sounds = dog.speak();
60
+                } else {
61
+                    sounds = "White Noise!!!";
62
+                }
63
+            }
64
+        }
65
+    }
7 66
 }
67
+
68
+//    public class Pet {
69
+//        public void Speak(){
70
+//            //String sound
71
+//            //System.out.println(sound);
72
+//        }
73
+//    }
74
+//
75
+//    public class Dog extends Pet{
76
+//        String sound = "Woof Woof";
77
+//    }
78
+//
79
+//    public class Cat extends Pet{
80
+//        String sound = "Meow Meow";
81
+//    }
82
+//
83
+//    public class Pig extends Pet{
84
+//        String sound = "Oink Oink";
85
+//    }
86
+
87
+
88
+

+ 20
- 0
src/main/java/io/zipcoder/polymorphism/Pet.java 查看文件

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

+ 1
- 0
src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java 查看文件

@@ -4,4 +4,5 @@ package io.zipcoder.polymorphism;
4 4
  * Created by leon on 11/6/17.
5 5
  */
6 6
 public class MainApplicationTest {
7
+
7 8
 }