Browse Source

Finished for if loop for pets array

Trinh Tong 6 years ago
parent
commit
34138846ef

+ 1
- 4
src/main/java/io/zipcoder/polymorphism/Cat.java View File

1
 package io.zipcoder.polymorphism;
1
 package io.zipcoder.polymorphism;
2
 
2
 
3
 public class Cat extends Pet {
3
 public class Cat extends Pet {
4
-
5
     public String speak() {
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 View File

1
 package io.zipcoder.polymorphism;
1
 package io.zipcoder.polymorphism;
2
 
2
 
3
 public class Dog extends Pet {
3
 public class Dog extends Pet {
4
-
5
-
6
-
7
     public String speak() {
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 View File

1
 package io.zipcoder.polymorphism;
1
 package io.zipcoder.polymorphism;
2
 
2
 
3
 public class Duck extends Pet {
3
 public class Duck extends Pet {
4
-
5
     public String speak() {
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 View File

1
 package io.zipcoder.polymorphism;
1
 package io.zipcoder.polymorphism;
2
 
2
 
3
 public class Frog extends Pet {
3
 public class Frog extends Pet {
4
-
5
     public String speak() {
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 View File

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

+ 1
- 6
src/main/java/io/zipcoder/polymorphism/Pet.java View File

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