Mexi Liang 6 anni fa
parent
commit
34b7eb7171
10 ha cambiato i file con 326 aggiunte e 11 eliminazioni
  1. BIN
      .DS_Store
  2. 20
    0
      Cat.java
  3. 23
    0
      CatTest.java
  4. 21
    0
      Dog.java
  5. 25
    0
      DogTest.java
  6. 20
    0
      Fish.java
  7. 23
    0
      FishTest.java
  8. 51
    0
      MainApplication.java
  9. 36
    0
      Pet.java
  10. 107
    11
      package.bluej

BIN
.DS_Store Vedi File


+ 20
- 0
Cat.java Vedi File

@@ -0,0 +1,20 @@
1
+
2
+/**
3
+ * Write a description of class Cat here.
4
+ *
5
+ * @author (your name)
6
+ * @version (a version number or a date)
7
+ */
8
+public class Cat extends Pet
9
+{
10
+    public Cat(String name){
11
+    
12
+    super(name);
13
+    super.setType("Cat");
14
+    }
15
+    
16
+    public String speak(){
17
+    
18
+        return "meowmeowmeow";
19
+}
20
+}

+ 23
- 0
CatTest.java Vedi File

@@ -0,0 +1,23 @@
1
+
2
+
3
+import org.junit.Assert;
4
+import org.junit.After;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+/**
9
+ * The test class CatTest.
10
+ *
11
+ * @author  (your name)
12
+ * @version (a version number or a date)
13
+ */
14
+public class CatTest
15
+{
16
+    public void testCatSpeak(){
17
+    
18
+    Pet test = new Cat("tst");
19
+    String expected = "meowmeowmeow";
20
+    String actual = test.speak();
21
+    Assert.assertEquals(expected,actual);
22
+    }
23
+}

+ 21
- 0
Dog.java Vedi File

@@ -0,0 +1,21 @@
1
+
2
+/**
3
+ * Write a description of class Dog here.
4
+ *
5
+ * @author (your name)
6
+ * @version (a version number or a date)
7
+ */
8
+public class Dog extends Pet
9
+{
10
+    public Dog(String name){
11
+    
12
+    super(name);
13
+    super.setType("Dog");
14
+    }
15
+    
16
+    public String speak(){
17
+    
18
+        return "wowowowo";
19
+    
20
+}
21
+}

+ 25
- 0
DogTest.java Vedi File

@@ -0,0 +1,25 @@
1
+
2
+
3
+import org.junit.Assert;
4
+import org.junit.After;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+/**
9
+ * The test class DogTest.
10
+ *
11
+ * @author  (your name)
12
+ * @version (a version number or a date)
13
+ */
14
+public class DogTest
15
+{
16
+    @Test
17
+    public void testDogSpeak(){
18
+    Pet test = new Dog("Test");
19
+    String expected ="wowowowo";
20
+    String actual = test.speak();
21
+    Assert.assertEquals(expected,actual);
22
+   
23
+    }
24
+
25
+}

+ 20
- 0
Fish.java Vedi File

@@ -0,0 +1,20 @@
1
+
2
+/**
3
+ * Write a description of class Fish here.
4
+ *
5
+ * @author (your name)
6
+ * @version (a version number or a date)
7
+ */
8
+public class Fish extends Pet
9
+{
10
+    public Fish(String name){
11
+    
12
+    super(name);
13
+    super.setType("Fish");
14
+    }
15
+    
16
+    public String speak(){
17
+    
18
+        return "blublublu";
19
+    }
20
+}

+ 23
- 0
FishTest.java Vedi File

@@ -0,0 +1,23 @@
1
+
2
+
3
+import org.junit.Assert;
4
+import org.junit.After;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+/**
9
+ * The test class FishTest.
10
+ *
11
+ * @author  (your name)
12
+ * @version (a version number or a date)
13
+ */
14
+public class FishTest
15
+{
16
+    @Test
17
+    public void testFishSpeak(){
18
+    Pet test = new Fish("test");
19
+    String expected = "blublublu";
20
+    String actual = test.speak();
21
+    Assert.assertEquals(expected,actual);
22
+}
23
+}

+ 51
- 0
MainApplication.java Vedi File

@@ -1,5 +1,56 @@
1 1
 /**
2 2
  * Created by leon on 11/6/17.
3 3
  */
4
+import java.util.Scanner;
5
+import java.util.*;
4 6
 public class MainApplication {
7
+    private enum petTypes {DOG, CAT, FISH};
8
+
9
+    public static void Main(String [] args) {    
10
+
11
+        Scanner in = new Scanner(System.in);
12
+        System.out.println("How many pets do you have?");
13
+        int numPet = in.nextInt();
14
+        Pet[] arr = new Pet[numPet];//get size of the array of pets
15
+        for (int i=0;i<numPet;i++){
16
+            System.out.println("What type of pet do you have?");
17
+            String typePet = in.nextLine();
18
+            System.out.println("Name of the pets?");
19
+            String petName = in.nextLine();
20
+            switch (petTypes.valueOf(typePet.toUpperCase())){
21
+                //access enum to get value 
22
+                case CAT:
23
+                arr[i] = new Cat(petName);
24
+                break;
25
+                case DOG:
26
+                arr[i] = new Dog(petName);
27
+                break;
28
+                case FISH:
29
+                arr[i] = new Fish(petName);
30
+                break;
31
+            }
32
+            //arr [i] = (typePet + ":" + petName);
33
+        }
34
+        for(Pet i: arr){
35
+
36
+            System.out.println(i.getName() + ":"+ i.getType() + "says" + i.speak());
37
+        }
38
+        //String output = Arrays.toString(arr);
39
+
40
+    }
41
+
42
+    
5 43
 }
44
+
45
+/*System.out.println("What pets do you have?");
46
+    String petType = in.nextLine();
47
+    if (numPet == 1){
48
+    System.out.println();
49
+    else if (numPet >1)
50
+    {
51
+
52
+    }
53
+    }
54
+
55
+    System.out.println("What are their name?");
56
+     */

+ 36
- 0
Pet.java Vedi File

@@ -0,0 +1,36 @@
1
+
2
+/**
3
+ * Write a description of class Pet here.
4
+ *
5
+ * @author (your name)
6
+ * @version (a version number or a date)
7
+ */
8
+public class Pet
9
+{
10
+    private String name;
11
+    private String type;
12
+
13
+    public Pet(String name){
14
+        this.name = name;
15
+        //this type = type;
16
+    }
17
+    
18
+    public String speak() {
19
+        return null; //
20
+    }
21
+
22
+    public void setType(String type){
23
+        this.type = type;
24
+    }
25
+    
26
+    public void setName(String name){
27
+        this.name = name;
28
+    
29
+    }
30
+    public String getName(){
31
+        return name;
32
+    } //getters have no parameter, setters do. getter just want to return value
33
+    public String getType(){
34
+        return type;
35
+    }
36
+}

+ 107
- 11
package.bluej Vedi File

@@ -1,20 +1,50 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=722
2
+dependency1.from=DogTest
3
+dependency1.to=Pet
4
+dependency1.type=UsesDependency
5
+dependency10.from=MainApplication
6
+dependency10.to=Fish
7
+dependency10.type=UsesDependency
8
+dependency2.from=DogTest
9
+dependency2.to=Dog
10
+dependency2.type=UsesDependency
11
+dependency3.from=FishTest
12
+dependency3.to=Pet
13
+dependency3.type=UsesDependency
14
+dependency4.from=FishTest
15
+dependency4.to=Fish
16
+dependency4.type=UsesDependency
17
+dependency5.from=CatTest
18
+dependency5.to=Pet
19
+dependency5.type=UsesDependency
20
+dependency6.from=CatTest
21
+dependency6.to=Cat
22
+dependency6.type=UsesDependency
23
+dependency7.from=MainApplication
24
+dependency7.to=Pet
25
+dependency7.type=UsesDependency
26
+dependency8.from=MainApplication
27
+dependency8.to=Cat
28
+dependency8.type=UsesDependency
29
+dependency9.from=MainApplication
30
+dependency9.to=Dog
31
+dependency9.type=UsesDependency
32
+editor.fx.0.height=716
3 33
 editor.fx.0.width=800
4
-editor.fx.0.x=560
5
-editor.fx.0.y=117
6
-objectbench.height=101
7
-objectbench.width=461
34
+editor.fx.0.x=290
35
+editor.fx.0.y=23
36
+objectbench.height=165
37
+objectbench.width=776
8 38
 package.divider.horizontal=0.6
9
-package.divider.vertical=0.8007380073800738
10
-package.editor.height=427
11
-package.editor.width=674
39
+package.divider.vertical=0.6826568265682657
40
+package.editor.height=363
41
+package.editor.width=675
12 42
 package.editor.x=141
13
-package.editor.y=59
43
+package.editor.y=100
14 44
 package.frame.height=600
15 45
 package.frame.width=800
16
-package.numDependencies=0
17
-package.numTargets=0
46
+package.numDependencies=10
47
+package.numTargets=9
18 48
 package.showExtends=true
19 49
 package.showUses=true
20 50
 project.charset=UTF-8
@@ -23,3 +53,69 @@ readme.name=@README
23 53
 readme.width=47
24 54
 readme.x=10
25 55
 readme.y=10
56
+target1.height=50
57
+target1.name=MainApplicationTest
58
+target1.showInterface=false
59
+target1.type=ClassTarget
60
+target1.width=160
61
+target1.x=130
62
+target1.y=10
63
+target2.height=50
64
+target2.name=FishTest
65
+target2.showInterface=false
66
+target2.type=UnitTestTargetJunit4
67
+target2.width=80
68
+target2.x=390
69
+target2.y=170
70
+target3.association=CatTest
71
+target3.height=50
72
+target3.name=Cat
73
+target3.showInterface=false
74
+target3.type=ClassTarget
75
+target3.width=80
76
+target3.x=230
77
+target3.y=290
78
+target4.association=FishTest
79
+target4.height=50
80
+target4.name=Fish
81
+target4.showInterface=false
82
+target4.type=ClassTarget
83
+target4.width=80
84
+target4.x=360
85
+target4.y=200
86
+target5.height=50
87
+target5.name=CatTest
88
+target5.showInterface=false
89
+target5.type=ClassTarget
90
+target5.width=80
91
+target5.x=260
92
+target5.y=260
93
+target6.height=50
94
+target6.name=DogTest
95
+target6.showInterface=false
96
+target6.type=UnitTestTargetJunit4
97
+target6.width=80
98
+target6.x=340
99
+target6.y=80
100
+target7.association=DogTest
101
+target7.height=50
102
+target7.name=Dog
103
+target7.showInterface=false
104
+target7.type=ClassTarget
105
+target7.width=80
106
+target7.x=310
107
+target7.y=110
108
+target8.height=50
109
+target8.name=Pet
110
+target8.showInterface=false
111
+target8.type=ClassTarget
112
+target8.width=80
113
+target8.x=192
114
+target8.y=191
115
+target9.height=50
116
+target9.name=MainApplication
117
+target9.showInterface=false
118
+target9.type=ClassTarget
119
+target9.width=130
120
+target9.x=70
121
+target9.y=70