瀏覽代碼

Finished for if loop for pets array

Trinh Tong 6 年之前
父節點
當前提交
34138846ef

+ 1
- 4
src/main/java/io/zipcoder/polymorphism/Cat.java 查看文件

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

+ 1
- 5
src/main/java/io/zipcoder/polymorphism/Dog.java 查看文件

@@ -1,11 +1,7 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3 3
 public class Dog extends Pet {
4
-
5
-
6
-
7 4
     public String speak() {
8
-
9
-        return null;
5
+        return "What's up dawg?";
10 6
     }
11 7
 }

+ 1
- 2
src/main/java/io/zipcoder/polymorphism/Duck.java 查看文件

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

+ 1
- 2
src/main/java/io/zipcoder/polymorphism/Frog.java 查看文件

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

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

@@ -1,14 +1,11 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
-import java.io.Console;
4 3
 import java.util.Scanner;
5 4
 
6 5
 /**
7 6
  * Created by leon on 11/6/17.
8 7
  */
9 8
 public class MainApplication {
10
-
11
-
12 9
     public static void main(String[] args) {
13 10
         // create scanner class for inputs
14 11
         Scanner keyboard = new Scanner(System.in);
@@ -22,7 +19,7 @@ public class MainApplication {
22 19
         Pet[] pets = new Pet[numOfPets];
23 20
 
24 21
         // What kind of pet each one is using a loop
25
-        for (int i = 0; i < pets.length -1; i++) {
22
+        for (int i = 0; i < pets.length; i++) {
26 23
             System.out.print("What kind of pet do you have?\n\n");
27 24
             System.out.print("1: Dog\n2: Cat\n3: Duck\n4: Frog\n(Enter the key for the pet)\n");
28 25
             int petType = keyboard.nextInt();
@@ -30,28 +27,31 @@ public class MainApplication {
30 27
 
31 28
             if (petType == 1) {
32 29
                 pets[i] = new Dog();
33
-                System.out.print("What is your dog's name?\n\n");
34
-                String petName = keyboard.nextLine();
30
+                System.out.print("What is your dog's name?\n");
31
+                String petName = keyboard.next();
32
+                System.out.println("Pet Name: " + petName);
35 33
                 pets[i].setName(petName);
36 34
 
37 35
             } else if (petType == 2) {
38 36
                 pets[i] = new Cat();
39
-                System.out.print("What is your cat's name?\n\n");
40
-                String petName = keyboard.nextLine();
37
+                System.out.print("What is your cat's name?\n");
38
+                String petName = keyboard.next();
41 39
                 pets[i].setName(petName);
42 40
 
43 41
             } else if (petType == 3) {
44 42
                 pets[i] = new Duck();
45
-                System.out.print("What is your duck's name?\n\n");
46
-                String petName = keyboard.nextLine();
43
+                System.out.print("What is your duck's name?\n");
44
+                String petName = keyboard.next();
47 45
                 pets[i].setName(petName);
48 46
 
49 47
             } else if (petType == 4) {
50 48
                 pets[i] = new Frog();
51
-                System.out.print("What is your frog's name?\n\n");
52
-                String petName = keyboard.nextLine();
49
+                System.out.print("What is your frog's name?\n");
50
+                String petName = keyboard.next();
53 51
                 pets[i].setName(petName);
54 52
             }
53
+        }
54
+        for (int i = 0; i < pets.length; i++) {
55 55
 
56 56
         }
57 57
         // Ask what kind of pet

+ 1
- 6
src/main/java/io/zipcoder/polymorphism/Pet.java 查看文件

@@ -11,11 +11,6 @@ public abstract class Pet {
11 11
     }
12 12
 
13 13
     public String getName() {
14
-
15
-        return null;
16
-
14
+        return this.name;
17 15
     }
18 16
 }
19
-
20
-
21
-