Can anyone help me with this code please. Ive answered the entire code except 2
ID: 3701635 • Letter: C
Question
Can anyone help me with this code please. Ive answered the entire code except 2 questions: public MyInteger(int decimal, int b) { and the last questionpublic MyInteger getValueInBase(int b) { return new MyInteger("0", b); //to be completed
HERE CODE:
import java.util.Arrays;
public class MyInteger {
private int base;
private int[] data;
/**
* DO NOT MODIFY
*/
public MyInteger() {
}
public int getBase() {
return base;
}
/**
* DO NOT MODIFY
* @return a DEEP copy of data
*/
public int[] getData() {
int[] result = new int[data.length];
for(int i=0; i < data.length; i++)
result[i] = data[i];
return result;
}
/**
* 10 marks
* set base to b such that any value of b less than 2 results in base becoming 2,
* any value of b more than 10 results in base becoming 10, and any value of b
* in the range [2, 10] results in base becoming b.
*
* IMPORTANT: if base already has a value between 2 and 10
* (inclusive on both sides), it should NOT be changed.
* @param b
*/
public void setBase(int b) {
if (base >= 2 && base <= 10)
return;
if (b < 2)
base =2;
else if (b > 10)
base = 10;
else base = b;
}
/**
* 30 marks
* assume that base has already been set (to a value between 2 and 10).
* populate array data with the contents of String passed such that -
* if d.charAt(i) is between '0' and character corresponding to (base-1),
* then data[i] becomes integer corresponding to d.charAt(i)
* otherwise data[i] becomes 0.
*
* For example,
* if base = 4 and d = "341518", data becomes {3,0,1,0,1,0}
* if base = 9 and d = "341518", data becomes {3,4,1,5,1,8}
* if base = 4 and d = "9sh2883^1", data becomes {0,0,0,2,0,0,3,0,1}
* @param d
*/
public void setDataBasic(String d) {
data = new int[d.length()];
for (int i = 0; i < data.length; i++) {
data[i] = d.charAt(i) - '0';
if (data[i] >= base || data[i] <= 0)
data[i] = 0;
}
}
/**
* 10 marks
* assume that base has already been set (to a value between 2 and 10).
* populate array data with the contents of String passed such that any invalid
* characters (outside the range ['0', character corresponding to (base-1)] are
* ignored.
*
* For example,
* if base = 4 and d = "341518", data becomes {3,1,1}
* if base = 9 and d = "341518", data becomes {3,4,1,5,1,8}
* if base = 4 and d = "9sh2883^1", data becomes {2,3,1}
* @param d
*/
public void setDataAdvanced(String d) {
int range = 0;
for (int i = 0; i < d.length(); i++) {
if (d.charAt(i) - '0' < base && d.charAt(i) - '0' >= 0) {
range++;
}
}
if (range == 0) {
data = new int[]{0};
return;
}
else data = new int[range];
int charPos = 0;
for (int i = 0; i < d.length(); i++){
if (d.charAt(i) - '0' >= 0 && d.charAt(i) - '0' < base) {
data[charPos] = d.charAt(i) - '0';
charPos++;
}
}
}
/**
* DO NOT MODIFY
* @param d
* @param b
*/
public MyInteger(String d, int b) {
this(d, b, false);
}
/**
* DO NOT MODIFY
* @param d
* @param b
* @param challenging
*/
public MyInteger(String d, int b, boolean challenging) {
setBase(b);
if(challenging)
setDataAdvanced(d);
else
setDataBasic(d);
}
/**
* 10 marks
* set base to b and populate data with values corresponding to decimal number provided.
* for example,
* if decimal = 47 and b = 2, then base should become 2 and data should become {1,0,1,1,1,1}
* if decimal = 225 and b = 5, then base should become 5 and data should become {1,4,0,0}
* @param decimal
* @param base
*/
public MyInteger(int decimal, int b) {
setBase(b);
data = new int[] {0}; //to be completed
}
/**
* 5 marks
* return String representation of the number. String containing the items of data
* followed by a space followed by an opening round bracket followed by "base"
* followed by a space followed by the base followed by the closing round bracket.
*
* for example,
* if base = 7 and data = {3, 1, 0, 5, 2}, return "31052 (base 7)"
* if base = 10 and data = {3, 1, 0, 5, 2}, return "31052 (base 10)"
* if base = 2 and data = {1, 1, 0, 0, 0, 1, 0, 1}, return "11000101 (base 2)"
*/
public String toString() {
String print = "";
for ( int n : data) print += n;
//for (int n : data) print += n;
print += " (base " + base + ")";
return print;
}
/**
* 5 marks
* @return the decimal value corresponding to this number.
* assume the decimal value is at most Integer.MAX_VALUE (2147483647)
*/
public int getDecimalValue() {
int num = 0;
int power = 1;
for (int i = data.length - 1; i >= 0; i--){
num += power*data[i];
power *= base;
}
return num;
}
/**
* 5 marks
* @param other
* @return
* 1 if value of calling object is more than value of parameter object
* -1 if value of calling object is less than value of parameter object
* 0 if value of calling object is equal to value of parameter object
*
*/
public int compareTo(MyInteger other) {
if(this.getDecimalValue() > other.getDecimalValue()) {
return 1;
}
if(this.getDecimalValue() < other.getDecimalValue()) {
return -1;
}
return 0;
}
/**
* 5 marks
* *TIP* useful if you complete MyInteger(int,int)
* and pass the test for it before attempting this method
*
* @param b
* @return object representing the calling object in base b
*/
public MyInteger getValueInBase(int b) {
return new MyInteger("0", b); //to be completed
}
}
Explanation / Answer
package myinteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MyInteger {
private int base;
private int[] data;
/**
*
* DO NOT MODIFY
*
*/
public MyInteger() {
}
public int getBase() {
return base;
}
/**
*
* DO NOT MODIFY
*
* @return a DEEP copy of data
*
*/
public int[] getData() {
int[] result = new int[data.length];
for (int i = 0; i < data.length; i++) {
result[i] = data[i];
}
return result;
}
/**
*
* 10 marks
*
* set base to b such that any value of b less than 2 results in base
* becoming 2,
*
* any value of b more than 10 results in base becoming 10, and any value of
* b
*
* in the range [2, 10] results in base becoming b.
*
*
*
* IMPORTANT: if base already has a value between 2 and 10
*
* (inclusive on both sides), it should NOT be changed.
*
* @param b
*
*/
public void setBase(int b) {
if (base >= 2 && base <= 10) {
return;
}
if (b < 2) {
base = 2;
} else if (b > 10) {
base = 10;
} else {
base = b;
}
}
/**
*
* 30 marks
*
* assume that base has already been set (to a value between 2 and 10).
*
* populate array data with the contents of String passed such that -
*
* if d.charAt(i) is between '0' and character corresponding to (base-1),
*
* then data[i] becomes integer corresponding to d.charAt(i)
*
* otherwise data[i] becomes 0.
*
*
*
* For example,
*
* if base = 4 and d = "341518", data becomes {3,0,1,0,1,0}
*
* if base = 9 and d = "341518", data becomes {3,4,1,5,1,8}
*
* if base = 4 and d = "9sh2883^1", data becomes {0,0,0,2,0,0,3,0,1}
*
* @param d
*
*/
public void setDataBasic(String d) {
data = new int[d.length()];
for (int i = 0; i < data.length; i++) {
data[i] = d.charAt(i) - '0';
if (data[i] >= base || data[i] <= 0) {
data[i] = 0;
}
}
}
/**
*
* 10 marks
*
* assume that base has already been set (to a value between 2 and 10).
*
* populate array data with the contents of String passed such that any
* invalid
*
* characters (outside the range ['0', character corresponding to (base-1)]
* are
*
* ignored.
*
*
*
* For example,
*
* if base = 4 and d = "341518", data becomes {3,1,1}
*
* if base = 9 and d = "341518", data becomes {3,4,1,5,1,8}
*
* if base = 4 and d = "9sh2883^1", data becomes {2,3,1}
*
* @param d
*
*/
public void setDataAdvanced(String d) {
int range = 0;
for (int i = 0; i < d.length(); i++) {
if (d.charAt(i) - '0' < base && d.charAt(i) - '0' >= 0) {
range++;
}
}
if (range == 0) {
data = new int[]{0};
return;
} else {
data = new int[range];
}
int charPos = 0;
for (int i = 0; i < d.length(); i++) {
if (d.charAt(i) - '0' >= 0 && d.charAt(i) - '0' < base) {
data[charPos] = d.charAt(i) - '0';
charPos++;
}
}
}
/**
*
* DO NOT MODIFY
*
* @param d
*
* @param b
*
*/
public MyInteger(String d, int b) {
this(d, b, false);
}
/**
*
* DO NOT MODIFY
*
* @param d
*
* @param b
*
* @param challenging
*
*/
public MyInteger(String d, int b, boolean challenging) {
setBase(b);
if (challenging) {
setDataAdvanced(d);
} else {
setDataBasic(d);
}
}
/**
*
* 10 marks
*
* set base to b and populate data with values corresponding to decimal
* number provided.
*
* for example,
*
* if decimal = 47 and b = 2, then base should become 2 and data should
* become {1,0,1,1,1,1}
*
* if decimal = 225 and b = 5, then base should become 5 and data should
* become {1,4,0,0}
*
* @param decimal
*
* @param base
*
*/
public MyInteger(int decimal, int b) {
setBase(b);
int tmp = decimal;
List<Integer> bits = new ArrayList<>();
do {
bits.add(tmp % b);
tmp /= b;
} while(tmp>1);
bits.add(tmp);
Collections.reverse(bits);
data = new int[bits.size()];
for(int i=0;i<bits.size();i++)
{
data[i] = bits.get(i);
}
}
/**
*
* 5 marks
*
* return String representation of the number. String containing the items
* of data
*
* followed by a space followed by an opening round bracket followed by
* "base"
*
* followed by a space followed by the base followed by the closing round
* bracket.
*
*
*
* for example,
*
* if base = 7 and data = {3, 1, 0, 5, 2}, return "31052 (base 7)"
*
* if base = 10 and data = {3, 1, 0, 5, 2}, return "31052 (base 10)"
*
* if base = 2 and data = {1, 1, 0, 0, 0, 1, 0, 1}, return "11000101 (base
* 2)"
*
*/
public String toString() {
String print = "";
for (int n : data) {
print += n;
}
//for (int n : data) print += n;
print += " (base " + base + ")";
return print;
}
/**
*
* 5 marks
*
* @return the decimal value corresponding to this number.
*
* assume the decimal value is at most Integer.MAX_VALUE (2147483647)
*
*/
public int getDecimalValue() {
int num = 0;
int power = 1;
for (int i = data.length - 1; i >= 0; i--) {
num += power * data[i];
power *= base;
}
return num;
}
/**
*
* 5 marks
*
* @param other
*
* @return
*
* 1 if value of calling object is more than value of parameter object
*
* -1 if value of calling object is less than value of parameter object
*
* 0 if value of calling object is equal to value of parameter object
*
*
*
*/
public int compareTo(MyInteger other) {
if (this.getDecimalValue() > other.getDecimalValue()) {
return 1;
}
if (this.getDecimalValue() < other.getDecimalValue()) {
return -1;
}
return 0;
}
/**
*
* 5 marks
*
* *TIP* useful if you complete MyInteger(int,int)
*
* and pass the test for it before attempting this method
*
*
*
* @param b
*
* @return object representing the calling object in base b
*
*/
public MyInteger getValueInBase(int b) {
return new MyInteger(this.getDecimalValue(), b); //to be completed
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.