-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHwrite.java
More file actions
executable file
·215 lines (190 loc) · 7.96 KB
/
Hwrite.java
File metadata and controls
executable file
·215 lines (190 loc) · 7.96 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/******************************************************************************
* Hwrite Class Final Version prepared by Hal Stringer, UCF
*
* This class provides support for formatted output to a FileWriter called
* The pointer to the FileWriter is passed to the method along with the output
* data and any formatting parameters.
*
* Most methods in the class accept two parameters: a primitive data
* value that is to be printed and 2) a field size. Each method will right
* justify, left justify or center the data value within the specified field and
* send the final string to output file. For double values, the programmer
* can optionally specify the number of decimal places to output. Digits
* occuring after the number specified are rounded off. Values that do not fit
* within the specified field length will be replaced by #### symbols.
*
*******************************************************************************/
import java.io.*;
import java.util.*;
import java.text.*;
public class Hwrite {
/*******************************************************************************
* STATIC VARIABLES (CLASS VARIABLES) *
*******************************************************************************/
// Variable "pad" set to 50 spaces - determines maximum field size.
// To increase maximum field length, add more spaces to the pad string.
private static final String pad = " ";
/*******************************************************************************
* STATIC PRINTING METHODS (CLASS METHODS) *
*******************************************************************************/
// Write Left Justified Integer
public static void left(int data, int fieldLength, FileWriter output) throws java.io.IOException {
String dataText = Integer.toString(data);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength, output);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
output.write(dataText + padding);
return;
}
// Write Right Justified Integer
public static void right(int data, int fieldLength, FileWriter output) throws java.io.IOException {
String dataText = Integer.toString(data);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength, output);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
output.write(padding + dataText);
return;
}
// Write Centered Integer
public static void center(int data, int fieldLength, FileWriter output) throws java.io.IOException {
String dataText = Integer.toString(data);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength, output);
return;
}
int totalPadLength = fieldLength - dataText.length();
int endPadLength = totalPadLength / 2;
int startPadLength = totalPadLength - endPadLength;
String startPadding = pad.substring(0, startPadLength);
String endPadding = pad.substring(0, endPadLength);
output.write(startPadding + dataText + endPadding);
return;
}
/*******************************************************************************/
// Write Left Justified Double - Default Decimal Places
public static void left(double data, int fieldLength, FileWriter output) throws java.io.IOException {
String dataText = Double.toString(data);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength, output);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
output.write(dataText + padding);
return;
}
// Write Right Justified Double - Default Decimal Places
public static void right(double data, int fieldLength, FileWriter output) throws java.io.IOException {
String dataText = Double.toString(data);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength, output);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
output.write(padding + dataText);
return;
}
// Write Centered Double - Default Decimal Places
public static void center(double data, int fieldLength, FileWriter output) throws java.io.IOException {
String dataText = Double.toString(data);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength, output);
return;
}
int totalPadLength = fieldLength - dataText.length();
int endPadLength = totalPadLength / 2;
int startPadLength = totalPadLength - endPadLength;
String startPadding = pad.substring(0, startPadLength);
String endPadding = pad.substring(0, endPadLength);
output.write(startPadding + dataText + endPadding);
return;
}
/*******************************************************************************/
// Write Left Justified Double - User Specified Decimal Places
public static void left(double data, int fieldLength, int places, FileWriter output) throws java.io.IOException {
String dataText = fixDecimals(data, places);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength, output);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
output.write(dataText + padding);
return;
}
// Write Right Justified Double - User Specified Decimal Places
public static void right(double data, int fieldLength, int places, FileWriter output) throws java.io.IOException {
String dataText = fixDecimals(data, places);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength, output);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
output.write(padding + dataText);
return;
}
// Write Centered Double - User Specified Decimal Places
public static void center(double data, int fieldLength, int places, FileWriter output) throws java.io.IOException {
String dataText = fixDecimals(data, places);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength, output);
return;
}
int totalPadLength = fieldLength - dataText.length();
int endPadLength = totalPadLength / 2;
int startPadLength = totalPadLength - endPadLength;
String startPadding = pad.substring(0, startPadLength);
String endPadding = pad.substring(0, endPadLength);
output.write(startPadding + dataText + endPadding);
return;
}
/*******************************************************************************/
// Write Left Justified String
public static void left(String dataText, int fieldLength, FileWriter output) throws java.io.IOException {
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength, output);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
output.write(dataText + padding);
return;
}
// Write Right Justified String
public static void right(String dataText, int fieldLength, FileWriter output) throws java.io.IOException {
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength, output);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
output.write(padding + dataText);
return;
}
// Write Centered String
public static void center(String dataText, int fieldLength, FileWriter output) throws java.io.IOException {
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength, output);
return;
}
int totalPadLength = fieldLength - dataText.length();
int endPadLength = totalPadLength / 2;
int startPadLength = totalPadLength - endPadLength;
String startPadding = pad.substring(0, startPadLength);
String endPadding = pad.substring(0, endPadLength);
output.write(startPadding + dataText + endPadding);
return;
}
/*******************************************************************************/
private static void fieldLengthError(int fieldLength, FileWriter output) throws java.io.IOException {
for(int i=1; i<=fieldLength; ++i){
output.write("#");
}
return;
}
private static String fixDecimals(double data, int places){
data=Math.round(data * Math.pow(10,places)) / Math.pow(10,places);
String dataText = Double.toString(data);
return(dataText);
}
} // End of Hwrite.java