Write a Pentadeca class that encapsulates a Pentadeca number value. A Pentadeca
ID: 3769429 • Letter: W
Question
Write a Pentadeca class that encapsulates a Pentadeca number value. A Pentadeca number is one with base 15. THE USE OF ARRAYS IS NOT PERMITTED. The methods the class has are: 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), and public String toString(). Write private methods to check for valid character input, remove the base p character and set the sign. Your class must consider both positive and negative values. Your class should have at least 3 constructors: a default constructor, an explicit constructor with an integer argument and an explicit constructor with a String argument. I will provide a main program as the demo. Recall the nature of numbers and their construction. 13710 is 1 * 102 + 3 * 101 + 7 * 100 . In the same way 1A915 would be 1 * 152 + 10* 151 + 9 * 150 . Given the decimal number 13710 the digits can be determined using the integer operators / and %. 137 % 10 gives the 7, 137/10 gives 13. 13%10 gives 3 and 13/10 gives 1. 1%10 gives 1 and 1/10 gives 0. The digits base 10 of 137 are 1, 3, 7. The number 13710 converted to base 5 would be 137%5 that gives 2, 137/5 gives 27. 27%5 gives 2, 27/5 gives 5. 5%5 gives 0, 5/5 gives 1. 1%5 gives 1 and 1/5 gives 0. This gives the base 5 number 10225. 13710 converted to base 15 would be 137%15 gives 2, 137/15 gives 9. 9%15 gives 9 and 9/15 gives 0. The number 13710 is therefore 9215
Explanation / Answer
public class Pentadeca {
private String pentadeca;
private Integer decimal;
private static final int BASE = 15;
public static void main(String[] args) {
Pentadeca penta = new Pentadeca(111);
try {
System.out.println(penta.add(112));
System.out.println(penta.subtract(112));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
public Pentadeca(int integer) {
this.set(integer);
}
public Pentadeca(String string) throws Exception {
this.set(string);
}
public void read() {
}
public void set(String string) throws Exception {
for (int i = 0; i < string.length(); i++) {
if (Character.isDigit(string.charAt(i)) || (string.charAt(i) >= 65 && string.charAt(i) <= 69))
continue;
else
throw new Exception("INVALID ARGUMENT");
}
this.pentadeca = string;
decimal = Integer.valueOf(pentadeca, BASE);
}
public void set(int integer) {
this.pentadeca = Integer.toString(integer);
decimal = Integer.valueOf(pentadeca, BASE);
}
public void set(Pentadeca p) {
this.decimal = p.getDecimal();
this.pentadeca = Integer.toString(this.decimal, BASE);
}
public Pentadeca get() {
return this;
}
public Integer getDecimal() {
return this.decimal;
}
public Pentadeca add(Pentadeca p) throws Exception {
int sum = this.decimal + p.getDecimal();
return new Pentadeca(Integer.toString(sum, BASE));
}
public Pentadeca subtract(Pentadeca p) throws Exception {
int difference = this.decimal - p.getDecimal();
return new Pentadeca(Integer.toString(difference, BASE));
}
public Pentadeca multiply(Pentadeca p) throws Exception {
int product = this.decimal * p.getDecimal();
return new Pentadeca(Integer.toString(product, BASE));
}
public Pentadeca divide(Pentadeca p) throws Exception {
int quotient = this.decimal / p.getDecimal();
return new Pentadeca(Integer.toString(quotient, BASE));
}
public Pentadeca add(int i) throws Exception {
return this.add(new Pentadeca(Integer.toString(i, BASE)));
}
public Pentadeca subtract(int i) throws Exception {
return this.subtract(new Pentadeca(Integer.toString(i, BASE)));
}
public Pentadeca multiply(int i) throws Exception {
return this.multiply(new Pentadeca(Integer.toString(i, BASE)));
}
public Pentadeca divide(int i) throws Exception {
return this.divide(new Pentadeca(Integer.toString(i, BASE)));
}
public Pentadeca add(String s) throws Exception {
return this.add(new Pentadeca(s));
}
public Pentadeca subtract(String s) throws Exception {
return this.subtract(new Pentadeca(s));
}
public Pentadeca multiply(String s) throws Exception {
return this.multiply(new Pentadeca(s));
}
public Pentadeca divide(String s) throws Exception {
return this.divide(new Pentadeca(s));
}
public boolean equals(Pentadeca p) {
return this.decimal == p.getDecimal();
}
public String toString() {
return this.pentadeca;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.