-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogic.java
More file actions
119 lines (107 loc) · 3.77 KB
/
Logic.java
File metadata and controls
119 lines (107 loc) · 3.77 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package mooc.vandy.java4android.diamonds.logic;
import mooc.vandy.java4android.diamonds.ui.OutputInterface;
/**
* This is where the logic of this App is centralized for this assignment.
* <p>
* The assignments are designed this way to simplify your early
* Android interactions. Designing the assignments this way allows
* you to first learn key 'Java' features without having to beforehand
* learn the complexities of Android.
*/
public class Logic
implements LogicInterface {
/**
* This is a String to be used in Logging (if/when you decide you
* need it for debugging).
*/
public static final String TAG = Logic.class.getName();
/**
* This is the variable that stores our OutputInterface instance.
* <p>
* This is how we will interact with the User Interface [MainActivity.java].
* <p>
* It is called 'out' because it is where we 'out-put' our
* results. (It is also the 'in-put' from where we get values
* from, but it only needs 1 name, and 'out' is good enough).
*/
private OutputInterface mOut;
/**
* This is the constructor of this class.
* <p>
* It assigns the passed in [MainActivity] instance (which
* implements [OutputInterface]) to 'out'.
*/
public Logic(OutputInterface out){
mOut = out;
}
/**
* This is the method that will (eventually) get called when the
* on-screen button labeled 'Process...' is pressed.
*/
public void process(int size) {
// TODO -- add your code here
int diamondHeight = (size*+2)+1;
int diamondWidth = (size*2)+2;
int count = -(size+1);
for (int n=1;n<=diamondHeight;n++)//iterate for diamond height
{
count++;
for (int i=1;i<=diamondWidth;i++)//iterate for diamond width
{
if (( n==1|| n==diamondHeight)&&(i==1||i==diamondWidth)){
mOut.print("+");
}else if ((n==1||n==diamondHeight)&&!(i==1||i==diamondWidth)){
mOut.print("-");
}else if (!(n==1||n==diamondHeight)&&(i==1||i==diamondWidth)){
mOut.print("|");
}else {
diamondShape(size, n, i, count);
}
}mOut.print("\n");
}
}
private void diamondShape(int size, int n, int i, int count) {
int rowDiamond;
if (count<=0){
rowDiamond = n*2-2;
}else {
rowDiamond = (n-count*2)*2-2;
}
int midDiamond = size + 1;
int diamondLeftBound = midDiamond - (rowDiamond/2-1);
int diamondRightBound = midDiamond+(rowDiamond/2);
int topOfFrame = 1;
int bottomOfFrame = size*2+1;
if (i >= diamondLeftBound && i <= diamondRightBound) {
if (i == diamondLeftBound|| i == diamondRightBound) {
if (n< midDiamond && n > topOfFrame) {
if (i == diamondLeftBound) {
mOut.print("/");
} else {
mOut.print("\\");
}
} else if (n == midDiamond) {
if (i == diamondLeftBound) {
mOut.print("<");
} else {
mOut.print(">");
}
} else if (n > midDiamond && n < bottomOfFrame) {
if (i == diamondLeftBound) {
mOut.print("\\");
} else {
mOut.print("/");
}
}
} else {
if (n % 2 == 0) {
mOut.print("=");
} else {
mOut.print("-");
}
}
} else {
mOut.print(" ");
}
}
}