-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayPatterns.java
More file actions
179 lines (114 loc) · 4.44 KB
/
Copy pathArrayPatterns.java
File metadata and controls
179 lines (114 loc) · 4.44 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Problem Statement:
// ------------------------------------------------
// Write a Java program which fill a 2D-array square array as per a given patterns 1 and 2 and display them.
//
// Your program should proceed as follows:
// 1. Display a welcome message.
// 2. Prompt the user for an integer which is >=3. If the number entered is < 3, keep prompting the user until they enter a number >=3.
// This number will determine the size of the square array.
// 3. Fill the array as per pattern 1 and display it using printf to format the array.
// 4. Fill the same array as per pattern 2 and display it using printf to format the array.
// 5. Display a closing message.
//
//
// Pattern 1: Pattern 2:
//
// 1 2 3 4 5 1 2 3 4 5
// 10 9 8 7 6 10 6 7 8 9
// 11 12 13 14 15 14 15 11 12 13
// 20 19 18 17 16 20 19 18 16 17
// 21 22 23 24 25 22 23 24 25 21
import static java.lang.System.out;
import java.util.Scanner;
class ArrayPatterns {
private final Scanner input = new Scanner (System.in);
private int [][]array;
void displayWelcomeMessage(){
out.println("\n [----------------]");
out.println(" [ Array Patterns ]");
out.println(" [----------------]");
takeInput();
}
void diplayClosingMessage(){
out.println("\n All done!!!!");
}
private void takeInput(){
out.print("\n How many rows/columns do you want your array to have? (Must be atleast 3): ");
int select = input.nextInt();
if( select < 3 ){
out.println(" Let's try this again ....");
takeInput();
}
else{
array = new int [select][select];
this.poplateArray();
}
}
private void poplateArray(){
int number = 1;
for (int[] array1 : array) {
for (int j = 0; j < array1.length; j++) {
array1[j] = number++;
}
}
}
void displayArray (){
out.println();
for (int[] array1 : array) {
for (int j : array1) {
out.printf(" %d", j);
}
out.println();
}
}
void patternOne(){
int [] temp = new int [array.length];
for( int i = 1; i < array.length; i+= 2 ){ // odd rows
int index = array.length - 1;
System.arraycopy(array[i], 0, temp, 0, array.length);
for(int j = 0; j < array.length; j++)
array[i][j] = temp[index--];
}
out.println("\n Pattern 1:");
this.displayArray();
this.poplateArray();
}
void patternTwo(){
int overwritePreviousValue = 0;
int minusValue = 1;
for ( int i = 1; i < array.length; i++ ) {
this.shiftingElements(array[i], overwritePreviousValue++, minusValue++);
}
out.println("\n Pattern 2:");
this.displayArray();
}
// My Algorithm
private void shiftingElements( int [] arr, int overwritePreviousValue, int minusValue ){
int [] temp = new int [arr.length];
System.arraycopy(arr, 0, temp, 0, arr.length);
int hold;
int index = arr.length - 1;
for( int j = index; j >= 0; j-- ){
if( overwritePreviousValue == j ){
arr[j] = temp[index--];
overwritePreviousValue--;
}
else{
arr[j] = arr[j-minusValue];
}
}
}
}
----------------------------------------------------------------------------------------
Main class
----------------------------------------------------------------------------------------
public class Main {
public static void main(String [] args) {
ArrayPatterns ap = new ArrayPatterns();
ap.displayWelcomeMessage();
ap.patternOne();
ap.patternTwo();
ap.diplayClosingMessage();
System.out.println();
}
}