|
@@ -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
|