KaiquanMah commited on
Commit
777b321
·
verified ·
1 Parent(s): f7bf95f

Create 3B. Print several

Browse files
Week 2: Methods, strings and lists/3B. Print several ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Write the method
2
+
3
+ public static void printSeveral(String str, int amount)
4
+
5
+ which takes a string and a number as parameters.
6
+
7
+
8
+
9
+ The method prints the given string to a single stack a given number of times.
10
+ Example on calling the method:
11
+
12
+ public static void main(String[] args) {
13
+ printSeveral("xy", 4);
14
+ printSeveral("bye",3);
15
+ }
16
+
17
+
18
+ Program outputs:
19
+ xyxyxyxy
20
+ byebyebye
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+ import java.util.Random;
31
+
32
+ public class Test{
33
+ public static void main(String[] args){
34
+ final Random r = new Random();
35
+
36
+
37
+ Object[][] p = {{"abc", 3}, {"hello ",4}, {"*xyz",5}, {"-",15}};
38
+
39
+ for (Object[] param : p) {
40
+ System.out.println("Testing with parameters \"" + param[0] + "\", " + param[1]);
41
+ printSeveral((String) param[0], (Integer) param[1]);
42
+ System.out.println("");
43
+ }
44
+ }
45
+
46
+ //CONTINUE HERE
47
+ public static void printSeveral(String str, int amount){
48
+ for (int i=1; i<=amount; i++) {
49
+ System.out.print(str);
50
+ }
51
+ }
52
+
53
+
54
+
55
+
56
+ }
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+ Testing with parameters "abc", 3
65
+ abcabcabc
66
+
67
+ Testing with parameters "hello ", 4
68
+ hello hello hello hello
69
+
70
+ Testing with parameters "*xyz", 5
71
+ *xyz*xyz*xyz*xyz*xyz
72
+
73
+ Testing with parameters "-", 15
74
+ ---------------
75
+
76
+