finish the java programming a. Begin by creating a standard list ADT in a new cl
ID: 3787712 • Letter: F
Question
finish the java programming
a. Begin by creating a standard list ADT in a new class called MyIntList. The underlying data
structure is to be an int array with a maximum size of 200 elements. Also implement the
following methods to provide an interface to your list ADT:
• int size() - which returns the number of elements in the list
• int get(int index) – which return the integer element at the specified zero-based index
• void set(int index, int value) – which sets the list element at index with the given value
b. Next, create a class called MyHugeInteger that is to represent an integer up to 200 digits
long. This class must provide a constructor that takes a single String as an argument. The
String value passed to the constructor is understood to be a string of digits representing a
large integer. You can assume that the value passed to the constructor is well formatted; it
contains no spaces or non-numeric characters other than the first character possibly being a
“-” if the large number is negative. Inside your class, represent the large integer as a list of
digits using the MyIntList you created in the previous section. In addition to these
requirements, provide the following interface methods:
int compareTo(MyHugeInteger hugeInt) – where the return value is -1 if hugeInt is less
than the object's large integer value, +1 if hugeInt is greater or 0 if the two values are
equal
String toString() - which returns a String of all the digits that make-up the huge integer,
including a negative sign if the large integer is negative. When printed to screen, the
String should read a normal, though large, integer.
c. Create a new class called MyHugeIntegerTools that provides the following methods which
operate on lists of MyHugeInteger (no constructor is necessary for this class):
MyHugeInteger getMin(Vector<MyHugeInteger> hugeIntList) – this method is to return
the MyHugeInteger with the smallest value in the given list
MyHugeInteger getMax(Vector<MyHugeInteger> hugeIntList) – this method is to return
the MyHugeInteger with the largest value in the given list
Vector<MyHugeInteger> getDuplicates(Vector<MyHugeInteger> hugeIntList) – this
method is to return a Vector of MyHugeInteger that appear more than once in the given
list
void sortAscending(Vector<MyHugeInteger> hugeIntList) – this method is to sort the
contents of the given list into ascending order. That is, when the sorting is complete, the
smallest value is at index 0, the largest is at the end index and all values in between are
sorted in increasing order. Note how there is no return value. This is because the sort is
to be done in-place. Since the given list is to be modified directly, there is no need to
copy it. You may implement the sorting algorithm of your choice from the ones
presented in Chapter 3 of the course textbook (i.e. Bubble, Selection or Insertion).
Explanation / Answer
import java.util.Arrays;
import java.util.Vector;
public class MyHugeIntegerTools {
MyHugeInteger getMin(Vector<MyHugeInteger> hugeIntList)
{
MyHugeInteger integer1=null;
for(MyHugeInteger integer:hugeIntList)
{
int min=Integer.parseInt(integer.no);
for (int i = 1; i < hugeIntList.size(); i++) {
integer1=hugeIntList.elementAt(i);
if (Integer.parseInt(integer1.no) < min) {
min = Integer.parseInt(integer1.no);
}
}
return integer1;
}
return null;
}
MyHugeInteger getMax(Vector<MyHugeInteger> hugeIntList)
{
MyHugeInteger integer1=null;
for(MyHugeInteger integer:hugeIntList)
{
int max=Integer.parseInt(integer.no);
for (int i = 1; i < hugeIntList.size(); i++) {
integer1=hugeIntList.elementAt(i);
if (Integer.parseInt(integer1.no) > max) {
max = Integer.parseInt(integer1.no);
}
}
return integer1;
}
return null;
}
Vector<MyHugeInteger> getDuplicates(Vector<MyHugeInteger> hugeIntList)
{
MyHugeInteger integer1=null;
for(MyHugeInteger integer:hugeIntList)
{
int no1=Integer.parseInt(integer.no);
for (int i = 1; i < hugeIntList.size(); i++) {
integer1=hugeIntList.elementAt(i);
if (Integer.parseInt(integer1.no) == no1) {
no1 = Integer.parseInt(integer1.no);
}
}
hugeIntList.add(integer1);
}
return hugeIntList;
}
void sortAscending(Vector<MyHugeInteger> hugeIntList)
{
MyHugeInteger[] array=(MyHugeInteger[]) hugeIntList.toArray();
Arrays.sort(array);
}
}
***************************************************************************
public class MyHugeInteger {
public String no;
public MyHugeInteger(String no) {
this.no = no;
}
public int compareTo(MyHugeInteger integer) {
if(Integer.parseInt(integer.no)<Integer.parseInt(this.no))
{
return -1;
}
if(Integer.parseInt(integer.no)>Integer.parseInt(this.no))
{
return 1;
}else
return 0;
}
@Override
public String toString() {
return "MyHugeInteger [no=" + no + "]";
}
}
************************************************************************************
public class MyIntList implements ADTBag {
@Override
public int size() {
return array.length;
}
@Override
public int getIndex(int index) {
try {
for (int i = 0; i < array.length; i++) {
if (index == array[i]) {
return index;
}
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("your passed argument not in the array");
return 0;
}
return index;
}
@Override
public void setIndex(int index, int value) {
try {
for (int i = 0; i < array.length; i++) {
if (index == array[i]) {
array[i] = value;
}
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("your passed argument not in the array");
}
}
}
*******************************************************************************************
public interface ADTBag {
int n = 200;
int[] array = new int[n];
int size();
int getIndex(int index);
void setIndex(int index, int value);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.