我正在尝试将字符串转换为pentadeca,但是无论我尝试做什么,似乎都无法使输出正常工作。
这是我的教授制定的指导方针:
编写一个封装了Pentadeca数字值的Pentadeca类。
不允许使用阵列
彭达卡编号是15位数字:0、1、2、3、4、5、6、7、8、9,A,B,C,D,E。该类具有的方法是:
public void read(), public void set(String), public void set(int), public void set(Pentadeca), public Pentadeca get(), public Pentadeca add(Pentadeca), public Pentadeca subtract(Pentadeca), public Pentadeca multiply(Pentadeca), public Pentadeca divide(Pentadeca), public Pentadeca add(int), public Pentadeca subtract(int), public Pentadeca multiply(int), public Pentadeca divide(int), public Pentadeca add(String), public Pentadeca subtract(String), public Pentadeca multiply(String), public Pentadeca divide(String), public boolean equals(Pentadeca), public String toString().
编写私有方法来检查有效的字符输入,删除基本的p字符并设置符号。
- 您的班级必须同时考虑正值和负值。
- 您的类至少应具有3个构造函数:默认构造函数,具有整数参数的显式构造函数和具有String参数的显式构造函数。
我将提供一个主程序作为演示。
回忆数字的性质及其结构。 13710是1 * 102 + 3 * 101 + 7 *100。以同样的方式1A915将是1 * 152 + 10 * 151 + 9 * 150。
给出十进制数字13710,可以使用整数运算符/和%确定数字。 137%10给出7,137 / 10得到13。13%10得到3和13/10得到1。1%10得到1和1/10得到0。
137的数字基数10为1、3、7。转换为基数5的数字13710为137%5,即2、137 / 5为27。27%5为2、27 / 5为5。 5%5给出0,5 / 5给出1。1%5给出1,1 / 5给出0。这给出以5为基数的10225。将13710转换为以15为基数将是137%15得到2,137 / 15给出9 。9%15给出9,而9/15给出0。因此数字13710为9215。
到目前为止,这是我的代码:
public class Pentadeca {
private int pentadecaInt, sign;
private String pentadecaString;
public String getPentadecaString() {
return pentadecaString;
}
public int getPentadecaInt() {
return pentadecaInt;
}
public void setPentadeca(String pentadeca) {
this.pentadecaString = pentadeca;
}
public void setPentadeca(int pentadeca) {
this.pentadecaInt = pentadeca;
}
public Pentadeca() {
this.pentadecaString = "";
this.pentadecaInt = 0;
}
public Pentadeca(String pentadeca) {
this.pentadecaString = pentadeca;
}
public Pentadeca(int pentadeca) {
this.pentadecaInt = pentadeca;
}
public Pentadeca(Pentadeca pentadeca) {
this.pentadecaString = pentadeca.pentadecaString;
this.pentadecaInt = toInt(pentadeca.pentadecaString);
}
public String toString() {
int stringInt = pentadecaInt;
if (pentadecaString == null)
pentadecaString = "";
sign = 0;
if (pentadecaInt < 0) {
sign = -1;
stringInt = pentadecaInt * -1;
}
while (!(stringInt == 0)) {
int part = stringInt % 15;
String partString = "";
if (part > 9) {
switch (part) {
case 10:
partString = "A";
break;
case 11:
partString = "B";
break;
case 12:
partString = "C";
break;
case 13:
partString = "D";
break;
case 14:
partString = "E";
break;
default:
System.exit(0);
break;
}
} else if (part < 0)
System.exit(0);
else
partString = part + "";
pentadecaString = pentadecaString + partString;
stringInt = stringInt / 15;
}
if (sign == -1)
pentadecaString = "-" + pentadecaString;
if (pentadecaString.indexOf('p') == -1)
pentadecaString = pentadecaString + "p";
return pentadecaString;
}
public int toInt(String pentadecaString) {
String integerString = pentadecaString;
integerString = pentadecaString.trim();
int p = pentadecaString.indexOf("p");
if (!(p == -1))
integerString = integerString.substring(0, p);
sign = pentadecaString.indexOf("-");
if (!(sign == -1))
integerString = integerString.substring(sign + 1);
integerString = integerString.toUpperCase();
int exponent, length = integerString.length();
for (exponent = 1; exponent <= length; exponent++) {
int power = length - exponent + 1;
String find = "" + power;
int baseInt = Integer.parseInt(find);
if (baseInt == -1) {
char base = integerString.charAt(power);
switch (base) {
case 'A':
baseInt = 10;
break;
case 'B':
baseInt = 11;
break;
case 'C':
baseInt = 12;
break;
case 'D':
baseInt = 13;
break;
case 'E':
baseInt = 14;
break;
default:
System.exit(0);
break;
}
} else if (baseInt > 9)
System.exit(0);
double pentadecaPower = Math.pow(15, exponent);
int powerInt = (int) pentadecaPower;
int total = baseInt * powerInt;
System.out.println();
pentadecaInt = pentadecaInt + total;
}
if ((!(sign == -1)) && (!((exponent % 2) == 0)))
pentadecaInt = pentadecaInt * -1;
return pentadecaInt;
}
public Pentadeca add(int number) {
Pentadeca answer = new Pentadeca();
int thisPentadecaInt = toInt(this.pentadecaString);
answer.pentadecaInt = number + thisPentadecaInt;
return answer;
}
public Pentadeca add(String number) {
Pentadeca answer = new Pentadeca();
// int thisPentadecaInt = toInt(number);
System.out.println(number);
answer.pentadecaInt = +this.pentadecaInt;
return answer;
}
public Pentadeca add(Pentadeca pentadeca) {
Pentadeca answer = new Pentadeca();
int thisPentadecaInt = toInt(this.pentadecaString), pentPentadecaInt = toInt(pentadeca.pentadecaString);
System.out.println(answer.pentadecaInt + ", "
+ pentadeca.pentadecaString + ", " + thisPentadecaInt);
answer.pentadecaInt = pentPentadecaInt + thisPentadecaInt;
return answer;
}
public Pentadeca subtract(int number) {
Pentadeca answer = new Pentadeca();
int thisPentadecaInt = toInt(this.pentadecaString);
answer.pentadecaInt = thisPentadecaInt - number;
return answer;
}
public Pentadeca subtract(String number) {
return new Pentadeca(number);
}
public Pentadeca subtract(Pentadeca pentadeca) {
return pentadeca;
}
public Pentadeca multiply(int number) {
Pentadeca answer = new Pentadeca();
int thisPentadecaInt = toInt(this.pentadecaString);
answer.pentadecaInt = number * thisPentadecaInt;
return answer;
}
public Pentadeca multiply(String number) {
return new Pentadeca(number);
}
public Pentadeca multiply(Pentadeca pentadeca) {
return pentadeca;
}
public Pentadeca divide(int number) {
Pentadeca answer = new Pentadeca();
int thisPentadecaInt = toInt(this.pentadecaString);
answer.pentadecaInt = thisPentadecaInt / number;
return answer;
}
public Pentadeca divide(String number) {
return new Pentadeca(number);
}
public Pentadeca divide(Pentadeca pentadeca) {
return pentadeca;
}
public boolean equals(Pentadeca pentadeca) {
return this.pentadecaString.equalsIgnoreCase(pentadeca.pentadecaString);
}
public static void main(String[] args)
{
Pentadeca Pentadeca1 = new Pentadeca ("123p");
Pentadeca Pentadeca2 = new Pentadeca ("126p");
Pentadeca Pentadeca4, Pentadeca5, Pentadeca6, Pentadeca7;
Pentadeca Pentadeca8 = new Pentadeca(1999);
System.out.println ("Pentadeca number Pentadeca8: " + Pentadeca8);
System.out.println ("First Pentadeca number: " + Pentadeca1);
System.out.println ("Second Pentadeca number: " + Pentadeca2);
if (Pentadeca1.equals(Pentadeca2))
System.out.println ("Pentadeca1 and Pentadeca2 are equal.");
else
System.out.println ("Pentadeca1 and Pentadeca2 are NOT equal.");
Pentadeca4 = Pentadeca1.add(Pentadeca2);
Pentadeca5 = Pentadeca1.subtract(Pentadeca2);
Pentadeca6 = Pentadeca1.multiply(Pentadeca2);
Pentadeca7 = Pentadeca1.divide(Pentadeca2);
System.out.println (Pentadeca1 + " + " + Pentadeca2 + " is: " +
Pentadeca4);
System.out.println (Pentadeca1 + " - " + Pentadeca2 + " is: " +
Pentadeca5);
System.out.println (Pentadeca1 + " * " + Pentadeca2 + " is: " +
Pentadeca6);
System.out.println (Pentadeca1 + " / " + Pentadeca2 + " is: " +
Pentadeca7);
System.out.println ();
int number = 6;
System.out.println ("using the integer " + number + " as the argument "
+
"to the math operators ");
Pentadeca4 = Pentadeca1.add(number);
Pentadeca5 = Pentadeca1.subtract(number);
Pentadeca6 = Pentadeca1.multiply(number);
Pentadeca7 = Pentadeca1.divide(number);
System.out.println (Pentadeca1 + " + " + number + " is: " + Pentadeca4);
System.out.println (Pentadeca1 + " - " + number + " is: " + Pentadeca5);
System.out.println (Pentadeca1 + " * " + number + " is: " + Pentadeca6);
System.out.println (Pentadeca1 + " / " + number + " is: " + Pentadeca7);
String value = "6p";
System.out.println ("using the String " + "\"" + value + "\"" + " as the argument " + "to the math operators ");
Pentadeca4 = Pentadeca1.add(value);
Pentadeca5 = Pentadeca1.subtract(value);
Pentadeca6 = Pentadeca1.multiply(value);
Pentadeca7 = Pentadeca1.divide(value);
System.out.println (Pentadeca1 + " + " + value + " is: " + Pentadeca4);
System.out.println (Pentadeca1 + " - " + value + " is: " + Pentadeca5);
System.out.println (Pentadeca1 + " * " + value + " is: " + Pentadeca6);
System.out.println (Pentadeca1 + " / " + value + " is: " + Pentadeca7);
}
}
我得到这个用于我的丑陋输出:
123p064206420642064203D6 + 6 is: 6963p
123p064206420642064203D603D6 - 6 is: 9B84p
123p064206420642064203D603D603D6 * 6 is: 00642p
123p064206420642064203D603D603D603D6 / 6 is: 0321p
using the String "6p" as the argument to the math operators
6p
123p064206420642064203D603D603D603D603D6 + 6p is: 03D6p
123p064206420642064203D603D603D603D603D603D6 - 6p is: 6p
123p064206420642064203D603D603D603D603D603D603D6 * 6p is: 6p
123p064206420642064203D603D603D603D603D603D603D603D6 / 6p is: 6p
但这应该是实际输出:
Pentadeca number Pentadeca8: 8D4p
First Pentadeca number: 123p
Second Pentadeca number: 126p
Pentadeca1 and Pentadeca2 are NOT equal.
123p + 126p is: 249p
123p - 126p is: -3p
123p * 126p is: 14E43p
123p / 126p is: 0
using the integer 6 as the argument to the math operators
123p + 6 is: 129p
123p - 6 is: 11Cp
123p * 6 is: 6D3p
123p / 6 is: 2Dp
using the String "6p" as the argument to the math operators
123p + 6p is: 129p
123p - 6p is: 11Cp
123p * 6p is: 6D3p
123p / 6p is: 2Dp
如果有人要帮助我查清我做错的事情,我将非常感激。 :1)我所有的期末考试都感到压力很大。