This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHprint.java
204 lines (187 loc) · 7.19 KB
/
Hprint.java
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
/******************************************************************************
* Hprint Class Final Version prepared by Hal Stringer, UCF
*
* This class provides support for formatted output to the system console.
* 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 the console. For double values, the programmer
* can optionally specify the number of decimal places to display. Digits
* occuring after the display point are rounded off. Values that will not fit
* within the specified field length will be replaced by #### symbols.
*
*******************************************************************************/
import java.io.*;
import java.util.*;
import java.text.*;
public class Hprint {
/*******************************************************************************
* 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) *
*******************************************************************************/
// Print Left Justified Integer
public static void left(int data, int fieldLength){
String dataText = Integer.toString(data);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
System.out.print(dataText + padding);
return;
}
// Print Right Justified Integer
public static void right(int data, int fieldLength){
String dataText = Integer.toString(data);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
System.out.print(padding + dataText);
return;
}
// Print Centered Integer
public static void center(int data, int fieldLength){
String dataText = Integer.toString(data);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength);
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);
System.out.print(startPadding + dataText + endPadding);
return;
}
/*******************************************************************************/
// Print Left Justified Double - Default Decimal Places
public static void left(double data, int fieldLength){
String dataText = Double.toString(data);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
System.out.print(dataText + padding);
return;
}
// Print Right Justified Double - Default Decimal Places
public static void right(double data, int fieldLength){
String dataText = Double.toString(data);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
System.out.print(padding + dataText);
return;
}
// Print Centered Double - Default Decimal Places
public static void center(double data, int fieldLength){
String dataText = Double.toString(data);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength);
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);
System.out.print(startPadding + dataText + endPadding);
return;
}
/*******************************************************************************/
// Print Left Justified Double - User Specified Decimal Places
public static void left(double data, int fieldLength, int places){
String dataText = fixDecimals(data, places);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
System.out.print(dataText + padding);
return;
}
// Print Right Justified Double - User Specified Decimal Places
public static void right(double data, int fieldLength, int places){
String dataText = fixDecimals(data, places);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
System.out.print(padding + dataText);
return;
}
// Print Centered Double - User Specified Decimal Places
public static void center(double data, int fieldLength, int places){
String dataText = fixDecimals(data, places);
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength);
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);
System.out.print(startPadding + dataText + endPadding);
return;
}
/*******************************************************************************/
// Print Left Justified String
public static void left(String dataText, int fieldLength){
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
System.out.print(dataText + padding);
return;
}
// Print Right Justified String
public static void right(String dataText, int fieldLength){
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength);
return;
}
String padding = pad.substring(0, fieldLength-dataText.length());
System.out.print(padding + dataText);
return;
}
// Print Centered String
public static void center(String dataText, int fieldLength){
if (fieldLength < dataText.length()){
fieldLengthError(fieldLength);
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);
System.out.print(startPadding + dataText + endPadding);
return;
}
/*******************************************************************************/
private static void fieldLengthError(int fieldLength){
for(int i=1; i<=fieldLength; ++i){
System.out.print("#");
}
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 Hprint.java