I need to separate the following program that I wrote into t
I need to separate the following program that I wrote into two classes. One reusable class and one with client code. I\'m confused on how to separate this one class into two.
1 /**
2 Takes two user-given numbers in 14-base format,
3 turn them to 10-base to perform summation and multiplication,
4 and finnaly return answer back to 14-base format, which it displays.
5
6 @author Pedro Torres
7 Homework 6
8 */
9
10 import java.util.*;
11
12 public class H6_pedro {
13
14 /*
15 Promps user for two 14-based numbers and checks for validity.
16
17 @return The inputs as a string array.
18 */
19 public static String[] getInputs()
20 {
21
22 //input scannner object
23 Scanner scan = new Scanner(System.in);
24
25 //welcome message to get 2 numbers
26 System.out.println(\"To give you the summation and product of 2 numbers, \");
27 System.out.println(\"please enter the two 14-based numbers separated by \");
28 System.out.println(\"a space on the next line.\ \");
29 String numbers14 = scan.nextLine();
30
31 //splits input by space into array
32 String[] inputs = numbers14.split(\"\\\\s\");
33 String input1 = inputs[0];
34 String input2 = inputs[1];
35 boolean isInput1Valid = isValid(input1);
36 boolean isInput2Valid = isValid(input2);
37
38 //while either input is invalid display error message
39 while (!isInput1Valid || !isInput2Valid)
40 {
41 if (!isInput1Valid)
42 {
43 System.out.println(\"First number is not valid!\");
44 }
45 if (!isInput2Valid)
46 {
47 System.out.println(\"Second number is not valid!\");
48 }
49
50 //if numbers invalid asks for numbers again
51 System.out.println(\"Please re-enter your number in 14-base format.\ \");
52
53
54 input1 = scan.nextLine();
55 input2 = scan.nextLine();
56
57 isInput1Valid = isValid(input1);
58 isInput2Valid = isValid(input2);
59 }
60 //return inputs as array
61 return inputs;
62 }
63
64 /**
65 Checks the validity of the input.
66
67 @param input String number from user in 14-base format.
68 @return If input is valid or not.
69
70 */
71 public static boolean isValid (String input)
72 {
73 //checks string for numbers or letters a,j,q,k case insensitive.
74 for (int i = 0; i < input.length(); i++)
75 {
76 char ch = Character.toLowerCase(input.charAt(i));
77 if (!Character.isDigit(ch) && (ch != \'a\' && ch!= \'j\' && ch != \'q\' && ch != \'k\'))
78 {
79 return false;
80 }
81 }
82 return true;
83 }
84
85 /**
86 Turns 14-base input to 10-base format.
87
88 @param alienNum 14-base number.
89 @return result 10-base number.
90 */
91 public static int turnTo10Base (String alienNum)
92 {
93 int result = 0;
94
95 //checks through each character to get number value
96 for (int i = 0; i < alienNum.length(); i++) {
97 char c = alienNum.charAt(i);
98 int val = Character.getNumericValue(c);
99 if ( val == -1)
100 {
101 val = numberEquivalent(c);
102 }
103 val *= Math.pow(14,(alienNum.length()-1-i));
104 result += val;
105 }
106 return result;
107 }
108
109 /**
110 Turn 10-base to 14-base number.
111
112 @param num 10-base number.
113 @return result 14-base number.
114 */
115 public static String turnTo14Base (int num)
116 {
117 boolean done = false;
118 int quotient = num;
119 int remainder = 0;
120 String result = \"\";
121 while (!done)
122 {
123 //separates number by 14
124 remainder = quotient % 14;
125 quotient = quotient / 14;
126 result = letterEquivalent(remainder) + result;
127 if ( quotient == 0)
128 {
129 done = true;
130 }
131 }
132 return result;
133 }
134
135
136 /**
137 Turns letter into number.
138
139 @param ch Letter to be turned into number.
140 @return 10,11,12,13 Number equivalent.
141 */
142 public static int numberEquivalent (char ch) {
143 if (ch == \'a\')
144 {
145 return 10;
146 }
147 else if (ch == \'j\')
148 {
149 return 11;
150 }
151 else if (ch == \'q\')
152 {
153 return 12;
154 }
155 else if (ch == \'k\')
156 {
157 return 13;
158 }
159 else
160 {
161 return Integer.parseInt(String.valueOf(ch));
162 }
163 }
164
165 /**
166 Turns number into letter equivalent.
167
168 @param num Number to be converted.
169 @return A Letter equivalent of number.
170 @return J Letter equivalent of number.
171 @return Q Letter equivalent of number.
172 @return K Letter equivalent of number.
173 */
174 public static String letterEquivalent( int num)
175 {
176 if (num == 10)
177 {
178 return \"A\";
179 }
180 else if (num == 11)
181 {
182 return \"J\";
183 }
184 else if (num == 12)
185 {
186 return \"Q\";
187 }
188 else if (num == 13)
189 {
190 return \"K\";
191 }
192 else
193 {
194 return \"\" + num;
195 }
196 }
197
198 /**
199 Adds numbers in 10-base format and returns it in 14-base.
200
201 @param input1 First number to convert.
202 @param input2 Second number to convert.
203 @return retult Addition of numbers.
204 */
205 public static String addNums (String input1, String input2)
206 {
207 int firstInput = turnTo10Base(input1);
208 int secondInput = turnTo10Base(input2);
209 String result = turnTo14Base(firstInput + secondInput);
210
211 return result;
212 }
213
214 /**
215 Multiplies numbers in 10-base format and returns it in 14-base.
216
217 @param input1 14-base number to be multiplied.
218 @param input2 14-base number to be multiplied.
219 @return result Product of the numbers.
220 */
221 public static String productNums (String input1, String input2)
222 {
223 int firstInput = turnTo10Base(input1);
224 int secondInput = turnTo10Base(input2);
225 String result = turnTo14Base(firstInput * secondInput);
226
227 return result;
228
229 }
230
231 //main method that gets 14-base inputs to add and multiply then
232 //displays answers.
233 public static void main(String[] args)
234 {
235 String[] inputs = getInputs();
236 String answerSum = addNums(inputs[0], inputs[1]);
237 String answerProduct = productNums(inputs[0], inputs[1]);
238 System.out.println(\"The sum is \" + answerSum + \".\");
239 System.out.println(\"The product is \" + answerProduct + \".\");
240 }
241
242 }
Solution
Conversion.java
import java.util.*;
public class Conversion {
//Default constructor
public Conversion()
{
}
/*
* Promps user for two 14-based numbers and checks for validity.
*
* @return The inputs as a string array.
*/
public String[] getInputs() {
// input scanner object
Scanner scan = new Scanner(System.in);
// welcome message to get 2 numbers
System.out
.println(\"To give you the summation and product of 2 numbers, \");
System.out
.println(\"please enter the two 14-based numbers separated by \");
System.out.println(\"a space on the next line.\ \");
String numbers14 = scan.nextLine();
// splits input by space into array
String[] inputs = numbers14.split(\"\\\\s\");
String input1 = inputs[0];
String input2 = inputs[1];
boolean isInput1Valid = isValid(input1);
boolean isInput2Valid = isValid(input2);
// while either input is invalid display error message
while (!isInput1Valid || !isInput2Valid) {
if (!isInput1Valid) {
System.out.println(\"First number is not valid!\");
}
if (!isInput2Valid) {
System.out.println(\"Second number is not valid!\");
}
// if numbers invalid asks for numbers again
System.out
.println(\"Please re-enter your number in 14-base format.\ \");
input1 = scan.nextLine();
input2 = scan.nextLine();
isInput1Valid = isValid(input1);
isInput2Valid = isValid(input2);
}
// return inputs as array
return inputs;
}
/**
* Checks the validity of the input.
*
* @param input
* String number from user in 14-base format.
* @return If input is valid or not.
*/
public boolean isValid(String input) {
// checks string for numbers or letters a,j,q,k case insensitive.
for (int i = 0; i < input.length(); i++) {
char ch = Character.toLowerCase(input.charAt(i));
if (!Character.isDigit(ch)
&& (ch != \'a\' && ch != \'j\' && ch != \'q\' && ch != \'k\')) {
return false;
}
}
return true;
}
/**
* Turns 14-base input to 10-base format.
*
* @param alienNum
* 14-base number.
* @return result 10-base number.
*/
public int turnTo10Base(String alienNum) {
int result = 0;
// checks through each character to get number value
for (int i = 0; i < alienNum.length(); i++) {
char c = alienNum.charAt(i);
int val = Character.getNumericValue(c);
if (val == -1) {
val = numberEquivalent(c);
}
val *= Math.pow(14, (alienNum.length() - 1 - i));
result += val;
}
return result;
}
/**
* Turn 10-base to 14-base number.
*
* @param num
* 10-base number.
* @return result 14-base number.
*/
public String turnTo14Base(int num) {
boolean done = false;
int quotient = num;
int remainder = 0;
String result = \"\";
while (!done) {
// separates number by 14
remainder = quotient % 14;
quotient = quotient / 14;
result = letterEquivalent(remainder) + result;
if (quotient == 0) {
done = true;
}
}
return result;
}
/**
* Turns letter into number.
*
* @param ch
* Letter to be turned into number.
* @return 10,11,12,13 Number equivalent.
*/
public int numberEquivalent(char ch) {
if (ch == \'a\') {
return 10;
} else if (ch == \'j\') {
return 11;
} else if (ch == \'q\') {
return 12;
} else if (ch == \'k\') {
return 13;
} else {
return Integer.parseInt(String.valueOf(ch));
}
}
/**
* Turns number into letter equivalent.
*
* @param num
* Number to be converted.
* @return A Letter equivalent of number.
* @return J Letter equivalent of number.
* @return Q Letter equivalent of number.
* @return K Letter equivalent of number.
*/
public String letterEquivalent(int num) {
if (num == 10) {
return \"A\";
} else if (num == 11) {
return \"J\";
} else if (num == 12) {
return \"Q\";
} else if (num == 13) {
return \"K\";
} else {
return \"\" + num;
}
}
/**
* Adds numbers in 10-base format and returns it in 14-base.
*
* @param input1
* First number to convert.
* @param input2
* Second number to convert.
* @return retult Addition of numbers.
*/
public String addNums(String input1, String input2) {
int firstInput = turnTo10Base(input1);
int secondInput = turnTo10Base(input2);
String result = turnTo14Base(firstInput + secondInput);
return result;
}
/**
* Multiplies numbers in 10-base format and returns it in 14-base.
*
* @param input1
* 14-base number to be multiplied.
* @param input2
* 14-base number to be multiplied.
* @return result Product of the numbers.
*/
public String productNums(String input1, String input2) {
int firstInput = turnTo10Base(input1);
int secondInput = turnTo10Base(input2);
String result = turnTo14Base(firstInput * secondInput);
return result;
}
}
______________________
ConversionClient.java
public class ConversionClient {
public static void main(String[] args) {
Conversion conv=new Conversion();
String[] inputs = conv.getInputs();
String answerSum = conv.addNums(inputs[0], inputs[1]);
String answerProduct = conv.productNums(inputs[0], inputs[1]);
System.out.println(\"The sum is \" + answerSum + \".\");
System.out.println(\"The product is \" + answerProduct + \".\");
}
}
_________________
Output:
To give you the summation and product of 2 numbers,
please enter the two 14-based numbers separated by
a space on the next line.
17 13
The sum is 2A.
The product is 1J7.
____________Thank You







