-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaircase.java
More file actions
81 lines (69 loc) · 1.65 KB
/
Staircase.java
File metadata and controls
81 lines (69 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.io.*;
import java.util.*;
public class Staircase implements IPattern {
int N = 10;
public static void main(String[] args){
IPattern obj = new Staircase();
System.out.println(obj.printPattern(10));
System.out.println(obj.printNthItem(10));
System.out.println(obj.printNthItem(2));
}
/**
*prints staircase pattern with a base of n
* Print the pattern for n=10
x
xx
xxx
xxxx
xxxxx
xxxxxx
xxxxxxx
xxxxxxxx
xxxxxxxxx
xxxxxxxxxx
*/
@Override
public String printPattern(int n) {
if (n == 0 || n < 0){
return "";
}
String newValue = ""; // empty string for return value
for (int line = 0; line <= n; line++){ // loop for lines
int space = 0;
for(space = 0;space < (n-line); space++) // loop for spaces
newValue += " ";
for(int star = space; star < n; star++) // loop for asterisks
newValue += "x";
newValue += "\n"; // skip line
}
return newValue;
}
/**
* prints the nth line in the previous staircase pattern
* print the 10th line of the pattern:
xxxxxxxxxx
*/
@Override
public String printNthItem(int n) {
if ( n ==0 || n < 0){
return "";
}
String newVal = ""; // empty string
int space = 0;
for(space = 0 ; space < (this.N - n); space++) { // space loop
newVal += " ";
}
for(int star = space; star < this.N; star++){ // asterisk loop
newVal += "x";
}
return newVal;
}
/**
* sets parameters
* This changes the value of class variable N to arg.
*/
@Override
public void setParam(int arg) {
this.N = arg; // sets parameter
}
}