Hi, all. I\'m going over a past assignment for a Java coding course and need hel
ID: 3699105 • Letter: H
Question
Hi, all. I'm going over a past assignment for a Java coding course and need help. No idea how to go about doing this. Here are the guidelines, and after that I'll post the template code.
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
COMP125 - Fundamentals of Computer Science
Session 1, 2018
ASSIGNMENT 1 - Worth 5%
Due: 11:45pm, 6th April, 2018
Starting point provided in assignment1template_junit4.zip
Marking
Note that we have used UML syntax to list the methods:
+/- methodName(parameterTypes): returnType
where + is for public and - is for private
It should be noted that the test for MyInteger(int, int) assumes that the test for setBase(int)
passes successfully.
Also, tests 5 to 8 assume that tests 1 to 4 pass successfully.
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
}
}
Explanation / Answer
Following is the required 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=b;
}
if (b>=10){
base=b;
}
if(b>2 && b<10){
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
int l = d.length();
data = new int[l];
for (int i=0; i<l;i++){
if(d.charAt(i)>'0' && d.charAt(i)<base){
data[i]=d.charAt(i);
}
else{
data[i]=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
int l = d.length();
data = new int[l];
int j=0;
for(int i=0;i<l;i++){
if(d.charAt(i)>'0' && d.charAt(i)<base){
data[j]=d.charAt(i);
j++;
}
}
}
/**
* 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 String convert(int number, int base)
{
int quotient = number / base;
int remainder = number % base;
if (quotient == 0) // base case
{
return Integer.toString(remainder);
}
else
{
return convert(quotient, base) + Integer.toString(remainder);
}
}
public MyInteger(int decimal, int b) {
setBase(2); //to be completed
int j = 0;
//to be completed
String ans = convert(decimal, b);
int l = ans.length();
data = new int[l];
for(int i=0;i<l;i++)
{
data[i] = ans.charAt(i)-'0';
}
}
/**
* 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 ans="";
for(int i=0;i<data.length;i++)
{
ans += data[i];
}
ans += "(base " + base + ")";
return ans; //to be completed
}
public int getDecimalValue() {
return 0;
}
public int compareTo(MyInteger other) {
return 0;
}
public MyInteger getValueInBase(int b) {
return new MyInteger("0", b);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.