Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help with this code please I have done the first method which is set

ID: 3700998 • Letter: C

Question

Can someone help with this code please

I have done the first method which is setBase but I am having errors in the 2nd method 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, can someone help please

Similar thing with the method SetDataAdvanced

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

}

}

Explanation / Answer

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];

        /*

        for (int i=0; i

        if(d.charAt(i)>0 || d.charAt(i)<(base-1)){

        data[i]=d.charAt(i);

        }

        else{

        data[i]=0;

        }

        }

        */

        // create array of size same as d

        data = new int[d.length()];

        // fill data with characters which are valid

        for( i = 0 ; i < d.length() ; i++ )

        {

            // get i th character using charAt(i)

           // get the ascii code of current character using (int)d.charAt(i)

            // if current character is greater than 0 and less than base

            if( (int)d.charAt(i) >= 48 && (int)d.charAt(i) < base )

            {

                // convert character to int using Integer.parseInt() and

                // add it to data

                data[i] = Integer.parseInt( 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

//setBase(2); //to be completed;

    //data = new int[] {0}; //to be completed

  

    // store the number of characters which are valid

    int count = 0;

    int i;

    // get the number of characters which are valid

    for( i = 0 ; i < d.length() ; i++ )

    {

        // if the current character is digit

        // get i th character using charAt(i)

        // isDigit() return true if the character is digit

        // get the ascii code of current character using (int)d.charAt(i)

        if( d.charAt(i).isDigit() == true && ( (int)d.charAt(i) >= 48 && (int)d.charAt(i) < base ) )

            count++;

    }

    // if no character is valid

    if( count == 0 )

        data = new int[1];

    else

    {

        // create array of size count

        data = new int[count];

        int index = 0;

        // fill data with characters which are valid

        for( i = 0 ; i < d.length() ; i++ )

        {

            // if the current character is digit

            // get i th character using charAt(i)

            // isDigit() return true if the character is digit

            // get the ascii code of current character using (int)d.charAt(i)

            // convert character to int using Integer.parseInt()

            if( d.charAt(i).isDigit() == true && ( (int)d.charAt(i) >= 48 && (int)d.charAt(i) < base ) )

            {

                // convert character to int using Integer.parseInt() and

                // add it to data

                data[index] = Integer.parseInt( d.charAt(i) );

                index++;

            }

        }

    }

}

/**

* 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

}

}