Question
I am a bit confused on this code- I have to create setters for the array data I have no idea how to get it
I am a bit confused on this code- I have to create setters for the array data I have no idea how to get it to work with the Junit test provided to me (I am doing this on the software ECLIPSE btw)
I have done the first setter which is setBase but I am having errors in the 2nd setter which is setDataBasic, when i am passing values to the array in my if condition the value just straight up all gets assigned to 0 and none of it passes the condition, when i try to create an array with data.length its also giving me null pointer exception, can anyone help please?
Similar thing with the setter SetDataAdvanced and myInteger constructor
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){
if(b<2)
base=2;
else if(b>10)
base=10;
else
base=b;
}
//base = 2; //to be completed
}
/**
* 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) {
setBase(2); //to be completed
data = new int[data.length-1];
for (int i=0; i
if(d.charAt(i)>0 || d.charAt(i)<(base-1)){
data[i]=d.charAt(i);
}
else{
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) {
setBase(2); //to be completed
//data = new int[] {0}; //to be completed
}
/**
* 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(2); //to be completed
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() {
return "0"; //to be completed
}
/**
* 5 marks
* @return the decimal value corresponding to this number.
* assume the decimal value is at most Integer.MAX_VALUE (2147483647)
*/
public int getDecimalValue() {
return 0; //to be completed
}
/**
* 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) {
return 0; //to be completed
}
/**
* 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
}
}
JunitTest for the setters:-
@Test @Graded(marks = 30, description = "setDataBasic(String)")
public void testSetDataBasic() {
MyInteger integer1 = new MyInteger();
//assume setBase works correctly
integer1.setBase(2);
integer1.setDataBasic("110001");
int[] dataReceived1 = integer1.getData();
assertEquals("[1, 1, 0, 0, 0, 1]", Arrays.toString(dataReceived1));
MyInteger integer2 = new MyInteger();
integer2.setBase(2);
integer2.setDataBasic("222");
int[] dataReceived2 = integer2.getData();
assertEquals("[0, 0, 0]", Arrays.toString(dataReceived2));
MyInteger integer3 = new MyInteger();
integer3.setBase(6);
integer3.setDataBasic("40361a14");
int[] dataReceived3 = integer3.getData();
assertEquals("[4, 0, 3, 0, 1, 0, 1, 4]", Arrays.toString(dataReceived3));
marks+=30;
feedback=feedback+30+" marks: setDataBasic(String) passed ";
}
@Test @Graded(marks = 10, description = "setDataAdvanced(String)")
public void testSetDataAdvanced() {
MyInteger integer1 = new MyInteger();
//assume setBase works correctly
integer1.setBase(2);
integer1.setDataAdvanced("110001");
int[] dataReceived1 = integer1.getData();
assertEquals("[1, 1, 0, 0, 0, 1]", Arrays.toString(dataReceived1));
MyInteger integer2 = new MyInteger();
integer2.setBase(2);
integer2.setDataAdvanced("222");
int[] dataReceived2 = integer2.getData();
assertEquals("[0]", Arrays.toString(dataReceived2));
MyInteger integer3 = new MyInteger();
integer3.setBase(6);
integer3.setDataAdvanced("40361a14");
int[] dataReceived3 = integer3.getData();
assertEquals("[4, 0, 3, 1, 1, 4]", Arrays.toString(dataReceived3));
marks+=10;
feedback=feedback+10+" marks: setDataAdvanced(String) passed ";
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started