-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoolArray.java
More file actions
173 lines (163 loc) · 5.53 KB
/
coolArray.java
File metadata and controls
173 lines (163 loc) · 5.53 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
/*
* Filename: coolArray.java
* Name: ***
* Login: ***
* Date: January 13, 2020
* Sources Used:
* [1]Handling negative numbers by Mod in java:
* https://stackoverflow.com/questions/5385024/mod-in-
* java-produces-negative-numbers
* [2]Convert string to char/char[]:
* https://docs.oracle.com/javase/6/docs/api/java/lang/String.html#charAt(int)
*
* The purpose of this profile is to review concepts in CSE8A.
* By using four methods, it reviewes the ASCII table, integer intArray
* 2D integer array, and arithmetic calculations.
*/
import java.util.*;
/**
* The class PA1 contains the four methods demonstrating concepts in CSE8A
*/
class PA1 {
/**
* Main method is for testing all other methods
*/
public static void main(String[] args) {
printASCIIValues("William");
int[] intArray = new int[]{1,2,3,4,5};
rotate1DArray(intArray, 1);
int[][] int2DArray = new int[][]{
{1,2,3},
{4,5,6},
{7,8,9}
};
rotate2DArray(int2DArray, -1, false);
printIntroduction("Dope pur");
}
/**
* Prints the ascii value of each letter in input
*
* @param input Input to print ASCII values for
*/
public static void printASCIIValues(String input) {
for (int i = 0; i < input.length(); i++){
char c = input.charAt(i);
System.out.println( c + " " + (int)c);
}
}
/**
* Returns the rotated 1D Array based on rotations
*
* @param input 1D integer array to rotate for
* @param rotations integer to decide how many times and the direction
* the rotations occur
* @return a rotated 1D integer array
*/
public static int[] rotate1DArray(int[] input, int rotations) {
int[] rotatedArray = new int[input.length];
// make sure the remainder by the module is positive (see Reference[1])
int r = ((rotations % input.length)+input.length) % input.length;
for (int i = 0; i < input.length; i++){
rotatedArray[i] = input[(i - r + input.length) % input.length];
//System.out.println(rotatedArray[i]);
}
return rotatedArray;
}
/**
* Returns the rotated the row or col of a 2D Array based on rotations
*
* @param input 2D integer array to rotate for
* @param rotations integer to decide how many times and the direction
* the rotations occur
* @param rotateRows condition to check whether rotate rows or columns
* @return a rotated 2D integer array
*/
public static int[][] rotate2DArray(int[][] input, int rotations,
boolean rotateRows) {
int row = input.length;
int col = input[0].length;
int[][]rotated2DArray = new int[row][col];
int rotate = ((rotations % input.length)+input.length) % input.length;
//if rotate row
if (rotateRows){
for(int r = 0; r < row; r++){
for(int c = 0; c < col; c++){
rotated2DArray[r][c] = input[(r - rotate + input.length)
% input.length][c];
System.out.print(rotated2DArray[r][c]);
}
System.out.println();
}
//if rotate column
}else{
for(int r = 0; r < row; r ++){
rotated2DArray[r] = rotate1DArray(input[r], rotate);
}for(int r = 0; r < row; r++){
for(int c = 0; c < col; c++){
System.out.print(rotated2DArray[r][c]);
}
System.out.println();
}
}
return rotated2DArray;
}
/**
* print a message along with input name in an asterisk box
*
* @param name name to print along with the message
*/
public static void printIntroduction(String name) {
int row;
int col;
int totalRow = 5;
String hi;
String words;
String ast = "*";
char cast = ast.charAt(0);
String spa = " ";
char cspa = spa.charAt(0);
//check whether name is null or an empty string
if (name == null || name.equals("")){
//decide the size(column specifically) of the array
col = 29;
//decide the text to put in the box
hi = " Hello";
words = hi + "! Welcome to CSE 8B! ";
}else{
int l = name.length();
col = 30 + l;
hi = " Hello ";
words = hi + name + "! Welcome to CSE 8B! ";
}
char[] cwords = words.toCharArray();
char[][] intro = new char[totalRow][col];
for(row = 0; row < totalRow; row ++){
if (row == 0 || row == totalRow - 1){
//creates upper and lower asterisk bound
for(int column = 0; column < col; column++){
intro[row][column] = cast;
}
//creates the second last upper, lower bound with asterisk and space
}else if(row == 1 || row == totalRow - 2){
for(int column = 1; column < col - 1; column++){
intro[row][column] = cspa;
}
intro[row][0] = cast;
intro[row][col - 1] = cast;
//fill in the reminding row with message and name
}else{
intro[row][0] = cast;
intro[row][col - 1] = cast;
for(int column = 1; column < col- 1; column++){
intro[row][column] = cwords[column - 1];
}
}
}
for(int r = 0; r < row; r++){
for(int c = 0; c < col; c++){
System.out.print(intro[r][c]);
}
System.out.println();
}
}
}