Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assigntment Help- i dont know how to post code here, this is my java assignment i would like you to double check my answers, thank

Assigntment Help- i dont know how to post code here, this is my java assignment i would like you to double check my answers, thank you

First Here is the Guideline-

The first assignments deals with a number stored in a specific base system. For example, 1101 base 2 is 13 base 10 is 23 base 5 is 17 base 8 . The class MyInteger holds the integers in a specific base. You may assume the integers held are, between the bases 2 and 10 non-negative (more than or equal to 0) small enough (say maximum a billion) The instance variables in class MyInteger are: base: int - holds the value for the base system data: int[] - holds every token of the number. For example, if we were to store the binary number 1101, data would be {1, 1, 0, 1} while if we were to store the decimal number 684, data would be {6, 8, 4} .

The methods that you need to complete are given below, in the order we suggest you complete them. 1. +setBase(int):void //10 marks 2. +setDataBasic(String):void //30 marks 3. +setDataAdvanced(String):void //10 marks 4. +MyInteger(int,int) //10 marks 5. +toString():String //5 marks 6. +getDecimalValue():int //5 marks 7. +compareTo(MyInteger):int //5 marks 8. +getValueInBase(int):MyInteger //5 marks

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) {

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[] {0}; //to be completed

}

/**

* 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

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions

Question

Distinguish between an expense and an expendi- ture. LO9

Answered: 1 week ago

Question

1. Describe the power of nonverbal communication

Answered: 1 week ago