-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryConversionTrainer.java
More file actions
202 lines (139 loc) · 6.42 KB
/
Copy pathBinaryConversionTrainer.java
File metadata and controls
202 lines (139 loc) · 6.42 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
/*
* @(#)BinaryConversionTrainer.java
* @author Charles Herrera
* @version 1.00 23 February 2015 at 13:22
* Program Purpose: Solves Reddit Daily Programmer Challenge #202 found at http://goo.gl/bbII6N.
* Extended to test binary conversion skills useful in networking and telecommunications
*/
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.Random;
import javax.swing.JFrame;
import java.util.ArrayList;
import static javax.swing.JOptionPane.showConfirmDialog;
public class BinaryConversionTrainer
{
public static void main(String [] args)
{
//declare all the things
Scanner input = new Scanner(System.in);
char toContinue = 'Y';
char binaryChoice = ' ';
char sample = ' ';
int hasAscii = 3;
int sampleCounter = 0;
char continueTo = ' ';
String answer = "";
String activeExercise = "";
String decimalResult = "";
Converter aConverter = new Converter();
do
{
//decide if we want to go with sample inputs from the spec
System.out.printf("Would you like to work on a sample exercise?%n");
sample = input.nextLine().charAt(0);
//we do
if(Character.toUpperCase(sample) == 'Y')
{
System.out.printf("%nOkay, we'll work on a sample problem. Would you like to start " +
"with a value already in binary?");
binaryChoice = input.nextLine().charAt(0);
//yes, binary to decimal/ASCII
if(Character.toUpperCase(binaryChoice) == 'Y')
{
//prompt user to get an ASCII conversion table
//hasAscii(hasAscii);
//end Ascii check
System.out.printf("%nAlright, here we go.%nEach of the following sets of 8 bits" +
" represents a character in the ASCII character set.%n" +
"Convert each byte to its decimal equivalent,%n" +
"then use those numbers from 0 to 255 "
+ "to find the corresponding ASCII character.");
do
{
activeExercise = aConverter.getBinaryExercise(sampleCounter);
System.out.printf("%nHere's your challenge: %s%nPlease enter the " +
"English translation here:",
activeExercise);
String activeExerciseHelper = "";
for(int index = 0; index < activeExercise.length(); index++)
{
activeExerciseHelper = activeExercise.replaceAll("[,\\[\\] ]", "");
}
answer = input.nextLine();
boolean correct = aConverter.checkAnswer(answer, activeExerciseHelper);
if(correct)
{
System.out.printf("%nGood job! %s is correct.%n", answer);
}
else
{
System.out.printf("%nSorry, %s is incorrect. Please try again.", answer);
}
decimalResult = aConverter.convertToDecimal(activeExerciseHelper);
System.out.println(decimalResult);
System.out.printf("The English translation of those values is: %s",
aConverter.getEnglishTranslation(decimalResult));
//more exercises?
boolean valid = false;
while(!valid)
{
System.out.printf("%nWould you like to try another exercise?");
continueTo = input.nextLine().charAt(0);
switch (Character.toUpperCase(continueTo))
{
case 'Y':
sampleCounter++;
valid = true;
break;
case 'N':
sampleCounter += 3;
valid = true;
break;
default:
System.out.printf("%nSorry. Please enter 'yes' or 'no' and try again.%n");
valid = false;
}
}
} while(sampleCounter < 3);
//System.out.printf("DEBUG: helloWorldBinary: %s", helloWorldBinary);
}//end if
else //going TO binary from decimal/ASCII
{
String debugOutput = aConverter.getDecimalValues("Hello World");
System.out.println(debugOutput);
System.out.print(aConverter.getBinary(debugOutput));
}//end else
}
//decide whether we are going to or from binary
System.out.printf("Are you starting with a value already in binary?%n");
binaryChoice = input.nextLine().charAt(0);
//yes, binary to decimal/ASCII
if(Character.toUpperCase(binaryChoice) == 'Y')
{
}//end if
else //going TO binary from decimal/ASCII
{
}//end else
System.out.printf("%n Would you like to continue?");
toContinue = input.nextLine().charAt(0);
}
while(Character.toUpperCase(toContinue) == 'Y');
//end main do/while
System.exit(0);
}//end main()
public static void hasAscii(int hasAscii)
{
while(hasAscii !=0)
{
hasAscii = showConfirmDialog(null, "Alright, you will need an ASCII table for this.\nGo ahead and Google one, I can wait.\nPlease let me know when you are ready");
//System.out.printf("%nDEBUG: n = %d%n", hasAscii);
//if user cancels, exit program
if(hasAscii == 2)
{
JOptionPane.showMessageDialog(null, "Alright then. Goodbye!");
System.exit(0);
}
}
}
}//end application class BinaryConversionTrainer